Skip to content

Commit 1aa25a6

Browse files
committed
Use Quarkus application framework - step 2: dependency injection in adapter
1 parent f01aaa3 commit 1aa25a6

File tree

16 files changed

+126
-230
lines changed

16 files changed

+126
-230
lines changed

adapter/pom.xml

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -64,18 +64,6 @@
6464
<scope>test</scope>
6565
</dependency>
6666

67-
<!-- Required temporarily during migration -->
68-
<dependency>
69-
<groupId>org.jboss.resteasy</groupId>
70-
<artifactId>resteasy-undertow</artifactId>
71-
<scope>test</scope>
72-
</dependency>
73-
<dependency>
74-
<groupId>org.testcontainers</groupId>
75-
<artifactId>mysql</artifactId>
76-
<scope>test</scope>
77-
</dependency>
78-
7967
<!-- To use the "attached test JAR" from the "model" module -->
8068
<dependency>
8169
<groupId>eu.happycoders.shop</groupId>
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package eu.happycoders.shop;
2+
3+
import eu.happycoders.shop.application.port.in.cart.AddToCartUseCase;
4+
import eu.happycoders.shop.application.port.in.cart.EmptyCartUseCase;
5+
import eu.happycoders.shop.application.port.in.cart.GetCartUseCase;
6+
import eu.happycoders.shop.application.port.in.product.FindProductsUseCase;
7+
import eu.happycoders.shop.application.port.out.persistence.CartRepository;
8+
import eu.happycoders.shop.application.port.out.persistence.ProductRepository;
9+
import eu.happycoders.shop.application.service.cart.AddToCartService;
10+
import eu.happycoders.shop.application.service.cart.EmptyCartService;
11+
import eu.happycoders.shop.application.service.cart.GetCartService;
12+
import eu.happycoders.shop.application.service.product.FindProductsService;
13+
import jakarta.enterprise.context.ApplicationScoped;
14+
import jakarta.enterprise.inject.Instance;
15+
import jakarta.enterprise.inject.Produces;
16+
import jakarta.inject.Inject;
17+
18+
class QuarkusAppConfig {
19+
20+
@Inject Instance<CartRepository> cartRepository;
21+
22+
@Inject Instance<ProductRepository> productRepository;
23+
24+
@Produces
25+
@ApplicationScoped
26+
GetCartUseCase getCartUseCase() {
27+
return new GetCartService(cartRepository.get());
28+
}
29+
30+
@Produces
31+
@ApplicationScoped
32+
EmptyCartUseCase emptyCartUseCase() {
33+
return new EmptyCartService(cartRepository.get());
34+
}
35+
36+
@Produces
37+
@ApplicationScoped
38+
FindProductsUseCase findProductsUseCase() {
39+
return new FindProductsService(productRepository.get());
40+
}
41+
42+
@Produces
43+
@ApplicationScoped
44+
AddToCartUseCase addToCartUseCase() {
45+
return new AddToCartService(cartRepository.get(), productRepository.get());
46+
}
47+
}

