-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SELC-4733] Feat: Refactor test for connector rest module (#455)
- Loading branch information
1 parent
37a413e
commit ba20cee
Showing
48 changed files
with
1,922 additions
and
758 deletions.
There are no files selected for viewing
21 changes: 21 additions & 0 deletions
21
...tor/rest/src/test/java/it/pagopa/selfcare/dashboard/connector/rest/BaseConnectorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
391 changes: 208 additions & 183 deletions
391
...rest/src/test/java/it/pagopa/selfcare/dashboard/connector/rest/CoreConnectorImplTest.java
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
179 changes: 131 additions & 48 deletions
179
...t/src/test/java/it/pagopa/selfcare/dashboard/connector/rest/ProductConnectorImplTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
|
Oops, something went wrong.