Skip to content

Commit

Permalink
Add end-to-end tests
Browse files Browse the repository at this point in the history
  • Loading branch information
SvenWoltmann committed Jun 2, 2023
1 parent 2d55c35 commit ee3ec2f
Show file tree
Hide file tree
Showing 11 changed files with 227 additions and 6 deletions.
17 changes: 17 additions & 0 deletions adapter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,21 @@
<scope>test</scope>
</dependency>
</dependencies>

<!-- To create an "attached test JAR" to be able to use this module's test classes in other modules -->
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
import io.restassured.path.json.JsonPath;
import io.restassured.response.Response;

final class CartsControllerAssertions {
public final class CartsControllerAssertions {

private CartsControllerAssertions() {}

static void assertThatResponseIsCart(Response response, Cart cart) {
public static void assertThatResponseIsCart(Response response, Cart cart) {
assertThat(response.statusCode()).isEqualTo(OK.getStatusCode());

JsonPath json = response.jsonPath();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@
import io.restassured.response.Response;
import java.util.List;

final class ProductsControllerAssertions {
public final class ProductsControllerAssertions {

private ProductsControllerAssertions() {}

static void assertThatResponseIsProduct(Response response, Product product) {
public static void assertThatResponseIsProduct(Response response, Product product) {
assertThat(response.statusCode()).isEqualTo(OK.getStatusCode());

JsonPath json = response.jsonPath();

assertThatJsonProductMatchesProduct(json, true, "", product);
}

static void assertThatResponseIsProductList(Response response, List<Product> products) {
public static void assertThatResponseIsProductList(Response response, List<Product> products) {
assertThat(response.statusCode()).isEqualTo(OK.getStatusCode());

JsonPath json = response.jsonPath();
Expand Down
17 changes: 17 additions & 0 deletions bootstrap/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,22 @@
<artifactId>hibernate-validator</artifactId><!-- Required by Hibernate at runtime -->
<scope>runtime</scope>
</dependency>

<!-- Test scope -->
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<scope>test</scope>
</dependency>

<!-- To use the "attached test JAR" from the "adapter" module -->
<dependency>
<groupId>eu.happycoders.shop</groupId>
<artifactId>adapter</artifactId>
<version>${project.version}</version>
<classifier>tests</classifier>
<type>test-jar</type>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,17 @@ public void startOnDefaultPort() {
startServer();
}

public void startOnPort(int port) {
server = new UndertowJaxrsServer().setPort(port);
startServer();
}

private void startServer() {
server.start();
server.deploy(RestEasyUndertowShopApplication.class);
}

public void stop() {
server.stop();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package eu.happycoders.shop.bootstrap.e2e;

import static eu.happycoders.shop.adapter.in.web.HttpTestCommons.TEST_PORT;
import static eu.happycoders.shop.adapter.in.web.cart.CartsControllerAssertions.assertThatResponseIsCart;
import static eu.happycoders.shop.adapter.out.persistence.DemoProducts.LED_LIGHTS;
import static eu.happycoders.shop.adapter.out.persistence.DemoProducts.MONITOR_DESK_MOUNT;
import static io.restassured.RestAssured.given;
import static jakarta.ws.rs.core.Response.Status.NO_CONTENT;

import eu.happycoders.shop.model.cart.Cart;
import eu.happycoders.shop.model.cart.NotEnoughItemsInStockException;
import eu.happycoders.shop.model.customer.CustomerId;
import io.restassured.response.Response;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;

@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
class CartTest extends EndToEndTest {

private static final CustomerId TEST_CUSTOMER_ID = new CustomerId(61157);
private static final String CARTS_PATH = "/carts/" + TEST_CUSTOMER_ID.value();

@Test
@Order(1)
void givenAnEmptyCart_addLineItem_addsTheLineItemAndReturnsTheCartWithTheAddedItem()
throws NotEnoughItemsInStockException {
Response response =
given()
.port(TEST_PORT)
.queryParam("productId", LED_LIGHTS.id().value())
.queryParam("quantity", 3)
.post(CARTS_PATH + "/line-items")
.then()
.extract()
.response();

Cart expectedCart = new Cart(TEST_CUSTOMER_ID);
expectedCart.addProduct(LED_LIGHTS, 3);

assertThatResponseIsCart(response, expectedCart);
}

@Test
@Order(2)
void givenACartWithOneLineItem_addLineItem_addsTheLineItemAndReturnsACartWithTwoLineItems()
throws NotEnoughItemsInStockException {
Response response =
given()
.port(TEST_PORT)
.queryParam("productId", MONITOR_DESK_MOUNT.id().value())
.queryParam("quantity", 1)
.post(CARTS_PATH + "/line-items")
.then()
.extract()
.response();

Cart expectedCart = new Cart(TEST_CUSTOMER_ID);
expectedCart.addProduct(LED_LIGHTS, 3);
expectedCart.addProduct(MONITOR_DESK_MOUNT, 1);

assertThatResponseIsCart(response, expectedCart);
}

@Test
@Order(3)
void givenACartWithTwoLineItems_getCart_returnsTheCart() throws NotEnoughItemsInStockException {
Response response = given().port(TEST_PORT).get(CARTS_PATH).then().extract().response();

Cart expectedCart = new Cart(TEST_CUSTOMER_ID);
expectedCart.addProduct(LED_LIGHTS, 3);
expectedCart.addProduct(MONITOR_DESK_MOUNT, 1);

assertThatResponseIsCart(response, expectedCart);
}

@Test
@Order(4)
void givenACartWithTwoLineItems_delete_returnsStatusCodeNoContent() {
given().port(TEST_PORT).delete(CARTS_PATH).then().statusCode(NO_CONTENT.getStatusCode());
}

@Test
@Order(5)
void givenAnEmptiedCart_getCart_returnsAnEmptyCart() {
Response response = given().port(TEST_PORT).get(CARTS_PATH).then().extract().response();

assertThatResponseIsCart(response, new Cart(TEST_CUSTOMER_ID));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package eu.happycoders.shop.bootstrap.e2e;

import static eu.happycoders.shop.adapter.in.web.HttpTestCommons.TEST_PORT;

import eu.happycoders.shop.bootstrap.Launcher;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;

class EndToEndTest {

private static Launcher launcher;

@BeforeAll
static void init() {
launcher = new Launcher();
launcher.startOnPort(TEST_PORT);
}

@AfterAll
static void stop() {
launcher.stop();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package eu.happycoders.shop.bootstrap.e2e;

import static eu.happycoders.shop.adapter.in.web.HttpTestCommons.TEST_PORT;
import static eu.happycoders.shop.adapter.in.web.product.ProductsControllerAssertions.assertThatResponseIsProductList;
import static eu.happycoders.shop.adapter.out.persistence.DemoProducts.COMPUTER_MONITOR;
import static eu.happycoders.shop.adapter.out.persistence.DemoProducts.MONITOR_DESK_MOUNT;
import static io.restassured.RestAssured.given;

import io.restassured.response.Response;
import java.util.List;
import org.junit.jupiter.api.Test;

class FindProductsTest extends EndToEndTest {

@Test
void givenTestProductsAndAQuery_findProducts_returnsMatchingProducts() {
String query = "monitor";

Response response =
given()
.port(TEST_PORT)
.queryParam("query", query)
.get("/products")
.then()
.extract()
.response();

assertThatResponseIsProductList(response, List.of(COMPUTER_MONITOR, MONITOR_DESK_MOUNT));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package eu.happycoders.shop.bootstrap.e2e;

import static eu.happycoders.shop.adapter.in.web.HttpTestCommons.TEST_PORT;
import static eu.happycoders.shop.adapter.in.web.product.ProductsControllerAssertions.assertThatResponseIsProduct;
import static eu.happycoders.shop.adapter.out.persistence.DemoProducts.PLASTIC_SHEETING;
import static io.restassured.RestAssured.given;

import io.restassured.response.Response;
import org.junit.jupiter.api.Test;

class GetProductTest extends EndToEndTest {

@Test
void givenATestProduct_getProduct_returnsTheProduct() {
Response response =
given()
.port(TEST_PORT)
.get("/products/" + PLASTIC_SHEETING.id().value())
.then()
.extract()
.response();

assertThatResponseIsProduct(response, PLASTIC_SHEETING);
}
}
1 change: 0 additions & 1 deletion model/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.3.0</version>
<executions>
<execution>
<goals>
Expand Down
10 changes: 10 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,16 @@
</executions>
</plugin>
</plugins>

<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.3.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>

<profiles>
Expand Down

0 comments on commit ee3ec2f

Please sign in to comment.