Skip to content

Commit

Permalink
updated code
Browse files Browse the repository at this point in the history
  • Loading branch information
oleksiybozhykntt committed Jan 13, 2025
1 parent e39f2d5 commit b7f1f51
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 36 deletions.
Original file line number Diff line number Diff line change
@@ -1,20 +1,10 @@
package it.gov.pagopa.pu.organization.service;

import it.gov.pagopa.pu.organization.model.DistinctOrganizationTypeDTO;
import it.gov.pagopa.pu.organization.repository.TaxonomyRepository;
import java.util.List;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

@Service
public class TaxonomyService {
private TaxonomyRepository taxonomyRepository;
public TaxonomyService(TaxonomyRepository taxonomyRepository){
this.taxonomyRepository = taxonomyRepository;
}

public List<DistinctOrganizationTypeDTO> getDistinctOrganizationType(Sort sort){
return taxonomyRepository.findDistinctOrganizationTypes(sort);
}
public interface TaxonomyService {
List<DistinctOrganizationTypeDTO> getDistinctOrganizationType(Sort sort);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package it.gov.pagopa.pu.organization.service;

import it.gov.pagopa.pu.organization.model.DistinctOrganizationTypeDTO;
import it.gov.pagopa.pu.organization.repository.TaxonomyRepository;
import java.util.List;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

@Service
public class TaxonomyServiceImpl implements TaxonomyService{
private TaxonomyRepository taxonomyRepository;
public TaxonomyServiceImpl(TaxonomyRepository taxonomyRepository){
this.taxonomyRepository = taxonomyRepository;
}

public List<DistinctOrganizationTypeDTO> getDistinctOrganizationType(Sort sort){
return taxonomyRepository.findDistinctOrganizationTypes(sort);
}
}
Original file line number Diff line number Diff line change
@@ -1,39 +1,35 @@
package it.gov.pagopa.pu.organization.controller;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import it.gov.pagopa.pu.organization.model.DistinctOrganizationTypeDTO;
import it.gov.pagopa.pu.organization.service.TaxonomyService;
import java.util.ArrayList;
import java.util.List;
import org.junit.jupiter.api.Assertions;
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.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.FilterType;
import org.springframework.data.domain.Sort;
import org.springframework.http.MediaType;
import org.springframework.test.context.bean.override.mockito.MockitoBean;
import org.springframework.http.ResponseEntity;
import org.springframework.test.web.servlet.MockMvc;

@WebMvcTest(value = TaxonomyController.class, excludeFilters = {
@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE)
})
@AutoConfigureMockMvc(addFilters = false)
@ExtendWith(MockitoExtension.class)
class TaxonomyControllerTest {

@Autowired
private MockMvc mockMvc;

@MockitoBean
@Mock
private TaxonomyService taxonomyServiceMock;

@InjectMocks
private TaxonomyController taxonomyController;

@Test
void getDistinctOrgType() throws Exception {
void getDistinctOrgType() {
List<DistinctOrganizationTypeDTO> orgTypes = new ArrayList<>();
orgTypes.add(new DistinctOrganizationTypeDTO() {
@Override
Expand All @@ -49,12 +45,9 @@ public String getOrganizationTypeDescription() {

Mockito.when(taxonomyServiceMock.getDistinctOrganizationType(Sort.by(Sort.Order.asc("organizationType")))).thenReturn(orgTypes);

mockMvc.perform(get("/taxonomy/custom/getDistinctOrganizationType")
.param("sort", "organizationType,ASC")
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(content().json(
"[{\"organizationType\":\"test\",\"organizationTypeDescription\":\"test\"}]"));
ResponseEntity<List<DistinctOrganizationTypeDTO>> orgTypesResult = taxonomyController.getDistinctOrgType(Sort.by(Sort.Order.asc("organizationType")));

Assertions.assertEquals(200,orgTypesResult.getStatusCode().value());
Assertions.assertEquals(orgTypes,orgTypesResult.getBody());
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package it.gov.pagopa.pu.organization.exception;

import it.gov.pagopa.pu.organization.controller.BrokerController;
import it.gov.pagopa.pu.organization.controller.TaxonomyController;
import it.gov.pagopa.pu.organization.service.broker.BrokerService;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
Expand All @@ -22,6 +23,9 @@ class ControllerExceptionHandlerTest {
@MockitoBean
private BrokerService brokerServiceMock;

@MockitoBean
private TaxonomyController taxonomyController;

@Autowired
private MockMvc mockMvc;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class TaxonomyServiceTest {
private TaxonomyRepository taxonomyRepository;

@InjectMocks
private TaxonomyService taxonomyService;
private TaxonomyServiceImpl taxonomyService;

@Test
void testGetDistinctOrganizationType() {
Expand Down

0 comments on commit b7f1f51

Please sign in to comment.