Commit bf6e2586 authored by dietricf's avatar dietricf
Browse files

Merge branch 'DEVELOP' of...

Merge branch 'DEVELOP' of ssh://git.eclipse.org:29418/openk-usermodules/org.eclipse.openk-usermodules.gridFailureInformation.backend into SI-722_Unittest_for_secured
parents 195933aa 3108ab39
......@@ -19,12 +19,16 @@ 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.viewmodel.AddressDto;
import org.eclipse.openk.gridfailureinformation.viewmodel.HousenumberUuidDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.annotation.Secured;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
@Log4j2
@RestController
......@@ -34,6 +38,16 @@ public class AddressController {
@Autowired
private AddressService addressService;
@GetMapping("/{uuid}")
@Secured({"ROLE_GRID-FAILURE-ADMIN", "ROLE_GRID-FAILURE-CREATOR", "ROLE_GRID-FAILURE-QUALIFIER", "ROLE_GRID-FAILURE-PUBLISHER"})
@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);
}
@ApiOperation(value = "Anzeige aller PLZs")
@ApiResponses(value = {@ApiResponse(code = 200, message = "Erfolgreich durchgeführt")})
......@@ -83,7 +97,7 @@ public class AddressController {
@ApiResponses(value = {@ApiResponse(code = 200, message = "Erfolgreich durchgeführt")})
@GetMapping("/housenumbers")
@Secured({"ROLE_GRID-FAILURE-ADMIN", "ROLE_GRID-FAILURE-CREATOR", "ROLE_GRID-FAILURE-QUALIFIER", "ROLE_GRID-FAILURE-PUBLISHER"})
public List<String> findHousenumbers(
public List<HousenumberUuidDto> findHousenumbers(
@RequestParam("postcode") String postcode,
@RequestParam("community") String community,
@RequestParam("street") String street
......
......@@ -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));
}
}
/**
******************************************************************************
* Copyright © 2017-2018 PTA GmbH.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
*
* http://www.eclipse.org/legal/epl-v10.html
*
******************************************************************************
*/
package org.eclipse.openk.gridfailureinformation.viewmodel;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import java.util.UUID;
@Data
@RequiredArgsConstructor
public class HousenumberUuidDto {
private UUID uuid;
private String housenumber;
}
\ No newline at end of file
......@@ -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