Skip to content

Commit a7ae24d

Browse files
committed
Updated dependencies
1 parent fac9d62 commit a7ae24d

File tree

2 files changed

+75
-27
lines changed

2 files changed

+75
-27
lines changed
Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
11
dependencies {
2-
api group: 'com.squareup.okhttp3', name: 'okhttp', version: '3.10.0'
2+
api group: 'com.squareup.okhttp3', name: 'okhttp', version: '3.12.1'
33

44
// Included in connector runtime
55
compileOnly group: 'org.apache.kafka', name: 'connect-api', version: kafkaVersion
66

7-
testImplementation group: 'ch.qos.logback', name: 'logback-classic', version:'1.2.3'
8-
testImplementation group: 'junit', name: 'junit', version:'4.12'
9-
testImplementation group: 'org.mockito', name: 'mockito-core', version:'1.10.19'
10-
testImplementation group: 'com.github.tomakehurst', name: 'wiremock', version:'2.14.0'
7+
testImplementation group: 'ch.qos.logback', name: 'logback-classic', version: '1.2.3'
8+
testImplementation group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.3.2'
9+
testRuntimeOnly group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: '5.3.2'
10+
testImplementation group: 'org.mockito', name: 'mockito-core', version: '2.23.4'
11+
testImplementation group: 'com.github.tomakehurst', name: 'wiremock', version: '2.20.0'
1112

12-
testImplementation group: 'org.apache.kafka', name: 'connect-api', version:'2.0.0'
13+
testImplementation group: 'org.apache.kafka', name: 'connect-api', version: kafkaVersion
1314
}
1415

1516
task copyDependencies(type: Copy) {
1617
from configurations.runtimeClasspath.files
1718
into "${buildDir}/third-party/"
1819
}
20+
21+
test {
22+
useJUnitPlatform()
23+
}

kafka-connect-rest-source/src/test/java/org/radarbase/connect/rest/RestTaskTest.java

Lines changed: 64 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,28 +22,41 @@
2222
import static com.github.tomakehurst.wiremock.client.WireMock.matching;
2323
import static com.github.tomakehurst.wiremock.client.WireMock.post;
2424
import static com.github.tomakehurst.wiremock.client.WireMock.postRequestedFor;
25+
import static com.github.tomakehurst.wiremock.client.WireMock.resetAllRequests;
2526
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
2627
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
2728
import static com.github.tomakehurst.wiremock.client.WireMock.urlMatching;
2829
import static com.github.tomakehurst.wiremock.client.WireMock.verify;
30+
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
2931
import static org.junit.Assert.assertEquals;
3032

31-
import com.github.tomakehurst.wiremock.junit.WireMockRule;
32-
import java.net.ServerSocket;
33+
import com.github.tomakehurst.wiremock.WireMockServer;
34+
import com.github.tomakehurst.wiremock.client.VerificationException;
35+
import com.github.tomakehurst.wiremock.client.WireMock;
36+
import com.github.tomakehurst.wiremock.verification.LoggedRequest;
37+
import com.github.tomakehurst.wiremock.verification.NearMiss;
3338
import java.util.HashMap;
3439
import java.util.List;
3540
import java.util.Map;
3641
import org.apache.kafka.connect.source.SourceRecord;
3742
import org.apache.kafka.connect.source.SourceTaskContext;
3843
import org.apache.kafka.connect.storage.OffsetStorageReader;
39-
import org.junit.Rule;
40-
import org.junit.Test;
44+
import org.junit.jupiter.api.Test;
45+
import org.junit.jupiter.api.extension.AfterEachCallback;
46+
import org.junit.jupiter.api.extension.BeforeEachCallback;
47+
import org.junit.jupiter.api.extension.ExtendWith;
48+
import org.junit.jupiter.api.extension.ExtensionContext;
49+
import org.junit.jupiter.api.extension.ParameterContext;
50+
import org.junit.jupiter.api.extension.ParameterResolutionException;
51+
import org.junit.jupiter.api.extension.ParameterResolver;
52+
import org.radarbase.connect.rest.RestTaskTest.WireMockRule;
4153
import org.radarbase.connect.rest.converter.BytesPayloadConverter;
4254
import org.radarbase.connect.rest.converter.StringPayloadConverter;
4355
import org.radarbase.connect.rest.selector.SimpleTopicSelector;
4456
import org.radarbase.connect.rest.single.SingleRestSourceConnector;
4557
import org.radarbase.connect.rest.single.SingleRestSourceConnectorConfig;
4658

