Skip to content

Commit

Permalink
[PRDP-307] added unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
giomella committed Jan 3, 2024
1 parent 831e155 commit 35dcf37
Show file tree
Hide file tree
Showing 4 changed files with 191 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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<CartForReceipt> queryResponse = cosmosContainer
Expand Down
115 changes: 115 additions & 0 deletions src/test/java/it/gov/pagopa/receipt/pdf/helpdesk/GetCartTest.java
Original file line number Diff line number Diff line change
@@ -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<Optional<String>> 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<HttpResponseMessage.Builder>) 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<HttpResponseMessage.Builder>) 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<HttpResponseMessage.Builder>) 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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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<CartForReceipt> 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<Receipt> 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"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()));
}
}

0 comments on commit 35dcf37

Please sign in to comment.