Skip to content

Commit

Permalink
Use RestAssured's validation capabilities where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
turing85 committed Nov 3, 2023
1 parent c7a6ac8 commit 53f86d8
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@ public interface BaseSendAndReceiveTest {
@Test
default void test() {
String body = "body";
RestAssured.given().body(body)
// @formatter:off
RestAssured
.given().body(body)
.when().post()
.then().statusCode(is(Response.Status.OK.getStatusCode())).body(is(body));
// @formatter:on
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.quarkus.it.artemis.common;

import static org.hamcrest.Matchers.is;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -22,12 +24,16 @@ public static void testJms(String endpoint, Set<String> expectedConfigurations)
}

private static void test(String endpoint, Set<String> expectedConfigurations, String healthCheckMessage) {
Response response = RestAssured.with().get(endpoint);
Assertions.assertEquals(jakarta.ws.rs.core.Response.Status.OK.getStatusCode(), response.statusCode());

// @formatter:off
Response response = RestAssured
.when().get(endpoint)
.then()
.statusCode(jakarta.ws.rs.core.Response.Status.OK.getStatusCode())
.body("status", is("UP"))
.extract().response();
// @formatter:on
Map<String, Object> body = response.as(new TypeRef<>() {
});
Assertions.assertEquals("UP", body.get("status"));

@SuppressWarnings("unchecked")
List<Map<String, Object>> checks = (List<Map<String, Object>>) body.get("checks");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public void send(String body) {
try (ClientProducer producer = session.createProducer(queueName)) {
producer.send(message);
}
} catch (ActiveMQException | NullPointerException e) {
} catch (ActiveMQException e) {
throw new RuntimeException("Could not send message", e);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package io.quarkus.it.artemis.core.common;

import static org.hamcrest.Matchers.is;

import java.util.Random;

import jakarta.ws.rs.core.Response;

import org.apache.activemq.artemis.api.core.ActiveMQException;
import org.apache.activemq.artemis.api.core.client.ActiveMQClient;
import org.apache.activemq.artemis.api.core.client.ClientMessage;
Expand All @@ -10,7 +14,6 @@
import org.junit.jupiter.api.Assertions;

import io.restassured.RestAssured;
import io.restassured.response.Response;

public class ArtemisCoreHelper {
private static final Random RANDOM = new Random();
Expand All @@ -37,15 +40,21 @@ public void sendAndVerify(ClientSession session, String addressName, String endp
autoClosedSession.createProducer(addressName).send(message);
}

Response response = RestAssured.with().get(endpoint);
Assertions.assertEquals(jakarta.ws.rs.core.Response.Status.OK.getStatusCode(), response.statusCode());
Assertions.assertEquals(body, response.getBody().asString());
// @formatter:off
RestAssured
.when().get(endpoint)
.then()
.statusCode(Response.Status.OK.getStatusCode())
.body(is(body));
// @formatter:on
}

public void receiveAndVerify(String endpoint, ClientSession session, String queueName) throws ActiveMQException {
String body = createBody();
Response response = RestAssured.with().body(body).post(endpoint);
Assertions.assertEquals(jakarta.ws.rs.core.Response.Status.NO_CONTENT.getStatusCode(), response.statusCode());
RestAssured
.given().body(body)
.when().post(endpoint)
.then().statusCode(Response.Status.NO_CONTENT.getStatusCode());

try (ClientSession autoClosedSession = session) {
session.start();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.quarkus.it.artemis.jms.common;

import java.util.Optional;

import jakarta.jms.*;
import jakarta.transaction.RollbackException;
import jakarta.transaction.Synchronization;
Expand All @@ -9,7 +11,7 @@
public class ArtemisJmsXaConsumerManager extends ArtemisJmsConsumerManager {
private final XAConnectionFactory xaConnectionFactory;
private final TransactionManager tm;
private String queueName;
private final String queueName;

/**
* This constructor exists solely for CDI ("You need to manually add a non-private no-args constructor").
Expand Down Expand Up @@ -37,6 +39,7 @@ public String receiveXA(boolean rollback) throws SystemException, RollbackExcept
tm.getTransaction().registerSynchronization(new Synchronization() {
@Override
public void beforeCompletion() {
// Nothing to do
}

@Override
Expand All @@ -49,7 +52,12 @@ public void afterCompletion(int i) {
tm.setRollbackOnly();
}
try {
return consumer.receive(1000L).getBody(String.class);
Optional<Message> maybeMessage = Optional.ofNullable(consumer.receive(1000L));
if (maybeMessage.isPresent()) {
return maybeMessage.get().getBody(String.class);
} else {
return null;
}
} catch (JMSException | NullPointerException e) {
throw new RuntimeException("Could not receive message", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
import jakarta.jms.JMSContext;
import jakarta.jms.JMSException;
import jakarta.jms.Message;
import jakarta.ws.rs.core.Response;

import org.apache.activemq.artemis.jms.client.ActiveMQJMSConnectionFactory;
import org.eclipse.microprofile.config.ConfigProvider;
import org.junit.jupiter.api.Assertions;

import io.restassured.RestAssured;
import io.restassured.response.Response;

public class ArtemisJmsHelper {
private static final Random RANDOM = new Random();
Expand Down Expand Up @@ -44,15 +44,21 @@ public void sendAndVerify(JMSContext context, String queueName, String endpoint)
context.createProducer().send(context.createQueue(queueName), body);
}

Response response = RestAssured.with().get(endpoint);
Assertions.assertEquals(jakarta.ws.rs.core.Response.Status.OK.getStatusCode(), response.statusCode());
Assertions.assertEquals(body, response.getBody().asString());
// @formatter:off
RestAssured
.when().get(endpoint)
.then()
.statusCode(Response.Status.OK.getStatusCode())
.body(is(body));
// @formatter:on
}

public void receiveAndVerify(String endpoint, JMSContext context, String queueName) throws JMSException {
String body = createBody();
Response response = RestAssured.with().body(body).post(endpoint);
Assertions.assertEquals(jakarta.ws.rs.core.Response.Status.NO_CONTENT.getStatusCode(), response.statusCode());
RestAssured
.given().body(body)
.when().post(endpoint)
.then().statusCode(Response.Status.NO_CONTENT.getStatusCode());

try (JMSContext autoClosedContext = context) {
JMSConsumer consumer = autoClosedContext.createConsumer(autoClosedContext.createQueue(queueName));
Expand All @@ -62,8 +68,10 @@ public void receiveAndVerify(String endpoint, JMSContext context, String queueNa
}

public void testRollback(String endpoint, JMSContext context, String queueName) {
Response response = RestAssured.with().body("fail").post(endpoint);
Assertions.assertEquals(jakarta.ws.rs.core.Response.Status.NO_CONTENT.getStatusCode(), response.statusCode());
RestAssured
.given().body("fail")
.when().post(endpoint)
.then().statusCode(Response.Status.NO_CONTENT.getStatusCode());

try (JMSContext autoClosedContext = context) {
JMSConsumer consumer = autoClosedContext.createConsumer(autoClosedContext.createQueue(queueName));
Expand All @@ -75,39 +83,42 @@ public void testRollback(String endpoint, JMSContext context, String queueName)
public void sendAndVerifyXACommit(JMSContext context, String queueName, String xaEndpoint, String endpoint) {
String body = createBody();
try (JMSContext autoClosedContext = context) {
context.createProducer().send(context.createQueue(queueName), body);
autoClosedContext.createProducer().send(context.createQueue(queueName), body);
}

// Consume the message in xa transaction
// @formatter:off
RestAssured
.when().get(xaEndpoint)
.then()
.statusCode(jakarta.ws.rs.core.Response.Status.OK.getStatusCode())
.statusCode(Response.Status.OK.getStatusCode())
.body(is(body));
// @formatter:on
// Receive from queue again to confirm nothing is received i.e. message was consumed
// and now there is no message in the queue
RestAssured
.when().get(endpoint)
.then().statusCode(jakarta.ws.rs.core.Response.Status.NO_CONTENT.getStatusCode());
.then().statusCode(Response.Status.NO_CONTENT.getStatusCode());
}

public void sendAndVerifyXARollback(JMSContext context, String queueName, String xaEndpoint, String endpoint) {
String body = createBody();
try (JMSContext autoClosedContext = context) {
context.createProducer().send(context.createQueue(queueName), body);
autoClosedContext.createProducer().send(context.createQueue(queueName), body);
}

// Consume the message but in rollback transaction
Response response = RestAssured.with().get(xaEndpoint);
Assertions.assertEquals(jakarta.ws.rs.core.Response.Status.OK.getStatusCode(), response.statusCode());
Assertions.assertEquals(body, response.getBody().asString());

// @formatter:off
RestAssured
.when().get(xaEndpoint)
.then()
.statusCode(Response.Status.OK.getStatusCode())
.body(is(body));
// @formatter:on
// Receive from queue again to confirm message is received i.e. message wasn't consumed
// and rollback occurred
response = RestAssured.with().get(endpoint);
Assertions.assertEquals(jakarta.ws.rs.core.Response.Status.OK.getStatusCode(),
response.statusCode());
RestAssured
.when().get(endpoint)
.then().statusCode(Response.Status.OK.getStatusCode());
}
}

0 comments on commit 53f86d8

Please sign in to comment.