59+
@ExtendWith(WireMockRule.class)
4760
public class RestTaskTest {
4861

4962
private static final String CONTENT_TYPE = "Content-Type";
@@ -62,15 +75,10 @@ public class RestTaskTest {
6275
private static final String STRING_PAYLOAD_CONVERTER = StringPayloadConverter.class.getName();
6376
private static final String DATA = "{\"A\":\"B\"}";
6477
private static final String RESPONSE_BODY = "{\"B\":\"A\"}";
65-
private static final int PORT = getPort();
6678
private static final String PATH = "/my/resource";
67-
private static final String URL = "http://localhost:" + PORT + PATH;
68-
69-
@Rule
70-
public WireMockRule wireMockRule = new WireMockRule(PORT);
7179

7280
@Test
73-
public void restTest() throws InterruptedException {
81+
public void restTest(WireMockRule wireMock) throws InterruptedException {
7482
stubFor(post(urlEqualTo(PATH))
7583
.withHeader(ACCEPT, equalTo(APPLICATION_JSON))
7684
.willReturn(aResponse()
@@ -83,7 +91,7 @@ public void restTest() throws InterruptedException {
8391
props.put("connector.class", SingleRestSourceConnector.class.getName());
8492
props.put(SingleRestSourceConnectorConfig.SOURCE_METHOD_CONFIG, METHOD);
8593
props.put(SingleRestSourceConnectorConfig.SOURCE_PROPERTIES_LIST_CONFIG, PROPERTIES_LIST);
86-
props.put(RestSourceConnectorConfig.SOURCE_URL_CONFIG, URL);
94+
props.put(RestSourceConnectorConfig.SOURCE_URL_CONFIG, wireMock.url(PATH));
8795
props.put(SingleRestSourceConnectorConfig.SOURCE_DATA_CONFIG, DATA);
8896
props.put(RestSourceConnectorConfig.SOURCE_TOPIC_SELECTOR_CONFIG, TOPIC_SELECTOR);
8997
props.put(RestSourceConnectorConfig.SOURCE_TOPIC_LIST_CONFIG, REST_SOURCE_DESTINATION_TOPIC_LIST);
@@ -134,18 +142,53 @@ public OffsetStorageReader offsetStorageReader() {
134142
verify(postRequestedFor(urlMatching(PATH))
135143
.withRequestBody(equalTo(DATA))
136144
.withHeader(CONTENT_TYPE, matching(APPLICATION_JSON)));
137-
138-
wireMockRule.resetRequests();
139145
}
140146

141-
private static int getPort() {
142-
try {
143-
ServerSocket s = new ServerSocket(0);
144-
int localPort = s.getLocalPort();
145-
s.close();
146-
return localPort;
147-
} catch (Exception e) {
148-
throw new RuntimeException("Failed to get a free PORT", e);
147+
public static class WireMockRule extends WireMockServer implements BeforeEachCallback,
148+
AfterEachCallback, ParameterResolver {
149+
150+
public WireMockRule() {
151+
super(wireMockConfig().dynamicPort());
152+
}
153+
154+
@Override
155+
public void beforeEach(ExtensionContext context) {
156+
start();
157+
WireMock.configureFor("localhost", port());
158+
}
159+
160+
@Override
161+
public void afterEach(ExtensionContext context) {
162+
try {
163+
checkForUnmatchedRequests();
164+
} finally {
165+
resetAllRequests();
166+
stop();
167+
}
168+
}
169+
170+
private void checkForUnmatchedRequests() {
171+
List<LoggedRequest> unmatchedRequests = findAllUnmatchedRequests();
172+
if (!unmatchedRequests.isEmpty()) {
173+
List<NearMiss> nearMisses = findNearMissesForAllUnmatchedRequests();
174+
if (nearMisses.isEmpty()) {
175+
throw VerificationException.forUnmatchedRequests(unmatchedRequests);
176+
} else {
177+
throw VerificationException.forUnmatchedNearMisses(nearMisses);
178+
}
179+
}
180+
}
181+
182+
@Override
183+
public boolean supportsParameter(ParameterContext parameterContext,
184+
ExtensionContext extensionContext) throws ParameterResolutionException {
185+
return parameterContext.getParameter().getType() == WireMockRule.class;
186+
}
187+
188+
@Override
189+
public Object resolveParameter(ParameterContext parameterContext,
190+
ExtensionContext extensionContext) throws ParameterResolutionException {
191+
return this;
149192
}
150193
}
151194
}

0 commit comments

Comments
 (0)