Skip to content

Commit

Permalink
added unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo-catalano committed Apr 4, 2024
1 parent dff37f3 commit 3bf667e
Show file tree
Hide file tree
Showing 4 changed files with 184 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ private CommonRPTFieldsDTO extractRPTContentDTOsFromNodoInviaCarrelloRPT(Envelop
NodoInviaCarrelloRPT soapBody = this.jaxbElementUtil.getSoapBody(envelope, NodoInviaCarrelloRPT.class);

// initializing common fields
boolean isMultibeneficiary = soapBody.isMultiBeneficiario();
boolean isMultibeneficiary = soapBody.isMultiBeneficiario() !=null && soapBody.isMultiBeneficiario();
String creditorInstitutionId = null;
String payerType = null;
String payerFiscalCode = null;
Expand Down Expand Up @@ -132,6 +132,7 @@ private CommonRPTFieldsDTO extractRPTContentDTOsFromNodoInviaCarrelloRPT(Envelop
.cartId(soapHeader.getIdentificativoCarrello())
.creditorInstitutionId(creditorInstitutionId)
.creditorInstitutionBrokerId(soapHeader.getIdentificativoIntermediarioPA())
.stationId(soapHeader.getIdentificativoStazioneIntermediarioPA())
.payerType(payerType)
.payerFiscalCode(payerFiscalCode)
.payerFullName(fullName)
Expand Down
150 changes: 150 additions & 0 deletions src/test/java/it/gov/pagopa/wispconverter/CarrelloTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
package it.gov.pagopa.wispconverter;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;

import com.fasterxml.jackson.databind.ObjectMapper;
import feign.Request;
import feign.Response;
import it.gov.pagopa.wispconverter.client.cache.model.ConfigDataV1;
import it.gov.pagopa.wispconverter.client.cache.model.Redirect;
import it.gov.pagopa.wispconverter.client.cache.model.Station;
import it.gov.pagopa.wispconverter.client.checkout.CheckoutClient;
import it.gov.pagopa.wispconverter.client.decoupler.DecouplerCachingClient;
import it.gov.pagopa.wispconverter.client.gpd.GPDClient;
import it.gov.pagopa.wispconverter.client.iuvgenerator.IUVGeneratorClient;
import it.gov.pagopa.wispconverter.client.iuvgenerator.model.IUVGeneratorResponse;
import it.gov.pagopa.wispconverter.repository.RPTRequestRepository;
import it.gov.pagopa.wispconverter.repository.model.RPTRequestEntity;
import it.gov.pagopa.wispconverter.service.ConfigCacheService;
import it.gov.pagopa.wispconverter.utils.TestUtils;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.zip.GZIPOutputStream;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;

@SpringBootTest(classes = Application.class)
@AutoConfigureMockMvc
class CarrelloTest {

@Autowired
ObjectMapper objectMapper;
@Autowired
private MockMvc mvc;
@Autowired private ConfigCacheService configCacheService;

// @MockBean private CosmosStationRepository cosmosStationRepository;
// @MockBean private CosmosEventsRepository cosmosEventsRepository;
// @MockBean private DatabaseStationsRepository databaseStationsRepository;
// @MockBean private EntityManagerFactory entityManagerFactory;
// @MockBean private EntityManager entityManager;
// @MockBean private DataSource dataSource;
// @MockBean private CosmosClient cosmosClient;
@MockBean private RPTRequestRepository rptRequestRepository;
@MockBean private IUVGeneratorClient iuveneratorClient;
@MockBean private GPDClient gpdClient;
@MockBean private CheckoutClient checkoutClient;
@MockBean private DecouplerCachingClient decouplerCachingClient;
@Qualifier("redisSimpleTemplate")
@MockBean private RedisTemplate<String, Object> redisSimpleTemplate;

private String getCarrelloPayload(int numofrpt,String station,String amount){
String rpt = TestUtils.loadFileContent("/requests/rpt.xml");
String rptreplace = rpt.replace("{amount}", amount);
StringBuilder listaRpt = new StringBuilder("");
for(int i=0;i<numofrpt;i++){
listaRpt.append(
("<elementoListaRPT>"+
"<identificativoDominio></identificativoDominio>"+
"<identificativoUnivocoVersamento></identificativoUnivocoVersamento>"+
"<codiceContestoPagamento></codiceContestoPagamento>"+
"<tipoFirma></tipoFirma>"+
"<rpt>{rpt}</rpt>" +
"</elementoListaRPT>").replace("{rpt}",Base64.getEncoder().encodeToString(rptreplace.getBytes(StandardCharsets.UTF_8)))
);
}

String carrello = TestUtils.loadFileContent("/requests/nodoInviaCarrelloRPT.xml");
return carrello
.replace("{station}",station)
.replace("{elementiRpt}", listaRpt.toString());
}

private byte[] zip(byte[] uncompressed) throws IOException {
ByteArrayOutputStream bais = new ByteArrayOutputStream();
GZIPOutputStream gzipOutputStream = new GZIPOutputStream(bais);
gzipOutputStream.write(uncompressed);
gzipOutputStream.close();
bais.close();
return bais.toByteArray();
}

@Test
void success() throws Exception {
ConfigDataV1 configDataV1 = new ConfigDataV1();
configDataV1.setStations(new HashMap<>());
Station station = new Station();
station.setStationCode("mystation");
station.setRedirect(new Redirect());
station.getRedirect().setIp("127.0.0.1");
station.getRedirect().setPath("/redirect");
station.getRedirect().setPort(8888l);
station.getRedirect().setProtocol(Redirect.ProtocolEnum.HTTPS);
station.getRedirect().setQueryString("param=1");
configDataV1.getStations().put(station.getStationCode(), station);
org.springframework.test.util.ReflectionTestUtils.setField(configCacheService, "configData",configDataV1);

HashMap<String, Collection<String>> headers = new HashMap<>();
headers.put("location", Arrays.asList("locationheader"));
Response executeCreationResponse =
Response.builder()
.status(302)
.headers(headers)
.request(Request.create(Request.HttpMethod.GET, "", new HashMap<>(),"".getBytes(StandardCharsets.UTF_8),null))
.build();

when(checkoutClient.executeCreation(any())).thenReturn(executeCreationResponse);

when(iuveneratorClient.generate(any(),any())).thenReturn(
IUVGeneratorResponse.builder().iuv("00000000").build()
);
when(rptRequestRepository.findById(any())).thenReturn(
Optional.of(
RPTRequestEntity.builder().primitive("nodoInviaCarrelloRPT")
.payload(
new String(Base64.getEncoder().encode(zip(
getCarrelloPayload(1,station.getStationCode(),
"100.00"
).getBytes(StandardCharsets.UTF_8))),StandardCharsets.UTF_8)
).build()
)
);
when(redisSimpleTemplate.opsForValue()).thenReturn(mock(ValueOperations.class));



mvc.perform(MockMvcRequestBuilders.get("/redirect?sessionId=aaaaaaaaaaaa").accept(MediaType.APPLICATION_JSON))
.andExpect(MockMvcResultMatchers.status().is3xxRedirection())
.andDo(
(result) -> {
assertNotNull(result);
assertNotNull(result.getResponse());
});

verify(checkoutClient,times(1)).executeCreation(any());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,15 @@
import it.gov.pagopa.wispconverter.client.gpd.GPDClient;
import it.gov.pagopa.wispconverter.client.iuvgenerator.IUVGeneratorClient;
import it.gov.pagopa.wispconverter.client.iuvgenerator.model.IUVGeneratorResponse;
import it.gov.pagopa.wispconverter.repository.CacheRepository;
import it.gov.pagopa.wispconverter.repository.RPTRequestRepository;
import it.gov.pagopa.wispconverter.repository.model.RPTRequestEntity;
import it.gov.pagopa.wispconverter.service.ConfigCacheService;
import it.gov.pagopa.wispconverter.utils.TestUtils;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.zip.GZIPOutputStream;
import org.apache.commons.io.IOUtils;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
Expand All @@ -41,7 +39,7 @@

@SpringBootTest(classes = Application.class)
@AutoConfigureMockMvc
class ApiTest {
class RptTest {

@Autowired
ObjectMapper objectMapper;
Expand All @@ -64,27 +62,10 @@ class ApiTest {
@Qualifier("redisSimpleTemplate")
@MockBean private RedisTemplate<String, Object> redisSimpleTemplate;

public String loadFileContent(String fileName) {
String content = null;
try {
// Get the InputStream of the resource
InputStream inputStream = this.getClass().getResourceAsStream(fileName);
if (inputStream != null) {
// Use Apache Commons IO to read the content from the InputStream
content = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
} else {
System.err.println("File not found: " + fileName);
}
} catch (IOException e) {
e.printStackTrace();
}
return content;
}

private String getRptPayload(String station,String amount){
String rpt = loadFileContent("/requests/rpt.xml");
String rpt = TestUtils.loadFileContent("/requests/rpt.xml");
String rptreplace = rpt.replace("{amount}", amount);
String nodoInviaRPT = loadFileContent("/requests/nodoInviaRPT.xml");
String nodoInviaRPT = TestUtils.loadFileContent("/requests/nodoInviaRPT.xml");
return nodoInviaRPT
.replace("{station}",station)
.replace("{rpt}", Base64.getEncoder().encodeToString(rptreplace.getBytes(StandardCharsets.UTF_8)));
Expand Down
28 changes: 28 additions & 0 deletions src/test/java/it/gov/pagopa/wispconverter/utils/TestUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package it.gov.pagopa.wispconverter.utils;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import org.apache.commons.io.IOUtils;

public class TestUtils {

public static String loadFileContent(String fileName) {
String content = null;
try {
// Get the InputStream of the resource
InputStream inputStream = TestUtils.class.getResourceAsStream(fileName);
if (inputStream != null) {
// Use Apache Commons IO to read the content from the InputStream
content = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
} else {
System.err.println("File not found: " + fileName);
}
} catch (IOException e) {
e.printStackTrace();
}
return content;
}


}

0 comments on commit 3bf667e

Please sign in to comment.