I am trying to write a mini-library for testing to mock common external services such as E-mail, SFTP, Buckets, HTTP APIs.
At the moment, I got stuck on WireMockServer. In WireMock docs it states that I can create both server and client to verify API calls.
I wrote the class:
public class WireMockTestServer {
private final WireMockServer server;
public WireMockTestServer(String address, MappingBuilder mappingBuilder) {
server = new WireMockServer(wireMockConfig().dynamicPort().dynamicHttpsPort());
}
public WireMockTestServer(int httpPort, int httpsPort, String address, MappingBuilder mappingBuilder) {
server = setup(
new WireMockServer(wireMockConfig().port(httpPort).httpsPort(httpsPort).bindAddress(address)),
mappingBuilder
);
}
private WireMockServer setup(WireMockServer server, MappingBuilder mappingBuilder) {
server.stubFor(mappingBuilder);
return server;
}
public void start() {
server.start();
}
public void stop() {
server.stop();
}
}
which I can path endpoint declaration and redirect my services toward it.
When I am trying to test it:
public class WireMockTestServerTest {
#Test
public void testSetup() throws Exception {
MappingBuilder mappingBuilder = get(urlEqualTo("/health"))
.willReturn(aResponse().withHeader("Content-Type", "application/json")
.withStatus(200));
WireMockTestServer server = new WireMockTestServer(8888, 9988, "127.0.0.1", mappingBuilder);
server.start();
// This line should fail
verify(getRequestedFor(urlEqualTo("/health")).withHeader("Content-Type", equalTo("text/xml")));
server.stop();
}
}
The test fails. The issue is, it fails not because of an assertion but because it starts on a wrong port 8080 which is occupied by other processes.
How can I start WireMockServer on another port and test it with JUnit 5?
I am using Java 8, Maven, Spring Boot.
As mentioned in comment static verify method tries to verify against default wiremock instance. Since you are creating a standalone instance in your test you should verify against it. Create a verify method in your WireMockTestServer :
public void verify(final RequestPatternBuilder requestPatternBuilder) {
server.verify(requestPatternBuilder);
}
and then you can verify against it :
#Test
public void testSetup() throws Exception {
MappingBuilder mappingBuilder = get(urlEqualTo("/health"))
.willReturn(aResponse().withHeader("Content-Type", "application/json")
.withStatus(200));
WireMockTestServer server = new WireMockTestServer(8888, 9988, "127.0.0.1", mappingBuilder);
server.start();
// This line should fail
server.verify(getRequestedFor(urlEqualTo("/health")).withHeader("Content-Type", equalTo("text/xml")));
server.stop();
}
Related
I have used this code using camel. Can some code be written to send mail without enabling less secure apps. What are the ways of sending mail without enabling less secure apps.
public class SendMail {
public static void main(String args[]) throws Exception {
CamelContext context = new DefaultCamelContext();
context.addRoutes(new RouteBuilder() {
#Override
public void configure() {
from("direct:start")
.to("smtp://smtp.gmail.com:587?username=XYZ#gmail.com&password=Password&from=ABC#gmail.com&contentType=text/html&mail.smtp.starttls.enable=true&mail.smtp.auth=true")
.log("Email sent");
}
});
context.start();
Endpoint endpoint = context.getEndpoint("direct:start");
Exchange exchange = endpoint.createExchange();
Message in = exchange.getIn();
in.setHeader("subject", "Camel logo updated !");
in.setHeader("to", "ABC#gmail.com");
in.setHeader("from", "XYZ#gmail.com");
in.setBody("Logo is in attachment");
Producer producer = endpoint.createProducer();
producer.start();
producer.process(exchange);
context.stop();
}
}
I am new to both springboot and websockets so please be gentle and this is my last question chance on this account. I have a websocket jar that sends messages from two urls ws:localhost/operations and ws:localhost/prices from 8080 port. My task is to read those messages. The Jar file streams messages like:
For operation:
"data":{ "description":"lorem ipsum", "id":"OJ1136453723" },
"type":"DELETE"
For price:
"data":{ "price":1384.1685, "id":"WN6427148286" }, "type":"PRICE"
I have spring-boot-starter-websocket, spring-boot-starter-security and lombok as dependencies.
I seem to be connecting to the jar file, because powershell says "Socket connected
Frame TEXT (fin=true, buffer len = 48)" when I run my code but it only hits the Application breakpoints whenever I debug, it doesnt even go to controller or config classes. It may be an obvious mistake but I am a bit lost so any help would be appriciated. Also, my port for this client is 8443 which is specified in application.yml and it starts there.
My Client:
public class WSClient{
private static String URL = "ws://localhost:8080";
public static void main(String[] args) throws Exception {
WebSocketClient client = new StandardWebSocketClient();
WebSocketStompClient stompClient = new WebSocketStompClient(client);
stompClient.setMessageConverter(new MappingJackson2MessageConverter());
StompSessionHandler sessionHandler = new MyStompSessionHandler();
stompClient.connect(URL, sessionHandler);
new Scanner(System.in).nextLine();
}
Controller:
#Controller
public class MsgController {
#Autowired
private SimpMessagingTemplate simpMessagingTemplate;
#MessageMapping("/operations")
public Operations operations(Operations operations)throws Exception{
System.out.println(operations);
simpMessagingTemplate.convertAndSend("/operations", operations);
return operations;
}
StompSessionHandler:
public class MyStompSessionHandler extends StompSessionHandlerAdapter {
private Logger logger = LogManager.getLogger(MyStompSessionHandler.class);
#Override
public void afterConnected(StompSession session, StompHeaders connectedHeaders) {
logger.info("New session established : " + session.getSessionId());
session.subscribe("/operations", this);
System.out.println("Subscribed to /operations");
logger.info("Subscribed to /operations");
}
#Override
public void handleException(StompSession session, StompCommand command, StompHeaders headers, byte[] payload, Throwable exception) {
logger.error("Got an exception", exception);
}
#Override
public Type getPayloadType(StompHeaders headers) {
return Operations.class;
}
#Override
public void handleFrame(StompHeaders headers, Object payload) {
Operations ops = (Operations) payload;
System.out.println(ops);
}
}
Operations:
#lombok.Data
public class Operations{
public String description;
public String id;
public String type;
}
EDIT:
Thanks to help of #OkanKonur the problem is that StompHandler's afterConnected is not called even though it seems like they are connected. We tried it with the github link he's given in the comments and it seems to work. The jar file is most probably not a spring project so is there a way to solve this? It still can't read anything.
When calling a JAX-WS endpoint, how can I get the HTTP response code?
In the sample code bellow, when calling the web service at port.getCustomer(customerID); an Exception may be thrown, such as 401 or 500.
In such cases, how can I get the HTTP status code from the HTTP response?
#Stateless
public class CustomerWSClient {
#WebServiceRef(wsdlLocation = "/customer.wsdl")
private CustomerService service;
public void getCustomer(Integer customerID) throws Exception {
Customer port = service.getCustomerPort();
port.getCustomer(customerID); // how to get HTTP status
}
}
Completing #Praveen answer, you have to turn the port into a raw BindingProvider and then get the values from the context.
Don't forget that transaction will be marked for rollback if an exception occours in your managed web service client.
#Stateless
public class CustomerWSClient {
#WebServiceRef(wsdlLocation = "/customer.wsdl")
private CustomerService service;
public void getCustomer(Integer customerID) throws Exception {
Customer port = service.getCustomerPort();
try {
port.getCustomer(customerID);
} catch(Exception e) {
throw e;
} finally {
// Get the HTTP code here!
int responseCode = (Integer)((BindingProvider) port).getResponseContext().get(MessageContext.HTTP_RESPONSE_CODE);
}
}
}
The below post is similar to your question. Hopefully it should work for you
https://stackoverflow.com/a/35914837/4896191
I have this code for server:
Undertow server = Undertow.builder()
.addHttpListener(8080, "localhost")
.setHandler(Handlers.path()
.addPrefixPath("/item", new ItemHandler())
)
.build();
server.start();
And handler:
private class ItemHandler implements HttpHandler {
#Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "application/json");
exchange.getPathParameters(); // always null
//ItemModel item = new ItemModel(1);
//exchange.getResponseSender().send(mapper.writeValueAsString(item));
}
}
I want to send request /item/10 and get 10 in my parameter. How to specify path and get it?
You need a PathTemplateHandler and not a PathHandler, see:
Undertow server = Undertow.builder()
.addHttpListener(8080, "0.0.0.0")
.setHandler(Handlers.pathTemplate()
.add("/item/{itemId}", new ItemHandler())
)
.build();
server.start();
Then, in your ItemHandler:
class ItemHandler implements HttpHandler {
#Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "application/json");
// Method 1
PathTemplateMatch pathMatch = exchange.getAttachment(PathTemplateMatch.ATTACHMENT_KEY);
String itemId1 = pathMatch.getParameters().get("itemId");
// Method 2
String itemId2 = exchange.getQueryParameters().get("itemId").getFirst();
}
}
The method 2 works because Undertow merges parameters in the path with the query parameters by default.
If you do not want this behavior, you can use instead:
Handlers.pathTemplate(false)
The same applies to the RoutingHandler, this is probably what you want to use eventually to handle multiple paths.
Handlers.rounting() or Handlers.routing(false)
I'm writing integration JUnit test. My task is to test whether the response of my local server is correct. The mentioned server takes as a GET parameter an address of page to be analysed (for example: localhost:8000/test?url=http://www.example.com).
To avoid being dependent on www.example.com I want to start for this particular test my own jetty server, which always serves the same content.
private static class MockPageHandler extends AbstractHandler {
public void handle(String target,Request baseRequest, HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
response.setContentType("text/html; charset=utf-8");
response.setStatus(HttpServletResponse.SC_OK);
final String responseString = loadResource("index.html");
response.getWriter().write(responseString);
baseRequest.setHandled(true);
}
}
public void test() throws Exception {
final int PORT = 8080;
final Server server = new Server(PORT);
server.setHandler(new MockPageHandler());
server.start();
final ContentResponse response =
client.newRequest("http://localhost:8000/test?url=http://localhost:8080").send();
/* some assertions. */
server.stop();
server.join();
}
Every time I execute this test, the handle method in MockPageHandler is never invoked.
Do you have any suggestions why this not works?
P.S. When I remove server.stop() and in browser type http://localhost:8080 the proper page is shown.
Quick answer:
Remove the server.join() line. That line makes the junit thread wait until the server thread stops. Which is not needed for unit testing.
Long answer:
What we (the jetty developers) have learned about using jetty embedded servers with junit.
Use the #Before and #After annotations to start and stop the server if you have 1 test method, or some requirement that the server be pristine between test methods.
Example #Before / #After (Jetty 9.x):
public class MyTest
{
private Server server;
private URI serverUri;
#Before
public void startServer() throws Exception
{
this.server = new Server();
ServerConnector connector = new ServerConnector(server);
connector.setPort(0); // let connector pick an unused port #
server.addConnector(connector);
ServletContextHandler context = new ServletContextHandler();
context.setContextPath("/");
server.setHandler(context);
// Serve capture servlet
context.addServlet(new ServletHolder(new MyServlet()),"/my/*");
// Start Server
server.start();
String host = connector.getHost();
if (host == null)
{
host = "localhost";
}
int port = connector.getLocalPort();
this.serverUri = new URI(String.format("http://%s:%d/",host,port));
}
#After
public void stopServer()
{
try
{
server.stop();
}
catch (Exception e)
{
e.printStackTrace(System.err);
}
}
#Test
public void testMe()
{
// Issue request to server
URI requestUri = serverUri.resolve("/my/test");
// assert the response
}
}
This technique makes the server start on port 0, which is a magic number that tells the underlying stack to pick an empty port and start listening. The test case then asks the server what port number it is listening on and builds out the serverUri field to be appropropriate for this test run.
This technique works great, however, it will start/stop the server for each method.
Enter, the better technique, use the #BeforeClass and #AfterClass annotations to start/stop the server once for the entire test class, running all of the methods inside of the test class against this started server.
Example #BeforeClass / #AfterClass (Jetty 9.x):
public class MyTest
{
private static Server server;
private static URI serverUri;
#BeforeClass
public static void startServer() throws Exception
{
server = new Server();
ServerConnector connector = new ServerConnector(server);
connector.setPort(0); // let connector pick an unused port #
server.addConnector(connector);
ServletContextHandler context = new ServletContextHandler();
context.setContextPath("/");
server.setHandler(context);
// Serve capture servlet
context.addServlet(new ServletHolder(new MyServlet()),"/my/*");
// Start Server
server.start();
String host = connector.getHost();
if (host == null)
{
host = "localhost";
}
int port = connector.getLocalPort();
serverUri = new URI(String.format("http://%s:%d/",host,port));
}
#AfterClass
public static void stopServer()
{
try
{
server.stop();
}
catch (Exception e)
{
e.printStackTrace(System.err);
}
}
#Test
public void testMe()
{
// Issue request to server
URI requestUri = serverUri.resolve("/my/test");
// assert the response
}
}
Doesn't look much different? Yes, the changes are subtle. #Before became #BeforeClass, #After became #AfterClass. The start/stop methods are now static. The server and serverUri fields are now static.
This technique is used where we have dozens of test methods that access the same server, and those requests do not alter the state in the server. This speeds up the test case execution by simply not recreating the server between each test method.
Give a try to "com.jayway.restassured" for your http test. too easy to write some test :
#Test
public void testNotGetAll() {
expect().
statusCode(404).
when().
get(baseUrl+"/games/");
}
this method call "http://mywebserver.local:8080/rest/games/" and verify that a 404 http status code is returned.
And this approach synchronised with a Jetty server (for example) started at pre-integration-test in the maven lifecycle, you match the perfect mix to process integration test !