Skip to content

Commit

Permalink
Merge pull request #58 from FrancescoScandiffio/service_layer
Browse files Browse the repository at this point in the history
Implement service layer
  • Loading branch information
FrancescoScandiffio committed Jun 11, 2022
2 parents fab6e29 + 8cd3df6 commit 34780da
Show file tree
Hide file tree
Showing 23 changed files with 1,130 additions and 193 deletions.
1 change: 0 additions & 1 deletion raffaelliscandiffio.shop-totem/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
**/model/Order.java,
**/model/OrderStatus.java,
**/utils/GUITestExtension.java,
**/controller/TotemController.java,
**/app/**/*
</sonar.coverage.exclusions>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

import static java.util.Arrays.asList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.awaitility.Awaitility.await;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.awaitility.Awaitility.await;

import java.util.concurrent.TimeUnit;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import org.assertj.core.api.SoftAssertions;
import org.assertj.swing.annotation.GUITest;
Expand Down Expand Up @@ -85,7 +85,7 @@ void testStartShoppingButtonInWelcome() {
Order order = new Order(OrderStatus.OPEN);
order.setId(orderId);

when(shoppingService.saveOrder(any(Order.class))).thenReturn(order);
when(shoppingService.openNewOrder()).thenReturn(order);
when(shoppingService.getAllProducts()).thenReturn(Arrays.asList(new Product("Pasta", 2.5)));

window.button("welcomeStartShopping").click();
Expand All @@ -107,7 +107,7 @@ void testStartShoppingButtonInWelcome() {
void testStartShoppingButtonInWelcomeShouldNotShowProductsAndShouldShowErrorWhenSaveOrderThrows() {

String errorMessage = "Error message";
doThrow(new TransactionException(errorMessage)).when(shoppingService).saveOrder(any(Order.class));
doThrow(new TransactionException(errorMessage)).when(shoppingService).openNewOrder();

window.button("welcomeStartShopping").click();

Expand Down Expand Up @@ -440,10 +440,10 @@ void testReturnQuantityButtonCartShowsError() {
window.list("cartList").selectItem(0);
window.spinner("cartReturnSpinner").enterText(String.valueOf(quantityToRemove));
window.button(JButtonMatcher.withText("Return quantity")).click();

await().atMost(TIMEOUT, TimeUnit.SECONDS).untilAsserted(() -> {
window.label("cartMessageLabel").requireText(errorMessage);

GuiActionRunner.execute(() -> totemView.showShopping());
softly.assertThat(window.list("productList").contents()).containsExactly("Pasta - Price: 2.0 €");
GuiActionRunner.execute(() -> totemView.showOrder());
Expand Down Expand Up @@ -577,7 +577,7 @@ void testStartShoppingButtonInGoodbye() {
totemView.showAllProducts(asList(product));
totemView.showAllOrderItems(asList(orderItem));
});
when(shoppingService.saveOrder(any(Order.class))).thenReturn(order);
when(shoppingService.openNewOrder()).thenReturn(order);
when(shoppingService.getAllProducts()).thenReturn(Arrays.asList(new Product("Pasta", 2.5)));

window.button("goodbyeStartShopping").click();
Expand All @@ -600,7 +600,7 @@ void testStartShoppingButtonInGoodbyeShouldNotShowProductsAndShouldShowErrorWhen
GuiActionRunner.execute(() -> totemView.showGoodbye());

String errorMessage = "Error message";
doThrow(new TransactionException(errorMessage)).when(shoppingService).saveOrder(any(Order.class));
doThrow(new TransactionException(errorMessage)).when(shoppingService).openNewOrder();

window.button("goodbyeStartShopping").click();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,12 @@
import com.github.raffaelliscandiffio.model.OrderItem;
import com.github.raffaelliscandiffio.model.OrderStatus;
import com.github.raffaelliscandiffio.model.Product;
import com.mongodb.client.ClientSession;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.client.ClientSession;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;



class OrderItemMongoRepositoryIT {

private static final String DATABASE_NAME = "totem";
Expand Down Expand Up @@ -59,9 +57,9 @@ class OrderItemMongoRepositoryIT {

@BeforeEach
public void setup() {
String uri = "mongodb://localhost:27017,localhost:27018,localhost:27019/?replicaSet=rs0&readPreference=primary&ssl=false";
String uri = "mongodb://localhost:27017,localhost:27018,localhost:27019/?replicaSet=rs0&readPreference=primary&ssl=false";
client = MongoClients.create(uri);

database = client.getDatabase(DATABASE_NAME);
database.drop();
database.createCollection(PRODUCT_COLLECTION_NAME);
Expand All @@ -71,7 +69,7 @@ public void setup() {
productCollection = database.getCollection(PRODUCT_COLLECTION_NAME);
orderCollection = database.getCollection(ORDER_COLLECTION_NAME);
orderItemCollection = database.getCollection(ORDERITEM_COLLECTION_NAME);

session = client.startSession();
orderItemRepository = new OrderItemMongoRepository(client, session, DATABASE_NAME, PRODUCT_COLLECTION_NAME,
ORDER_COLLECTION_NAME, ORDERITEM_COLLECTION_NAME);
Expand Down Expand Up @@ -104,7 +102,7 @@ void testSaveOrderItem() {
@Test
@DisplayName("Method 'save' should throw when the product reference does not exist")
void testSaveOrderItemWhenTheProductReferenceDoesNotExistShouldThrow() {
productCollection.drop();
dropAndCreateCollection(productCollection);
OrderItem orderItem = new OrderItem(product_1, order_1, QUANTITY_1);
SoftAssertions softly = new SoftAssertions();
softly.assertThatThrownBy(() -> orderItemRepository.save(orderItem)).isInstanceOf(NoSuchElementException.class)
Expand All @@ -117,7 +115,7 @@ void testSaveOrderItemWhenTheProductReferenceDoesNotExistShouldThrow() {
@Test
@DisplayName("Method 'save' should throw when the order reference does not exist")
void testSaveOrderItemWhenTheOrderReferenceDoesNotExistShouldThrow() {
orderCollection.drop();
dropAndCreateCollection(orderCollection);
OrderItem orderItem = new OrderItem(product_1, order_1, QUANTITY_1);
SoftAssertions softly = new SoftAssertions();
softly.assertThatThrownBy(() -> orderItemRepository.save(orderItem)).isInstanceOf(NoSuchElementException.class)
Expand Down Expand Up @@ -243,6 +241,86 @@ void testUpdateOrderItemShouldBeBoundToTheRepositorySession() {
session.commitTransaction();
}

@Test
@DisplayName("Get list of OrderItems by order_id when there is exactly one match")
void testGetListByOrderIdWhenThereIsExactlyOneMatch() {
OrderItem match_1 = newOrderItemWithId(getNewStringId(), product_1, order_1, QUANTITY_1);
OrderItem match_2 = newOrderItemWithId(getNewStringId(), product_2, order_2, QUANTITY_1);
saveTestOrderItemToDatabase(match_1);
saveTestOrderItemToDatabase(match_2);

assertThat(orderItemRepository.getListByOrderId(order_1.getId())).containsExactly(match_1);
}

@Test
@DisplayName("Get list of OrderItems by order_id when there are multiple matches")
void testGetListByOrderIdWhenThereAreMultipleMatches() {
OrderItem match_1 = newOrderItemWithId(getNewStringId(), product_1, order_1, QUANTITY_1);
OrderItem match_2 = newOrderItemWithId(getNewStringId(), product_2, order_1, QUANTITY_1);
saveTestOrderItemToDatabase(match_1);
saveTestOrderItemToDatabase(match_2);
assertThat(orderItemRepository.getListByOrderId(order_1.getId())).containsExactlyInAnyOrder(match_1, match_2);
}

@Test
@DisplayName("Get list of OrderItems by order_id when there are 0 matches should return empty list")
void testGetListByOrderIdWhenThereIsNoMatchShouldReturnEmptyList() {
OrderItem match_1 = newOrderItemWithId(getNewStringId(), product_1, order_2, QUANTITY_1);
OrderItem match_2 = newOrderItemWithId(getNewStringId(), product_2, order_2, QUANTITY_1);
saveTestOrderItemToDatabase(match_1);
saveTestOrderItemToDatabase(match_2);
assertThat(orderItemRepository.getListByOrderId(order_1.getId())).isEmpty();
}

@Test
@DisplayName("Method 'getListByOrderId' should be bound to the repository session")
void testGetListByOrderIdShouldBeBoundToTheRepositorySession() {
dropAndCreateCollection(orderCollection);
session.startTransaction();
OrderItem match_1 = newOrderItemWithId(getNewStringId(), product_1, order_1, QUANTITY_1);
saveTestOrderToDatabaseWithSession(session, order_1);
saveTestOrderItemToDatabaseWithSession(session, match_1);
assertThat(orderItemRepository.getListByOrderId(order_1.getId())).containsExactly(match_1);
session.commitTransaction();
}

@Test
@DisplayName("Find OrderItem by order_id and product_id when there is a match on both fields")
void testFindOrderItemByOrderIdAndProductId() {
OrderItem match_1 = newOrderItemWithId(getNewStringId(), product_1, order_1, QUANTITY_1);
saveTestOrderItemToDatabase(match_1);

assertThat(orderItemRepository.findByProductAndOrderId(product_1.getId(), order_1.getId())).isEqualTo(match_1);
}

@Test
@DisplayName("Find OrderItem by order_id and product_id when there is no match on both fields should return null")
void testFindOrderItemByOrderIdAndProductIdWhenAtLeasOneFieldDoesNotMatchShouldReturnNull() {
OrderItem match_1 = newOrderItemWithId(getNewStringId(), product_1, order_1, QUANTITY_1);
OrderItem match_2 = newOrderItemWithId(getNewStringId(), product_2, order_2, QUANTITY_1);
saveTestOrderItemToDatabase(match_1);
saveTestOrderItemToDatabase(match_2);

SoftAssertions softly = new SoftAssertions();
softly.assertThat(orderItemRepository.findByProductAndOrderId(product_1.getId(), order_2.getId())).isNull();
softly.assertThat(orderItemRepository.findByProductAndOrderId(product_2.getId(), order_1.getId())).isNull();
softly.assertAll();
}

@Test
@DisplayName("Method 'findByProductAndOrderId' should be bound to the repository session")
void testFindOrderItemByOrderIdAndProductIdShouldBeBoundToTheRepositorySession() {
dropAndCreateCollection(orderCollection);
session.startTransaction();
saveTestOrderToDatabaseWithSession(session, order_1);
OrderItem match_1 = newOrderItemWithId(getNewStringId(), product_1, order_1, QUANTITY_1);
saveTestOrderItemToDatabaseWithSession(session, match_1);

assertThat(orderItemRepository.findByProductAndOrderId(product_1.getId(), order_1.getId())).isEqualTo(match_1);
session.commitTransaction();

}

// Private utility methods

private String getNewStringId() {
Expand All @@ -252,7 +330,7 @@ private String getNewStringId() {
private ObjectId getObjectId(String id) {
return new ObjectId(id);
}

private void dropAndCreateCollection(MongoCollection<Document> collection) {
String name = collection.getNamespace().getCollectionName();
collection.drop();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,12 @@

import com.github.raffaelliscandiffio.model.Product;
import com.github.raffaelliscandiffio.model.Stock;
import com.mongodb.client.ClientSession;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.client.ClientSession;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;



class StockMongoRepositoryIT {

private static final String DATABASE_NAME = "totem";
Expand All @@ -38,7 +36,6 @@ class StockMongoRepositoryIT {
private static final int QUANTITY_1 = 1;
private static final int QUANTITY_2 = 2;


private MongoClient client;
private ClientSession session;
private StockMongoRepository stockRepository;
Expand All @@ -60,7 +57,7 @@ public void setup() {
database.drop();
database.createCollection(PRODUCT_COLLECTION_NAME);
database.createCollection(STOCK_COLLECTION_NAME);

productCollection = database.getCollection(PRODUCT_COLLECTION_NAME);
stockCollection = database.getCollection(STOCK_COLLECTION_NAME);
product_1 = saveTestProductToDatabase(new Product(PRODUCT_NAME_1, PRICE));
Expand Down Expand Up @@ -138,7 +135,7 @@ void testFindByIdShouldBeBoundToTheRepositorySession() {
String idToFind = getNewStringId();
Stock stock_1 = newStockWithId(idToFind, product_1, QUANTITY_1);
Stock expectedResult = newStockWithId(idToFind, product_1, QUANTITY_1);

saveTestStockToDatabaseWithSession(session, stock_1);
assertThat(stockRepository.findById(idToFind)).isEqualTo(expectedResult);
session.commitTransaction();
Expand Down Expand Up @@ -182,6 +179,34 @@ void testUpdateStockShouldBeBoundToTheRepositorySession() {
session.commitTransaction();
}

@Test
@DisplayName("Retrieve Stock by Product id")
void testFindByProductId() {
String productId = product_1.getId();
Stock storedStock = newStockWithId(getNewStringId(), product_1, QUANTITY_1);
saveTestStockToDatabase(storedStock);
assertThat(stockRepository.findByProductId(productId)).isEqualTo(storedStock);
}

@Test
@DisplayName("Method 'findByProductId' when not found should return null")
void testFindByProductIdWhenNotFoundShouldReturnNull() {
assertThat(stockRepository.findByProductId("missing_id")).isNull();
}

@Test
@DisplayName("Method 'findByProductId' should be bound to the repository session")
void testFindByProductIdShouldBeBoundToTheRepositorySession() {
dropAndCreateCollection(productCollection);
session.startTransaction();
saveTestProductToDatabaseWithSession(session, product_1);
String idToFind = getNewStringId();
Stock stock_1 = newStockWithId(idToFind, product_1, QUANTITY_1);
saveTestStockToDatabaseWithSession(session, stock_1);
assertThat(stockRepository.findById(idToFind)).isEqualTo(stock_1);
session.commitTransaction();
}

// Private utility methods

private String getNewStringId() {
Expand Down Expand Up @@ -236,12 +261,11 @@ private Product saveTestProductToDatabaseWithSession(ClientSession session, Prod
productWithoutId.setId(doc.get("_id").toString());
return productWithoutId;
}

private void dropAndCreateCollection(MongoCollection<Document> collection) {
String name = collection.getNamespace().getCollectionName();
collection.drop();
database.createCollection(name);
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

import org.assertj.core.api.SoftAssertions;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
Expand All @@ -20,7 +21,6 @@
import com.github.raffaelliscandiffio.model.OrderStatus;
import com.github.raffaelliscandiffio.model.Product;


class OrderItemMySqlRepositoryIT {

private static final String DATABASE_NAME = "totem";
Expand All @@ -35,7 +35,6 @@ class OrderItemMySqlRepositoryIT {
private Order order_1;
private Order order_2;


@BeforeAll
public static void createEntityManagerFactory() {
System.setProperty("db.port", "3306");
Expand Down Expand Up @@ -133,6 +132,57 @@ void testDeleteOrderItem() {
assertThat(readAllOrderItemsFromDatabase()).containsExactly(item);
}

@Test
@DisplayName("Get list of OrderItems by order_id when there is exactly one match")
void testGetListByOrderIdWhenThereIsExactlyOneMatch() {
OrderItem match_1 = new OrderItem(product_1, order_1, QUANTITY_1);
OrderItem match_2 = new OrderItem(product_2, order_2, QUANTITY_1);
persistObjectToDatabase(match_1);
persistObjectToDatabase(match_2);
assertThat(orderItemRepository.getListByOrderId(order_1.getId())).containsExactly(match_1);
}

@Test
@DisplayName("Get list of OrderItems by order_id when there are multiple matches")
void testGetListByOrderIdWhenThereAreMultipleMatches() {
OrderItem match_1 = new OrderItem(product_1, order_1, QUANTITY_1);
OrderItem match_2 = new OrderItem(product_2, order_1, QUANTITY_1);
persistObjectToDatabase(match_1);
persistObjectToDatabase(match_2);
assertThat(orderItemRepository.getListByOrderId(order_1.getId())).containsExactlyInAnyOrder(match_1, match_2);
}

@Test
@DisplayName("Get list of OrderItems by order_id when there are 0 matches should return empty list")
void testGetListByOrderIdWhenThereIsNoMatchShouldReturnEmptyList() {
OrderItem match_1 = new OrderItem(product_1, order_2, QUANTITY_1);
OrderItem match_2 = new OrderItem(product_2, order_2, QUANTITY_1);
persistObjectToDatabase(match_1);
persistObjectToDatabase(match_2);
assertThat(orderItemRepository.getListByOrderId(order_1.getId())).isEmpty();
}

@Test
@DisplayName("Find OrderItem by order_id and product_id when there is a match on both fields")
void testFindOrderItemByOrderIdAndProductId() {
OrderItem match_1 = new OrderItem(product_1, order_1, QUANTITY_1);
persistObjectToDatabase(match_1);
assertThat(orderItemRepository.findByProductAndOrderId(product_1.getId(), order_1.getId())).isEqualTo(match_1);
}

@Test
@DisplayName("Find OrderItem by order_id and product_id when there is no match on both fields should return null")
void testFindOrderItemByOrderIdAndProductIdWhenAtLeasOneFieldDoesNotMatchShouldReturnNull() {
OrderItem match_1 = new OrderItem(product_1, order_1, QUANTITY_1);
OrderItem match_2 = new OrderItem(product_2, order_2, QUANTITY_1);
persistObjectToDatabase(match_1);
persistObjectToDatabase(match_2);
SoftAssertions softly = new SoftAssertions();
softly.assertThat(orderItemRepository.findByProductAndOrderId(product_1.getId(), order_2.getId())).isNull();
softly.assertThat(orderItemRepository.findByProductAndOrderId(product_2.getId(), order_1.getId())).isNull();
softly.assertAll();
}

private void persistObjectToDatabase(Object object) {
entityManager.getTransaction().begin();
entityManager.persist(object);
Expand Down
Loading

0 comments on commit 34780da

Please sign in to comment.