2222import static com .github .tomakehurst .wiremock .client .WireMock .matching ;
2323import static com .github .tomakehurst .wiremock .client .WireMock .post ;
2424import static com .github .tomakehurst .wiremock .client .WireMock .postRequestedFor ;
25+ import static com .github .tomakehurst .wiremock .client .WireMock .resetAllRequests ;
2526import static com .github .tomakehurst .wiremock .client .WireMock .stubFor ;
2627import static com .github .tomakehurst .wiremock .client .WireMock .urlEqualTo ;
2728import static com .github .tomakehurst .wiremock .client .WireMock .urlMatching ;
2829import static com .github .tomakehurst .wiremock .client .WireMock .verify ;
30+ import static com .github .tomakehurst .wiremock .core .WireMockConfiguration .wireMockConfig ;
2931import 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 ;
3338import java .util .HashMap ;
3439import java .util .List ;
3540import java .util .Map ;
3641import org .apache .kafka .connect .source .SourceRecord ;
3742import org .apache .kafka .connect .source .SourceTaskContext ;
3843import 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 ;
4153import org .radarbase .connect .rest .converter .BytesPayloadConverter ;
4254import org .radarbase .connect .rest .converter .StringPayloadConverter ;
4355import org .radarbase .connect .rest .selector .SimpleTopicSelector ;
4456import org .radarbase .connect .rest .single .SingleRestSourceConnector ;
4557import org .radarbase .connect .rest .single .SingleRestSourceConnectorConfig ;
4658
59+ @ ExtendWith (WireMockRule .class )
4760public 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