generated from pagopa/template-java-microservice
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[PPD-206] crud GPS: added EnrollmentsController Junit test
- Loading branch information
Showing
9 changed files
with
278 additions
and
3 deletions.
There are no files selected for viewing
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
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
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
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
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
1 change: 0 additions & 1 deletion
1
src/main/java/it/gov/pagopa/spontaneouspayment/service/EnrollmentsService.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
85 changes: 85 additions & 0 deletions
85
src/test/java/it/gov/pagopa/spontaneouspayment/config/TestUtil.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,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(); | ||
} | ||
|
||
} |
149 changes: 149 additions & 0 deletions
149
src/test/java/it/gov/pagopa/spontaneouspayment/controller/EnrollmentsControllerTest.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,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")); | ||
} | ||
} |
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,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 |