Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SELC-4733] Feat: Refactor test for connector rest module #455

Merged
merged 1 commit into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package it.pagopa.selfcare.dashboard.connector.rest;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;

public class BaseConnectorTest {

protected ObjectMapper objectMapper;

public BaseConnectorTest() {
}

public void setUp() {
MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
mappingJackson2HttpMessageConverter.setObjectMapper((new ObjectMapper()).disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS).registerModule(new JavaTimeModule()));
this.objectMapper = new ObjectMapper();
this.objectMapper.registerModule(new JavaTimeModule());
}
}

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package it.pagopa.selfcare.dashboard.connector.rest;

import com.fasterxml.jackson.core.type.TypeReference;
import it.pagopa.selfcare.backoffice.generated.openapi.v1.dto.Broker;
import it.pagopa.selfcare.backoffice.generated.openapi.v1.dto.BrokerPsp;
import it.pagopa.selfcare.backoffice.generated.openapi.v1.dto.Brokers;
Expand All @@ -8,24 +9,25 @@
import it.pagopa.selfcare.dashboard.connector.rest.client.MsBackOfficeChannelApiClient;
import it.pagopa.selfcare.dashboard.connector.rest.client.MsBackOfficeStationApiClient;
import it.pagopa.selfcare.dashboard.connector.rest.model.mapper.BrokerMapper;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.core.io.ClassPathResource;
import org.springframework.http.ResponseEntity;

