Skip to content

Commit

Permalink
Rename secondary ports and adapters
Browse files Browse the repository at this point in the history
  • Loading branch information
SvenWoltmann committed Jun 21, 2023
1 parent ec5a815 commit 23782f8
Show file tree
Hide file tree
Showing 26 changed files with 208 additions and 182 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package eu.happycoders.shop.adapter.out.persistence.inmemory;

import eu.happycoders.shop.application.port.out.persistence.CartPersistencePort;
import eu.happycoders.shop.application.port.out.persistence.CartRepository;
import eu.happycoders.shop.model.cart.Cart;
import eu.happycoders.shop.model.customer.CustomerId;
import java.util.Map;
Expand All @@ -12,7 +12,7 @@
*
* @author Sven Woltmann
*/
public class InMemoryCartPersistenceAdapter implements CartPersistencePort {
public class InMemoryCartRepository implements CartRepository {

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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package eu.happycoders.shop.adapter.out.persistence.inmemory;

import eu.happycoders.shop.adapter.out.persistence.DemoProducts;
import eu.happycoders.shop.application.port.out.persistence.ProductPersistencePort;
import eu.happycoders.shop.application.port.out.persistence.ProductRepository;
import eu.happycoders.shop.model.product.Product;
import eu.happycoders.shop.model.product.ProductId;
import java.util.List;
Expand All @@ -15,11 +15,11 @@
*
* @author Sven Woltmann
*/
public class InMemoryProductPersistenceAdapter implements ProductPersistencePort {
public class InMemoryProductRepository implements ProductRepository {

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

public InMemoryProductPersistenceAdapter() {
public InMemoryProductRepository() {
createDemoProducts();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package eu.happycoders.shop.adapter.out.persistence.jpa;

import eu.happycoders.shop.application.port.out.persistence.CartPersistencePort;
import eu.happycoders.shop.application.port.out.persistence.CartRepository;
import eu.happycoders.shop.model.cart.Cart;
import eu.happycoders.shop.model.customer.CustomerId;
import jakarta.persistence.EntityManager;
Expand All @@ -12,11 +12,11 @@
*
* @author Sven Woltmann
*/
public class JpaCartPersistenceAdapter implements CartPersistencePort {
public class JpaCartRepository implements CartRepository {

private final EntityManagerFactory entityManagerFactory;

public JpaCartPersistenceAdapter(EntityManagerFactory entityManagerFactory) {
public JpaCartRepository(EntityManagerFactory entityManagerFactory) {
this.entityManagerFactory = entityManagerFactory;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package eu.happycoders.shop.adapter.out.persistence.jpa;

import eu.happycoders.shop.adapter.out.persistence.DemoProducts;
import eu.happycoders.shop.application.port.out.persistence.ProductPersistencePort;
import eu.happycoders.shop.application.port.out.persistence.ProductRepository;
import eu.happycoders.shop.model.product.Product;
import eu.happycoders.shop.model.product.ProductId;
import jakarta.persistence.EntityManager;
Expand All @@ -15,11 +15,11 @@
*
* @author Sven Woltmann
*/
public class JpaProductPersistenceAdapter implements ProductPersistencePort {
public class JpaProductRepository implements ProductRepository {

private final EntityManagerFactory entityManagerFactory;

public JpaProductPersistenceAdapter(EntityManagerFactory entityManagerFactory) {
public JpaProductRepository(EntityManagerFactory entityManagerFactory) {
this.entityManagerFactory = entityManagerFactory;
createDemoProducts();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import static eu.happycoders.shop.model.product.TestProductFactory.createTestProduct;
import static org.assertj.core.api.Assertions.assertThat;

import eu.happycoders.shop.application.port.out.persistence.CartPersistencePort;
import eu.happycoders.shop.application.port.out.persistence.ProductPersistencePort;
import eu.happycoders.shop.application.port.out.persistence.CartRepository;
import eu.happycoders.shop.application.port.out.persistence.ProductRepository;
import eu.happycoders.shop.model.cart.Cart;
import eu.happycoders.shop.model.cart.CartLineItem;
import eu.happycoders.shop.model.cart.NotEnoughItemsInStockException;
Expand All @@ -16,23 +16,23 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public abstract class AbstractCartPersistenceAdapterTest<
T extends CartPersistencePort, U extends ProductPersistencePort> {
public abstract class AbstractCartRepositoryTest<
T extends CartRepository, U extends ProductRepository> {

private static final Product TEST_PRODUCT_1 = createTestProduct(euros(19, 99));
private static final Product TEST_PRODUCT_2 = createTestProduct(euros(1, 49));

private static final AtomicInteger CUSTOMER_ID_SEQUENCE_GENERATOR = new AtomicInteger();

protected T persistenceAdapter;
protected T cartRepository;

@BeforeEach
void resetPersistenceAdapter() {
persistenceAdapter = createPersistenceAdapter();
cartRepository = createCartRepository();
persistTestProducts();
}

protected abstract T createPersistenceAdapter();
protected abstract T createCartRepository();

private void persistTestProducts() {
U productPersistenceAdapter = createProductPersistenceAdapter();
Expand All @@ -46,7 +46,7 @@ private void persistTestProducts() {
void givenACustomerIdForWhichNoCartIsPersisted_findByCustomerId_returnsAnEmptyOptional() {
CustomerId customerId = createUniqueCustomerId();

Optional<Cart> cart = persistenceAdapter.findByCustomerId(customerId);
Optional<Cart> cart = cartRepository.findByCustomerId(customerId);

assertThat(cart).isEmpty();
}
Expand All @@ -58,9 +58,9 @@ void givenPersistedCartWithProduct_findByCustomerId_returnsTheAppropriateCart()

Cart persistedCart = new Cart(customerId);
persistedCart.addProduct(TEST_PRODUCT_1, 1);
persistenceAdapter.save(persistedCart);
cartRepository.save(persistedCart);

Optional<Cart> cart = persistenceAdapter.findByCustomerId(customerId);
Optional<Cart> cart = cartRepository.findByCustomerId(customerId);

assertThat(cart).isNotEmpty();
assertThat(cart.get().id()).isEqualTo(customerId);
Expand All @@ -77,13 +77,13 @@ void givenPersistedCartWithProduct_findByCustomerId_returnsTheAppropriateCart()

Cart existingCart = new Cart(customerId);
existingCart.addProduct(TEST_PRODUCT_1, 1);
persistenceAdapter.save(existingCart);
cartRepository.save(existingCart);

Cart newCart = new Cart(customerId);
newCart.addProduct(TEST_PRODUCT_2, 2);
persistenceAdapter.save(newCart);
cartRepository.save(newCart);

Optional<Cart> cart = persistenceAdapter.findByCustomerId(customerId);
Optional<Cart> cart = cartRepository.findByCustomerId(customerId);
assertThat(cart).isNotEmpty();
assertThat(cart.get().id()).isEqualTo(customerId);
assertThat(cart.get().lineItems()).hasSize(1);
Expand All @@ -98,13 +98,13 @@ void givenExistingCartWithProduct_addProductAndSaveCart_updatesTheExistingCart()

Cart existingCart = new Cart(customerId);
existingCart.addProduct(TEST_PRODUCT_1, 1);
persistenceAdapter.save(existingCart);
cartRepository.save(existingCart);

existingCart = persistenceAdapter.findByCustomerId(customerId).orElseThrow();
existingCart = cartRepository.findByCustomerId(customerId).orElseThrow();
existingCart.addProduct(TEST_PRODUCT_2, 2);
persistenceAdapter.save(existingCart);
cartRepository.save(existingCart);

Optional<Cart> cart = persistenceAdapter.findByCustomerId(customerId);
Optional<Cart> cart = cartRepository.findByCustomerId(customerId);
assertThat(cart).isNotEmpty();
assertThat(cart.get().id()).isEqualTo(customerId);
assertThat(cart.get().lineItems())
Expand All @@ -117,23 +117,23 @@ void givenExistingCart_deleteById_deletesTheCart() {
CustomerId customerId = createUniqueCustomerId();

Cart existingCart = new Cart(customerId);
persistenceAdapter.save(existingCart);
cartRepository.save(existingCart);

assertThat(persistenceAdapter.findByCustomerId(customerId)).isNotEmpty();
assertThat(cartRepository.findByCustomerId(customerId)).isNotEmpty();

persistenceAdapter.deleteById(customerId);
cartRepository.deleteById(customerId);

assertThat(persistenceAdapter.findByCustomerId(customerId)).isEmpty();
assertThat(cartRepository.findByCustomerId(customerId)).isEmpty();
}

@Test
void givenNotExistingCart_deleteById_doesNothing() {
CustomerId customerId = createUniqueCustomerId();
assertThat(persistenceAdapter.findByCustomerId(customerId)).isEmpty();
assertThat(cartRepository.findByCustomerId(customerId)).isEmpty();

persistenceAdapter.deleteById(customerId);
cartRepository.deleteById(customerId);

assertThat(persistenceAdapter.findByCustomerId(customerId)).isEmpty();
assertThat(cartRepository.findByCustomerId(customerId)).isEmpty();
}

private static CustomerId createUniqueCustomerId() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@

import static org.assertj.core.api.Assertions.assertThat;

import eu.happycoders.shop.application.port.out.persistence.ProductPersistencePort;
import eu.happycoders.shop.application.port.out.persistence.ProductRepository;
import eu.happycoders.shop.model.product.Product;
import eu.happycoders.shop.model.product.ProductId;
import java.util.List;
import java.util.Optional;
import org.junit.jupiter.api.Test;

public abstract class AbstractProductPersistenceAdapterTest<T extends ProductPersistencePort> {
public abstract class AbstractProductRepositoryTest<T extends ProductRepository> {

protected T productPersistenceAdapter;
protected T productRepository;

@Test
void givenTestProductsAndATestProductId_findById_returnsATestProduct() {
ProductId productId = DemoProducts.COMPUTER_MONITOR.id();

Optional<Product> product = productPersistenceAdapter.findById(productId);
Optional<Product> product = productRepository.findById(productId);

assertThat(product).contains(DemoProducts.COMPUTER_MONITOR);
}
Expand All @@ -26,7 +26,7 @@ void givenTestProductsAndATestProductId_findById_returnsATestProduct() {
void givenTheIdOfAProductNotPersisted_findById_returnsAnEmptyOptional() {
ProductId productId = new ProductId("00000");

Optional<Product> product = productPersistenceAdapter.findById(productId);
Optional<Product> product = productRepository.findById(productId);

assertThat(product).isEmpty();
}
Expand All @@ -35,7 +35,7 @@ void givenTheIdOfAProductNotPersisted_findById_returnsAnEmptyOptional() {
void givenTestProductsAndASearchQueryNotMatchingAndProduct_findById_returnsAnEmptyList() {
String query = "not matching any product";

List<Product> products = productPersistenceAdapter.findByNameOrDescription(query);
List<Product> products = productRepository.findByNameOrDescription(query);

assertThat(products).isEmpty();
}
Expand All @@ -44,7 +44,7 @@ void givenTestProductsAndASearchQueryNotMatchingAndProduct_findById_returnsAnEmp
void givenTestProductsAndASearchQueryMatchingOneProduct_findById_returnsThatProduct() {
String query = "lights";

List<Product> products = productPersistenceAdapter.findByNameOrDescription(query);
List<Product> products = productRepository.findByNameOrDescription(query);

assertThat(products).containsExactlyInAnyOrder(DemoProducts.LED_LIGHTS);
}
Expand All @@ -53,7 +53,7 @@ void givenTestProductsAndASearchQueryMatchingOneProduct_findById_returnsThatProd
void givenTestProductsAndASearchQueryMatchingTwoProducts_findById_returnsThoseProducts() {
String query = "monitor";

List<Product> products = productPersistenceAdapter.findByNameOrDescription(query);
List<Product> products = productRepository.findByNameOrDescription(query);

assertThat(products)
.containsExactlyInAnyOrder(DemoProducts.COMPUTER_MONITOR, DemoProducts.MONITOR_DESK_MOUNT);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package eu.happycoders.shop.adapter.out.persistence.inmemory;

import eu.happycoders.shop.adapter.out.persistence.AbstractCartRepositoryTest;

class InMemoryCartRepositoryTest
extends AbstractCartRepositoryTest<InMemoryCartRepository, InMemoryProductRepository> {

@Override
protected InMemoryCartRepository createCartRepository() {
return new InMemoryCartRepository();
}

@Override
protected InMemoryProductRepository createProductPersistenceAdapter() {
return new InMemoryProductRepository();
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package eu.happycoders.shop.adapter.out.persistence.inmemory;

import eu.happycoders.shop.adapter.out.persistence.AbstractProductRepositoryTest;
import org.junit.jupiter.api.BeforeEach;

class InMemoryProductRepositoryTest
extends AbstractProductRepositoryTest<InMemoryProductRepository> {

@BeforeEach
void setUpPersistenceAdapter() {
productRepository = new InMemoryProductRepository();
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package eu.happycoders.shop.adapter.out.persistence.jpa;

import eu.happycoders.shop.adapter.out.persistence.AbstractCartPersistenceAdapterTest;
import eu.happycoders.shop.adapter.out.persistence.AbstractCartRepositoryTest;
import jakarta.persistence.EntityManagerFactory;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.testcontainers.containers.MySQLContainer;
import org.testcontainers.utility.DockerImageName;

class JpaCartPersistenceAdapterTest
extends AbstractCartPersistenceAdapterTest<
JpaCartPersistenceAdapter, JpaProductPersistenceAdapter> {
class JpaCartRepositoryTest
extends AbstractCartRepositoryTest<JpaCartRepository, JpaProductRepository> {

private static MySQLContainer<?> mysql;
private static EntityManagerFactory entityManagerFactory;
Expand All @@ -25,13 +24,13 @@ static void startDatabase() {
}

@Override
protected JpaCartPersistenceAdapter createPersistenceAdapter() {
return new JpaCartPersistenceAdapter(entityManagerFactory);
protected JpaCartRepository createCartRepository() {
return new JpaCartRepository(entityManagerFactory);
}

@Override
protected JpaProductPersistenceAdapter createProductPersistenceAdapter() {
return new JpaProductPersistenceAdapter(entityManagerFactory);
protected JpaProductRepository createProductPersistenceAdapter() {
return new JpaProductRepository(entityManagerFactory);
}

@AfterAll
Expand Down
Loading

0 comments on commit 23782f8

Please sign in to comment.