You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by gn...@apache.org on 2020/07/20 19:21:22 UTC
[camel] 04/08: [CAMEL-11807] Upgrade camel-rest-swagger to junit5
This is an automated email from the ASF dual-hosted git repository.
gnodet pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git
commit 5204b09fd9eb9fd94c9fe18e044802d8b9c1f9ed
Author: Guillaume Nodet <gn...@gmail.com>
AuthorDate: Mon Jul 20 13:44:53 2020 +0200
[CAMEL-11807] Upgrade camel-rest-swagger to junit5
---
components/camel-rest-swagger/pom.xml | 8 +-
.../camel/component/rest/swagger/HttpsTest.java | 56 +++++++++-----
.../rest/swagger/RestSwaggerComponentTest.java | 86 ++++++++++++++--------
.../rest/swagger/RestSwaggerDelegateHttpsTest.java | 3 -
.../rest/swagger/RestSwaggerEndpointTest.java | 13 ++--
.../swagger/RestSwaggerEndpointUriParsingTest.java | 34 ++-------
.../rest/swagger/RestSwaggerHelperTest.java | 18 +++--
7 files changed, 124 insertions(+), 94 deletions(-)
diff --git a/components/camel-rest-swagger/pom.xml b/components/camel-rest-swagger/pom.xml
index b5d35c8..383d1c1 100644
--- a/components/camel-rest-swagger/pom.xml
+++ b/components/camel-rest-swagger/pom.xml
@@ -79,19 +79,19 @@
<!-- test -->
<dependency>
<groupId>org.apache.camel</groupId>
- <artifactId>camel-test</artifactId>
+ <artifactId>camel-test-junit5</artifactId>
<scope>test</scope>
</dependency>
<dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
+ <groupId>org.junit.jupiter</groupId>
+ <artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
- <artifactId>mockito-core</artifactId>
+ <artifactId>mockito-junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
diff --git a/components/camel-rest-swagger/src/test/java/org/apache/camel/component/rest/swagger/HttpsTest.java b/components/camel-rest-swagger/src/test/java/org/apache/camel/component/rest/swagger/HttpsTest.java
index d0f5e48..dcaf0f0 100644
--- a/components/camel-rest-swagger/src/test/java/org/apache/camel/component/rest/swagger/HttpsTest.java
+++ b/components/camel-rest-swagger/src/test/java/org/apache/camel/component/rest/swagger/HttpsTest.java
@@ -32,8 +32,8 @@ import javax.net.ssl.TrustManagerFactory;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
+import com.github.tomakehurst.wiremock.WireMockServer;
import com.github.tomakehurst.wiremock.common.HttpsSettings;
-import com.github.tomakehurst.wiremock.junit.WireMockRule;
import com.google.common.io.Resources;
import org.apache.camel.CamelContext;
import org.apache.camel.RoutesBuilder;
@@ -43,16 +43,14 @@ import org.apache.camel.converter.jaxb.JaxbDataFormat;
import org.apache.camel.support.jsse.CipherSuitesParameters;
import org.apache.camel.support.jsse.SSLContextParameters;
import org.apache.camel.support.jsse.TrustManagersParameters;
-import org.apache.camel.test.junit4.CamelTestSupport;
+import org.apache.camel.test.junit5.CamelTestSupport;
import org.eclipse.jetty.util.resource.Resource;
import org.eclipse.jetty.util.security.CertificateUtils;
-import org.junit.Before;
-import org.junit.BeforeClass;
-import org.junit.ClassRule;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.junit.runners.Parameterized;
-import org.junit.runners.Parameterized.Parameter;
+import org.junit.jupiter.api.AfterAll;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.MethodSource;
import org.junit.runners.Parameterized.Parameters;
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
@@ -61,28 +59,47 @@ import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static com.github.tomakehurst.wiremock.client.WireMock.getRequestedFor;
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
-@RunWith(Parameterized.class)
public abstract class HttpsTest extends CamelTestSupport {
- @ClassRule
- public static WireMockRule petstore = new WireMockRule(
+ protected static WireMockServer petstore = new WireMockServer(
wireMockConfig().httpServerFactory(new Jetty94ServerFactory()).containerThreads(13).dynamicPort()
.dynamicHttpsPort().keystorePath(Resources.getResource("localhost.p12").toString()).keystoreType("PKCS12")
.keystorePassword("changeit"));
static final Object NO_BODY = null;
- @Parameter
public String componentName;
- @Before
+ @BeforeAll
+ public static void startWireMockServer() {
+ petstore.start();
+ }
+
+ @AfterAll
+ public static void stopWireMockServer() {
+ petstore.stop();
+ }
+
+ @Override
+ public void setUp() throws Exception {
+ }
+
+ @BeforeEach
public void resetWireMock() {
petstore.resetRequests();
}
- @Test
- public void shouldBeConfiguredForHttps() throws Exception {
+ public void doSetUp(String componentName) throws Exception {
+ this.componentName = componentName;
+ super.setUp();
+ }
+
+ @ParameterizedTest @MethodSource("knownProducers")
+ public void shouldBeConfiguredForHttps(String componentName) throws Exception {
+ doSetUp(componentName);
final Pet pet = template.requestBodyAndHeader("direct:getPetById", NO_BODY, "petId", 14, Pet.class);
assertNotNull(pet);
@@ -95,8 +112,9 @@ public abstract class HttpsTest extends CamelTestSupport {
}
- @Test
- public void swaggerJsonOverHttps() throws Exception {
+ @ParameterizedTest @MethodSource("knownProducers")
+ public void swaggerJsonOverHttps(String componentName) throws Exception {
+ doSetUp(componentName);
final Pet pet = template.requestBodyAndHeader("direct:httpsJsonGetPetById", NO_BODY, "petId", 14, Pet.class);
assertNotNull(pet);
@@ -146,7 +164,7 @@ public abstract class HttpsTest extends CamelTestSupport {
return producers;
}
- @BeforeClass
+ @BeforeAll
public static void setupStubs() throws IOException, URISyntaxException {
petstore.stubFor(get(urlEqualTo("/swagger.json")).willReturn(aResponse().withBody(
Files.readAllBytes(Paths.get(RestSwaggerGlobalHttpsTest.class.getResource("/swagger.json").toURI())))));
diff --git a/components/camel-rest-swagger/src/test/java/org/apache/camel/component/rest/swagger/RestSwaggerComponentTest.java b/components/camel-rest-swagger/src/test/java/org/apache/camel/component/rest/swagger/RestSwaggerComponentTest.java
index f7a87fc..5b72cfe 100644
--- a/components/camel-rest-swagger/src/test/java/org/apache/camel/component/rest/swagger/RestSwaggerComponentTest.java
+++ b/components/camel-rest-swagger/src/test/java/org/apache/camel/component/rest/swagger/RestSwaggerComponentTest.java
@@ -29,21 +29,18 @@ import java.util.Map;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
-import com.github.tomakehurst.wiremock.junit.WireMockRule;
+import com.github.tomakehurst.wiremock.WireMockServer;
import org.apache.camel.CamelContext;
import org.apache.camel.RoutesBuilder;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.rest.RestEndpoint;
import org.apache.camel.converter.jaxb.JaxbDataFormat;
-import org.apache.camel.test.junit4.CamelTestSupport;
-import org.junit.Before;
-import org.junit.BeforeClass;
-import org.junit.ClassRule;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.junit.runners.Parameterized;
-import org.junit.runners.Parameterized.Parameter;
-import org.junit.runners.Parameterized.Parameters;
+import org.apache.camel.test.junit5.CamelTestSupport;
+import org.junit.jupiter.api.AfterAll;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.MethodSource;
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.equalTo;
@@ -54,25 +51,45 @@ import static com.github.tomakehurst.wiremock.client.WireMock.postRequestedFor;
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
-@RunWith(Parameterized.class)
public class RestSwaggerComponentTest extends CamelTestSupport {
- @ClassRule
- public static WireMockRule petstore = new WireMockRule(wireMockConfig().dynamicPort());
-
static final Object NO_BODY = null;
- @Parameter
+ private static WireMockServer petstore = new WireMockServer(wireMockConfig().dynamicPort());
+
public String componentName;
- @Before
+ @BeforeAll
+ public static void startWireMockServer() {
+ petstore.start();
+ }
+
+ @AfterAll
+ public static void stopWireMockServer() {
+ petstore.stop();
+ }
+
+ @Override
+ public void setUp() throws Exception {
+ }
+
+ @BeforeEach
public void resetWireMock() {
petstore.resetRequests();
}
- @Test
- public void shouldBeAddingPets() {
+ public void doSetUp(String componentName) throws Exception {
+ this.componentName = componentName;
+ super.setUp();
+ }
+
+ @ParameterizedTest @MethodSource("knownProducers")
+ public void shouldBeAddingPets(String componentName) throws Exception {
+ doSetUp(componentName);
+
final Pet pet = new Pet();
pet.name = "Jean-Luc Picard";
@@ -87,8 +104,10 @@ public class RestSwaggerComponentTest extends CamelTestSupport {
.withHeader("Content-Type", equalTo("application/xml")));
}
- @Test
- public void shouldBeGettingPetsById() {
+ @ParameterizedTest @MethodSource("knownProducers")
+ public void shouldBeGettingPetsById(String componentName) throws Exception {
+ doSetUp(componentName);
+
final Pet pet = template.requestBodyAndHeader("direct:getPetById", NO_BODY, "petId", 14, Pet.class);
assertNotNull(pet);
@@ -100,8 +119,10 @@ public class RestSwaggerComponentTest extends CamelTestSupport {
equalTo("application/xml, application/json")));
}
- @Test
- public void shouldBeGettingPetsByIdSpecifiedInEndpointParameters() {
+ @ParameterizedTest @MethodSource("knownProducers")
+ public void shouldBeGettingPetsByIdSpecifiedInEndpointParameters(String componentName) throws Exception {
+ doSetUp(componentName);
+
final Pet pet = template.requestBody("direct:getPetByIdWithEndpointParams", NO_BODY, Pet.class);
assertNotNull(pet);
@@ -113,8 +134,10 @@ public class RestSwaggerComponentTest extends CamelTestSupport {
equalTo("application/xml, application/json")));
}
- @Test
- public void shouldBeGettingPetsByIdWithApiKeysInHeader() {
+ @ParameterizedTest @MethodSource("knownProducers")
+ public void shouldBeGettingPetsByIdWithApiKeysInHeader(String componentName) throws Exception {
+ doSetUp(componentName);
+
final Map<String, Object> headers = new HashMap<>();
headers.put("petId", 14);
headers.put("api_key", "dolphins");
@@ -130,8 +153,10 @@ public class RestSwaggerComponentTest extends CamelTestSupport {
.withHeader("api_key", equalTo("dolphins")));
}
- @Test
- public void shouldBeGettingPetsByIdWithApiKeysInQueryParameter() {
+ @ParameterizedTest @MethodSource("knownProducers")
+ public void shouldBeGettingPetsByIdWithApiKeysInQueryParameter(String componentName) throws Exception {
+ doSetUp(componentName);
+
final Map<String, Object> headers = new HashMap<>();
headers.put("petId", 14);
headers.put("api_key", "dolphins");
@@ -146,8 +171,10 @@ public class RestSwaggerComponentTest extends CamelTestSupport {
equalTo("application/xml, application/json")));
}
- @Test
- public void shouldBeGettingPetsByStatus() {
+ @ParameterizedTest @MethodSource("knownProducers")
+ public void shouldBeGettingPetsByStatus(String componentName) throws Exception {
+ doSetUp(componentName);
+
final Pets pets = template.requestBodyAndHeader("direct:findPetsByStatus", NO_BODY, "status", "available",
Pets.class);
@@ -202,12 +229,11 @@ public class RestSwaggerComponentTest extends CamelTestSupport {
};
}
- @Parameters(name = "component = {0}")
public static Iterable<String> knownProducers() {
return Arrays.asList(RestEndpoint.DEFAULT_REST_PRODUCER_COMPONENTS);
}
- @BeforeClass
+ @BeforeAll
public static void setupStubs() throws IOException, URISyntaxException {
petstore.stubFor(get(urlEqualTo("/swagger.json")).willReturn(aResponse().withBody(
Files.readAllBytes(Paths.get(RestSwaggerComponentTest.class.getResource("/swagger.json").toURI())))));
diff --git a/components/camel-rest-swagger/src/test/java/org/apache/camel/component/rest/swagger/RestSwaggerDelegateHttpsTest.java b/components/camel-rest-swagger/src/test/java/org/apache/camel/component/rest/swagger/RestSwaggerDelegateHttpsTest.java
index a9cd380..52c77c2 100644
--- a/components/camel-rest-swagger/src/test/java/org/apache/camel/component/rest/swagger/RestSwaggerDelegateHttpsTest.java
+++ b/components/camel-rest-swagger/src/test/java/org/apache/camel/component/rest/swagger/RestSwaggerDelegateHttpsTest.java
@@ -20,10 +20,7 @@ import org.apache.camel.CamelContext;
import org.apache.camel.Component;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.support.PropertyBindingSupport;
-import org.junit.runner.RunWith;
-import org.junit.runners.Parameterized;
-@RunWith(Parameterized.class)
public class RestSwaggerDelegateHttpsTest extends HttpsTest {
@Override
diff --git a/components/camel-rest-swagger/src/test/java/org/apache/camel/component/rest/swagger/RestSwaggerEndpointTest.java b/components/camel-rest-swagger/src/test/java/org/apache/camel/component/rest/swagger/RestSwaggerEndpointTest.java
index 0a6d289..af65636 100644
--- a/components/camel-rest-swagger/src/test/java/org/apache/camel/component/rest/swagger/RestSwaggerEndpointTest.java
+++ b/components/camel-rest-swagger/src/test/java/org/apache/camel/component/rest/swagger/RestSwaggerEndpointTest.java
@@ -34,10 +34,11 @@ import io.swagger.models.parameters.QueryParameter;
import org.apache.camel.CamelContext;
import org.apache.camel.impl.engine.DefaultClassResolver;
import org.apache.camel.spi.RestConfiguration;
-import org.junit.Test;
+import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.entry;
+import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@@ -47,7 +48,7 @@ public class RestSwaggerEndpointTest {
URI endpointUri = URI.create("endpoint.json");
- @Test(expected = IllegalArgumentException.class)
+ @Test
public void shouldComplainForUnknownOperations() throws Exception {
final CamelContext camelContext = mock(CamelContext.class);
when(camelContext.getClassResolver()).thenReturn(new DefaultClassResolver());
@@ -57,7 +58,8 @@ public class RestSwaggerEndpointTest {
final RestSwaggerEndpoint endpoint = new RestSwaggerEndpoint("rest-swagger:unknown", "unknown", component,
Collections.emptyMap());
- endpoint.createProducer();
+ assertThrows(IllegalArgumentException.class,
+ () -> endpoint.createProducer());
}
@Test
@@ -351,12 +353,13 @@ public class RestSwaggerEndpointTest {
assertThat(RestSwaggerEndpoint.pickBestScheme("file", null)).isNull();
}
- @Test(expected = IllegalArgumentException.class)
+ @Test
public void shouldRaiseExceptionsForMissingSpecifications() throws IOException {
final CamelContext camelContext = mock(CamelContext.class);
when(camelContext.getClassResolver()).thenReturn(new DefaultClassResolver());
- RestSwaggerEndpoint.loadSpecificationFrom(camelContext, URI.create("non-existant.json"), null);
+ assertThrows(IllegalArgumentException.class,
+ () -> RestSwaggerEndpoint.loadSpecificationFrom(camelContext, URI.create("non-existant.json"), null));
}
@Test
diff --git a/components/camel-rest-swagger/src/test/java/org/apache/camel/component/rest/swagger/RestSwaggerEndpointUriParsingTest.java b/components/camel-rest-swagger/src/test/java/org/apache/camel/component/rest/swagger/RestSwaggerEndpointUriParsingTest.java
index 61cc70f..0c7a81a 100644
--- a/components/camel-rest-swagger/src/test/java/org/apache/camel/component/rest/swagger/RestSwaggerEndpointUriParsingTest.java
+++ b/components/camel-rest-swagger/src/test/java/org/apache/camel/component/rest/swagger/RestSwaggerEndpointUriParsingTest.java
@@ -19,31 +19,15 @@ package org.apache.camel.component.rest.swagger;
import java.util.Arrays;
import java.util.Collections;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.junit.runners.Parameterized;
-import org.junit.runners.Parameterized.Parameter;
-import org.junit.runners.Parameterized.Parameters;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.MethodSource;
import static org.assertj.core.api.Assertions.assertThat;
-@RunWith(Parameterized.class)
public class RestSwaggerEndpointUriParsingTest {
- @Parameter(3)
- public String operationId;
-
- @Parameter(1)
- public String remaining;
-
- @Parameter(2)
- public String specificationUri;
-
- @Parameter(0)
- public String uri;
-
- @Test
- public void shouldParseEndpointUri() {
+ @ParameterizedTest @MethodSource("parameters")
+ public void shouldParseEndpointUri(String uri, String remaining, String specificationUri, String operationId) {
final RestSwaggerComponent component = new RestSwaggerComponent();
final RestSwaggerEndpoint endpoint = new RestSwaggerEndpoint(specificationUri, remaining, component,
@@ -53,16 +37,14 @@ public class RestSwaggerEndpointUriParsingTest {
assertThat(endpoint.getOperationId()).isEqualTo(operationId);
}
- @Parameters(name = "uri={0}, remaining={1}")
public static Iterable<Object[]> parameters() {
- return Arrays.asList(params("rest-swagger:operation", "operation", "swagger.json", "operation"),
+ return Arrays.asList(
+ params("rest-swagger:operation", "operation", "swagger.json", "operation"),
params("rest-swagger:my-api.json#operation", "my-api.json#operation", "my-api.json", "operation"),
- params("rest-swagger:http://api.example.com/swagger.json#operation",
- "http://api.example.com/swagger.json#operation", "http://api.example.com/swagger.json", "operation"));
+ params("rest-swagger:http://api.example.com/swagger.json#operation", "http://api.example.com/swagger.json#operation", "http://api.example.com/swagger.json", "operation"));
}
- static Object[] params(final String uri, final String remaining, final String specificationUri,
- final String operationId) {
+ static Object[] params(final String uri, final String remaining, final String specificationUri, final String operationId) {
return new Object[] {uri, remaining, specificationUri, operationId};
}
}
diff --git a/components/camel-rest-swagger/src/test/java/org/apache/camel/component/rest/swagger/RestSwaggerHelperTest.java b/components/camel-rest-swagger/src/test/java/org/apache/camel/component/rest/swagger/RestSwaggerHelperTest.java
index 975c09b..8ddf13f 100644
--- a/components/camel-rest-swagger/src/test/java/org/apache/camel/component/rest/swagger/RestSwaggerHelperTest.java
+++ b/components/camel-rest-swagger/src/test/java/org/apache/camel/component/rest/swagger/RestSwaggerHelperTest.java
@@ -16,25 +16,29 @@
*/
package org.apache.camel.component.rest.swagger;
-import org.junit.Test;
+import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.junit.jupiter.api.Assertions.assertThrows;
public class RestSwaggerHelperTest {
- @Test(expected = IllegalArgumentException.class)
+ @Test
public void emptyHostParamsAreNotAllowed() {
- RestSwaggerHelper.isHostParam("");
+ assertThrows(IllegalArgumentException.class,
+ () -> RestSwaggerHelper.isHostParam(""));
}
- @Test(expected = IllegalArgumentException.class)
+ @Test
public void nonUriHostParametersAreNotAllowed() {
- RestSwaggerHelper.isHostParam("carrot");
+ assertThrows(IllegalArgumentException.class,
+ () -> RestSwaggerHelper.isHostParam("carrot"));
}
- @Test(expected = IllegalArgumentException.class)
+ @Test
public void nullHostParamsAreNotAllowed() {
- RestSwaggerHelper.isHostParam(null);
+ assertThrows(IllegalArgumentException.class,
+ () -> RestSwaggerHelper.isHostParam(null));
}
@Test