import java.io.IOException;
import java.nio.file.Files;
import java.util.List;

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.ArgumentMatchers.anyInt;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
class PagoPABackOfficeConnectorImplTest {
class PagoPABackOfficeConnectorImplTest extends BaseConnectorTest {

@InjectMocks
private PagoPABackOfficeConnectorImpl pagoPABackOfficeConnector;
Expand All @@ -39,50 +41,99 @@ class PagoPABackOfficeConnectorImplTest {
@Mock
private BrokerMapper brokerMapper;

@BeforeEach
public void setup() {
super.setUp();
}

@Test
void getBrokersEC() {
void getBrokersEC() throws IOException {
// given
BrokerInfo brokerInfo = new BrokerInfo();
brokerInfo.setCode("code");
brokerInfo.setDescription("description");
ClassPathResource resource = new ClassPathResource("stubs/BrokerInfo.json");
byte[] resourceStream = Files.readAllBytes(resource.getFile().toPath());
BrokerInfo brokerInfo = objectMapper.readValue(resourceStream, new TypeReference<>() {});
Brokers brokersResource = new Brokers();

Broker broker = new Broker();
broker.setBrokerCode("code");
broker.setDescription("description");

brokersResource.setBrokers(List.of(new Broker().brokerCode("code").description("description")));
ResponseEntity<Brokers> response = ResponseEntity.ok(brokersResource);
// when
when(brokerMapper.fromBrokerResource(any())).thenReturn(brokerInfo);
when(backOfficeStationApiClient._getBrokers(anyInt(), any(), anyInt(), any(), any(), any(), any()))
when(brokerMapper.fromBrokerResource(broker)).thenReturn(brokerInfo);
when(backOfficeStationApiClient._getBrokers(1, null, 1000, null, null, null, "ASC"))
.thenReturn(response);


List<BrokerInfo> brokers = pagoPABackOfficeConnector.getBrokersEC(1, 1000);
// then
assertNotNull(brokers);
assertEquals(1, brokers.size());
assertEquals(brokers.get(0).getDescription(), brokerInfo.getDescription());
assertEquals(brokers.get(0).getCode(), brokerInfo.getCode());
assertEquals(brokers.get(0), brokerInfo);
verifyNoMoreInteractions(backOfficeStationApiClient);

}

@Test
void getBrokersPSP() {
void getBrokersPSP() throws IOException {
// given
BrokerInfo brokerInfo = new BrokerInfo();
brokerInfo.setCode("code");
brokerInfo.setDescription("description");
ClassPathResource resource = new ClassPathResource("stubs/BrokerInfo.json");
byte[] resourceStream = Files.readAllBytes(resource.getFile().toPath());
BrokerInfo brokerInfo = objectMapper.readValue(resourceStream, new TypeReference<>() {
});

BrokerPsp innerResource = new BrokerPsp().brokerPspCode("code").description("description");
BrokersPsp brokersResource = new BrokersPsp();
brokersResource.setBrokersPsp(List.of(innerResource));
ResponseEntity<BrokersPsp> response = ResponseEntity.ok(brokersResource);
when(brokerMapper.fromBrokerPSPResource(any())).thenReturn(brokerInfo);
when(backOfficeChannelApiClient._getBrokersPsp(anyInt(), any(), anyInt(), any(), any(), any(), any()))

BrokerPsp brokerPsp = new BrokerPsp().brokerPspCode("code").description("description");

when(brokerMapper.fromBrokerPSPResource(brokerPsp)).thenReturn(brokerInfo);
when(backOfficeChannelApiClient._getBrokersPsp(1, null, 1000, null, null, null, "ASC"))
.thenReturn(response);
// when
List<BrokerInfo> brokers = pagoPABackOfficeConnector.getBrokersPSP(1, 1000);
// then
assertNotNull(brokers);
assertEquals(1, brokers.size());
assertEquals(brokers.get(0).getDescription(), innerResource.getDescription());
assertEquals(brokers.get(0), brokerInfo);
verifyNoMoreInteractions(backOfficeChannelApiClient);

}

@Test
void testGetBrokersEC_NullResponse() {
// given
ResponseEntity<Brokers> response = ResponseEntity.ok(null);
when(backOfficeStationApiClient._getBrokers(1, null, 1000, null, null, null, "ASC"))
.thenReturn(response);

// when
List<BrokerInfo> brokers = pagoPABackOfficeConnector.getBrokersEC(1, 1000);

// then
assertNotNull(brokers);
assertTrue(brokers.isEmpty());
verifyNoMoreInteractions(backOfficeStationApiClient, brokerMapper);
}

@Test
void testGetBrokersPSP_EmptyResponse() {
// given
BrokersPsp brokersResource = new BrokersPsp();
ResponseEntity<BrokersPsp> response = ResponseEntity.ok(brokersResource);
when(backOfficeChannelApiClient._getBrokersPsp(1, null, 1000, null, null, null, "ASC"))
.thenReturn(response);

// when
List<BrokerInfo> brokers = pagoPABackOfficeConnector.getBrokersPSP(1, 1000);

// then
assertNotNull(brokers);
assertTrue(brokers.isEmpty());
verifyNoMoreInteractions(backOfficeChannelApiClient, brokerMapper);
}

}
Original file line number Diff line number Diff line change
@@ -1,84 +1,167 @@
package it.pagopa.selfcare.dashboard.connector.rest;

import com.fasterxml.jackson.core.type.TypeReference;
import it.pagopa.selfcare.dashboard.connector.model.product.ProductTree;
import it.pagopa.selfcare.dashboard.connector.model.product.mapper.ProductMapper;
import it.pagopa.selfcare.onboarding.common.PartyRole;
import it.pagopa.selfcare.product.entity.Product;
import it.pagopa.selfcare.product.entity.ProductStatus;
import it.pagopa.selfcare.product.entity.ProductRoleInfo;
import it.pagopa.selfcare.product.service.ProductService;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.Spy;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.core.io.ClassPathResource;

import java.io.IOException;
import java.nio.file.Files;
import java.util.List;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.mockito.Mockito.*;

@ContextConfiguration(classes = {ProductConnectorImpl.class})
@ExtendWith(SpringExtension.class)
class ProductConnectorImplTest {
@Autowired
@ExtendWith(MockitoExtension.class)
class ProductConnectorImplTest extends BaseConnectorTest {

@InjectMocks
private ProductConnectorImpl productConnectorImpl;
@MockBean
@Spy
private ProductMapper productMapper;
@Mock
private ProductService productServiceMock;
@BeforeEach
public void setUp() {
super.setUp();
}

@Test
void GetProductById() throws IOException {

String productId = "123";

ClassPathResource resource = new ClassPathResource("stubs/Product.json");
byte[] resourceStream = Files.readAllBytes(resource.getFile().toPath());
Product product = objectMapper.readValue(resourceStream, new TypeReference<>() {
});

when(productServiceMock.getProduct(productId)).thenReturn(product);

Product result = productConnectorImpl.getProduct(productId);
Assertions.assertEquals(product, result);
Mockito.verify(productServiceMock, times(1)).getProduct(productId);
}

@Test
void getProductByIdWithoutProductId() {

Assertions.assertThrows(IllegalArgumentException.class, () -> productConnectorImpl.getProduct(null));
verify(productServiceMock, never()).getProduct(null);
}

@Test
void GetProductByIdWithEmptyProduct() {

String productId = "123";
when(productServiceMock.getProduct(productId)).thenReturn(null);
Product result = productConnectorImpl.getProduct(productId);
assertNull(result);
Mockito.verify(productServiceMock, times(1)).getProduct(productId);
}


@MockBean
private ProductService productService;
@Test
void getProductsWithProductEmpty() {

when(productServiceMock.getProducts(false, true)).thenReturn(null);
List<Product> result = productConnectorImpl.getProducts();
assertNull(result);
verify(productServiceMock, times(1)).getProducts(false, true);
}

@Test
void testGetProductById() {
Product product = dummyProduct();
when(productService.getProduct(any())).thenReturn(product);
assertSame(product, productConnectorImpl.getProduct("42"));
verify(productService).getProduct(any());
void GetProducts() throws IOException {

ClassPathResource resource = new ClassPathResource("stubs/ProductList.json");
byte[] resourceStream = Files.readAllBytes(resource.getFile().toPath());
List<Product> product = objectMapper.readValue(resourceStream, new TypeReference<>() {
});

when(productServiceMock.getProducts(false, true)).thenReturn(product);

List<Product> result = productConnectorImpl.getProducts();
Assertions.assertEquals(product, result);
Mockito.verify(productServiceMock, times(1)).getProducts(false, true);
}

@Test
void testGetProducts() {
Product product = dummyProduct();
when(productService.getProducts(anyBoolean(),anyBoolean())).thenReturn(List.of(product));
assertSame(product, productConnectorImpl.getProducts().get(0));
verify(productService).getProducts(anyBoolean(),anyBoolean());
void getProductRoleMappingsWithoutProductId() {

Assertions.assertThrows(IllegalArgumentException.class, () -> productConnectorImpl.getProductRoleMappings(null));
verify(productServiceMock, never()).getProduct(null);
}

@Test
void testGetProductRoleMappings() {
Product product = dummyProduct();
when(productService.getProduct(any())).thenReturn(product);
assertSame(product.getRoleMappings(), productConnectorImpl.getProductRoleMappings("42"));
verify(productService).getProduct(any());
void getProductRoleMappingsWithEmptyProduct() {

String productId = "123";
when(productServiceMock.getProduct(productId)).thenReturn(null);
Map<PartyRole, ProductRoleInfo> result = productConnectorImpl.getProductRoleMappings(productId);
assertNull(result);
Mockito.verify(productServiceMock, times(1)).getProduct(productId);
}

@Test
void testGetProductRoleMappings_noProduct() {
when(productService.getProduct(any())).thenReturn(null);
assertSame(null, productConnectorImpl.getProductRoleMappings("42"));
verify(productService).getProduct(any());
void testGetProductRoleMappings() throws IOException {

String productId = "123";

ClassPathResource resource = new ClassPathResource("stubs/Product.json");
byte[] resourceStream = Files.readAllBytes(resource.getFile().toPath());
Product product = objectMapper.readValue(resourceStream, new TypeReference<>() {
});

when(productServiceMock.getProduct(productId)).thenReturn(product);
Map<PartyRole, ProductRoleInfo> result = productConnectorImpl.getProductRoleMappings(productId);
Assertions.assertEquals(product.getRoleMappings(), result);
Mockito.verify(productServiceMock, times(1)).getProduct(productId);
}

@Test
void getProductsTree() {
Product product = dummyProduct();
when(productService.getProducts(anyBoolean(),anyBoolean())).thenReturn(List.of(product));
when(productMapper.toTreeResource(List.of(product))).thenReturn(List.of(new ProductTree()));
productConnectorImpl.getProductsTree();
verify(productService).getProducts(anyBoolean(),anyBoolean());
verify(productMapper).toTreeResource(anyList());
void getProductsTreeWithEmptyProduct() {

when(productServiceMock.getProducts(false, true)).thenReturn(null);
List<ProductTree> result = productConnectorImpl.getProductsTree();
assertNull(result);
verify(productServiceMock, times(1)).getProducts(false, true);
}

private Product dummyProduct(){
Product product = new Product();
product.setContractTemplatePath("Contract Template Path");
product.setContractTemplateVersion("1.0.2");
product.setId("42");
product.setParentId("42");
product.setRoleMappings(null);
product.setStatus(ProductStatus.ACTIVE);
product.setTitle("Dr");
return product;

@Test
void getProductsTree() throws IOException {

ClassPathResource resource = new ClassPathResource("stubs/ProductList.json");
byte[] resourceStream = Files.readAllBytes(resource.getFile().toPath());
List<Product> product = objectMapper.readValue(resourceStream, new TypeReference<>() {
});

ClassPathResource productTreeResource = new ClassPathResource("stubs/ProductTree.json");
byte[] productTreeStream = Files.readAllBytes(productTreeResource.getFile().toPath());
List<ProductTree> productTrees = objectMapper.readValue(productTreeStream, new TypeReference<>() {
});

when(productServiceMock.getProducts(anyBoolean(), anyBoolean())).thenReturn(product);
when(productMapper.toTreeResource(product)).thenReturn(productTrees);

List<ProductTree> result = productConnectorImpl.getProductsTree();
assertSame(productTrees, result);
verify(productServiceMock, times(1)).getProducts(false, true);
}
}

Loading
Loading