diff --git a/resteasy-spring-boot-starter-test/src/test/java/com/test/resourcesprovidersperapp/EchoMessage.java b/resteasy-spring-boot-starter-test/src/test/java/com/test/resourcesprovidersperapp/EchoMessage.java new file mode 100644 index 00000000..c35ff793 --- /dev/null +++ b/resteasy-spring-boot-starter-test/src/test/java/com/test/resourcesprovidersperapp/EchoMessage.java @@ -0,0 +1,27 @@ +package com.test.resourcesprovidersperapp; + +/** + * A simple echo message, containing the text to be echoed + * and timestamp of the moment the message was created + * + * @author facarvalho + */ +public class EchoMessage { + + private long timestamp; + private String echoText; + + public EchoMessage(String echoText) { + timestamp = System.currentTimeMillis(); + this.echoText = echoText; + } + + public long getTimestamp() { + return timestamp; + } + + public String getEchoText() { + return echoText; + } + +} \ No newline at end of file diff --git a/resteasy-spring-boot-starter-test/src/test/java/com/test/resourcesprovidersperapp/EchoMessageCreator.java b/resteasy-spring-boot-starter-test/src/test/java/com/test/resourcesprovidersperapp/EchoMessageCreator.java new file mode 100644 index 00000000..e839a4d9 --- /dev/null +++ b/resteasy-spring-boot-starter-test/src/test/java/com/test/resourcesprovidersperapp/EchoMessageCreator.java @@ -0,0 +1,20 @@ +package com.test.resourcesprovidersperapp; + +import com.test.multicontexttest.EchoMessage; +import org.springframework.stereotype.Component; + +/** + * This bean creates {@link EchoMessage} objects but, + * different than the original application it was copied from, + * ignoring echo texts received as input + * + * @author facarvalho + */ +@Component +public class EchoMessageCreator { + + public EchoMessage createEchoMessage(String echoText) { + return new EchoMessage("I don't want to echo anything today"); + } + +} diff --git a/resteasy-spring-boot-starter-test/src/test/java/com/test/resourcesprovidersperapp/EchoV1.java b/resteasy-spring-boot-starter-test/src/test/java/com/test/resourcesprovidersperapp/EchoV1.java new file mode 100644 index 00000000..ab13a42b --- /dev/null +++ b/resteasy-spring-boot-starter-test/src/test/java/com/test/resourcesprovidersperapp/EchoV1.java @@ -0,0 +1,40 @@ +package com.test.resourcesprovidersperapp; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import javax.ws.rs.Consumes; +import javax.ws.rs.POST; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import javax.ws.rs.core.MediaType; + +/** + * Echo REST endpoint class + * + * Created by facarvalho on 12/7/15. + */ +@Path("/echo1") +@Component +public class EchoV1 { + + @Autowired + private EchoMessageCreator echoer; + + /** + * Receives a simple POST request message containing as payload + * a text, in text plain format, to be echoed by the service. + * It returns as response, in JSON, the text to be echoed plus a timestamp of the + * moment the echo response was created on the server side + * + * @param echoText + * @return + */ + @POST + @Consumes({ MediaType.TEXT_PLAIN }) + @Produces({ MediaType.APPLICATION_JSON }) + public EchoMessage echo(String echoText) { + return new EchoMessage("echoed v1 -> " + echoText); + } + +} diff --git a/resteasy-spring-boot-starter-test/src/test/java/com/test/resourcesprovidersperapp/EchoV2.java b/resteasy-spring-boot-starter-test/src/test/java/com/test/resourcesprovidersperapp/EchoV2.java new file mode 100644 index 00000000..d5e58444 --- /dev/null +++ b/resteasy-spring-boot-starter-test/src/test/java/com/test/resourcesprovidersperapp/EchoV2.java @@ -0,0 +1,40 @@ +package com.test.resourcesprovidersperapp; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import javax.ws.rs.Consumes; +import javax.ws.rs.POST; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import javax.ws.rs.core.MediaType; + +/** + * Echo REST endpoint class + * + * Created by facarvalho on 12/7/15. + */ +@Path("/echo2") +@Component +public class EchoV2 { + + @Autowired + private EchoMessageCreator echoer; + + /** + * Receives a simple POST request message containing as payload + * a text, in text plain format, to be echoed by the service. + * It returns as response, in JSON, the text to be echoed plus a timestamp of the + * moment the echo response was created on the server side + * + * @param echoText + * @return + */ + @POST + @Consumes({ MediaType.TEXT_PLAIN }) + @Produces({ MediaType.APPLICATION_JSON }) + public EchoMessage echo(String echoText) { + return new EchoMessage("echoed v2 -> " + echoText); + } + +} diff --git a/resteasy-spring-boot-starter-test/src/test/java/com/test/resourcesprovidersperapp/JaxrsApplicationV1.java b/resteasy-spring-boot-starter-test/src/test/java/com/test/resourcesprovidersperapp/JaxrsApplicationV1.java new file mode 100644 index 00000000..a508f947 --- /dev/null +++ b/resteasy-spring-boot-starter-test/src/test/java/com/test/resourcesprovidersperapp/JaxrsApplicationV1.java @@ -0,0 +1,20 @@ +package com.test.resourcesprovidersperapp; + +import org.springframework.stereotype.Component; + +import javax.ws.rs.ApplicationPath; +import javax.ws.rs.core.Application; +import java.util.HashSet; +import java.util.Set; + +@Component +@ApplicationPath("/resourcesproviders/") +public class JaxrsApplicationV1 extends Application { + + @Override + public Set> getClasses() { + Set> classes = new HashSet>(); + classes.add(EchoV1.class); + return classes; + } +} \ No newline at end of file diff --git a/resteasy-spring-boot-starter-test/src/test/java/com/test/resourcesprovidersperapp/ResoucesAndProvidersPerApp.java b/resteasy-spring-boot-starter-test/src/test/java/com/test/resourcesprovidersperapp/ResoucesAndProvidersPerApp.java new file mode 100644 index 00000000..f8b2933f --- /dev/null +++ b/resteasy-spring-boot-starter-test/src/test/java/com/test/resourcesprovidersperapp/ResoucesAndProvidersPerApp.java @@ -0,0 +1,18 @@ +package com.test.resourcesprovidersperapp; + + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; + +/** + * SpringBoot entry point application + *

+ * Created by facarvalho on 12/7/15. + */ +@SpringBootApplication +public class ResoucesAndProvidersPerApp extends SpringBootServletInitializer { + public static void main(String[] args) { + SpringApplication.run(ResoucesAndProvidersPerApp.class, args); + } +} diff --git a/resteasy-spring-boot-starter-test/src/test/java/com/test/resourcesprovidersperapp/ResourcesAndProvidersPerAppIT.java b/resteasy-spring-boot-starter-test/src/test/java/com/test/resourcesprovidersperapp/ResourcesAndProvidersPerAppIT.java new file mode 100644 index 00000000..af09eca0 --- /dev/null +++ b/resteasy-spring-boot-starter-test/src/test/java/com/test/resourcesprovidersperapp/ResourcesAndProvidersPerAppIT.java @@ -0,0 +1,52 @@ +package com.test.resourcesprovidersperapp; + +import io.restassured.RestAssured; +import io.restassured.response.Response; +import org.springframework.boot.SpringApplication; +import org.springframework.util.SocketUtils; +import org.testng.Assert; +import org.testng.annotations.AfterClass; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; + +import static io.restassured.RestAssured.given; +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.CoreMatchers.notNullValue; + +/** + * This is an integration test based on a simple sample application and + * very common use cases (see sample-app project) + * + * @author facarvalho + */ +public class ResourcesAndProvidersPerAppIT { + + @BeforeClass + public void startingApplicationUp() { + RestAssured.basePath = "/resourcesproviders/"; + int port = SocketUtils.findAvailableTcpPort(); + RestAssured.port = port; + System.out.println("::: " + port); + SpringApplication springApplication = new SpringApplication(ResoucesAndProvidersPerApp.class); + springApplication.run("--server.port=" + port).registerShutdownHook(); + } + + @AfterClass + public void shuttingDownApplication() { + Response response = given().basePath("/").contentType("application/json").post("/actuator/shutdown"); + response.then().statusCode(200).body("message", equalTo("Shutting down, bye...")); + } + + @Test + public void echoSucceeds() { + Response response = given().body("oh behave").post("/echo1"); + response.then().statusCode(200).body("timestamp", notNullValue()).body("echoText", equalTo + ("echoed v1 -> oh behave")); + } + + @Test + public void echoV2MustNotBeReachable() { + Response response = given().body("oh behave").post("/echo2"); + Assert.assertNotEquals(200, response.getStatusCode(), "echo2 must not be reachable"); + } +}