Skip to content

Commit

Permalink
[PPD-206] crud GPS: added EnrollmentsController Junit test
Browse files Browse the repository at this point in the history
  • Loading branch information
aacitelli committed Jun 23, 2022
2 parents 68959e4 + 14d4561 commit 59aba33
Show file tree
Hide file tree
Showing 9 changed files with 278 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@
import it.gov.pagopa.spontaneouspayment.model.response.EnrollmentModelResponse;
import it.gov.pagopa.spontaneouspayment.model.response.OrganizationModelResponse;
import it.gov.pagopa.spontaneouspayment.service.EnrollmentsService;
import lombok.extern.slf4j.Slf4j;

@Controller
@Slf4j
public class EnrollmentsController implements IEnrollmentsController{

@Autowired
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@

import javax.validation.constraints.NotBlank;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class CreateEnrollmentModel implements Serializable{

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@

import java.io.Serializable;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class EnrollmentModel implements Serializable{

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class OrganizationEnrollmentModel implements Serializable{
/**
* generated serialVersionUID
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@
import java.io.Serializable;

import it.gov.pagopa.spontaneouspayment.model.enumeration.Status;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class OrganizationModel implements Serializable{

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package it.gov.pagopa.spontaneouspayment.service;

import java.util.Optional;
import java.util.stream.StreamSupport;

import javax.validation.constraints.NotBlank;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package it.gov.pagopa.spontaneouspayment.config;

import java.util.ArrayList;
import java.util.List;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import it.gov.pagopa.spontaneouspayment.entity.Organization;
import it.gov.pagopa.spontaneouspayment.entity.ServiceRef;
import it.gov.pagopa.spontaneouspayment.model.CreateEnrollmentModel;
import it.gov.pagopa.spontaneouspayment.model.EnrollmentModel;
import it.gov.pagopa.spontaneouspayment.model.OrganizationEnrollmentModel;
import it.gov.pagopa.spontaneouspayment.model.OrganizationModel;
import it.gov.pagopa.spontaneouspayment.model.enumeration.Status;
import lombok.experimental.UtilityClass;

@UtilityClass
public class TestUtil {

/**
* @param object to map into the Json string
* @return object as Json string
* @throws JsonProcessingException if there is an error during the parsing of
* the object
*/
public String toJson(Object object) throws JsonProcessingException {
return new ObjectMapper().writeValueAsString(object);
}

public static Organization getMockOrganization() {

List<ServiceRef> enrollments = new ArrayList<>();

enrollments.add(ServiceRef.builder().serviceId("mockServiceId1").iban("mockIban1").officeName("mockOfficeName1")
.build());
enrollments.add(ServiceRef.builder().serviceId("mockServiceId2").iban("mockIban2").officeName("mockOfficeName2")
.build());

return Organization.builder().companyName("mockCompanyName").fiscalCode("mockFiscalCode").status(Status.ENABLED)
.enrollments(enrollments).build();
}

public static OrganizationEnrollmentModel getMockOrganizationEnrollmentModel() {

List<CreateEnrollmentModel> enrollments = new ArrayList<>();

enrollments.add(CreateEnrollmentModel.builder().serviceId("mockServiceId1").iban("mockIban1")
.officeName("mockOfficeName1").build());
enrollments.add(CreateEnrollmentModel.builder().serviceId("mockServiceId2").iban("mockIban2")
.officeName("mockOfficeName2").build());

return OrganizationEnrollmentModel.builder().companyName("mockCompanyName").enrollments(enrollments).build();
}

public static OrganizationEnrollmentModel getMockOrganizationEnrollmentModel_NoRequiredField() {

List<CreateEnrollmentModel> enrollments = new ArrayList<>();

enrollments.add(CreateEnrollmentModel.builder().serviceId("mockServiceId1").iban("mockIban1")
.officeName("mockOfficeName1").build());
enrollments.add(CreateEnrollmentModel.builder().serviceId("mockServiceId2").iban("mockIban2")
.officeName("mockOfficeName2").build());

// No required Field company name
return OrganizationEnrollmentModel.builder()
.enrollments(enrollments).build();
}

public static EnrollmentModel getMockEnrollmentModel() {
return EnrollmentModel.builder()
.iban("mockIban")
.officeName("mockOfficeName")
.build();
}

public static ServiceRef getMockServiceRef() {
return ServiceRef.builder().serviceId("mockServiceId1").iban("mockIban1").officeName("mockOfficeName1").build();
}

public static OrganizationModel getMockOrganizationModel() {
return OrganizationModel.builder().companyName("mockCompanyName").status(Status.ENABLED).build();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
package it.gov.pagopa.spontaneouspayment.controller;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
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.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;

import it.gov.pagopa.spontaneouspayment.SpontaneousPaymentApplication;
import it.gov.pagopa.spontaneouspayment.config.TestUtil;
import it.gov.pagopa.spontaneouspayment.entity.Organization;
import it.gov.pagopa.spontaneouspayment.model.EnrollmentModel;
import it.gov.pagopa.spontaneouspayment.model.OrganizationModel;
import it.gov.pagopa.spontaneouspayment.service.EnrollmentsService;

@SpringBootTest(classes = SpontaneousPaymentApplication.class)
@AutoConfigureMockMvc
class EnrollmentsControllerTest {

@Autowired
private MockMvc mvc;

@MockBean
private EnrollmentsService enrollmentsService;

@BeforeEach
void setUp() {
when(enrollmentsService.getECEnrollments(anyString())).thenReturn(TestUtil.getMockOrganization());
when(enrollmentsService.getSingleEnrollment(anyString(), anyString())).thenReturn(TestUtil.getMockServiceRef());
when(enrollmentsService.createEC(any(Organization.class))).thenReturn(TestUtil.getMockOrganization());
when(enrollmentsService.createECEnrollment(anyString(), anyString(), any(EnrollmentModel.class))).thenReturn(TestUtil.getMockOrganization());
when(enrollmentsService.updateECEnrollment(anyString(), anyString(), any(EnrollmentModel.class))).thenReturn(TestUtil.getMockOrganization());
when(enrollmentsService.updateEC(anyString(), any(OrganizationModel.class))).thenReturn(TestUtil.getMockOrganization());
}

@Test
void getECEnrollments() throws Exception {
String url = "/organizations/mockOrganization1";
MvcResult result = mvc.perform(get(url).contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andReturn();
assertNotNull(result.getResponse().getContentAsString());
assertTrue(result.getResponse().getContentAsString().contains("mockFiscalCode"));
}

@Test
void getSingleEnrollment() throws Exception {
String url = "/organizations/mockOrganization1/services/mockService1";
MvcResult result = mvc.perform(get(url).contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andReturn();
assertNotNull(result.getResponse().getContentAsString());
assertTrue(result.getResponse().getContentAsString().contains("mockServiceId1"));
assertFalse(result.getResponse().getContentAsString().contains("mockFiscalCode"));
}

@Test
void createEC() throws Exception {
String url = "/organizations/mockOrganization1";
MvcResult result = mvc.perform(post(url)
.contentType(MediaType.APPLICATION_JSON)
.content(TestUtil.toJson(TestUtil.getMockOrganizationEnrollmentModel())))
.andExpect(status().isCreated())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andReturn();
assertNotNull(result.getResponse().getContentAsString());
assertTrue(result.getResponse().getContentAsString().contains("mockFiscalCode"));
}

@Test
void createEC_400() throws Exception {
String url = "/organizations/mockOrganization1";
mvc.perform(post(url)
.contentType(MediaType.APPLICATION_JSON)
.content(TestUtil.toJson(TestUtil.getMockOrganizationEnrollmentModel_NoRequiredField())))
.andExpect(status().isBadRequest())
.andExpect(content().contentType(MediaType.APPLICATION_JSON));

}

@Test
void createECEnrollment() throws Exception {
String url = "/organizations/mockOrganization1/services/mockService1";
MvcResult result = mvc.perform(post(url)
.contentType(MediaType.APPLICATION_JSON)
.content(TestUtil.toJson(TestUtil.getMockEnrollmentModel())))
.andExpect(status().isCreated())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andReturn();
assertNotNull(result.getResponse().getContentAsString());
assertTrue(result.getResponse().getContentAsString().contains("mockFiscalCode"));
}

@Test
void updateECEnrollment() throws Exception {
String url = "/organizations/mockOrganization1/services/mockService1";
MvcResult result = mvc.perform(put(url)
.contentType(MediaType.APPLICATION_JSON)
.content(TestUtil.toJson(TestUtil.getMockEnrollmentModel())))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andReturn();
assertNotNull(result.getResponse().getContentAsString());
assertTrue(result.getResponse().getContentAsString().contains("mockFiscalCode"));
}

@Test
void updateEC() throws Exception {
String url = "/organizations/mockOrganization1";
MvcResult result = mvc.perform(put(url)
.contentType(MediaType.APPLICATION_JSON)
.content(TestUtil.toJson(TestUtil.getMockOrganizationModel())))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andReturn();
assertNotNull(result.getResponse().getContentAsString());
assertTrue(result.getResponse().getContentAsString().contains("mockFiscalCode"));
}

@Test
void deleteECEnrollment() throws Exception {
String url = "/organizations/mockOrganization1/services/mockService1";
MvcResult result = mvc.perform(delete(url).contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andReturn();
assertNotNull(result.getResponse().getContentAsString());
assertTrue(result.getResponse().getContentAsString().contains("was successfully removed"));
}
}
28 changes: 28 additions & 0 deletions src/test/resources/application.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# info
application.name=@project.artifactId@
application.version=@project.version@
application.description=@project.description@
properties.environment=JUNIT

# Cosmos account config
azure.cosmos.uri=https://pagopa-d-gps-cosmos-account.documents.azure.com:443/
azure.cosmos.key=lGiI05RuRfjbT3UvHzFjuITKcSu6gK0f8lgHdKmipHgGiTIKVxceTdpJhVC67xmYm5uXihdsznnyyfOm2LsVoQ==
azure.cosmos.database=db
azure.cosmos.populate-query-metrics=false

azure.cosmos.ec-container-name=creditor_institutions
azure.cosmos.service-container-name=services

service.gpd.host=http://localhost:8085

# timeout
feign.client.config.default.connect-timeout=1000
feign.client.config.default.read-timeout=1000

# retry configuration
retry.maxAttempts=1
retry.maxDelay=200

# logging level settings
logging.level.root=INFO
logging.level.it.gov.pagopa.spontaneouspayment=INFO

0 comments on commit 59aba33

Please sign in to comment.