-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #72 from NashTech-Labs/feature/unit-test-for-order…
…-service added unit test cases and readMe file
- Loading branch information
Showing
12 changed files
with
444 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# Order Service | ||
This service monitors incoming order requests from the car UI (Car Dashboard), executes the order placement, and provides the relevant response. | ||
|
||
## Prerequisites | ||
We must set up Axon Server to handle command and query operations, and configure MySQL as the persistent store destination. | ||
|
||
![order-config.png](..%2Fdocumentation%2Forder-config.png) | ||
|
||
## Local setup | ||
|
||
- Setup axon-server and MySQL docker images | ||
``` | ||
local-dev > docker compose up -d | ||
``` | ||
- Run the application | ||
``` | ||
order-service > mvn clean springboot:run | ||
``` | ||
- Endpoint for resting | ||
- Generate a request for an order. | ||
```arm | ||
curl --location 'http://localhost:9090/orders/create' \ | ||
--header 'Content-Type: application/json' \ | ||
--data '{ | ||
"productId": "199", | ||
"quantity": 1, | ||
"userId": "1652" | ||
}' | ||
``` | ||
- Get user's order. | ||
``` | ||
curl --location 'http://localhost:9090/orders/1652' | ||
``` | ||
## Contributing | ||
|
||
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change. | ||
|
||
Please make sure to update tests as appropriate. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
order-service/src/test/java/com/nashtech/order/OrderAggregateTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package com.nashtech.order; | ||
|
||
import com.nashtech.common.utils.OrderStatus; | ||
import com.nashtech.order.aggregate.OrderAggregate; | ||
import com.nashtech.order.commands.ApproveOrderCommand; | ||
import com.nashtech.order.commands.CreateOrderCommand; | ||
import com.nashtech.order.commands.RejectOrderCommand; | ||
import com.nashtech.order.events.OrderApprovedEvent; | ||
import com.nashtech.order.events.OrderCancelledEvent; | ||
import com.nashtech.order.events.OrderCreatedEvent; | ||
import org.axonframework.test.aggregate.AggregateTestFixture; | ||
import org.axonframework.test.aggregate.FixtureConfiguration; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import java.util.UUID; | ||
|
||
public class OrderAggregateTest { | ||
|
||
private static final String ORDER_ID = UUID.randomUUID().toString(); | ||
private static final String PRODUCT_ID = UUID.randomUUID().toString(); | ||
private static final String PAYMENT_ID = UUID.randomUUID().toString(); | ||
private static final String SHIPMENT_ID = UUID.randomUUID().toString(); | ||
private static final String USER_ID = "1652"; | ||
|
||
private FixtureConfiguration<OrderAggregate> fixture; | ||
|
||
@Before | ||
public void setUp() { | ||
fixture = new AggregateTestFixture<>(OrderAggregate.class); | ||
} | ||
|
||
private static final OrderCreatedEvent orderCreatedEvent = OrderCreatedEvent.builder() | ||
.orderId(ORDER_ID) | ||
.productId(PRODUCT_ID) | ||
.quantity(2) | ||
.userId(USER_ID) | ||
.orderStatus(OrderStatus.ORDER_PARTIALLY_APPROVED) | ||
.build(); | ||
|
||
@Test | ||
public void testCreateOrderCommand() { | ||
CreateOrderCommand createOrderCommand = CreateOrderCommand.builder() | ||
.productId(PRODUCT_ID) | ||
.userId(USER_ID) | ||
.quantity(2) | ||
.orderId(ORDER_ID) | ||
.build(); | ||
|
||
fixture.givenNoPriorActivity() | ||
.when(createOrderCommand) | ||
.expectSuccessfulHandlerExecution() | ||
.expectEvents(orderCreatedEvent); | ||
} | ||
|
||
@Test | ||
public void testApproveOrderCommand() { | ||
ApproveOrderCommand approveOrderCommand = ApproveOrderCommand.builder() | ||
.orderId(ORDER_ID) | ||
.paymentId(PAYMENT_ID) | ||
.shipmentId(SHIPMENT_ID) | ||
.orderStatus(OrderStatus.ORDER_PLACED) | ||
.build(); | ||
|
||
OrderApprovedEvent orderApprovedEvent = OrderApprovedEvent.builder() | ||
.orderId(approveOrderCommand.getOrderId()) | ||
.paymentId(approveOrderCommand.getPaymentId()) | ||
.shipmentId(approveOrderCommand.getShipmentId()) | ||
.orderStatus(approveOrderCommand.getOrderStatus()) | ||
.build(); | ||
|
||
fixture.given(orderCreatedEvent) | ||
.when(approveOrderCommand) | ||
.expectSuccessfulHandlerExecution() | ||
.expectEvents(orderApprovedEvent); | ||
} | ||
|
||
@Test | ||
public void testRejectOrderCommand() { | ||
RejectOrderCommand rejectOrderCommand = RejectOrderCommand.builder() | ||
.userId(USER_ID) | ||
.orderId(ORDER_ID) | ||
.productId(PRODUCT_ID) | ||
.paymentId(PAYMENT_ID) | ||
.reasonToFailed("The user has insufficient amounts.") | ||
.orderStatus(OrderStatus.ORDER_NOT_APPROVED) | ||
.build(); | ||
|
||
OrderCancelledEvent orderCancelledEvent = OrderCancelledEvent.builder() | ||
.orderId(rejectOrderCommand.getOrderId()) | ||
.productId(rejectOrderCommand.getProductId()) | ||
.paymentId(rejectOrderCommand.getPaymentId()) | ||
.shipmentId(rejectOrderCommand.getShipmentId()) | ||
.userId(rejectOrderCommand.getUserId()) | ||
.reasonToFailed(rejectOrderCommand.getReasonToFailed()) | ||
.orderStatus(rejectOrderCommand.getOrderStatus()) | ||
.build(); | ||
|
||
fixture.given(orderCreatedEvent) | ||
.when(rejectOrderCommand) | ||
.expectSuccessfulHandlerExecution() | ||
.expectEvents(orderCancelledEvent); | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
order-service/src/test/java/com/nashtech/order/OrderEventsHandlerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package com.nashtech.order; | ||
|
||
import com.nashtech.common.utils.OrderStatus; | ||
import com.nashtech.order.events.OrderApprovedEvent; | ||
import com.nashtech.order.events.OrderCancelledEvent; | ||
import com.nashtech.order.events.OrderCreatedEvent; | ||
import com.nashtech.order.handler.OrderEventsHandler; | ||
import com.nashtech.order.repository.FailedOrderRepository; | ||
import com.nashtech.order.repository.OrderRepository; | ||
import com.nashtech.order.repository.entity.OrderEntity; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import java.util.Date; | ||
import java.util.UUID; | ||
|
||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
public class OrderEventsHandlerTest { | ||
private static final String ORDER_ID = UUID.randomUUID().toString(); | ||
private static final String PRODUCT_ID = UUID.randomUUID().toString(); | ||
private static final String PAYMENT_ID = UUID.randomUUID().toString(); | ||
private static final String SHIPMENT_ID = UUID.randomUUID().toString(); | ||
private static final String USER_ID = "1652"; | ||
|
||
|
||
private OrderRepository orderRepository = mock(OrderRepository.class); | ||
|
||
private FailedOrderRepository failedOrderRepository = mock(FailedOrderRepository.class); | ||
|
||
// @Autowired | ||
private OrderEventsHandler handler; | ||
|
||
@Before | ||
public void setUp() { | ||
handler = new OrderEventsHandler(orderRepository, failedOrderRepository); | ||
} | ||
|
||
|
||
@Test | ||
public void testOrderCreatedEventHandler() { | ||
OrderCreatedEvent orderCreatedEvent = OrderCreatedEvent.builder() | ||
.orderId(ORDER_ID) | ||
.productId(PRODUCT_ID) | ||
.quantity(2) | ||
.userId(USER_ID) | ||
.orderStatus(OrderStatus.ORDER_PLACED) | ||
.build(); | ||
handler.on(orderCreatedEvent); | ||
} | ||
|
||
@Test | ||
public void testOrderApprovedEventHandler() { | ||
OrderApprovedEvent orderApprovedEvent = OrderApprovedEvent.builder() | ||
.orderId(ORDER_ID) | ||
.paymentId(PAYMENT_ID) | ||
.shipmentId(SHIPMENT_ID) | ||
.orderStatus(OrderStatus.ORDER_PLACED) | ||
.build(); | ||
|
||
OrderEntity order = new OrderEntity(orderApprovedEvent.getOrderId(), USER_ID, PRODUCT_ID, | ||
null, null, new Date(), orderApprovedEvent.getOrderStatus().toString()); | ||
|
||
when(orderRepository.findByOrderId(ORDER_ID)).thenReturn(order); | ||
|
||
handler.on(orderApprovedEvent); | ||
} | ||
|
||
@Test | ||
public void testOrderCancelledEventHandler() { | ||
OrderCancelledEvent orderCancelledEvent = OrderCancelledEvent.builder() | ||
.orderId(ORDER_ID) | ||
.userId(USER_ID) | ||
.productId(PRODUCT_ID) | ||
.reasonToFailed("Car quantity not sufficient in inventory") | ||
.orderStatus(OrderStatus.ORDER_NOT_APPROVED) | ||
.build(); | ||
handler.on(orderCancelledEvent); | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
order-service/src/test/java/com/nashtech/order/OrderQueriesHandlerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package com.nashtech.order; | ||
|
||
import com.nashtech.common.utils.OrderStatus; | ||
import com.nashtech.order.query.FindOrdersByUserQuery; | ||
import com.nashtech.order.query.OrderQueriesHandler; | ||
import com.nashtech.order.repository.OrderRepository; | ||
import com.nashtech.order.repository.entity.OrderEntity; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Date; | ||
import java.util.List; | ||
import java.util.UUID; | ||
|
||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
public class OrderQueriesHandlerTest { | ||
|
||
private static final String ORDER_ID1 = UUID.randomUUID().toString(); | ||
private static final String ORDER_ID2 = UUID.randomUUID().toString(); | ||
private static final String PRODUCT_ID = UUID.randomUUID().toString(); | ||
private static final String USER_ID = "1652"; | ||
|
||
private final OrderRepository orderRepository = mock(OrderRepository.class); | ||
|
||
private OrderQueriesHandler queriesHandler; | ||
|
||
@Before | ||
public void setUp() { | ||
queriesHandler = new OrderQueriesHandler(orderRepository); | ||
} | ||
|
||
@Test | ||
public void testOrderCreatedEventHandler() { | ||
FindOrdersByUserQuery findOrdersByUserQuery = new FindOrdersByUserQuery(USER_ID); | ||
|
||
List<OrderEntity> orderEntityList = new ArrayList<>(); | ||
OrderEntity order1 = new OrderEntity(ORDER_ID1, USER_ID, PRODUCT_ID, | ||
null, null, new Date(), OrderStatus.ORDER_APPROVED.toString()); | ||
orderEntityList.add(order1); | ||
OrderEntity order2= new OrderEntity(ORDER_ID2, USER_ID, PRODUCT_ID, | ||
null, null, new Date(), OrderStatus.ORDER_APPROVED.toString()); | ||
orderEntityList.add(order2); | ||
when(orderRepository.findByUserId(USER_ID)).thenReturn(orderEntityList); | ||
|
||
List<OrderEntity> orders = queriesHandler.findOrders(findOrdersByUserQuery); | ||
|
||
Assert.assertEquals(2,orders.size()); | ||
Assert.assertEquals(ORDER_ID1,orders.get(0).getOrderId()); | ||
Assert.assertEquals(ORDER_ID2,orders.get(1).getOrderId()); | ||
} | ||
} |
Oops, something went wrong.