Skip to content

Commit

Permalink
MINOR: Use Mockito's strict stubs functionality for Connect tests and…
Browse files Browse the repository at this point in the history
… cleanup unused stubs (#14166)


Reviewers: Mickael Maison <mickael.maison@gmail.com>
  • Loading branch information
yashmayya authored Aug 9, 2023
1 parent ddeb89f commit f233943
Show file tree
Hide file tree
Showing 21 changed files with 68 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,16 @@
import org.apache.kafka.connect.header.Header;
import org.apache.kafka.connect.sink.SinkRecord;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.junit.MockitoJUnitRunner;

import java.util.Collections;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;

@RunWith(MockitoJUnitRunner.StrictStubs.class)
public class InternalSinkRecordTest {

private static final String TOPIC = "test-topic";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import org.apache.kafka.connect.transforms.Transformation;
import org.apache.kafka.connect.transforms.predicates.Predicate;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.junit.MockitoJUnitRunner;

import static java.util.Collections.singletonMap;
import static org.junit.Assert.assertEquals;
Expand All @@ -28,6 +30,7 @@
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.StrictStubs.class)
public class TransformationStageTest {

private final SourceRecord initial = new SourceRecord(singletonMap("initial", 1), null, null, null, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
@RunWith(MockitoJUnitRunner.StrictStubs.class)
public class WorkerConfigTransformerTest {

public static final String MY_KEY = "myKey";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import org.apache.kafka.common.config.ConfigException;
import org.apache.kafka.common.security.auth.SecurityProtocol;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.junit.MockitoJUnitRunner;

import javax.crypto.KeyGenerator;
import javax.crypto.Mac;
Expand Down Expand Up @@ -52,6 +54,7 @@
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.StrictStubs.class)
public class DistributedConfigTest {

public Map<String, String> configs() {
Expand Down Expand Up @@ -93,10 +96,7 @@ public void testDefaultAlgorithmsNotPresent() throws NoSuchAlgorithmException {
doReturn(fakeKeyGenerator)
.when(crypto).keyGenerator(fakeKeyGenerationAlgorithm);

// And for the signature algorithm
doThrow(new NoSuchAlgorithmException())
.when(crypto).mac(DistributedConfig.INTER_WORKER_SIGNATURE_ALGORITHM_DEFAULT);
// Likewise for key verification algorithms
// And for the key verification algorithms
for (String verificationAlgorithm : DistributedConfig.INTER_WORKER_VERIFICATION_ALGORITHMS_DEFAULT) {
doThrow(new NoSuchAlgorithmException())
.when(crypto).mac(verificationAlgorithm);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import org.apache.kafka.connect.util.ConnectorTaskId;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.junit.MockitoJUnitRunner;

import java.nio.ByteBuffer;
import java.util.ArrayList;
Expand Down Expand Up @@ -55,6 +57,7 @@
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.StrictStubs.class)
public class IncrementalCooperativeAssignorTest {

// Offset isn't used in most tests but is required for creating a config snapshot object,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
import org.mockito.quality.Strictness;

import java.nio.ByteBuffer;
import java.util.ArrayList;
Expand Down Expand Up @@ -72,7 +73,7 @@
@RunWith(value = Parameterized.class)
public class WorkerCoordinatorIncrementalTest {
@Rule
public MockitoRule rule = MockitoJUnit.rule();
public MockitoRule rule = MockitoJUnit.rule().strictness(Strictness.STRICT_STUBS);

private String connectorId1 = "connector1";
private String connectorId2 = "connector2";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,14 @@
import org.apache.kafka.connect.util.ConnectorTaskId;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
import org.mockito.quality.Strictness;

import java.nio.ByteBuffer;
import java.util.ArrayList;
Expand Down Expand Up @@ -74,6 +78,9 @@
@RunWith(value = Parameterized.class)
public class WorkerCoordinatorTest {

@Rule
public MockitoRule rule = MockitoJUnit.rule().strictness(Strictness.STRICT_STUBS);

private static final String LEADER_URL = "leaderUrl:8083";
private static final String MEMBER_URL = "memberUrl:8083";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@
import org.apache.kafka.connect.runtime.WorkerConfig;
import org.apache.kafka.connect.storage.ConfigBackingStore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;

import javax.management.MBeanServer;
import javax.management.ObjectName;
Expand All @@ -43,6 +45,7 @@
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;

@RunWith(MockitoJUnitRunner.StrictStubs.class)
public class WorkerGroupMemberTest {
@Mock
private ConfigBackingStore configBackingStore;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doAnswer;

@RunWith(MockitoJUnitRunner.class)
@RunWith(MockitoJUnitRunner.StrictStubs.class)
public class ConnectClusterStateImplTest {
protected static final String KAFKA_CLUSTER_ID = "franzwashere";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import org.apache.kafka.connect.sink.SinkConnector;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.junit.MockitoJUnitRunner;

import java.net.MalformedURLException;
import java.net.URL;
Expand All @@ -31,6 +33,7 @@
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.StrictStubs.class)
public class DelegatingClassLoaderTest {

public PluginClassLoader parent;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.junit.MockitoJUnitRunner;
import org.slf4j.LoggerFactory;

import javax.ws.rs.core.MediaType;
Expand All @@ -62,6 +64,7 @@
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;

@RunWith(MockitoJUnitRunner.StrictStubs.class)
public class ConnectRestServerTest {

private Herder herder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
import org.apache.kafka.connect.runtime.rest.errors.BadRequestException;
import org.eclipse.jetty.client.api.Request;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.junit.MockitoJUnitRunner;

import javax.crypto.Mac;
import javax.crypto.SecretKey;
Expand All @@ -43,6 +45,7 @@
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.StrictStubs.class)
public class InternalRequestSignatureTest {

private static final byte[] REQUEST_BODY =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoJUnitRunner;
import org.mockito.junit.MockitoRule;
import org.mockito.quality.Strictness;

import javax.crypto.SecretKey;
import javax.ws.rs.core.Response;
Expand Down Expand Up @@ -107,7 +108,7 @@ private static RestClient.HttpResponse<TestDTO> httpRequest(HttpClient httpClien
public static class RequestFailureParameterizedTest {

@Rule
public MockitoRule initRule = MockitoJUnit.rule();
public MockitoRule initRule = MockitoJUnit.rule().strictness(Strictness.STRICT_STUBS);

@Mock
private HttpClient httpClient;
Expand Down Expand Up @@ -142,7 +143,7 @@ public void testFailureDuringRequestCausesInternalServerError() throws Exception
}


@RunWith(MockitoJUnitRunner.class)
@RunWith(MockitoJUnitRunner.StrictStubs.class)
public static class Tests {
@Mock
private HttpClient httpClient;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@
import org.apache.kafka.connect.util.Callback;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.junit.MockitoJUnitRunner;

import javax.ws.rs.BadRequestException;
import java.util.ArrayList;
Expand Down Expand Up @@ -89,6 +91,7 @@
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.StrictStubs.class)
public class ConnectorPluginsResourceTest {

private static final Map<String, String> PROPS;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.junit.MockitoJUnitRunner;

import java.util.Arrays;
import java.util.Collections;
Expand All @@ -38,6 +40,7 @@
import static org.mockito.Mockito.when;

@SuppressWarnings("unchecked")
@RunWith(MockitoJUnitRunner.StrictStubs.class)
public class LoggingResourceTest {

@Test
Expand Down Expand Up @@ -71,7 +74,6 @@ public void getLoggerFallsbackToEffectiveLogLevelTest() {
Logger b = hierarchy.getLogger("b");
b.setLevel(Level.INFO);
when(loggingResource.currentLoggers()).thenReturn(loggers(a, b));
when(loggingResource.rootLogger()).thenReturn(root);
when(loggingResource.getLogger(any())).thenCallRealMethod();
Map<String, String> level = (Map<String, String>) loggingResource.getLogger("a").getEntity();
assertEquals(1, level.size());
Expand All @@ -90,7 +92,6 @@ public void getUnknownLoggerTest() {
Logger b = hierarchy.getLogger("b");
b.setLevel(Level.INFO);
when(loggingResource.currentLoggers()).thenReturn(loggers(a, b));
when(loggingResource.rootLogger()).thenReturn(root);
when(loggingResource.getLogger(any())).thenCallRealMethod();
assertThrows(NotFoundException.class, () -> loggingResource.getLogger("c"));
}
Expand All @@ -113,7 +114,6 @@ public void setLevelTest() {
w.setLevel(Level.INFO);
when(loggingResource.currentLoggers()).thenReturn(loggers(x, y, z, w));
when(loggingResource.lookupLogger("a.b.c.p")).thenReturn(p);
when(loggingResource.rootLogger()).thenReturn(root);
when(loggingResource.setLevel(any(), any())).thenCallRealMethod();
List<String> modified = (List<String>) loggingResource.setLevel("a.b.c.p", Collections.singletonMap("level", "DEBUG")).getEntity();
assertEquals(4, modified.size());
Expand Down Expand Up @@ -141,7 +141,6 @@ public void setRootLevelTest() {
z.setLevel(Level.INFO);
w.setLevel(Level.INFO);
when(loggingResource.currentLoggers()).thenReturn(loggers(x, y, z, w));
when(loggingResource.lookupLogger("a.b.c.p")).thenReturn(p);
when(loggingResource.rootLogger()).thenReturn(root);
when(loggingResource.setLevel(any(), any())).thenCallRealMethod();
List<String> modified = (List<String>) loggingResource.setLevel("root", Collections.singletonMap("level", "DEBUG")).getEntity();
Expand All @@ -166,8 +165,6 @@ public void setLevelWithEmptyArgTest() {
a.setLevel(null);
Logger b = hierarchy.getLogger("b");
b.setLevel(Level.INFO);
when(loggingResource.currentLoggers()).thenReturn(loggers(a, b));
when(loggingResource.rootLogger()).thenReturn(root);
when(loggingResource.setLevel(any(), any())).thenCallRealMethod();
assertThrows(BadRequestException.class, () -> loggingResource.setLevel("@root", Collections.emptyMap()));
}
Expand All @@ -183,8 +180,6 @@ public void setLevelWithInvalidArgTest() {
a.setLevel(null);
Logger b = hierarchy.getLogger("b");
b.setLevel(Level.INFO);
when(loggingResource.currentLoggers()).thenReturn(loggers(a, b));
when(loggingResource.rootLogger()).thenReturn(root);
when(loggingResource.setLevel(any(), any())).thenCallRealMethod();
assertThrows(NotFoundException.class, () -> loggingResource.setLevel("@root", Collections.singletonMap("level", "HIGH")));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.junit.MockitoJUnitRunner;

import java.io.File;
import java.io.IOException;
Expand All @@ -49,6 +51,7 @@
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.StrictStubs.class)
public class FileOffsetBackingStoreTest {

private FileOffsetBackingStore store;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
import org.apache.kafka.connect.util.KafkaBasedLog;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.junit.MockitoJUnitRunner;

import java.util.Arrays;
import java.util.Collections;
Expand All @@ -52,6 +54,7 @@
import static org.mockito.Mockito.verify;

@SuppressWarnings("unchecked")
@RunWith(MockitoJUnitRunner.StrictStubs.class)
public class KafkaStatusBackingStoreFormatTest {

private static final String STATUS_TOPIC = "status-topic";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@
import org.apache.kafka.connect.util.TopicAdmin;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.junit.MockitoJUnitRunner;

import java.util.Collections;
import java.util.HashMap;
Expand All @@ -62,6 +64,7 @@
import static org.mockito.Mockito.when;

@SuppressWarnings("unchecked")
@RunWith(MockitoJUnitRunner.StrictStubs.class)
public class KafkaStatusBackingStoreTest {

private static final String STATUS_TOPIC = "status-topic";
Expand Down Expand Up @@ -369,7 +372,6 @@ public void deleteConnectorState() {
statusMap.put("state", "RUNNING");
statusMap.put("generation", 0L);

when(converter.fromConnectData(eq(STATUS_TOPIC), any(Schema.class), any(Struct.class))).thenReturn(value);
when(converter.fromConnectData(eq(STATUS_TOPIC), any(Schema.class), any(Struct.class))).thenReturn(value);
when(converter.toConnectData(STATUS_TOPIC, value)).thenReturn(new SchemaAndValue(null, statusMap));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.junit.MockitoJUnitRunner;

import java.nio.ByteBuffer;
import java.util.Arrays;
Expand All @@ -44,6 +46,7 @@
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;

@RunWith(MockitoJUnitRunner.StrictStubs.class)
public class OffsetStorageWriterTest {
private static final String NAMESPACE = "namespace";
// Connect format - any types should be accepted here
Expand Down
Loading

0 comments on commit f233943

Please sign in to comment.