Skip to content

Commit

Permalink
Merge pull request #81 from NashTech-Labs/testcases/issues
Browse files Browse the repository at this point in the history
Testcases/issues
  • Loading branch information
sabiaparveen1 authored Jan 25, 2024
2 parents f5ab1d7 + 65c1339 commit fc0bb96
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 108 deletions.
2 changes: 1 addition & 1 deletion common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

<groupId>com.nashtech.common</groupId>
<artifactId>common</artifactId>
<version>2.3</version>
<version>2.2</version>
<name>common</name>

<repositories>
Expand Down
4 changes: 2 additions & 2 deletions inventory-service/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<name>inventory-service</name>
<properties>
<java.version>19</java.version>
<common.version>2.3</common.version>
<common.version>2.2</common.version>
<validation-api.version>2.0.1.Final</validation-api.version>
<spring-cloud-gcp.version>4.1.1</spring-cloud-gcp.version>
<spring-cloud.version>2022.0.1</spring-cloud.version>
Expand Down Expand Up @@ -134,7 +134,7 @@
<dependency>
<groupId>org.axonframework</groupId>
<artifactId>axon-test</artifactId>
<version>4.6.0</version>
<version>4.9.1</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
3 changes: 3 additions & 0 deletions order-service/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,9 @@
**/*common*/**,
**/*constant*/**,
**/*model*/**,
**/*exception*/**,
**/*request*/**,
**/*response*/**,
**/*Application.*
</sonar.coverage.exclusions>
</properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

Expand All @@ -11,6 +12,7 @@
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Entity(name = "failed_orders")
public class FailedOrderEntity {
@Id
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.nashtech.order.aggregate;

import com.nashtech.common.utils.OrderStatus;
import com.nashtech.order.commands.CreateOrderCommand;
import com.nashtech.order.restapi.OrdersCommandController;
import com.nashtech.order.restapi.request.OrderCreateRequest;
import com.nashtech.order.restapi.response.OrderSummary;
import org.axonframework.commandhandling.gateway.CommandGateway;
import org.axonframework.queryhandling.QueryGateway;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import java.util.concurrent.CompletableFuture;

import static org.hamcrest.Matchers.any;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

class OrdersCommandControllerTest {

@Mock
private CommandGateway commandGateway;

@Mock
private QueryGateway queryGateway;

@InjectMocks
private OrdersCommandController controller;

public OrdersCommandControllerTest() {
MockitoAnnotations.openMocks(this);
}

@Test
void createOrder_shouldCreateOrderAndReturnOrderSummary() {

OrderCreateRequest orderRequest = new OrderCreateRequest("123", 2, "user123");

when(commandGateway.send(any(CreateOrderCommand.class))).thenReturn(CompletableFuture.completedFuture(null));

OrderSummary result = controller.createOrder(orderRequest);

assertEquals(OrderStatus.ORDER_PLACED.toString(), result.getOrderStatus());
assertEquals("Thank you for your order! We’ll let you know as soon as it ships. " +
"You can track your order here,review us here, or shop again here.", result.getMessage());

ArgumentCaptor<CreateOrderCommand> commandCaptor = ArgumentCaptor.forClass(CreateOrderCommand.class);
verify(commandGateway).send(commandCaptor.capture());
CreateOrderCommand sentCommand = commandCaptor.getValue();
assertEquals(orderRequest.getProductId(), sentCommand.getProductId());
assertEquals(orderRequest.getUserId(), sentCommand.getUserId());
assertEquals(orderRequest.getQuantity(), sentCommand.getQuantity());
}

}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

package com.nashtech.order.handler;

import com.nashtech.common.utils.OrderStatus;
Expand All @@ -8,7 +9,6 @@
import com.nashtech.order.repository.OrderRepository;
import com.nashtech.order.repository.entity.FailedOrderEntity;
import com.nashtech.order.repository.entity.OrderEntity;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mockito;
Expand All @@ -22,19 +22,16 @@
import java.util.Date;
import java.util.Optional;

import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.*;

@ContextConfiguration(classes = {OrderEventsHandler.class})
@ExtendWith(SpringExtension.class)
class OrderEventsHandlerTest {
@MockBean
private FailedOrderRepository failedOrderRepository;

@Autowired
private OrderEventsHandler orderEventsHandler;

@MockBean
private OrderRepository orderRepository;

Expand All @@ -52,7 +49,6 @@ void testOrderApprovedEventHandler() {
orderEntity.setShipmentId("42");
orderEntity.setTimestamp(Date.from(LocalDate.of(1970, 1, 1).atStartOfDay().atZone(ZoneOffset.UTC).toInstant()));
orderEntity.setUserId("42");

OrderEntity orderEntity2 = new OrderEntity();
orderEntity2.setOrderId("42");
orderEntity2.setOrderStatus("Order Status");
Expand All @@ -70,10 +66,8 @@ void testOrderApprovedEventHandler() {
.paymentId("42")
.shipmentId("42")
.build();

// Act
orderEventsHandler.on(orderApprovedEvent);

// Assert
verify(orderRepository).findByOrderId(Mockito.<String>any());
verify(failedOrderRepository).delete(Mockito.<FailedOrderEntity>any());
Expand All @@ -97,10 +91,8 @@ void testOrderCancelledEventHandler() {
.shipmentId("42")
.userId("42")
.build();

// Act
orderEventsHandler.on(event);

// Assert that nothing has changed
verify(failedOrderRepository).findById(Mockito.<String>any());
}
Expand All @@ -110,7 +102,6 @@ void testOrderCancelledEventHandler() {
*/
@Test
void testOrderCreatedEventHandler() {
// Arrange
OrderEntity orderEntity = new OrderEntity();
orderEntity.setOrderId("42");
orderEntity.setOrderStatus("Order Status");
Expand All @@ -120,7 +111,6 @@ void testOrderCreatedEventHandler() {
orderEntity.setTimestamp(Date.from(LocalDate.of(1970, 1, 1).atStartOfDay().atZone(ZoneOffset.UTC).toInstant()));
orderEntity.setUserId("42");
when(orderRepository.save(Mockito.<OrderEntity>any())).thenReturn(orderEntity);

FailedOrderEntity failedOrderEntity = new FailedOrderEntity();
failedOrderEntity.setOrderId("42");
failedOrderEntity.setOrderStatus("Order Status");
Expand All @@ -139,11 +129,7 @@ void testOrderCreatedEventHandler() {
.quantity(1)
.userId("42")
.build();

// Act
orderEventsHandler.on(event);

// Assert
verify(failedOrderRepository).save(Mockito.<FailedOrderEntity>any());
verify(orderRepository).save(Mockito.<OrderEntity>any());
}
Expand All @@ -155,9 +141,35 @@ void testOrderCreatedEventHandler() {
void testExceptionHandle() {
Exception exception = new Exception("foo");
try {
Assertions.assertEquals("foo", exception.getMessage());
assertEquals("foo", exception.getMessage());
orderEventsHandler.handle(exception);
}catch (Exception e){}
} catch (Exception e) {
}
}

@Test
void testOrderCancelledEventHandlers() {
FailedOrderEntity existingFailedOrder = new FailedOrderEntity(); // create or mock as needed
Optional<FailedOrderEntity> orderOptional = Optional.of(existingFailedOrder);
when(failedOrderRepository.findById(Mockito.<String>any())).thenReturn(orderOptional);
OrderCancelledEvent event = OrderCancelledEvent.builder()
.orderId("42")
.orderStatus(OrderStatus.ORDER_NOT_APPROVED)
.paymentId("42")
.productId("42")
.reasonToFailed("Just cause")
.shipmentId("42")
.userId("42")
.build();
orderEventsHandler.on(event);
verify(failedOrderRepository).findById(Mockito.<String>any());
verify(failedOrderRepository).save(Mockito.<FailedOrderEntity>any());
FailedOrderEntity updatedFailedOrder = orderOptional.get();
assertEquals(event.getProductId(), updatedFailedOrder.getProductId());
assertEquals(event.getPaymentId(), updatedFailedOrder.getPaymentId());
assertEquals(event.getShipmentId(), updatedFailedOrder.getShipmentId());
assertEquals(event.getUserId(), updatedFailedOrder.getUserId());
assertEquals(event.getOrderStatus().toString(), updatedFailedOrder.getOrderStatus());
assertEquals(event.getReasonToFailed(), updatedFailedOrder.getReasonToFailed());
}
}

This file was deleted.

4 changes: 2 additions & 2 deletions payment-service/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@

<properties>
<java.version>19</java.version>
<common.version>2.3</common.version>
<common.version>2.2</common.version>
<maven.compiler.source>19</maven.compiler.source>
<maven.sonar.version>3.9.1.2184</maven.sonar.version>
<maven.jacoco.version>0.8.8</maven.jacoco.version>
Expand All @@ -67,7 +67,7 @@
<dependency>
<groupId>org.axonframework</groupId>
<artifactId>axon-test</artifactId>
<version>4.8.0</version>
<version>4.9.1</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
4 changes: 1 addition & 3 deletions shipment-service/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
<dependency>
<groupId>org.axonframework</groupId>
<artifactId>axon-test</artifactId>
<version>4.8.0</version>
<version>4.9.1</version>
<scope>test</scope>
</dependency>

Expand Down Expand Up @@ -195,8 +195,6 @@
</activation>
<properties>
<!-- Sonar Config -->
<sonar.projectKey>NashTech-Labs:car-demo:shipment-service</sonar.projectKey>
<sonar.branch.name>main</sonar.branch.name>
<sonar.organization>nashtech</sonar.organization>
<sonar.host.url>https://sonarcloud.io</sonar.host.url>
<sonarConfig>b47aeba29df2889126c736ee7012a5a490edc34a</sonarConfig> <!-- NOSONAR -->
Expand Down

0 comments on commit fc0bb96

Please sign in to comment.