Commit c2d3e997 authored by Holger Rudolph's avatar Holger Rudolph
Browse files

[SI-381] new service getAdressByUuid in AddressService, additional returning...


[SI-381] new service getAdressByUuid in AddressService, additional returning of uuid in AddressService.getHousenumbers()
Signed-off-by: Holger Rudolph's avatarHolger Rudolph <holger.rudolph@pta.de>
parent 01ccb63d
......@@ -19,10 +19,10 @@ import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.extern.log4j.Log4j2;
import org.eclipse.openk.gridfailureinformation.service.AddressService;
import org.eclipse.openk.gridfailureinformation.service.BranchService;
import org.eclipse.openk.gridfailureinformation.viewmodel.BranchDto;
import org.eclipse.openk.gridfailureinformation.viewmodel.AddressDto;
import org.eclipse.openk.gridfailureinformation.viewmodel.HousenumberUuidDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import java.util.List;
......@@ -37,6 +37,16 @@ public class AddressController {
@Autowired
private AddressService addressService;
@GetMapping("/{uuid}")
@ApiOperation(value = "Anzeigen einer bestimmten Adresse")
@ApiResponses(value = { @ApiResponse(code = 200, message = "Erfolgreich durchgeführt"),
@ApiResponse(code = 404, message = "Adresse wurde nicht gefunden")})
@ResponseStatus(HttpStatus.OK)
public AddressDto readAddress(
@PathVariable UUID uuid) {
return addressService.getAdressByUuid(uuid);
}
//@Secured({"ROLE_GRID-FAILURE-ADMIN", "ROLE_GRID-FAILURE-CREATOR", "ROLE_GRID-FAILURE-QUALIFIER", "ROLE_GRID-FAILURE-PUBLISHER"})
@ApiOperation(value = "Anzeige aller PLZs")
@ApiResponses(value = {@ApiResponse(code = 200, message = "Erfolgreich durchgeführt")})
......@@ -81,7 +91,7 @@ public class AddressController {
@ApiOperation(value = "Anzeige Strassen nach Postleitzahl und Ort und Strasse")
@ApiResponses(value = {@ApiResponse(code = 200, message = "Erfolgreich durchgeführt")})
@GetMapping("/housenumbers")
public List<String> findHousenumbers(
public List<HousenumberUuidDto> findHousenumbers(
@RequestParam ("postcode") String postcode,
@RequestParam ("community") String community,
@RequestParam ("street") String street
......@@ -90,8 +100,4 @@ public class AddressController {
}
}
......@@ -47,7 +47,6 @@ public interface AddressRepository extends JpaRepository<TblAddress, Long > {
@Query("select distinct a.street from TblAddress a where postcode = :postcode and community = :community and district = :district")
List<String> findStreetsByPostcodeAndCommunityAndDistrict(@Param("postcode") String postcode, @Param("community") String community, @Param("district") String district);
@Query("select distinct a.housenumber from TblAddress a where postcode = :postcode and community = :community and street = :street")
List<String> findHousenumbersByPostcodeAndCommunityAndStreet(@Param("postcode") String postcode, @Param("community") String community, @Param("street") String street);
List<TblAddress> findByPostcodeAndCommunityAndStreet(String postcode, String community, String street);
}
......@@ -14,16 +14,21 @@
*/
package org.eclipse.openk.gridfailureinformation.service;
import org.eclipse.openk.gridfailureinformation.exceptions.NotFoundException;
import org.eclipse.openk.gridfailureinformation.mapper.AddressMapper;
import org.eclipse.openk.gridfailureinformation.model.TblAddress;
import org.eclipse.openk.gridfailureinformation.repository.AddressRepository;
import org.eclipse.openk.gridfailureinformation.viewmodel.AddressDto;
import org.eclipse.openk.gridfailureinformation.viewmodel.HousenumberUuidDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;
import java.util.*;
import java.util.stream.Collectors;
import static java.util.stream.Collectors.collectingAndThen;
import static java.util.stream.Collectors.toCollection;
@Service
public class AddressService {
......@@ -33,6 +38,13 @@ public class AddressService {
@Autowired
private AddressMapper addressMapper;
private static HousenumberUuidDto toHousenumberUuid(TblAddress tblAddress) {
HousenumberUuidDto housenumberUuidDto = new HousenumberUuidDto();
housenumberUuidDto.setHousenumber(tblAddress.getHousenumber());
housenumberUuidDto.setUuid(tblAddress.getUuid());
return housenumberUuidDto;
}
public List<AddressDto> getAddresses() {
return addressRepository.findAll().stream()
......@@ -40,6 +52,12 @@ public class AddressService {
.collect(Collectors.toList());
}
public AddressDto getAdressByUuid(UUID uuid) {
TblAddress tblAddress = addressRepository.findByUuid(uuid)
.orElseThrow(NotFoundException::new);
return addressMapper.toAddressDto(tblAddress);
}
public List<String> getPostcodes(){
return addressRepository.findAllPostcodes();
}
......@@ -62,11 +80,15 @@ public class AddressService {
}
}
public List<String> getHousenumbers(String postcode, String community, String street) {
return addressRepository.findHousenumbersByPostcodeAndCommunityAndStreet(postcode, community, street);
}
public List<HousenumberUuidDto> getHousenumbers(String postcode, String community, String street) {
return addressRepository.findByPostcodeAndCommunityAndStreet(postcode, community, street)
.stream()
.map(AddressService::toHousenumberUuid)
.collect(collectingAndThen(toCollection(() -> new TreeSet<>(Comparator.comparing(HousenumberUuidDto::getHousenumber))),
ArrayList::new));
}
}
......@@ -17,6 +17,7 @@ package org.eclipse.openk.gridfailureinformation.controller;
import org.eclipse.openk.gridfailureinformation.GridFailureInformationApplication;
import org.eclipse.openk.gridfailureinformation.service.AddressService;
import org.eclipse.openk.gridfailureinformation.support.MockDataHelper;
import org.eclipse.openk.gridfailureinformation.viewmodel.HousenumberUuidDto;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
......@@ -114,8 +115,8 @@ public class AddressControllerTest {
String postcode = "71111";
String community = "com1";
String street = "street1";
List<String> strings = MockDataHelper.mockStringList();
when(addressService.getHousenumbers(postcode, community, street)).thenReturn(strings);
List<HousenumberUuidDto> housenumberUuidDtos = MockDataHelper.mockHousnumberUuidList();
when(addressService.getHousenumbers(postcode, community, street)).thenReturn(housenumberUuidDtos);
mockMvc.perform(
get("/addresses/housenumbers")
......
......@@ -19,6 +19,7 @@ import org.eclipse.openk.gridfailureinformation.model.TblAddress;
import org.eclipse.openk.gridfailureinformation.repository.AddressRepository;
import org.eclipse.openk.gridfailureinformation.support.MockDataHelper;
import org.eclipse.openk.gridfailureinformation.viewmodel.AddressDto;
import org.eclipse.openk.gridfailureinformation.viewmodel.HousenumberUuidDto;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
......@@ -95,13 +96,16 @@ public class AddressServiceTest {
String postcode = "12345";
String community = "com1";
String street = "street1";
List<String> strings = MockDataHelper.mockStringList();
when(addressRepository.findHousenumbersByPostcodeAndCommunityAndStreet(postcode, community, street)).thenReturn(strings);
List<String> housenumbers = addressService.getHousenumbers(postcode, community, street);
List<HousenumberUuidDto> housenumberUuids = MockDataHelper.mockHousnumberUuidList();
List<TblAddress> tblAddresses = MockDataHelper.mockAddressList();
when(addressRepository.findByPostcodeAndCommunityAndStreet(postcode, community, street)).thenReturn(tblAddresses);
List<HousenumberUuidDto> housenumbers = addressService.getHousenumbers(postcode, community, street);
assertEquals(housenumbers.size(), strings.size());
assertEquals(housenumbers.size(), housenumberUuids.size());
assertEquals(2, housenumbers.size());
assertEquals(housenumbers.get(1), strings.get(1));
assertEquals(housenumbers.get(1).getHousenumber(), housenumberUuids.get(1).getHousenumber());
}
@Test
......
......@@ -890,16 +890,34 @@ public class MockDataHelper {
address1.setUuid(UUID.randomUUID());
address1.setStreet("stree1");
address1.setPostcode("23443");
address1.setHousenumber("44");
address1.setLongitude(BigDecimal.valueOf(443443));
TblAddress address2 = new TblAddress();
address2.setUuid(UUID.randomUUID());
address2.setStreet("stree1");
address2.setPostcode("45465");
address2.setHousenumber("93g");
address2.setLongitude(BigDecimal.valueOf(546765));
addressList.add(address1);
addressList.add(address2);
return addressList;
}
public static List<HousenumberUuidDto> mockHousnumberUuidList() {
List<HousenumberUuidDto> housenumberUuidDtoList = new ArrayList<>();
HousenumberUuidDto housenumberUuidDto1 = new HousenumberUuidDto();
housenumberUuidDto1.setHousenumber("44");
housenumberUuidDto1.setUuid(UUID.randomUUID());
HousenumberUuidDto housenumberUuidDto2 = new HousenumberUuidDto();
housenumberUuidDto2.setHousenumber("93g");
housenumberUuidDto2.setUuid(UUID.randomUUID());
housenumberUuidDtoList.add(housenumberUuidDto1);
housenumberUuidDtoList.add(housenumberUuidDto2);
return housenumberUuidDtoList;
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment