BAEL-3777: Working version
This commit is contained in:
parent
1981d10ffd
commit
b05d42a6ea
|
@ -4,6 +4,7 @@ import java.math.BigDecimal;
|
|||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.UUID;
|
||||
|
||||
public class Order {
|
||||
|
@ -77,6 +78,22 @@ public class Order {
|
|||
return Collections.unmodifiableList(orderItems);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(id, orderItems, price, status);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (!(obj instanceof Order))
|
||||
return false;
|
||||
Order other = (Order) obj;
|
||||
return Objects.equals(id, other.id) && Objects.equals(orderItems, other.orderItems) && Objects.equals(price, other.price) && status == other.status;
|
||||
}
|
||||
|
||||
private Order() {
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,12 +4,14 @@ import java.util.Optional;
|
|||
import java.util.UUID;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import com.baeldung.dddhexagonalspring.domain.Order;
|
||||
import com.baeldung.dddhexagonalspring.domain.repository.OrderRepository;
|
||||
|
||||
@Component
|
||||
@Primary
|
||||
public class CassandraDbOrderRepository implements OrderRepository {
|
||||
|
||||
private final SpringDataCassandraOrderRepository orderRepository;
|
||||
|
@ -21,14 +23,17 @@ public class CassandraDbOrderRepository implements OrderRepository {
|
|||
|
||||
@Override
|
||||
public Optional<Order> findById(UUID id) {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
Optional<OrderEntity> orderEntity = orderRepository.findById(id);
|
||||
if (orderEntity.isPresent()) {
|
||||
return Optional.of(orderEntity.get().toOrder());
|
||||
} else {
|
||||
return Optional.empty();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void save(Order order) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
orderRepository.save(new OrderEntity(order));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -10,7 +10,6 @@ import java.util.Optional;
|
|||
import java.util.UUID;
|
||||
|
||||
@Component
|
||||
@Primary
|
||||
public class MongoDbOrderRepository implements OrderRepository {
|
||||
|
||||
private final SpringDataMongoOrderRepository orderRepository;
|
||||
|
|
|
@ -0,0 +1,75 @@
|
|||
package com.baeldung.dddhexagonalspring.infrastracture.repository;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.springframework.data.cassandra.core.mapping.PrimaryKey;
|
||||
|
||||
import com.baeldung.dddhexagonalspring.domain.Order;
|
||||
import com.baeldung.dddhexagonalspring.domain.OrderItem;
|
||||
import com.baeldung.dddhexagonalspring.domain.OrderStatus;
|
||||
import com.baeldung.dddhexagonalspring.domain.Product;
|
||||
|
||||
public class OrderEntity {
|
||||
|
||||
@PrimaryKey
|
||||
private UUID id;
|
||||
private OrderStatus status;
|
||||
private List<OrderItemEntity> orderItemEntities;
|
||||
private BigDecimal price;
|
||||
|
||||
public OrderEntity(UUID id, OrderStatus status, List<OrderItemEntity> orderItemEntities, BigDecimal price) {
|
||||
this.id = id;
|
||||
this.status = status;
|
||||
this.orderItemEntities = orderItemEntities;
|
||||
this.price = price;
|
||||
}
|
||||
|
||||
public OrderEntity() {
|
||||
}
|
||||
|
||||
public OrderEntity(Order order) {
|
||||
this.id = order.getId();
|
||||
this.price = order.getPrice();
|
||||
this.status = order.getStatus();
|
||||
this.orderItemEntities = order.getOrderItems()
|
||||
.stream()
|
||||
.map(OrderItemEntity::new)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
}
|
||||
|
||||
public Order toOrder() {
|
||||
List<OrderItem> orderItems = orderItemEntities.stream()
|
||||
.map(OrderItemEntity::toOrderItem)
|
||||
.collect(Collectors.toList());
|
||||
List<Product> namelessProducts = orderItems.stream()
|
||||
.map(orderItem -> new Product(orderItem.getProductId(), orderItem.getPrice(), ""))
|
||||
.collect(Collectors.toList());
|
||||
Order order = new Order(id, namelessProducts.remove(0));
|
||||
namelessProducts.forEach(product -> order.addOrder(product));
|
||||
if (status == OrderStatus.COMPLETED) {
|
||||
order.complete();
|
||||
}
|
||||
return order;
|
||||
}
|
||||
|
||||
public UUID getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public OrderStatus getStatus() {
|
||||
return status;
|
||||
}
|
||||
|
||||
public List<OrderItemEntity> getOrderItems() {
|
||||
return orderItemEntities;
|
||||
}
|
||||
|
||||
public BigDecimal getPrice() {
|
||||
return price;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,44 @@
|
|||
package com.baeldung.dddhexagonalspring.infrastracture.repository;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.springframework.data.cassandra.core.mapping.UserDefinedType;
|
||||
|
||||
import com.baeldung.dddhexagonalspring.domain.OrderItem;
|
||||
import com.baeldung.dddhexagonalspring.domain.Product;
|
||||
|
||||
@UserDefinedType
|
||||
public class OrderItemEntity {
|
||||
|
||||
private UUID productId;
|
||||
private BigDecimal price;
|
||||
|
||||
public OrderItemEntity() {
|
||||
}
|
||||
|
||||
public OrderItemEntity(final OrderItem orderItem) {
|
||||
this.productId = orderItem.getProductId();
|
||||
this.price = orderItem.getPrice();
|
||||
}
|
||||
|
||||
public OrderItem toOrderItem() {
|
||||
return new OrderItem(new Product(productId, price, ""));
|
||||
}
|
||||
|
||||
public UUID getProductId() {
|
||||
return productId;
|
||||
}
|
||||
|
||||
public void setProductId(UUID productId) {
|
||||
this.productId = productId;
|
||||
}
|
||||
|
||||
public BigDecimal getPrice() {
|
||||
return price;
|
||||
}
|
||||
|
||||
public void setPrice(BigDecimal price) {
|
||||
this.price = price;
|
||||
}
|
||||
}
|
|
@ -8,6 +8,6 @@ import org.springframework.stereotype.Repository;
|
|||
import com.baeldung.dddhexagonalspring.domain.Order;
|
||||
|
||||
@Repository
|
||||
public interface SpringDataCassandraOrderRepository extends CassandraRepository<Order, UUID>{
|
||||
public interface SpringDataCassandraOrderRepository extends CassandraRepository<OrderEntity, UUID>{
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,55 @@
|
|||
package com.baeldung.dddhexagonalspring.infrastracture.repository;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.TestPropertySource;
|
||||
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
|
||||
|
||||
import com.baeldung.dddhexagonalspring.domain.Order;
|
||||
import com.baeldung.dddhexagonalspring.domain.Product;
|
||||
import com.baeldung.dddhexagonalspring.domain.repository.OrderRepository;
|
||||
|
||||
@SpringJUnitConfig
|
||||
@SpringBootTest
|
||||
@TestPropertySource("classpath:ddd-layers-test.properties")
|
||||
class CassandraDbOrderRepositoryIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private SpringDataCassandraOrderRepository cassandraOrderRepository;
|
||||
|
||||
@Autowired
|
||||
private OrderRepository orderRepository;
|
||||
|
||||
@After
|
||||
void cleanUp(){
|
||||
cassandraOrderRepository.deleteAll();
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldFindById_thenReturnOrder() {
|
||||
|
||||
// given
|
||||
final UUID id = UUID.randomUUID();
|
||||
final Order order = createOrder(id);
|
||||
order.complete();
|
||||
|
||||
// when
|
||||
orderRepository.save(order);
|
||||
|
||||
final Optional<Order> result = orderRepository.findById(id);
|
||||
|
||||
assertEquals(order, result.get());
|
||||
}
|
||||
|
||||
private Order createOrder(UUID id) {
|
||||
return new Order(id, new Product(UUID.randomUUID(), BigDecimal.TEN, "product"));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,54 @@
|
|||
package com.baeldung.dddhexagonalspring.infrastracture.repository;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.TestPropertySource;
|
||||
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
|
||||
|
||||
import com.baeldung.dddhexagonalspring.domain.Order;
|
||||
import com.baeldung.dddhexagonalspring.domain.Product;
|
||||
import com.baeldung.dddhexagonalspring.domain.repository.OrderRepository;
|
||||
|
||||
@SpringJUnitConfig
|
||||
@SpringBootTest
|
||||
@TestPropertySource("classpath:ddd-layers-test.properties")
|
||||
class MongoDbOrderRepositoryIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private SpringDataMongoOrderRepository mongoOrderRepository;
|
||||
|
||||
@Autowired
|
||||
private OrderRepository orderRepository;
|
||||
|
||||
@After
|
||||
void cleanUp(){
|
||||
mongoOrderRepository.deleteAll();
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldFindById_thenReturnOrder() {
|
||||
|
||||
// given
|
||||
final UUID id = UUID.randomUUID();
|
||||
final Order order = createOrder(id);
|
||||
|
||||
// when
|
||||
orderRepository.save(order);
|
||||
|
||||
final Optional<Order> result = orderRepository.findById(id);
|
||||
|
||||
assertEquals(order, result.get());
|
||||
}
|
||||
|
||||
private Order createOrder(UUID id) {
|
||||
return new Order(id, new Product(UUID.randomUUID(), BigDecimal.TEN, "product"));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
CREATE KEYSPACE IF NOT exists order_database
|
||||
WITH replication = {'class':'SimpleStrategy', 'replication_factor':1};
|
||||
|
||||
CREATE TYPE order_database.orderitementity (productid uuid, price decimal);
|
||||
|
||||
CREATE TABLE order_database.orderentity(
|
||||
id uuid,
|
||||
status text,
|
||||
orderitementities list<frozen<orderitementity>>,
|
||||
price decimal,
|
||||
primary key(id)
|
||||
);
|
|
@ -0,0 +1,12 @@
|
|||
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.mongo.embedded.EmbeddedMongoAutoConfiguration
|
||||
spring.data.mongodb.host=127.0.0.1
|
||||
spring.data.mongodb.port=27017
|
||||
spring.data.mongodb.database=order-database
|
||||
spring.data.mongodb.username=order
|
||||
spring.data.mongodb.password=order
|
||||
|
||||
spring.data.cassandra.keyspaceName=order_database
|
||||
spring.data.cassandra.username=cassandra
|
||||
spring.data.cassandra.password=cassandra
|
||||
spring.data.cassandra.contactPoints=127.0.0.1
|
||||
spring.data.cassandra.port=9042
|
Loading…
Reference in New Issue