Skip to content

Commit

Permalink
[SELC-4728] Feat: Refactor test classes in core module (#243)
Browse files Browse the repository at this point in the history
  • Loading branch information
flaminiaScarciofolo authored May 23, 2024
1 parent d7f10ff commit bec02ed
Show file tree
Hide file tree
Showing 27 changed files with 1,540 additions and 919 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.ZoneId;
import java.util.*;
Expand Down Expand Up @@ -75,14 +76,14 @@ public UserDetailsWrapper getUserOnboardedProductsDetailsV2(String userId, Strin
onboardedProductResponses.addAll(onboardedProducts);
});
if (!onboardedProductResponses.isEmpty()) {
onboardedProductResponses.forEach(onboardedProductResponse -> {
roles.add(onboardedProductResponse.getProductRole());
});
onboardedProductResponses.forEach(onboardedProductResponse -> roles.add(onboardedProductResponse.getProductRole()));
productDetails = new ProductDetails();
productDetails.setRole(PartyRole.valueOf(onboardedProductResponses.get(0).getRole()));
productDetails.setProductId(productId);
productDetails.setRoles(roles);
productDetails.setCreatedAt(onboardedProductResponses.get(0).getCreatedAt().atZone(ZoneId.systemDefault()).toOffsetDateTime());
if(onboardedProductResponses.get(0).getCreatedAt() != null) {
productDetails.setCreatedAt(onboardedProductResponses.get(0).getCreatedAt().atZone(ZoneId.systemDefault()).toOffsetDateTime());
}
}
result = UserDetailsWrapper.builder()
.userId(userId)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package it.pagopa.selfcare.external_api.core;

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 BaseServiceTestUtils {

protected ObjectMapper objectMapper;

public BaseServiceTestUtils() {
}

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());
}
}
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
package it.pagopa.selfcare.external_api.core;

import it.pagopa.selfcare.commons.utils.TestUtils;
import it.pagopa.selfcare.external_api.api.FileStorageConnector;
import it.pagopa.selfcare.external_api.api.MsCoreConnector;
import it.pagopa.selfcare.external_api.api.OnboardingMsConnector;
import it.pagopa.selfcare.external_api.exceptions.ResourceNotFoundException;
import it.pagopa.selfcare.external_api.model.documents.ResourceResponse;
import it.pagopa.selfcare.external_api.model.onboarding.InstitutionOnboarding;
import it.pagopa.selfcare.external_api.model.token.Token;
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.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.core.io.ClassPathResource;

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

import static it.pagopa.selfcare.commons.utils.TestUtils.mockInstance;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;


