From 35dcf37deb3931b6353a095c949e638d9a6ed47d Mon Sep 17 00:00:00 2001 From: giomella Date: Wed, 3 Jan 2024 12:59:18 +0100 Subject: [PATCH] [PRDP-307] added unit tests --- .../client/impl/ReceiptCosmosClientImpl.java | 2 +- .../receipt/pdf/helpdesk/GetCartTest.java | 115 ++++++++++++++++++ .../impl/ReceiptCosmosClientImplTest.java | 50 ++++++++ .../impl/ReceiptCosmosServiceImplTest.java | 25 ++++ 4 files changed, 191 insertions(+), 1 deletion(-) create mode 100644 src/test/java/it/gov/pagopa/receipt/pdf/helpdesk/GetCartTest.java diff --git a/src/main/java/it/gov/pagopa/receipt/pdf/helpdesk/client/impl/ReceiptCosmosClientImpl.java b/src/main/java/it/gov/pagopa/receipt/pdf/helpdesk/client/impl/ReceiptCosmosClientImpl.java index ba46ab5..609e00e 100644 --- a/src/main/java/it/gov/pagopa/receipt/pdf/helpdesk/client/impl/ReceiptCosmosClientImpl.java +++ b/src/main/java/it/gov/pagopa/receipt/pdf/helpdesk/client/impl/ReceiptCosmosClientImpl.java @@ -270,7 +270,7 @@ public CartForReceipt getCartDocument(String cartId) throws CartNotFoundExceptio CosmosContainer cosmosContainer = cosmosDatabase.getContainer(containerCartId); //Build query - String query = String.format("SELECT * FROM c WHERE c.id = '%s'", cartId); + String query = String.format("SELECT * FROM c WHERE c.id = %s", cartId); //Query the container CosmosPagedIterable queryResponse = cosmosContainer diff --git a/src/test/java/it/gov/pagopa/receipt/pdf/helpdesk/GetCartTest.java b/src/test/java/it/gov/pagopa/receipt/pdf/helpdesk/GetCartTest.java new file mode 100644 index 0000000..275ce8a --- /dev/null +++ b/src/test/java/it/gov/pagopa/receipt/pdf/helpdesk/GetCartTest.java @@ -0,0 +1,115 @@ +package it.gov.pagopa.receipt.pdf.helpdesk; + +import com.microsoft.azure.functions.ExecutionContext; +import com.microsoft.azure.functions.HttpRequestMessage; +import com.microsoft.azure.functions.HttpResponseMessage; +import com.microsoft.azure.functions.HttpStatus; +import it.gov.pagopa.receipt.pdf.helpdesk.entity.cart.CartForReceipt; +import it.gov.pagopa.receipt.pdf.helpdesk.exception.CartNotFoundException; +import it.gov.pagopa.receipt.pdf.helpdesk.model.ProblemJson; +import it.gov.pagopa.receipt.pdf.helpdesk.service.ReceiptCosmosService; +import it.gov.pagopa.receipt.pdf.helpdesk.util.HttpResponseMessageMock; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.mockito.stubbing.Answer; + +import java.util.Optional; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.doAnswer; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.when; + +class GetCartTest { + + private static final String CART_ID = "cartId"; + + @Mock + private ExecutionContext executionContextMock; + + @Mock + private ReceiptCosmosService receiptCosmosServiceMock; + + @Mock + private HttpRequestMessage> requestMock; + + private GetCart sut; + + private AutoCloseable closeable; + + @BeforeEach + public void openMocks() { + closeable = MockitoAnnotations.openMocks(this); + sut = spy(new GetCart(receiptCosmosServiceMock)); + } + + @AfterEach + public void releaseMocks() throws Exception { + closeable.close(); + } + + + @Test + void getCartReceiptSuccess() throws CartNotFoundException { + CartForReceipt cart = new CartForReceipt(); + when(receiptCosmosServiceMock.getCart(CART_ID)).thenReturn(cart); + + doAnswer((Answer) invocation -> { + HttpStatus status = (HttpStatus) invocation.getArguments()[0]; + return new HttpResponseMessageMock.HttpResponseMessageBuilderMock().status(status); + }).when(requestMock).createResponseBuilder(any(HttpStatus.class)); + + // test execution + HttpResponseMessage response = sut.run(requestMock, CART_ID, executionContextMock); + + // test assertion + assertNotNull(response); + assertEquals(HttpStatus.OK, response.getStatus()); + assertNotNull(response.getBody()); + assertEquals(cart, response.getBody()); + } + + @Test + void getCartForMissingEventId() { + doAnswer((Answer) invocation -> { + HttpStatus status = (HttpStatus) invocation.getArguments()[0]; + return new HttpResponseMessageMock.HttpResponseMessageBuilderMock().status(status); + }).when(requestMock).createResponseBuilder(any(HttpStatus.class)); + + // test execution + HttpResponseMessage response = sut.run(requestMock, "", executionContextMock); + + // test assertion + assertNotNull(response); + assertEquals(HttpStatus.BAD_REQUEST, response.getStatus()); + + ProblemJson problemJson = (ProblemJson) response.getBody(); + assertNotNull(problemJson); + assertEquals(HttpStatus.BAD_REQUEST.value(), problemJson.getStatus()); + assertEquals(HttpStatus.BAD_REQUEST.name(), problemJson.getTitle()); + assertNotNull(problemJson.getDetail()); + } + + @Test + void getCartReceiptNotFound() throws CartNotFoundException { + when(receiptCosmosServiceMock.getCart(CART_ID)).thenThrow(CartNotFoundException.class); + + doAnswer((Answer) invocation -> { + HttpStatus status = (HttpStatus) invocation.getArguments()[0]; + return new HttpResponseMessageMock.HttpResponseMessageBuilderMock().status(status); + }).when(requestMock).createResponseBuilder(any(HttpStatus.class)); + + // test execution + HttpResponseMessage response = sut.run(requestMock, CART_ID, executionContextMock); + + // test assertion + assertNotNull(response); + assertEquals(HttpStatus.NOT_FOUND, response.getStatus()); + assertNotNull(response.getBody()); + } +} \ No newline at end of file diff --git a/src/test/java/it/gov/pagopa/receipt/pdf/helpdesk/client/impl/ReceiptCosmosClientImplTest.java b/src/test/java/it/gov/pagopa/receipt/pdf/helpdesk/client/impl/ReceiptCosmosClientImplTest.java index 7b637ef..1e83fed 100644 --- a/src/test/java/it/gov/pagopa/receipt/pdf/helpdesk/client/impl/ReceiptCosmosClientImplTest.java +++ b/src/test/java/it/gov/pagopa/receipt/pdf/helpdesk/client/impl/ReceiptCosmosClientImplTest.java @@ -4,8 +4,10 @@ import com.azure.cosmos.CosmosContainer; import com.azure.cosmos.CosmosDatabase; import com.azure.cosmos.util.CosmosPagedIterable; +import it.gov.pagopa.receipt.pdf.helpdesk.entity.cart.CartForReceipt; import it.gov.pagopa.receipt.pdf.helpdesk.entity.receipt.IOMessage; import it.gov.pagopa.receipt.pdf.helpdesk.entity.receipt.Receipt; +import it.gov.pagopa.receipt.pdf.helpdesk.exception.CartNotFoundException; import it.gov.pagopa.receipt.pdf.helpdesk.exception.IoMessageNotFoundException; import it.gov.pagopa.receipt.pdf.helpdesk.exception.ReceiptNotFoundException; import org.junit.jupiter.api.BeforeEach; @@ -26,6 +28,7 @@ class ReceiptCosmosClientImplTest { private static final String RECEIPT_ID = "a valid receipt id"; + private static final Long CART_ID = 1L; private CosmosClient mockClient; @@ -243,4 +246,51 @@ void getIoMessageReceiptDocumentFail() { assertThrows(IoMessageNotFoundException.class, () -> client.getIoMessage("an invalid receipt id")); } + + @Test + void getCartDocumentSuccess() { + CosmosDatabase mockDatabase = mock(CosmosDatabase.class); + CosmosContainer mockContainer = mock(CosmosContainer.class); + + CosmosPagedIterable mockIterable = mock(CosmosPagedIterable.class); + + Iterator mockIterator = mock(Iterator.class); + CartForReceipt cart = new CartForReceipt(); + cart.setId(CART_ID); + + when(mockIterator.hasNext()).thenReturn(true); + when(mockIterator.next()).thenReturn(cart); + + when(mockIterable.iterator()).thenReturn(mockIterator); + + when(mockContainer.queryItems(anyString(), any(), eq(CartForReceipt.class))) + .thenReturn(mockIterable); + when(mockDatabase.getContainer(any())).thenReturn(mockContainer); + when(mockClient.getDatabase(any())).thenReturn(mockDatabase); + + CartForReceipt cartForReceipt = assertDoesNotThrow(() -> client.getCartDocument(CART_ID.toString())); + + assertEquals(CART_ID, cartForReceipt.getId()); + } + + @Test + void getCartDocumentFail() { + CosmosDatabase mockDatabase = mock(CosmosDatabase.class); + CosmosContainer mockContainer = mock(CosmosContainer.class); + + CosmosPagedIterable mockIterable = mock(CosmosPagedIterable.class); + + Iterator mockIterator = mock(Iterator.class); + + when(mockIterator.hasNext()).thenReturn(false); + + when(mockIterable.iterator()).thenReturn(mockIterator); + + when(mockContainer.queryItems(anyString(), any(), eq(CartForReceipt.class))) + .thenReturn(mockIterable); + when(mockDatabase.getContainer(any())).thenReturn(mockContainer); + when(mockClient.getDatabase(any())).thenReturn(mockDatabase); + + assertThrows(CartNotFoundException.class, () -> client.getCartDocument("an invalid receipt id")); + } } \ No newline at end of file diff --git a/src/test/java/it/gov/pagopa/receipt/pdf/helpdesk/service/impl/ReceiptCosmosServiceImplTest.java b/src/test/java/it/gov/pagopa/receipt/pdf/helpdesk/service/impl/ReceiptCosmosServiceImplTest.java index 2dcbcec..622278a 100644 --- a/src/test/java/it/gov/pagopa/receipt/pdf/helpdesk/service/impl/ReceiptCosmosServiceImplTest.java +++ b/src/test/java/it/gov/pagopa/receipt/pdf/helpdesk/service/impl/ReceiptCosmosServiceImplTest.java @@ -2,9 +2,11 @@ import com.azure.cosmos.models.FeedResponse; import it.gov.pagopa.receipt.pdf.helpdesk.client.ReceiptCosmosClient; +import it.gov.pagopa.receipt.pdf.helpdesk.entity.cart.CartForReceipt; import it.gov.pagopa.receipt.pdf.helpdesk.entity.receipt.IOMessage; import it.gov.pagopa.receipt.pdf.helpdesk.entity.receipt.Receipt; import it.gov.pagopa.receipt.pdf.helpdesk.entity.receipt.enumeration.ReceiptStatusType; +import it.gov.pagopa.receipt.pdf.helpdesk.exception.CartNotFoundException; import it.gov.pagopa.receipt.pdf.helpdesk.exception.IoMessageNotFoundException; import it.gov.pagopa.receipt.pdf.helpdesk.exception.ReceiptNotFoundException; import it.gov.pagopa.receipt.pdf.helpdesk.service.ReceiptCosmosService; @@ -199,4 +201,27 @@ void getReceiptMessageFailClientReturnNull() throws IoMessageNotFoundException { assertThrows(IoMessageNotFoundException.class, () -> sut.getReceiptMessage(anyString())); } + + @Test + void getCartSuccess() throws CartNotFoundException { + when(receiptCosmosClientMock.getCartDocument(anyString())).thenReturn(new CartForReceipt()); + + CartForReceipt cart = assertDoesNotThrow(() -> sut.getCart(anyString())); + + assertNotNull(cart); + } + + @Test + void getCartFailClientThrowsCartNotFoundException() throws CartNotFoundException { + when(receiptCosmosClientMock.getCartDocument(anyString())).thenThrow(CartNotFoundException.class); + + assertThrows(CartNotFoundException.class, () -> sut.getCart(anyString())); + } + + @Test + void getCartFailClientReturnNull() throws CartNotFoundException { + when(receiptCosmosClientMock.getCartDocument(anyString())).thenReturn(null); + + assertThrows(CartNotFoundException.class, () -> sut.getCart(anyString())); + } } \ No newline at end of file