Skip to content

Commit

Permalink
Added API to search an insurance by originId
Browse files Browse the repository at this point in the history
  • Loading branch information
[email protected] authored and [email protected] committed Apr 10, 2024
1 parent 49555cc commit 16d56c0
Show file tree
Hide file tree
Showing 6 changed files with 203 additions and 1 deletion.
73 changes: 73 additions & 0 deletions app/src/main/resources/swagger/api-docs.json
Original file line number Diff line number Diff line change
Expand Up @@ -910,6 +910,79 @@
} ]
}
},
"/insurance-companies/origin/{originId}" : {
"get" : {
"tags" : [ "insurance-companies" ],
"summary" : "Search insurance company by its taxCode",
"description" : "Returns only one insurance company.",
"operationId" : "searchByOriginIdUsingGET",
"parameters" : [ {
"name" : "originId",
"in" : "path",
"description" : "insurance company's IVASS unique identifier",
"required" : true,
"style" : "simple",
"schema" : {
"type" : "string"
}
} ],
"responses" : {
"200" : {
"description" : "OK",
"content" : {
"application/json" : {
"schema" : {
"$ref" : "#/components/schemas/InsuranceCompanyResource"
}
}
}
},
"400" : {
"description" : "Bad Request",
"content" : {
"application/problem+json" : {
"schema" : {
"$ref" : "#/components/schemas/Problem"
}
}
}
},
"401" : {
"description" : "Unauthorized",
"content" : {
"application/problem+json" : {
"schema" : {
"$ref" : "#/components/schemas/Problem"
}
}
}
},
"404" : {
"description" : "Not Found",
"content" : {
"application/problem+json" : {
"schema" : {
"$ref" : "#/components/schemas/Problem"
}
}
}
},
"500" : {
"description" : "Internal Server Error",
"content" : {
"application/problem+json" : {
"schema" : {
"$ref" : "#/components/schemas/Problem"
}
}
}
}
},
"security" : [ {
"bearerAuth" : [ "global" ]
} ]
}
},
"/insurance-companies/{taxId}" : {
"get" : {
"tags" : [ "insurance-companies" ],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@
public interface IvassService {
QueryResult<InsuranceCompany> search(Optional<String> searchText, int page, int limit);
InsuranceCompany findByTaxCode(String taxId);
InsuranceCompany findByOriginId(String ivassCode);
}

Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,21 @@ public InsuranceCompany findByTaxCode(String taxId) {
return company;
}

@Override
public InsuranceCompany findByOriginId(String originId) {
log.trace("findByIvassCode start");
final List<InsuranceCompany> companies = indexSearchService.findById(InsuranceCompany.Field.ORIGIN_ID, originId.toUpperCase());
if (companies.isEmpty()) {
throw new ResourceNotFoundException();
} else if (companies.size() > 1) {
throw new TooManyResourceFoundException();
}
final InsuranceCompany company = companies.get(0);
log.debug("findByIvassCode result = {}", company);
log.trace("findByIvassCode end");
return company;
}

@Override
public QueryResult<InsuranceCompany> search(Optional<String> searchText, int page, int limit) {
log.trace("search start");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,17 @@ void findById_ResourceNotFound() {
assertThrows(ResourceNotFoundException.class, executable);
}

@Test
void findByOriginId_ResourceNotFound() {
// given
final String originId = "originId";
when(indexSearchService.findById(any(), anyString()))
.thenReturn(List.of());
// when
final Executable executable = () -> ivassService.findByOriginId(originId);
// then
assertThrows(ResourceNotFoundException.class, executable);
}

@Test
void findById_TooManyResourceFound() {
Expand All @@ -53,6 +64,19 @@ void findById_TooManyResourceFound() {
assertThrows(TooManyResourceFoundException.class, executable);
}

@Test
void findByOriginId_TooManyResourceFound() {
// given
final String originId = "originId";
final DummyInsuranceCompany dummyCompany = new DummyInsuranceCompany();
when(indexSearchService.findById(any(), anyString()))
.thenReturn(List.of(dummyCompany, dummyCompany));
// when
final Executable executable = () -> ivassService.findByOriginId(originId);
// then
assertThrows(TooManyResourceFoundException.class, executable);
}

@Test
void findById_found() {
// given
Expand All @@ -68,6 +92,21 @@ void findById_found() {
assertSame(dummyCompany, result);
}

@Test
void findByOriginId_found() {
// given
final String originId = "originId";
final DummyInsuranceCompany dummyCompany = new DummyInsuranceCompany();
dummyCompany.setId("id");
dummyCompany.setOriginId("originId");
when(indexSearchService.findById(any(), anyString()))
.thenReturn(List.of(dummyCompany));
// when
final InsuranceCompany result = ivassService.findByOriginId(originId);
// then
assertSame(dummyCompany, result);
}

@Test
void search_emptySearchText() {
// given
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,24 @@ public InsuranceCompanyResource searchByTaxCode(@ApiParam("${swagger.model.insur
return insuranceCompany;
}

@GetMapping("/origin/{originId}")
@ResponseStatus(HttpStatus.OK)
@ApiOperation(value = "${swagger.api.insurance-company.search.byId.summary}", notes = "${swagger.api.insurance-company.search.byId.notes}")
public InsuranceCompanyResource searchByOriginId(@ApiParam("${swagger.model.insurance-company.originId}")
@PathVariable("originId") String originId) {
log.trace("searchByTaxCode start");
final InsuranceCompanyResource insuranceCompany = insuranceCompanyMapper.toResource(ivassService.findByOriginId(originId));
log.debug("searchByTaxCode result = {}", insuranceCompany);
log.trace("searchByTaxCode end");
return insuranceCompany;
}

@GetMapping
@ResponseStatus(HttpStatus.OK)
@ApiOperation(value = "${swagger.api.insurance-company.search.summary}", notes = "${swagger.api.insurance-company.search.notes}")
public InsuranceCompaniesResource search(@ApiParam("${swagger.model.*.search}")
@RequestParam(value = "search", required = false)
Optional<String> search,
Optional<String> search,
@ApiParam(value = "${swagger.model.*.page}")
@RequestParam(value = "page", required = false, defaultValue = "1")
Integer page,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ class IvassControllerTest {
@MockBean
private IvassService ivassService;

/**
* Method under test: {@link IvassController#searchByTaxCode(String)}
*/
@Test
void findInsurance() throws Exception {
// given
Expand All @@ -66,7 +69,39 @@ void findInsurance() throws Exception {
.findByTaxCode(taxId);
verifyNoMoreInteractions(ivassService);
}

/**
* Method under test: {@link IvassController#searchByOriginId(String)}
*/
@Test
void findInsuranceByOriginId() throws Exception {
// given
final String originId = "originId";
when(ivassService.findByOriginId(any()))
.thenReturn(mockInstance(new DummyInsuranceCompany()));
// when
mvc.perform(MockMvcRequestBuilders
.get(BASE_URL + "/origin/{originId}", originId)
.contentType(APPLICATION_JSON_VALUE)
.accept(APPLICATION_JSON_VALUE))
.andExpect(status().isOk())
.andExpect(jsonPath("$.id", notNullValue()))
.andExpect(jsonPath("$.originId", notNullValue()))
.andExpect(jsonPath("$.taxCode", notNullValue()))
.andExpect(jsonPath("$.description", notNullValue()))
.andExpect(jsonPath("$.digitalAddress", notNullValue()))
.andExpect(jsonPath("$.address", notNullValue()));
// then

verify(ivassService, times(1))
.findByOriginId(originId);
verifyNoMoreInteractions(ivassService);
}


/**
* Method under test: {@link IvassController#searchByTaxCode(String)}
*/
@Test
void findInsuranceNotFound() throws Exception {
// given
Expand All @@ -85,6 +120,30 @@ void findInsuranceNotFound() throws Exception {
verifyNoMoreInteractions(ivassService);
}

/**
* Method under test: {@link IvassController#searchByOriginId(String)}
*/
@Test
void findInsuranceNyOriginIdNotFound() throws Exception {
// given
final String originId = "originId";
when(ivassService.findByOriginId(any()))
.thenThrow(ResourceNotFoundException.class);
// when
mvc.perform(MockMvcRequestBuilders
.get(BASE_URL + "/origin/{originId}", originId)
.contentType(APPLICATION_JSON_VALUE)
.accept(APPLICATION_JSON_VALUE))
.andExpect(status().isNotFound());
// then
verify(ivassService, times(1))
.findByOriginId(originId);
verifyNoMoreInteractions(ivassService);
}

/**
* Method under test: {@link IvassController#search(Optional, Integer, Integer)}
*/
@Test
void search() throws Exception {
// given
Expand Down Expand Up @@ -118,6 +177,9 @@ void search() throws Exception {
verifyNoMoreInteractions(ivassService);
}

/**
* Method under test: {@link IvassController#search(Optional, Integer, Integer)}
*/
@Test
void search_defaultInputParams() throws Exception {
// given
Expand Down

0 comments on commit 16d56c0

Please sign in to comment.