diff --git a/common/pom.xml b/common/pom.xml index fc7ab40d..7506a89a 100644 --- a/common/pom.xml +++ b/common/pom.xml @@ -11,7 +11,7 @@ com.nashtech.common common - 2.3 + 2.2 common diff --git a/inventory-service/pom.xml b/inventory-service/pom.xml index 3f30fe98..462b4509 100644 --- a/inventory-service/pom.xml +++ b/inventory-service/pom.xml @@ -15,7 +15,7 @@ inventory-service 19 - 2.3 + 2.2 2.0.1.Final 4.1.1 2022.0.1 @@ -134,7 +134,7 @@ org.axonframework axon-test - 4.6.0 + 4.9.1 test diff --git a/order-service/pom.xml b/order-service/pom.xml index af6390d7..346165b8 100644 --- a/order-service/pom.xml +++ b/order-service/pom.xml @@ -200,6 +200,9 @@ **/*common*/**, **/*constant*/**, **/*model*/**, + **/*exception*/**, + **/*request*/**, + **/*response*/**, **/*Application.* diff --git a/order-service/src/main/java/com/nashtech/order/repository/entity/FailedOrderEntity.java b/order-service/src/main/java/com/nashtech/order/repository/entity/FailedOrderEntity.java index a6e91939..466a14fb 100644 --- a/order-service/src/main/java/com/nashtech/order/repository/entity/FailedOrderEntity.java +++ b/order-service/src/main/java/com/nashtech/order/repository/entity/FailedOrderEntity.java @@ -3,6 +3,7 @@ import jakarta.persistence.Entity; import jakarta.persistence.Id; import lombok.AllArgsConstructor; +import lombok.Getter; import lombok.NoArgsConstructor; import lombok.Setter; @@ -11,6 +12,7 @@ @Setter @NoArgsConstructor @AllArgsConstructor +@Getter @Entity(name = "failed_orders") public class FailedOrderEntity { @Id diff --git a/order-service/src/test/java/com/nashtech/order/aggregate/OrdersCommandControllerTest.java b/order-service/src/test/java/com/nashtech/order/aggregate/OrdersCommandControllerTest.java new file mode 100644 index 00000000..35440ae6 --- /dev/null +++ b/order-service/src/test/java/com/nashtech/order/aggregate/OrdersCommandControllerTest.java @@ -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 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()); + } + +} \ No newline at end of file diff --git a/order-service/src/test/java/com/nashtech/order/handler/OrderEventsHandlerTest.java b/order-service/src/test/java/com/nashtech/order/handler/OrderEventsHandlerTest.java index 6a223df2..8f3c4912 100644 --- a/order-service/src/test/java/com/nashtech/order/handler/OrderEventsHandlerTest.java +++ b/order-service/src/test/java/com/nashtech/order/handler/OrderEventsHandlerTest.java @@ -1,3 +1,4 @@ + package com.nashtech.order.handler; import com.nashtech.common.utils.OrderStatus; @@ -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; @@ -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; @@ -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"); @@ -70,10 +66,8 @@ void testOrderApprovedEventHandler() { .paymentId("42") .shipmentId("42") .build(); - // Act orderEventsHandler.on(orderApprovedEvent); - // Assert verify(orderRepository).findByOrderId(Mockito.any()); verify(failedOrderRepository).delete(Mockito.any()); @@ -97,10 +91,8 @@ void testOrderCancelledEventHandler() { .shipmentId("42") .userId("42") .build(); - // Act orderEventsHandler.on(event); - // Assert that nothing has changed verify(failedOrderRepository).findById(Mockito.any()); } @@ -110,7 +102,6 @@ void testOrderCancelledEventHandler() { */ @Test void testOrderCreatedEventHandler() { - // Arrange OrderEntity orderEntity = new OrderEntity(); orderEntity.setOrderId("42"); orderEntity.setOrderStatus("Order Status"); @@ -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.any())).thenReturn(orderEntity); - FailedOrderEntity failedOrderEntity = new FailedOrderEntity(); failedOrderEntity.setOrderId("42"); failedOrderEntity.setOrderStatus("Order Status"); @@ -139,11 +129,7 @@ void testOrderCreatedEventHandler() { .quantity(1) .userId("42") .build(); - - // Act orderEventsHandler.on(event); - - // Assert verify(failedOrderRepository).save(Mockito.any()); verify(orderRepository).save(Mockito.any()); } @@ -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 orderOptional = Optional.of(existingFailedOrder); + when(failedOrderRepository.findById(Mockito.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.any()); + verify(failedOrderRepository).save(Mockito.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()); } } diff --git a/order-service/src/test/java/com/nashtech/order/restapi/RestTest.java b/order-service/src/test/java/com/nashtech/order/restapi/RestTest.java deleted file mode 100644 index b5a9d4a8..00000000 --- a/order-service/src/test/java/com/nashtech/order/restapi/RestTest.java +++ /dev/null @@ -1,81 +0,0 @@ -package com.nashtech.order.restapi; - -import com.fasterxml.jackson.databind.ObjectMapper; -import com.nashtech.order.restapi.request.OrderCreateRequest; -import com.nashtech.order.restapi.response.OrderSummary; -import org.junit.Assert; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.http.MediaType; -import org.springframework.mock.web.MockHttpServletResponse; -import org.springframework.test.context.ActiveProfiles; -import org.springframework.test.context.junit4.SpringRunner; -import org.springframework.test.web.servlet.MockMvc; -import org.springframework.test.web.servlet.MvcResult; -import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; - -import java.util.Collection; -import java.util.Objects; - -import static org.hamcrest.MatcherAssert.assertThat; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; -import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; - -@RunWith(SpringRunner.class) -@SpringBootTest -@AutoConfigureMockMvc -@ActiveProfiles("test") -public class RestTest { - @Autowired - private MockMvc mvc; - - @Test - public void testCreateOrder() throws Exception { - ObjectMapper objectMapper= new ObjectMapper(); - OrderCreateRequest orderRequest = OrderCreateRequest.builder().build(); - MvcResult result = mvc.perform(post("/orders/create") - .contentType(MediaType.APPLICATION_JSON) - .content(objectMapper.writeValueAsString(orderRequest)) - .characterEncoding("utf-8")) - .andExpect(status().isOk()) - .andDo(print()) - .andReturn(); - - MockHttpServletResponse mockResponse = result.getResponse(); - Assert.assertEquals(200, mockResponse.getStatus()); - assertThat(mockResponse.getContentType(), - Objects.requireNonNull(mockResponse.getContentType()).contains("application/json")); - - Assert.assertFalse(mockResponse.getContentAsString().isEmpty()); - OrderSummary orderSummary = objectMapper.readValue(mockResponse.getContentAsString(), OrderSummary.class); - - assertEquals("ORDER_PLACED", orderSummary.getOrderStatus()); - - Collection responseHeaders = mockResponse.getHeaderNames(); - Assert.assertNotNull(responseHeaders); - Assert.assertFalse(responseHeaders.isEmpty()); - } - - @Test - public void testGetOrdersByUser() throws Exception { - - MvcResult result = mvc - .perform(MockMvcRequestBuilders.get("/orders/{userId}", "1652")) - .andExpect(status().isOk()) - .andDo(print()) - .andReturn(); - - MockHttpServletResponse mockResponse = result.getResponse(); - assertThat(mockResponse.getContentType(), - Objects.requireNonNull(mockResponse.getContentType()).contains("application/json")); - - Collection responseHeaders = mockResponse.getHeaderNames(); - Assert.assertNotNull(responseHeaders); - Assert.assertFalse(responseHeaders.isEmpty()); - } -} diff --git a/payment-service/pom.xml b/payment-service/pom.xml index 077348e3..a1ba2f68 100644 --- a/payment-service/pom.xml +++ b/payment-service/pom.xml @@ -41,7 +41,7 @@ 19 - 2.3 + 2.2 19 3.9.1.2184 0.8.8 @@ -67,7 +67,7 @@ org.axonframework axon-test - 4.8.0 + 4.9.1 test diff --git a/shipment-service/pom.xml b/shipment-service/pom.xml index 2ec52675..318538c0 100644 --- a/shipment-service/pom.xml +++ b/shipment-service/pom.xml @@ -64,7 +64,7 @@ org.axonframework axon-test - 4.8.0 + 4.9.1 test @@ -195,8 +195,6 @@ - NashTech-Labs:car-demo:shipment-service - main nashtech https://sonarcloud.io b47aeba29df2889126c736ee7012a5a490edc34a