Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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;
}

}
Original file line number Diff line number Diff line change
@@ -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");
}

}
Original file line number Diff line number Diff line change
@@ -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);
}

}
Original file line number Diff line number Diff line change
@@ -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);
}

}
Original file line number Diff line number Diff line change
@@ -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<Class<?>> getClasses() {
Set<Class<?>> classes = new HashSet<Class<?>>();
classes.add(EchoV1.class);
return classes;
}
}
Original file line number Diff line number Diff line change
@@ -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
* <p>
* Created by facarvalho on 12/7/15.
*/
@SpringBootApplication
public class ResoucesAndProvidersPerApp extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(ResoucesAndProvidersPerApp.class, args);
}
}
Original file line number Diff line number Diff line change
@@ -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");
}
}