Commit e9d899a6 authored by Simon Reis's avatar Simon Reis
Browse files

Merge branch 'SI-379_ImportAdressen' into DEVELOP

parents deb6d7f3 3bc76720
FROM openjdk:8-jre-alpine
COPY /target/grid-failure-information.addressimport.service.jar /usr/src/cbd/
WORKDIR usr/src/cbd
CMD ["java", "-jar", "-Dspring.profiles.active=devserver", "grid-failure-information.addressimport.service.jar"]
\ No newline at end of file
config.stopBubbling = true
lombok.addLombokGeneratedAnnotation = true
\ No newline at end of file
...@@ -9,10 +9,10 @@ ...@@ -9,10 +9,10 @@
<relativePath/> <!-- lookup parent from repository --> <relativePath/> <!-- lookup parent from repository -->
</parent> </parent>
<groupId>org.eclipse.openk</groupId> <groupId>org.eclipse.openk</groupId>
<artifactId>grid-failure-information.backend.service</artifactId> <artifactId>grid-failure-information.addressimport.service</artifactId>
<version>0.0.1-SNAPSHOT</version> <version>0.0.1-SNAPSHOT</version>
<name>grid-failure-information</name> <name>grid-failure-information</name>
<description>Grid-Failure-Information Project for openKONSEQUENZ</description> <description>Address Import of Grid-Failure-Information Project for openKONSEQUENZ</description>
<properties> <properties>
<maven.test.skip>false</maven.test.skip> <maven.test.skip>false</maven.test.skip>
......
/*
*******************************************************************************
* 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.importadresses.exceptions;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;
@ResponseStatus(code = HttpStatus.INTERNAL_SERVER_ERROR)
public class InternalServerErrorException extends RuntimeException {
public InternalServerErrorException(String message) {
super(message);
}
}
...@@ -42,7 +42,7 @@ public class AddressImportService { ...@@ -42,7 +42,7 @@ public class AddressImportService {
@Value("${adressimport.file.power-stations}") @Value("${adressimport.file.power-stations}")
public String filePowerStations; public String filePowerStations;
public void importAddresses(Boolean cleanUp) { public void importAddresses(boolean cleanUp) {
if (cleanUp) { if (cleanUp) {
log.info("CleanUp START"); log.info("CleanUp START");
...@@ -83,9 +83,9 @@ public class AddressImportService { ...@@ -83,9 +83,9 @@ public class AddressImportService {
while ((line = reader.readNext()) != null) { while ((line = reader.readNext()) != null) {
AddressDto addressDto = new AddressDto(); AddressDto addressDto = new AddressDto();
// !!! Parsing BigDecimal anpassen, locale // !!! Parsing BigDecimal anpassen, locale
BigDecimal x1 = new BigDecimal(line[0].replaceAll(",", ".")); BigDecimal x1 = new BigDecimal(line[0].replace(",", "."));
addressDto.setSdox1(new BigDecimal(line[0].replaceAll(",", "."))); addressDto.setSdox1(new BigDecimal(line[0].replace(",", ".")));
addressDto.setSdoy1(new BigDecimal(line[1].replaceAll(",", "."))); addressDto.setSdoy1(new BigDecimal(line[1].replace(",", ".")));
addressDto.setG3efid(Long.parseLong(line[2])); addressDto.setG3efid(Long.parseLong(line[2]));
addressDto.setPostcode(line[3]); addressDto.setPostcode(line[3]);
addressDto.setCommunity(line[4]); addressDto.setCommunity(line[4]);
...@@ -139,9 +139,9 @@ public class AddressImportService { ...@@ -139,9 +139,9 @@ public class AddressImportService {
while ((line = reader.readNext()) != null) { while ((line = reader.readNext()) != null) {
AddressDto addressDto = new AddressDto(); AddressDto addressDto = new AddressDto();
// !!! Parsing BigDecimal anpassen, locale??? // !!! Parsing BigDecimal anpassen, locale???
BigDecimal x1 = new BigDecimal(line[0].replaceAll(",", ".")); BigDecimal x1 = new BigDecimal(line[0].replace(",", "."));
addressDto.setSdox1(new BigDecimal(line[0].replaceAll(",", "."))); addressDto.setSdox1(new BigDecimal(line[0].replace(",", ".")));
addressDto.setSdoy1(new BigDecimal(line[1].replaceAll(",", "."))); addressDto.setSdoy1(new BigDecimal(line[1].replace(",", ".")));
addressDto.setG3efid(Long.parseLong(line[2])); addressDto.setG3efid(Long.parseLong(line[2]));
addressDto.setPostcode(line[3]); addressDto.setPostcode(line[3]);
addressDto.setCommunity(line[4]); addressDto.setCommunity(line[4]);
...@@ -180,9 +180,9 @@ public class AddressImportService { ...@@ -180,9 +180,9 @@ public class AddressImportService {
while ((line = reader.readNext()) != null) { while ((line = reader.readNext()) != null) {
AddressDto addressDto = new AddressDto(); AddressDto addressDto = new AddressDto();
// !!! Parsing BigDecimal anpassen, locale??? // !!! Parsing BigDecimal anpassen, locale???
BigDecimal x1 = new BigDecimal(line[0].replaceAll(",", ".")); BigDecimal x1 = new BigDecimal(line[0].replace(",", "."));
addressDto.setSdox1(new BigDecimal(line[0].replaceAll(",", "."))); addressDto.setSdox1(new BigDecimal(line[0].replace(",", ".")));
addressDto.setSdoy1(new BigDecimal(line[1].replaceAll(",", "."))); addressDto.setSdoy1(new BigDecimal(line[1].replace(",", ".")));
addressDto.setG3efid(Long.parseLong(line[2])); addressDto.setG3efid(Long.parseLong(line[2]));
addressDto.setPostcode(line[3]); addressDto.setPostcode(line[3]);
addressDto.setCommunity(line[4]); addressDto.setCommunity(line[4]);
...@@ -220,9 +220,9 @@ public class AddressImportService { ...@@ -220,9 +220,9 @@ public class AddressImportService {
while ((line = reader.readNext()) != null) { while ((line = reader.readNext()) != null) {
AddressDto addressDto = new AddressDto(); AddressDto addressDto = new AddressDto();
// !!! Parsing BigDecimal anpassen, locale??? // !!! Parsing BigDecimal anpassen, locale???
BigDecimal x1 = new BigDecimal(line[0].replaceAll(",", ".")); BigDecimal x1 = new BigDecimal(line[0].replace(",", "."));
addressDto.setSdox1(new BigDecimal(line[0].replaceAll(",", "."))); addressDto.setSdox1(new BigDecimal(line[0].replace(",", ".")));
addressDto.setSdoy1(new BigDecimal(line[1].replaceAll(",", "."))); addressDto.setSdoy1(new BigDecimal(line[1].replace(",", ".")));
addressDto.setG3efid(Long.parseLong(line[2])); addressDto.setG3efid(Long.parseLong(line[2]));
addressDto.setPostcode(line[3]); addressDto.setPostcode(line[3]);
addressDto.setCommunity(line[4]); addressDto.setCommunity(line[4]);
...@@ -260,9 +260,9 @@ public class AddressImportService { ...@@ -260,9 +260,9 @@ public class AddressImportService {
while ((line = reader.readNext()) != null) { while ((line = reader.readNext()) != null) {
StationDto stationDto = new StationDto(); StationDto stationDto = new StationDto();
// !!! Parsing BigDecimal anpassen, locale // !!! Parsing BigDecimal anpassen, locale
BigDecimal x1 = new BigDecimal(line[0].replaceAll(",", ".")); BigDecimal x1 = new BigDecimal(line[0].replace(",", "."));
stationDto.setSdox1(new BigDecimal(line[0].replaceAll(",", "."))); stationDto.setSdox1(new BigDecimal(line[0].replace(",", ".")));
stationDto.setSdoy1(new BigDecimal(line[1].replaceAll(",", "."))); stationDto.setSdoy1(new BigDecimal(line[1].replace(",", ".")));
stationDto.setG3efid(Long.parseLong(line[2])); stationDto.setG3efid(Long.parseLong(line[2]));
stationDto.setStationId(line[3]); stationDto.setStationId(line[3]);
stationDto.setStationName(line[4]); stationDto.setStationName(line[4]);
......
...@@ -72,7 +72,7 @@ public class AddressService { ...@@ -72,7 +72,7 @@ public class AddressService {
} catch (Exception ex) { } catch (Exception ex) {
log.info("Adresse [sdo_x1= " + addressDto.getSdox1() log.warn("Adresse [sdo_x1= " + addressDto.getSdox1()
+ ", sdo_y1= " + addressDto.getSdoy1() + ", sdo_y1= " + addressDto.getSdoy1()
+ ", g3e_fid=" + addressDto.getG3efid() + ", g3e_fid=" + addressDto.getG3efid()
+ ", plz=" + addressDto.getPostcode() + ", plz=" + addressDto.getPostcode()
......
...@@ -59,7 +59,7 @@ public class StationService { ...@@ -59,7 +59,7 @@ public class StationService {
} catch (Exception ex) { } catch (Exception ex) {
log.info("Adresse [sdo_x1= " + stationDto.getSdox1() log.warn("Adresse [sdo_x1= " + stationDto.getSdox1()
+ ", sdo_y1= " + stationDto.getSdoy1() + ", sdo_y1= " + stationDto.getSdoy1()
+ ", g3e_fid=" + stationDto.getG3efid() + ", g3e_fid=" + stationDto.getG3efid()
+ ", station_id=" + stationDto.getStationId() + ", station_id=" + stationDto.getStationId()
......
...@@ -5,6 +5,7 @@ import org.springframework.beans.factory.annotation.Value; ...@@ -5,6 +5,7 @@ import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
...@@ -38,10 +39,9 @@ public class UtmConverter { ...@@ -38,10 +39,9 @@ public class UtmConverter {
longitude=Math.round(lon*10000000); longitude=Math.round(lon*10000000);
longitude = longitude/10000000; longitude = longitude/10000000;
List<BigDecimal> decimals = new LinkedList<BigDecimal>(); List<BigDecimal> decimals = new LinkedList<>();
decimals.add(BigDecimal.valueOf(longitude).setScale(6, RoundingMode.HALF_EVEN));
decimals.add(new BigDecimal(longitude).setScale(6, BigDecimal.ROUND_HALF_EVEN)); decimals.add(BigDecimal.valueOf(latitude).setScale(6, RoundingMode.HALF_EVEN));
decimals.add(new BigDecimal(latitude).setScale(6, BigDecimal.ROUND_HALF_EVEN));
return decimals; return decimals;
} }
......
...@@ -25,13 +25,13 @@ utm: ...@@ -25,13 +25,13 @@ utm:
adressimport: adressimport:
cleanup: true cleanup: true
cron: 0 27 9 3 4 FRI cron: 0 0 0 1 * ?
file: file:
addresses: /temp/Importtest/adressen.csv addresses: /usr/src/cbd/temp/import_files/adressen.csv
power-connections: /temp/Importtest/strom_hausanschluss.csv power-connections: /usr/src/cbd/temp/import_files/strom_hausanschluss.csv
water-connections: /temp/Importtest/wasser_hausanschluss.csv water-connections: /usr/src/cbd/temp/import_files/wasser_hausanschluss.csv
gas-connections: /temp/Importtest/gas_hausanschluss.csv gas-connections: /usr/src/cbd/temp/import_files/gas_hausanschluss.csv
power-stations: /temp/Importtest/strom_station.csv power-stations: /usr/src/cbd/temp/import_files/strom_station.csv
jwt: jwt:
tokenHeader: Authorization tokenHeader: Authorization
......
...@@ -24,11 +24,11 @@ adressimport: ...@@ -24,11 +24,11 @@ adressimport:
cleanup: true cleanup: true
cron: 0 27 9 3 4 FRI cron: 0 27 9 3 4 FRI
file: file:
addresses: /temp/Importtest/adressen.csv addresses: C:/temp/import_files/adressen.csv
power-connections: /temp/Importtest/strom_hausanschluss.csv power-connections: C:/temp/import_files/strom_hausanschluss.csv
water-connections: /temp/Importtest/wasser_hausanschluss.csv water-connections: C:/temp/import_files/wasser_hausanschluss.csv
gas-connections: /temp/Importtest/gas_hausanschluss.csv gas-connections: C:/temp/import_files/gas_hausanschluss.csv
power-stations: /temp/Importtest/strom_station.csv power-stations: C:/temp/import_files/strom_station.csv
eureka: eureka:
client: client:
......
...@@ -27,13 +27,13 @@ import org.springframework.boot.autoconfigure.domain.EntityScan; ...@@ -27,13 +27,13 @@ import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.test.context.ConfigFileApplicationContextInitializer; import org.springframework.boot.test.context.ConfigFileApplicationContextInitializer;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories; import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestPropertySource;
@EnableJpaRepositories(basePackages = "org.eclipse.openk.gridfailureinformation") @EnableJpaRepositories(basePackages = "org.eclipse.openk.gridfailureinformation")
@EntityScan(basePackageClasses = AddressImportApplication.class) @EntityScan(basePackageClasses = AddressImportApplication.class)
@ContextConfiguration( initializers = {ConfigFileApplicationContextInitializer.class}) @ContextConfiguration( initializers = {ConfigFileApplicationContextInitializer.class})
@TestPropertySource("spring.config.location=classpath:application.yml") @ActiveProfiles("test")
public class TestConfiguration { public class TestConfiguration {
@Bean @Bean
AddressMapper addressMapper() { return new AddressMapperImpl(); } AddressMapper addressMapper() { return new AddressMapperImpl(); }
......
...@@ -15,18 +15,25 @@ ...@@ -15,18 +15,25 @@
package org.eclipse.openk.gridfailureinformation.importadresses.service; package org.eclipse.openk.gridfailureinformation.importadresses.service;
import org.eclipse.openk.gridfailureinformation.importadresses.config.TestConfiguration; import org.eclipse.openk.gridfailureinformation.importadresses.config.TestConfiguration;
import org.eclipse.openk.gridfailureinformation.importadresses.mapper.AddressMapper;
import org.eclipse.openk.gridfailureinformation.importadresses.model.TblAddress;
import org.eclipse.openk.gridfailureinformation.importadresses.repository.AddressRepository; import org.eclipse.openk.gridfailureinformation.importadresses.repository.AddressRepository;
import org.eclipse.openk.gridfailureinformation.importadresses.support.MockDataHelper;
import org.eclipse.openk.gridfailureinformation.importadresses.viewmodel.AddressDto;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.mockito.stubbing.Answer;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.ContextConfiguration;
import static org.mockito.Mockito.times; import java.util.Optional;
import static org.mockito.Mockito.verify;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;
//@RunWith(SpringRunner.class)
@DataJpaTest @DataJpaTest
@ContextConfiguration(classes = {TestConfiguration.class}) @ContextConfiguration(classes = {TestConfiguration.class})
public class AddressServiceTest { public class AddressServiceTest {
...@@ -34,6 +41,9 @@ public class AddressServiceTest { ...@@ -34,6 +41,9 @@ public class AddressServiceTest {
@Autowired @Autowired
private AddressService addressService; private AddressService addressService;
@Autowired
private AddressMapper addressMapper;
@MockBean @MockBean
AddressRepository addressRepository; AddressRepository addressRepository;
...@@ -42,4 +52,64 @@ public class AddressServiceTest { ...@@ -42,4 +52,64 @@ public class AddressServiceTest {
addressService.deleteAllAddresses(); addressService.deleteAllAddresses();
verify(addressRepository, times(1)).deleteAll(); verify(addressRepository, times(1)).deleteAll();
} }
@Test
public void shouldInsertAdressesProperly() {
AddressDto addressDto = MockDataHelper.mockAddressDto(1);
addressDto.setUuid(null);
when(addressRepository.findByG3efid(any(Long.class))).thenReturn(Optional.empty());
when(addressRepository.save(any(TblAddress.class)))
.then((Answer<TblAddress>) invocation -> {
Object[] args = invocation.getArguments();
return (TblAddress) args[0];
});
AddressDto addressDtoSaved = addressService.updateOrInsertAddressByG3efid(addressDto);
assertEquals(addressDtoSaved.getCommunity(), addressDto.getCommunity());
assertEquals(addressDtoSaved.getLatitude(), addressDto.getLatitude());
assertEquals(addressDtoSaved.getLongitude(), addressDto.getLongitude());
assertNotNull(addressDtoSaved.getUuid());
}
@Test
public void shouldUpdateAdressesProperly() {
AddressDto addressDto = MockDataHelper.mockAddressDto(1);
addressDto.setPowerConnection(true);
addressDto.setWaterConnection(true);
addressDto.setGasConnection(true);
TblAddress tblAddress = addressMapper.toTableAddress(addressDto);
when(addressRepository.findByG3efid(any(Long.class))).thenReturn(Optional.of(tblAddress));
when(addressRepository.save(any(TblAddress.class)))
.then((Answer<TblAddress>) invocation -> {
Object[] args = invocation.getArguments();
return (TblAddress) args[0];
});
AddressDto addressDtoSaved = addressService.updateOrInsertAddressByG3efid(addressDto);
assertEquals(addressDtoSaved.getCommunity(), addressDto.getCommunity());
assertEquals(addressDtoSaved.getLatitude(), addressDto.getLatitude());
assertEquals(addressDtoSaved.getLongitude(), addressDto.getLongitude());
assertTrue(addressDtoSaved.isPowerConnection());
assertTrue(addressDtoSaved.isGasConnection());
assertTrue(addressDtoSaved.isWaterConnection());
}
@Test
public void shouldThrowErrorOnUpdateAdresses() {
AddressDto addressDto = MockDataHelper.mockAddressDto(1);
TblAddress tblAddress = addressMapper.toTableAddress(addressDto);
when(addressRepository.findByG3efid(any(Long.class))).thenReturn(Optional.of(tblAddress));
when(addressRepository.save(any(TblAddress.class)))
.thenThrow(new IllegalArgumentException());
AddressDto addressDtoSaved = addressService.updateOrInsertAddressByG3efid(addressDto);
assertNotNull(addressDtoSaved);
}
} }
/*
*******************************************************************************
* 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.importadresses.service;
import org.eclipse.openk.gridfailureinformation.importadresses.config.TestConfiguration;
import org.eclipse.openk.gridfailureinformation.importadresses.viewmodel.AddressDto;
import org.eclipse.openk.gridfailureinformation.importadresses.viewmodel.StationDto;
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.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
@DataJpaTest
@ContextConfiguration(classes = {TestConfiguration.class})
@ActiveProfiles("test")
public class AdressImportServiceTest {
@Qualifier("myAddressService")
@MockBean
private AddressService addressService;
@Qualifier("myStationService")
@MockBean
private StationService stationService;
@Qualifier("myAddressImportService")
@Autowired
private AddressImportService addressImportService;
@Test
public void shouldImportAndInsertProperly() {
addressImportService.importAddresses(true);
verify(addressService, times(1)).deleteAllAddresses();
verify(stationService, times(1)).deleteAllStations();
verify(addressService, times(3)).insertAddress(any(AddressDto.class));
verify(stationService, times(3)).insertStation(any(StationDto.class));
verify(addressService, times(9)).updateOrInsertAddressByG3efid(any(AddressDto.class));
}
@Test
public void shouldImportAndInsertProperlyWithoutClean() {
addressImportService.importAddresses(false);
verify(addressService, times(0)).deleteAllAddresses();
verify(stationService, times(0)).deleteAllStations();
verify(stationService, times(3)).updateOrInsertStationByG3efid(any(StationDto.class));
verify(addressService, times(12)).updateOrInsertAddressByG3efid(any(AddressDto.class));
}
}
/*
*******************************************************************************
* 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.importadresses.service;
import org.eclipse.openk.gridfailureinformation.importadresses.config.TestConfiguration;
import org.eclipse.openk.gridfailureinformation.importadresses.mapper.StationMapper;
import org.eclipse.openk.gridfailureinformation.importadresses.model.TblStation;
import org.eclipse.openk.gridfailureinformation.importadresses.repository.StationRepository;
import org.eclipse.openk.gridfailureinformation.importadresses.support.MockDataHelper;
import org.eclipse.openk.gridfailureinformation.importadresses.viewmodel.StationDto;
import org.junit.jupiter.api.Test;
import org.mockito.stubbing.Answer;
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.Optional;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;
@DataJpaTest
@ContextConfiguration(classes = {TestConfiguration.class})
public class StationServiceTest {
@Qualifier("myStationService")
@Autowired
private StationService stationService;
@Autowired
private StationMapper stationMapper;
@MockBean
StationRepository stationRepository;
@Test
public void shouldDeleteAllStationsProperly() {
stationService.deleteAllStations();
verify(stationRepository, times(1)).deleteAll();
}
@Test