adapter/src/main/java/eu/happycoders/shop/adapter/out/persistence/inmemory/InMemoryCartRepository.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import eu.happycoders.shop.application.port.out.persistence.CartRepository;
44
import eu.happycoders.shop.model.cart.Cart;
55
import eu.happycoders.shop.model.customer.CustomerId;
6+
import io.quarkus.arc.lookup.LookupIfProperty;
7+
import jakarta.enterprise.context.ApplicationScoped;
68
import java.util.Map;
79
import java.util.Optional;
810
import java.util.concurrent.ConcurrentHashMap;
@@ -12,6 +14,8 @@
1214
*
1315
* @author Sven Woltmann
1416
*/
17+
@LookupIfProperty(name = "persistence", stringValue = "inmemory", lookupIfMissing = true)
18+
@ApplicationScoped
1519
public class InMemoryCartRepository implements CartRepository {
1620

1721
private final Map<CustomerId, Cart> carts = new ConcurrentHashMap<>();

adapter/src/main/java/eu/happycoders/shop/adapter/out/persistence/inmemory/InMemoryProductRepository.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import eu.happycoders.shop.application.port.out.persistence.ProductRepository;
55
import eu.happycoders.shop.model.product.Product;
66
import eu.happycoders.shop.model.product.ProductId;
7+
import io.quarkus.arc.lookup.LookupIfProperty;
8+
import jakarta.enterprise.context.ApplicationScoped;
79
import java.util.List;
810
import java.util.Locale;
911
import java.util.Map;
@@ -15,6 +17,8 @@
1517
*
1618
* @author Sven Woltmann
1719
*/
20+
@LookupIfProperty(name = "persistence", stringValue = "inmemory", lookupIfMissing = true)
21+
@ApplicationScoped
1822
public class InMemoryProductRepository implements ProductRepository {
1923

2024
private final Map<ProductId, Product> products = new ConcurrentHashMap<>();

adapter/src/main/java/eu/happycoders/shop/adapter/out/persistence/jpa/JpaCartRepository.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import eu.happycoders.shop.application.port.out.persistence.CartRepository;
44
import eu.happycoders.shop.model.cart.Cart;
55
import eu.happycoders.shop.model.customer.CustomerId;
6+
import io.quarkus.arc.lookup.LookupIfProperty;
7+
import jakarta.enterprise.context.ApplicationScoped;
68
import jakarta.persistence.EntityManager;
79
import jakarta.persistence.EntityManagerFactory;
810
import java.util.Optional;
@@ -12,6 +14,8 @@
1214
*
1315
* @author Sven Woltmann
1416
*/
17+
@LookupIfProperty(name = "persistence", stringValue = "mysql")
18+
@ApplicationScoped
1519
public class JpaCartRepository implements CartRepository {
1620

1721
private final EntityManagerFactory entityManagerFactory;

adapter/src/main/java/eu/happycoders/shop/adapter/out/persistence/jpa/JpaProductRepository.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import eu.happycoders.shop.application.port.out.persistence.ProductRepository;
55
import eu.happycoders.shop.model.product.Product;
66
import eu.happycoders.shop.model.product.ProductId;
7+
import io.quarkus.arc.lookup.LookupIfProperty;
8+
import jakarta.enterprise.context.ApplicationScoped;
79
import jakarta.persistence.EntityManager;
810
import jakarta.persistence.EntityManagerFactory;
911
import jakarta.persistence.TypedQuery;
@@ -15,6 +17,8 @@
1517
*
1618
* @author Sven Woltmann
1719
*/
20+
@LookupIfProperty(name = "persistence", stringValue = "mysql")
21+
@ApplicationScoped
1822
public class JpaProductRepository implements ProductRepository {
1923

2024
private final EntityManagerFactory entityManagerFactory;

adapter/src/main/resources/META-INF/persistence.xml

Lines changed: 0 additions & 8 deletions
This file was deleted.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package eu.happycoders.shop.adapter;
2+
3+
import io.quarkus.test.junit.QuarkusTestProfile;
4+
import java.util.Map;
5+
6+
public class TestProfileWithMySQL implements QuarkusTestProfile {
7+
8+
@Override
9+
public Map<String, String> getConfigOverrides() {
10+
return Map.of("persistence", "mysql");
11+
}
12+
}

adapter/src/test/java/eu/happycoders/shop/adapter/in/rest/cart/CartsControllerTest.java

Lines changed: 9 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
package eu.happycoders.shop.adapter.in.rest.cart;
22

3-
import static eu.happycoders.shop.adapter.in.rest.HttpTestCommons.TEST_PORT;
43
import static eu.happycoders.shop.adapter.in.rest.HttpTestCommons.assertThatResponseIsError;
54
import static eu.happycoders.shop.adapter.in.rest.cart.CartsControllerAssertions.assertThatResponseIsCart;
65
import static eu.happycoders.shop.model.money.TestMoneyFactory.euros;
76
import static eu.happycoders.shop.model.product.TestProductFactory.createTestProduct;
87
import static io.restassured.RestAssured.given;
98
import static jakarta.ws.rs.core.Response.Status.BAD_REQUEST;
109
import static jakarta.ws.rs.core.Response.Status.NO_CONTENT;
11-
import static org.mockito.Mockito.mock;
1210
import static org.mockito.Mockito.verify;
1311
import static org.mockito.Mockito.when;
1412

@@ -21,62 +19,27 @@
2119
import eu.happycoders.shop.model.customer.CustomerId;
2220
import eu.happycoders.shop.model.product.Product;
2321
import eu.happycoders.shop.model.product.ProductId;
22+
import io.quarkus.test.InjectMock;
23+
import io.quarkus.test.junit.QuarkusTest;
2424
import io.restassured.response.Response;
25-
import jakarta.ws.rs.core.Application;
26-
import java.util.Set;
27-
import org.jboss.resteasy.plugins.server.undertow.UndertowJaxrsServer;
28-
import org.junit.jupiter.api.AfterAll;
29-
import org.junit.jupiter.api.BeforeAll;
30-
import org.junit.jupiter.api.BeforeEach;
3125
import org.junit.jupiter.api.Test;
32-
import org.mockito.Mockito;
3326

27+
@QuarkusTest
3428
class CartsControllerTest {
3529

3630
private static final CustomerId TEST_CUSTOMER_ID = new CustomerId(61157);
3731
private static final Product TEST_PRODUCT_1 = createTestProduct(euros(19, 99));
3832
private static final Product TEST_PRODUCT_2 = createTestProduct(euros(25, 99));
3933

40-
private static final AddToCartUseCase addToCartUseCase = mock(AddToCartUseCase.class);
41-
private static final GetCartUseCase getCartUseCase = mock(GetCartUseCase.class);
42-
private static final EmptyCartUseCase emptyCartUseCase = mock(EmptyCartUseCase.class);
43-
44-
private static UndertowJaxrsServer server;
45-
46-
@BeforeAll
47-
static void init() {
48-
server =
49-
new UndertowJaxrsServer()
50-
.setPort(TEST_PORT)
51-
.start()
52-
.deploy(
53-
new Application() {
54-
@Override
55-
public Set<Object> getSingletons() {
56-
return Set.of(
57-
new AddToCartController(addToCartUseCase),
58-
new GetCartController(getCartUseCase),
59-
new EmptyCartController(emptyCartUseCase));
60-
}
61-
});
62-
}
63-
64-
@AfterAll
65-
static void stop() {
66-
server.stop();
67-
}
68-
69-
@BeforeEach
70-
void resetMocks() {
71-
Mockito.reset(addToCartUseCase, getCartUseCase, emptyCartUseCase);
72-
}
34+
@InjectMock AddToCartUseCase addToCartUseCase;
35+
@InjectMock GetCartUseCase getCartUseCase;
36+
@InjectMock EmptyCartUseCase emptyCartUseCase;
7337

7438
@Test
7539
void givenASyntacticallyInvalidCustomerId_getCart_returnsAnError() {
7640
String customerId = "foo";
7741

78-
Response response =
79-
given().port(TEST_PORT).get("/carts/" + customerId).then().extract().response();
42+
Response response = given().get("/carts/" + customerId).then().extract().response();
8043

8144
assertThatResponseIsError(response, BAD_REQUEST, "Invalid 'customerId'");
8245
}
@@ -92,8 +55,7 @@ void givenAValidCustomerIdAndACart_getCart_requestsCartFromUseCaseAndReturnsIt()
9255

9356
when(getCartUseCase.getCart(customerId)).thenReturn(cart);
9457

95-
Response response =
96-
given().port(TEST_PORT).get("/carts/" + customerId.value()).then().extract().response();
58+
Response response = given().get("/carts/" + customerId.value()).then().extract().response();
9759

9860
assertThatResponseIsCart(response, cart);
9961
}
@@ -112,7 +74,6 @@ void givenSomeTestData_addLineItem_invokesAddToCartUseCaseAndReturnsUpdatedCart(
11274

11375
Response response =
11476
given()
115-
.port(TEST_PORT)
11677
.queryParam("productId", productId.value())
11778
.queryParam("quantity", quantity)
11879
.post("/carts/" + customerId.value() + "/line-items")
@@ -131,7 +92,6 @@ void givenAnInvalidProductId_addLineItem_returnsAnError() {
13192

13293
Response response =
13394
given()
134-
.port(TEST_PORT)
13595
.queryParam("productId", productId)
13696
.queryParam("quantity", quantity)
13797
.post("/carts/" + customerId.value() + "/line-items")
@@ -154,7 +114,6 @@ void givenProductNotFound_addLineItem_returnsAnError()
154114

155115
Response response =
156116
given()
157-
.port(TEST_PORT)
158117
.queryParam("productId", productId.value())
159118
.queryParam("quantity", quantity)
160119
.post("/carts/" + customerId.value() + "/line-items")
@@ -177,7 +136,6 @@ void givenNotEnoughItemsInStock_addLineItem_returnsAnError()
177136

178137
Response response =
179138
given()
180-
.port(TEST_PORT)
181139
.queryParam("productId", productId.value())
182140
.queryParam("quantity", quantity)
183141
.post("/carts/" + customerId.value() + "/line-items")
@@ -192,11 +150,7 @@ void givenNotEnoughItemsInStock_addLineItem_returnsAnError()
192150
void givenACustomerId_deleteCart_invokesDeleteCartUseCaseAndReturnsUpdatedCart() {
193151
CustomerId customerId = TEST_CUSTOMER_ID;
194152

195-
given()
196-
.port(TEST_PORT)
197-
.delete("/carts/" + customerId.value())
198-
.then()
199-
.statusCode(NO_CONTENT.getStatusCode());
153+
given().delete("/carts/" + customerId.value()).then().statusCode(NO_CONTENT.getStatusCode());
200154

201155
verify(emptyCartUseCase).emptyCart(customerId);
202156
}

0 commit comments

Comments
 (0)