@ExtendWith(MockitoExtension.class)
class ContractServiceImplTest {

@ExtendWith({MockitoExtension.class})
class ContractServiceImplTest extends BaseServiceTestUtils {
@InjectMocks
private ContractServiceImpl contractService;

Expand All @@ -38,71 +38,60 @@ class ContractServiceImplTest {
private OnboardingMsConnector onboardingMsConnectorMock;


@Test
void getContractV2(){
//given
final String institutionId = "institutionId";
final String productId = "productId";
final InstitutionOnboarding institutionOnboarding = new InstitutionOnboarding();
institutionOnboarding.setTokenId("tokenId");
final Token token = new Token();
token.setContractSigned("contractSigned");

ResourceResponse response = mockInstance(new ResourceResponse());
when(msCoreConnectorMock.getInstitutionOnboardings(institutionId, productId))
.thenReturn(institutionOnboarding);

when(onboardingMsConnectorMock.getToken(institutionOnboarding.getTokenId()))
.thenReturn(List.of(token));

when(storageConnectorMock.getFile(any())).thenReturn(response);

//when
ResourceResponse result = contractService.getContractV2(institutionId, productId);
//then
assertEquals(response, result);
verify(msCoreConnectorMock, times(1)).getInstitutionOnboardings(institutionId, productId);
verify(storageConnectorMock, times(1)).getFile(token.getContractSigned());
verifyNoMoreInteractions(msCoreConnectorMock, storageConnectorMock);
@BeforeEach
public void setUp() {
super.setUp();
}


@Test
void getContractV2_tokenSignedIsEmpty(){
//given
final String institutionId = "institutionId";
final String productId = "productId";
final InstitutionOnboarding institutionOnboarding = new InstitutionOnboarding();
void getContractV2() throws Exception {
InstitutionOnboarding institutionOnboarding = new InstitutionOnboarding();
institutionOnboarding.setTokenId("tokenId");
final Token token = new Token();

when(msCoreConnectorMock.getInstitutionOnboardings(institutionId, productId))
.thenReturn(institutionOnboarding);

when(onboardingMsConnectorMock.getToken(institutionOnboarding.getTokenId()))
.thenReturn(List.of(token));

//when
assertThrows(ResourceNotFoundException.class, () -> contractService.getContractV2(institutionId, productId));
ClassPathResource inputResource = new ClassPathResource("expectations/Token.json");
byte[] tokenStream = Files.readAllBytes(inputResource.getFile().toPath());
Token token = objectMapper.readValue(tokenStream, Token.class);
ResourceResponse response = TestUtils.mockInstance(new ResourceResponse());
Mockito.when(msCoreConnectorMock.getInstitutionOnboardings("institutionId", "productId")).thenReturn(institutionOnboarding);
Mockito.when(onboardingMsConnectorMock.getToken(institutionOnboarding.getTokenId())).thenReturn(List.of(token));
Mockito.when(storageConnectorMock.getFile(token.getContractSigned())).thenReturn(response);
ResourceResponse result = contractService.getContractV2("institutionId", "productId");
Assertions.assertEquals(response, result);
}


@Test
void getContractV2_tokenNotFound(){
//given
final String institutionId = "institutionId";
final String productId = "productId";
final InstitutionOnboarding institutionOnboarding = new InstitutionOnboarding();
institutionOnboarding.setTokenId("tokenId");

when(msCoreConnectorMock.getInstitutionOnboardings(institutionId, productId))
.thenReturn(institutionOnboarding);

when(onboardingMsConnectorMock.getToken(institutionOnboarding.getTokenId()))
.thenReturn(List.of());
void getContractV2WhereTokenIsEmpty() {
InstitutionOnboarding institutionOnboarding = new InstitutionOnboarding();
institutionOnboarding.setTokenId(null);
Mockito.when(msCoreConnectorMock.getInstitutionOnboardings("institutionId", "productId")).thenReturn(institutionOnboarding);
Mockito.when(onboardingMsConnectorMock.getToken(institutionOnboarding.getTokenId())).thenReturn(Collections.emptyList());
Assertions.assertThrows(ResourceNotFoundException.class,
() -> contractService.getContractV2("institutionId", "productId"),
"Token for institutionId and productId not found!");
}

//when
assertThrows(ResourceNotFoundException.class, () -> contractService.getContractV2(institutionId, productId));
@Test
void getContractV2WhereTokenIsNull() {
InstitutionOnboarding institutionOnboarding = new InstitutionOnboarding();
institutionOnboarding.setTokenId(null);
Mockito.when(msCoreConnectorMock.getInstitutionOnboardings("institutionId", "productId")).thenReturn(institutionOnboarding);
Mockito.when(onboardingMsConnectorMock.getToken(institutionOnboarding.getTokenId())).thenReturn(null);
Assertions.assertThrows(ResourceNotFoundException.class,
() -> contractService.getContractV2("institutionId", "productId"),
"Token for institutionId and productId not found!");
}

}
@Test
void getContractV2WhereContractSignedIsNull() throws IOException {
InstitutionOnboarding institutionOnboarding = new InstitutionOnboarding();
institutionOnboarding.setTokenId(null);
ClassPathResource inputResource = new ClassPathResource("expectations/Token.json");
byte[] tokenStream = Files.readAllBytes(inputResource.getFile().toPath());
Token token = objectMapper.readValue(tokenStream, Token.class);
token.setContractSigned(null);
Mockito.when(msCoreConnectorMock.getInstitutionOnboardings("institutionId", "productId")).thenReturn(institutionOnboarding);
Mockito.when(onboardingMsConnectorMock.getToken(institutionOnboarding.getTokenId())).thenReturn(List.of(token));
Assertions.assertThrows(ResourceNotFoundException.class,
() -> contractService.getContractV2("institutionId", "productId"),
"Token for institutionId and productId found but contract signed reference is empty!");
}
}
Loading

0 comments on commit bec02ed

Please sign in to comment.