Skip to content

Commit

Permalink
Merge pull request #72 from NashTech-Labs/feature/unit-test-for-order…
Browse files Browse the repository at this point in the history
…-service

added unit test cases and readMe file
  • Loading branch information
abidknashtech authored Jan 16, 2024
2 parents c2f3244 + 712ca9c commit 066dbe3
Show file tree
Hide file tree
Showing 12 changed files with 444 additions and 15 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ The listed services are operate sequentially.
***Phase-1***
- admin-service
- inventory-service
- order-service
- order-service [README.md](order-service%2FREADME.md)
- payment-service
- shipment-service
- elasticsearch
Expand Down Expand Up @@ -197,12 +197,13 @@ To verify the deployment of Kubernetes on Google Cloud Platform (GCP), follow th
Examine logs for applications and services to identify any issues or error messages.
```

#### Data platform
Data Analytics and Visualization
--
For the analytical purpose, the insightful data generated by microservices brings to the databricks
Lakehouse platform.
find the details here [README.md](databricks%2FREADME.md)

Documentation
--
- Demo session [available](documentation/automobile ecommerce-platform.pptx)
- Demo Presentation [available](https://harveynashvn-my.sharepoint.com/personal/sushant_gupta_nashtechglobal_com/_layouts/15/stream.aspx?id=%2Fpersonal%2Fsushant%5Fgupta%5Fnashtechglobal%5Fcom%2FDocuments%2FJava%2Dcompetency%2Dartifacts%2Dinternal%2Dproject%2FRecordings%2Fdemo%5F%20automobile%20eCommerce%20platform%2D20231206%5F142041%2DMeeting%20Recording%2Emp4&referrer=StreamWebApp%2EWeb&referrerScenario=AddressBarCopied%2Eview&isSPOFile=1&OR=Teams%2DHL&CT=1705060735545&clickparams=eyJBcHBOYW1lIjoiVGVhbXMtRGVza3RvcCIsIkFwcFZlcnNpb24iOiI0OS8yMzExMzAyODcyMCIsIkhhc0ZlZGVyYXRlZFVzZXIiOmZhbHNlfQ%3D%3D)
- Demo session [available](https://harveynashvn-my.sharepoint.com/personal/sushant_gupta_nashtechglobal_com/_layouts/15/stream.aspx?id=%2Fpersonal%2Fsushant%5Fgupta%5Fnashtechglobal%5Fcom%2FDocuments%2FJava%2Dcompetency%2Dartifacts%2Dinternal%2Dproject%2FRecordings%2Fdemo%5F%20automobile%20eCommerce%20platform%2D20231206%5F142041%2DMeeting%20Recording%2Emp4&referrer=StreamWebApp%2EWeb&referrerScenario=AddressBarCopied%2Eview&isSPOFile=1&OR=Teams%2DHL&CT=1705060735545&clickparams=eyJBcHBOYW1lIjoiVGVhbXMtRGVza3RvcCIsIkFwcFZlcnNpb24iOiI0OS8yMzExMzAyODcyMCIsIkhhc0ZlZGVyYXRlZFVzZXIiOmZhbHNlfQ%3D%3D)
- Demo Presentation [available](documentation/automobile ecommerce-platform.pptx)
2 changes: 1 addition & 1 deletion common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@

<properties>
<java.version>19</java.version>
<axon-starter.version>4.8.1</axon-starter.version>
<axon-starter.version>4.9.1</axon-starter.version>
<dotenv.version>4.0.0</dotenv.version>
<mysql-connector.version>8.0.33</mysql-connector.version>
<maven.jacoco.version>0.8.8</maven.jacoco.version>
Expand Down
Binary file added documentation/order-config.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
38 changes: 38 additions & 0 deletions order-service/README.md
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.
19 changes: 19 additions & 0 deletions order-service/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,25 @@
<artifactId>validation-api</artifactId>
<version>${validation-api.version}</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.axonframework</groupId>
<artifactId>axon-test</artifactId>
<version>4.9.1</version>
</dependency>

</dependencies>

<build>
Expand Down
104 changes: 104 additions & 0 deletions order-service/src/test/java/com/nashtech/order/OrderAggregateTest.java
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);
}
}
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);
}
}
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());
}
}
Loading

0 comments on commit 066dbe3

Please sign in to comment.