Commit f92e6af5 authored by Dimitrios Chalepakis's avatar Dimitrios Chalepakis
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-710-hide-passwords-from-yml
parents 3c66d9d3 de4e5553
......@@ -17,10 +17,13 @@ package org.eclipse.openk.gridfailureinformation.api;
import org.eclipse.openk.gridfailureinformation.api.dto.CommunicationDto;
import org.eclipse.openk.gridfailureinformation.api.dto.VwDetailedContact;
import org.eclipse.openk.gridfailureinformation.api.impl.CustomPageImpl;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.List;
import java.util.UUID;
......@@ -37,4 +40,11 @@ public interface ContactApi {
public List<CommunicationDto> getContactCommunications(@PathVariable UUID contactUuid,
@RequestHeader("Authorization") String token);
@GetMapping("/contacts")
public CustomPageImpl<VwDetailedContact> findContacts(
@RequestParam(required = false) String searchText,
@RequestParam(required = false) String moduleName,
Pageable pageable,
@RequestHeader("Authorization") String token
);
}
package org.eclipse.openk.gridfailureinformation.api.impl;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.JsonNode;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import java.util.ArrayList;
import java.util.List;
public class CustomPageImpl<T> extends PageImpl<T> {
private static final long serialVersionUID = 3248189030448292002L;
@JsonCreator(mode = JsonCreator.Mode.PROPERTIES)
public CustomPageImpl(@JsonProperty("content") List<T> content, @JsonProperty("number") int number, @JsonProperty("size") int size,
@JsonProperty("totalElements") Long totalElements, @JsonProperty("pageable") JsonNode pageable, @JsonProperty("last") boolean last,
@JsonProperty("totalPages") int totalPages, @JsonProperty("sort") JsonNode sort, @JsonProperty("first") boolean first,
@JsonProperty("numberOfElements") int numberOfElements) {
super(content, PageRequest.of(number, size), totalElements);
}
public CustomPageImpl(List<T> content, Pageable pageable, long total) {
super(content, pageable, total);
}
public CustomPageImpl(List<T> content) {
super(content);
}
public CustomPageImpl() {
super(new ArrayList<T>());
}
}
......@@ -38,6 +38,15 @@ public class AddressController {
@Autowired
private AddressService addressService;
@Secured({"ROLE_GRID-FAILURE-ADMIN", "ROLE_GRID-FAILURE-READER", "ROLE_GRID-FAILURE-CREATOR", "ROLE_GRID-FAILURE-QUALIFIER", "ROLE_GRID-FAILURE-PUBLISHER"})
@ApiOperation(value = "Anzeigen aller Addressen")
@ApiResponses(value = {@ApiResponse(code = 200, message = "Erfolgreich durchgeführt")})
@GetMapping
public List<AddressDto> getAddresses(
@RequestParam("branch") Optional<String> branch) {
return addressService.getAddresses(branch);
}
@GetMapping("/{uuid}")
@Secured({"ROLE_GRID-FAILURE-ADMIN", "ROLE_GRID-FAILURE-READER", "ROLE_GRID-FAILURE-CREATOR", "ROLE_GRID-FAILURE-QUALIFIER", "ROLE_GRID-FAILURE-PUBLISHER"})
@ApiOperation(value = "Anzeigen einer bestimmten Adresse")
......
/*
*******************************************************************************
* Copyright (c) 2019 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************
*/
package org.eclipse.openk.gridfailureinformation.controller;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.extern.log4j.Log4j2;
import org.eclipse.openk.gridfailureinformation.api.dto.VwDetailedContact;
import org.eclipse.openk.gridfailureinformation.service.ContactService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.security.access.annotation.Secured;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.Optional;
import static org.springframework.data.domain.Sort.Direction.ASC;
@Log4j2
@RestController
@RequestMapping("/contacts")
public class ContactController {
@Autowired
private ContactService contactService;
@Secured({"ROLE_GRID-FAILURE-ADMIN", "ROLE_GRID-FAILURE-READER", "ROLE_GRID-FAILURE-CREATOR", "ROLE_GRID-FAILURE-QUALIFIER", "ROLE_GRID-FAILURE-PUBLISHER"})
@ApiOperation(value = "Anzeigen aller Kontakte")
@ApiResponses(value = {@ApiResponse(code = 200, message = "Erfolgreich durchgeführt")})
@GetMapping
public Page<VwDetailedContact> findContacts(
@RequestParam( "searchText" ) Optional<String> searchText,
@RequestParam( "moduleName") Optional<String> moduleName,
@PageableDefault( sort = {"name"}, size = 500, direction = ASC) Pageable pageable) {
return contactService.findContacts(searchText, moduleName, pageable);
}
}
......@@ -24,9 +24,11 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.Optional;
@Log4j2
@RestController
......@@ -40,7 +42,10 @@ public class ExpectedReasonController {
@ApiOperation(value = "Anzeigen aller erwarteten Gründe")
@ApiResponses(value = {@ApiResponse(code = 200, message = "Erfolgreich durchgeführt")})
@GetMapping
public List<ExpectedReasonDto> findExpectedReasonsTexts() { return expectedReasonService.getExpectedReasons(); }
public List<ExpectedReasonDto> findExpectedReasonsTexts(
@RequestParam("branch") Optional<String> branch) {
return expectedReasonService.getExpectedReasons(branch);
}
......
......@@ -17,6 +17,8 @@ package org.eclipse.openk.gridfailureinformation.mapper;
import org.eclipse.openk.gridfailureinformation.model.RefExpectedReason;
import org.eclipse.openk.gridfailureinformation.viewmodel.ExpectedReasonDto;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import org.mapstruct.ReportingPolicy;
@Mapper(componentModel = "spring", unmappedTargetPolicy = ReportingPolicy.IGNORE)
......
......@@ -30,4 +30,5 @@ public class RefExpectedReason {
private UUID uuid;
private String text;
private String description;
private String branches;
}
......@@ -51,4 +51,8 @@ public interface AddressRepository extends JpaRepository<TblAddress, Long > {
List<TblAddress> findByPostcodeAndCommunityAndStreet(String postcode, String community, String street);
List<TblAddress> findByPowerConnection(Boolean powerConnection);
List<TblAddress> findByWaterConnection(Boolean waterConnection);
List<TblAddress> findByGasConnection(Boolean gasConnection);
}
......@@ -15,8 +15,11 @@
package org.eclipse.openk.gridfailureinformation.repository;
import org.eclipse.openk.gridfailureinformation.model.RefBranch;
import org.eclipse.openk.gridfailureinformation.model.RefExpectedReason;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import java.util.List;
......@@ -29,4 +32,7 @@ public interface ExpectedReasonRepository extends JpaRepository<RefExpectedReaso
List<RefExpectedReason> findAll();
Optional<RefExpectedReason> findByUuid(UUID uuid);
@Query("select er from RefExpectedReason er where ',' || branches || ',' like '%,' || :branch || ',%' ")
List<RefExpectedReason> findByBranch(@Param("branch") String branch);
}
......@@ -11,7 +11,7 @@
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************
*/
*/
package org.eclipse.openk.gridfailureinformation.service;
import org.eclipse.openk.gridfailureinformation.exceptions.NotFoundException;
......@@ -39,16 +39,32 @@ public class AddressService {
private AddressMapper addressMapper;
private static HousenumberUuidDto toHousenumberUuid(TblAddress tblAddress) {
HousenumberUuidDto housenumberUuidDto = new HousenumberUuidDto();
HousenumberUuidDto housenumberUuidDto = new HousenumberUuidDto();
housenumberUuidDto.setHousenumber(tblAddress.getHousenumber());
housenumberUuidDto.setUuid(tblAddress.getUuid());
return housenumberUuidDto;
}
public List<AddressDto> getAddresses() {
public List<AddressDto> getAddresses(Optional<String> branchOpt) {
List<TblAddress> addressList;
String branch = branchOpt.orElseGet(String::new);
switch (branch) {
case "S":
addressList = addressRepository.findByPowerConnection(true);
break;
case "G":
addressList = addressRepository.findByGasConnection(true);
break;
case "W":
addressList = addressRepository.findByWaterConnection(true);
break;
default:
addressList = addressRepository.findAll();
}
return addressRepository.findAll().stream()
.map( addressMapper::toAddressDto )
return addressList.stream()
.map(addressMapper::toAddressDto)
.collect(Collectors.toList());
}
......@@ -58,7 +74,7 @@ public class AddressService {
return addressMapper.toAddressDto(tblAddress);
}
public List<String> getPostcodes(){
public List<String> getPostcodes() {
return addressRepository.findAllPostcodes();
}
......@@ -67,15 +83,14 @@ public class AddressService {
}
public List<String> getDistricts(String postcode, String community) {
return addressRepository.findDistrictsByPostcodeAndCommunity(postcode, community);
return addressRepository.findDistrictsByPostcodeAndCommunity(postcode, community);
}
public List<String> getStreets(String postcode, String community, Optional<String> district) {
if(!district.isPresent()){
if (!district.isPresent()) {
return addressRepository.findStreetsByPostcodeAndCommunity(postcode, community);
}
else{
} else {
return addressRepository.findStreetsByPostcodeAndCommunityAndDistrict(postcode, community, district.get());
}
}
......
/*
*******************************************************************************
* Copyright (c) 2019 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************
*/
package org.eclipse.openk.gridfailureinformation.service;
import org.eclipse.openk.gridfailureinformation.api.ContactApi;
import org.eclipse.openk.gridfailureinformation.api.dto.VwDetailedContact;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import java.util.Optional;
@Service
public class ContactService {
@Autowired
private ContactApi contactApi;
public Page<VwDetailedContact> findContacts(Optional<String> searchText, Optional<String> moduleName, Pageable pageable) {
String jwt = (String) SecurityContextHolder.getContext().getAuthentication().getDetails();
String st = searchText.orElse(null);
String mn = moduleName.orElse(null);
return contactApi.findContacts(st, mn, pageable, jwt );
}
}
......@@ -15,12 +15,15 @@
package org.eclipse.openk.gridfailureinformation.service;
import org.eclipse.openk.gridfailureinformation.mapper.ExpectedReasonMapper;
import org.eclipse.openk.gridfailureinformation.model.RefBranch;
import org.eclipse.openk.gridfailureinformation.repository.BranchRepository;
import org.eclipse.openk.gridfailureinformation.repository.ExpectedReasonRepository;
import org.eclipse.openk.gridfailureinformation.viewmodel.ExpectedReasonDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
@Service
......@@ -32,12 +35,19 @@ public class ExpectedReasonService {
@Autowired
private ExpectedReasonMapper expectedReasonMapper;
public List<ExpectedReasonDto> getExpectedReasons() {
return expectedReasonRepository.findAll().stream()
.map( expectedReasonMapper::toExpectedReasonDto )
.collect(Collectors.toList());
@Autowired
BranchRepository branchRepository;
public List<ExpectedReasonDto> getExpectedReasons(Optional<String> branchOpt) {
if (branchOpt.isPresent()) {
return expectedReasonRepository.findByBranch(branchOpt.get()).stream()
.map(expectedReasonMapper::toExpectedReasonDto)
.collect(Collectors.toList());
} else {
return expectedReasonRepository.findAll().stream()
.map( expectedReasonMapper::toExpectedReasonDto )
.collect(Collectors.toList());
}
}
}
......@@ -26,4 +26,5 @@ public class ExpectedReasonDto implements Serializable {
private UUID uuid;
private String text;
private String description;
private String branches;
}
......@@ -28,6 +28,7 @@ import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.web.servlet.MockMvc;
import java.util.List;
import java.util.Optional;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
......@@ -48,8 +49,9 @@ public class ExpectedReasonControllerTest {
@Test
public void shouldReturnExpectedReasons() throws Exception {
Optional<String> branchOptional = Optional.empty();
List<ExpectedReasonDto> expectedReasonDtoList = MockDataHelper.mockExpectedReasonDtoList();
when(expectedReasonService.getExpectedReasons()).thenReturn(expectedReasonDtoList);
when(expectedReasonService.getExpectedReasons(branchOptional)).thenReturn(expectedReasonDtoList);
mockMvc.perform(get("/expected-reasons"))
.andExpect(status().is2xxSuccessful())
......
......@@ -46,9 +46,58 @@ public class AddressServiceTest {
@Test
public void shouldGetAllAddresses() {
Optional<String> branchOptional = Optional.empty();
List<TblAddress> addresses = MockDataHelper.mockAddressList();
when(addressRepository.findAll()).thenReturn(addresses);
List<AddressDto> addressesDtoList = addressService.getAddresses();
List<AddressDto> addressesDtoList = addressService.getAddresses(branchOptional);
assertEquals(addressesDtoList.size(), addresses.size());
assertEquals(2, addressesDtoList.size());
assertEquals(addressesDtoList.get(1).getUuid(), addresses.get(1).getUuid());
}
@Test
public void shouldGetAddressesWithPowerConnection() {
Optional<String> branchOptional = Optional.of("S");
List<TblAddress> addresses = MockDataHelper.mockAddressList();
when(addressRepository.findByPowerConnection(true)).thenReturn(addresses);
List<AddressDto> addressesDtoList = addressService.getAddresses(branchOptional);
assertEquals(addressesDtoList.size(), addresses.size());
assertEquals(2, addressesDtoList.size());
assertEquals(addressesDtoList.get(1).getUuid(), addresses.get(1).getUuid());
}
@Test
public void shouldGetAddressesWithWaterConnection() {
Optional<String> branchOptional = Optional.of("W");
List<TblAddress> addresses = MockDataHelper.mockAddressList();
when(addressRepository.findByWaterConnection(true)).thenReturn(addresses);
List<AddressDto> addressesDtoList = addressService.getAddresses(branchOptional);
assertEquals(addressesDtoList.size(), addresses.size());
assertEquals(2, addressesDtoList.size());
assertEquals(addressesDtoList.get(1).getUuid(), addresses.get(1).getUuid());
}
@Test
public void shouldGetAddressesWithGasConnection() {
Optional<String> branchOptional = Optional.of("G");
List<TblAddress> addresses = MockDataHelper.mockAddressList();
when(addressRepository.findByGasConnection(true)).thenReturn(addresses);
List<AddressDto> addressesDtoList = addressService.getAddresses(branchOptional);
assertEquals(addressesDtoList.size(), addresses.size());
assertEquals(2, addressesDtoList.size());
assertEquals(addressesDtoList.get(1).getUuid(), addresses.get(1).getUuid());
}
@Test
public void shouldGetAllAddressesWithOtherBranch() {
Optional<String> branchOptional = Optional.of("XXX");
List<TblAddress> addresses = MockDataHelper.mockAddressList();
when(addressRepository.findAll()).thenReturn(addresses);
List<AddressDto> addressesDtoList = addressService.getAddresses(branchOptional);
assertEquals(addressesDtoList.size(), addresses.size());
assertEquals(2, addressesDtoList.size());
......
......@@ -14,22 +14,26 @@
*/
package org.eclipse.openk.gridfailureinformation.service;
import org.eclipse.openk.gridfailureinformation.config.TestConfiguration;
import org.eclipse.openk.gridfailureinformation.model.RefExpectedReason;
import org.eclipse.openk.gridfailureinformation.repository.ExpectedReasonRepository;
import org.eclipse.openk.gridfailureinformation.support.MockDataHelper;
import org.eclipse.openk.gridfailureinformation.viewmodel.ExpectedReasonDto;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.ContextConfiguration;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.when;
import org.eclipse.openk.gridfailureinformation.config.TestConfiguration;
import org.eclipse.openk.gridfailureinformation.model.RefBranch;
import org.eclipse.openk.gridfailureinformation.model.RefExpectedReason;
import org.eclipse.openk.gridfailureinformation.repository.BranchRepository;
import org.eclipse.openk.gridfailureinformation.repository.ExpectedReasonRepository;
import org.eclipse.openk.gridfailureinformation.support.MockDataHelper;
import org.eclipse.openk.gridfailureinformation.viewmodel.ExpectedReasonDto;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.ContextConfiguration;
import java.util.List;
import java.util.Optional;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;
//@RunWith(SpringRunner.class)
@DataJpaTest
......@@ -43,11 +47,31 @@ public class ExpectedReasonServiceTest {
@MockBean
private ExpectedReasonRepository expectedReasonRepository;
@MockBean
private BranchRepository branchRepository;
@Test
public void shouldGetRadiiProperly() {
public void shouldGetExpectedReasonsProperly() {
Optional<String> branchOptional = Optional.empty();
List<RefExpectedReason> mockRefExpectedReasonList = MockDataHelper.mockRefExpectedReasonList();
when(expectedReasonRepository.findAll()).thenReturn(mockRefExpectedReasonList);
List<ExpectedReasonDto> expectedReasons = expectedReasonService.getExpectedReasons();
List<ExpectedReasonDto> expectedReasons = expectedReasonService.getExpectedReasons(branchOptional);
assertEquals(expectedReasons.size(), mockRefExpectedReasonList.size());
assertEquals(2, expectedReasons.size());
assertEquals(expectedReasons.get(1).getUuid(), mockRefExpectedReasonList.get(1).getUuid());
}
@Test
public void shouldGetExpectedReasonsForBranch() {
Optional<String> branchOptional = Optional.of("S");
RefBranch mockBranch = MockDataHelper.mockRefBranch();
List<RefExpectedReason> mockRefExpectedReasonList = MockDataHelper.mockRefExpectedReasonList();
when(branchRepository.findByName(any(String.class))).thenReturn(Optional.of(mockBranch));
when(expectedReasonRepository.findByBranch(any(String.class))).thenReturn(mockRefExpectedReasonList);
List<ExpectedReasonDto> expectedReasons = expectedReasonService.getExpectedReasons(branchOptional);
assertEquals(expectedReasons.size(), mockRefExpectedReasonList.size());
assertEquals(2, expectedReasons.size());
......
......@@ -710,6 +710,8 @@ public class MockDataHelper {
expectedReasonDto1.setUuid(UUID.fromString("cd22ff10-6cde-11ea-bc55-0242ac130003"));
expectedReasonDto1.setDescription("Beschreibung Rohrbruch");
expectedReasonDto1.setDescription("Rohrbruch");
expectedReasonDto1.setBranches("S");
return expectedReasonDto1;
}
......@@ -720,6 +722,7 @@ public class MockDataHelper {