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 @@
<relativePath/> <!-- lookup parent from repository -->
</parent>
<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>
<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>
<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 {
@Value("${adressimport.file.power-stations}")
public String filePowerStations;
public void importAddresses(Boolean cleanUp) {
public void importAddresses(boolean cleanUp) {
if (cleanUp) {
log.info("CleanUp START");
......@@ -83,9 +83,9 @@ public class AddressImportService {
while ((line = reader.readNext()) != null) {
AddressDto addressDto = new AddressDto();
// !!! Parsing BigDecimal anpassen, locale
BigDecimal x1 = new BigDecimal(line[0].replaceAll(",", "."));
addressDto.setSdox1(new BigDecimal(line[0].replaceAll(",", ".")));
addressDto.setSdoy1(new BigDecimal(line[1].replaceAll(",", ".")));
BigDecimal x1 = new BigDecimal(line[0].replace(",", "."));
addressDto.setSdox1(new BigDecimal(line[0].replace(",", ".")));
addressDto.setSdoy1(new BigDecimal(line[1].replace(",", ".")));
addressDto.setG3efid(Long.parseLong(line[2]));
addressDto.setPostcode(line[3]);
addressDto.setCommunity(line[4]);
......@@ -139,9 +139,9 @@ public class AddressImportService {
while ((line = reader.readNext()) != null) {
AddressDto addressDto = new AddressDto();
// !!! Parsing BigDecimal anpassen, locale???
BigDecimal x1 = new BigDecimal(line[0].replaceAll(",", "."));
addressDto.setSdox1(new BigDecimal(line[0].replaceAll(",", ".")));
addressDto.setSdoy1(new BigDecimal(line[1].replaceAll(",", ".")));
BigDecimal x1 = new BigDecimal(line[0].replace(",", "."));
addressDto.setSdox1(new BigDecimal(line[0].replace(",", ".")));
addressDto.setSdoy1(new BigDecimal(line[1].replace(",", ".")));
addressDto.setG3efid(Long.parseLong(line[2]));
addressDto.setPostcode(line[3]);
addressDto.setCommunity(line[4]);
......@@ -180,9 +180,9 @@ public class AddressImportService {
while ((line = reader.readNext()) != null) {
AddressDto addressDto = new AddressDto();
// !!! Parsing BigDecimal anpassen, locale???
BigDecimal x1 = new BigDecimal(line[0].replaceAll(",", "."));
addressDto.setSdox1(new BigDecimal(line[0].replaceAll(",", ".")));
addressDto.setSdoy1(new BigDecimal(line[1].replaceAll(",", ".")));
BigDecimal x1 = new BigDecimal(line[0].replace(",", "."));
addressDto.setSdox1(new BigDecimal(line[0].replace(",", ".")));
addressDto.setSdoy1(new BigDecimal(line[1].replace(",", ".")));
addressDto.setG3efid(Long.parseLong(line[2]));
addressDto.setPostcode(line[3]);
addressDto.setCommunity(line[4]);
......@@ -220,9 +220,9 @@ public class AddressImportService {
while ((line = reader.readNext()) != null) {
AddressDto addressDto = new AddressDto();
// !!! Parsing BigDecimal anpassen, locale???
BigDecimal x1 = new BigDecimal(line[0].replaceAll(",", "."));
addressDto.setSdox1(new BigDecimal(line[0].replaceAll(",", ".")));
addressDto.setSdoy1(new BigDecimal(line[1].replaceAll(",", ".")));
BigDecimal x1 = new BigDecimal(line[0].replace(",", "."));
addressDto.setSdox1(new BigDecimal(line[0].replace(",", ".")));
addressDto.setSdoy1(new BigDecimal(line[1].replace(",", ".")));
addressDto.setG3efid(Long.parseLong(line[2]));
addressDto.setPostcode(line[3]);
addressDto.setCommunity(line[4]);
......@@ -260,9 +260,9 @@ public class AddressImportService {
while ((line = reader.readNext()) != null) {
StationDto stationDto = new StationDto();
// !!! Parsing BigDecimal anpassen, locale
BigDecimal x1 = new BigDecimal(line[0].replaceAll(",", "."));
stationDto.setSdox1(new BigDecimal(line[0].replaceAll(",", ".")));
stationDto.setSdoy1(new BigDecimal(line[1].replaceAll(",", ".")));
BigDecimal x1 = new BigDecimal(line[0].replace(",", "."));
stationDto.setSdox1(new BigDecimal(line[0].replace(",", ".")));
stationDto.setSdoy1(new BigDecimal(line[1].replace(",", ".")));
stationDto.setG3efid(Long.parseLong(line[2]));
stationDto.setStationId(line[3]);
stationDto.setStationName(line[4]);
......
......@@ -72,7 +72,7 @@ public class AddressService {
} catch (Exception ex) {
log.info("Adresse [sdo_x1= " + addressDto.getSdox1()
log.warn("Adresse [sdo_x1= " + addressDto.getSdox1()
+ ", sdo_y1= " + addressDto.getSdoy1()
+ ", g3e_fid=" + addressDto.getG3efid()
+ ", plz=" + addressDto.getPostcode()
......
......@@ -59,7 +59,7 @@ public class StationService {
} catch (Exception ex) {
log.info("Adresse [sdo_x1= " + stationDto.getSdox1()
log.warn("Adresse [sdo_x1= " + stationDto.getSdox1()
+ ", sdo_y1= " + stationDto.getSdoy1()
+ ", g3e_fid=" + stationDto.getG3efid()
+ ", station_id=" + stationDto.getStationId()
......
......@@ -5,6 +5,7 @@ import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.LinkedList;
import java.util.List;
......@@ -38,10 +39,9 @@ public class UtmConverter {
longitude=Math.round(lon*10000000);
longitude = longitude/10000000;
List<BigDecimal> decimals = new LinkedList<BigDecimal>();
decimals.add(new BigDecimal(longitude).setScale(6, BigDecimal.ROUND_HALF_EVEN));
decimals.add(new BigDecimal(latitude).setScale(6, BigDecimal.ROUND_HALF_EVEN));
List<BigDecimal> decimals = new LinkedList<>();
decimals.add(BigDecimal.valueOf(longitude).setScale(6, RoundingMode.HALF_EVEN));
decimals.add(BigDecimal.valueOf(latitude).setScale(6, RoundingMode.HALF_EVEN));
return decimals;
}
......
......@@ -25,13 +25,13 @@ utm:
adressimport:
cleanup: true
cron: 0 27 9 3 4 FRI
cron: 0 0 0 1 * ?
file:
addresses: /temp/Importtest/adressen.csv
power-connections: /temp/Importtest/strom_hausanschluss.csv
water-connections: /temp/Importtest/wasser_hausanschluss.csv
gas-connections: /temp/Importtest/gas_hausanschluss.csv
power-stations: /temp/Importtest/strom_station.csv
addresses: /usr/src/cbd/temp/import_files/adressen.csv
power-connections: /usr/src/cbd/temp/import_files/strom_hausanschluss.csv
water-connections: /usr/src/cbd/temp/import_files/wasser_hausanschluss.csv
gas-connections: /usr/src/cbd/temp/import_files/gas_hausanschluss.csv
power-stations: /usr/src/cbd/temp/import_files/strom_station.csv
jwt:
tokenHeader: Authorization
......
......@@ -24,11 +24,11 @@ adressimport:
cleanup: true
cron: 0 27 9 3 4 FRI
file:
addresses: /temp/Importtest/adressen.csv
power-connections: /temp/Importtest/strom_hausanschluss.csv
water-connections: /temp/Importtest/wasser_hausanschluss.csv
gas-connections: /temp/Importtest/gas_hausanschluss.csv
power-stations: /temp/Importtest/strom_station.csv
addresses: C:/temp/import_files/adressen.csv
power-connections: C:/temp/import_files/strom_hausanschluss.csv
water-connections: C:/temp/import_files/wasser_hausanschluss.csv
gas-connections: C:/temp/import_files/gas_hausanschluss.csv
power-stations: C:/temp/import_files/strom_station.csv
eureka:
client:
......
......@@ -27,13 +27,13 @@ import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.test.context.ConfigFileApplicationContextInitializer;
import org.springframework.context.annotation.Bean;
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.TestPropertySource;
@EnableJpaRepositories(basePackages = "org.eclipse.openk.gridfailureinformation")
@EntityScan(basePackageClasses = AddressImportApplication.class)
@ContextConfiguration( initializers = {ConfigFileApplicationContextInitializer.class})
@TestPropertySource("spring.config.location=classpath:application.yml")
@ActiveProfiles("test")
public class TestConfiguration {
@Bean
AddressMapper addressMapper() { return new AddressMapperImpl(); }
......
......@@ -15,18 +15,25 @@
package org.eclipse.openk.gridfailureinformation.importadresses.service;
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.support.MockDataHelper;
import org.eclipse.openk.gridfailureinformation.importadresses.viewmodel.AddressDto;
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 static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import java.util.Optional;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;
//@RunWith(SpringRunner.class)
@DataJpaTest
@ContextConfiguration(classes = {TestConfiguration.class})
public class AddressServiceTest {
......@@ -34,6 +41,9 @@ public class AddressServiceTest {
@Autowired
private AddressService addressService;
@Autowired
private AddressMapper addressMapper;
@MockBean
AddressRepository addressRepository;
......@@ -42,4 +52,64 @@ public class AddressServiceTest {
addressService.deleteAllAddresses();
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
public void shouldInsertStationProperly() {
StationDto stationDto = MockDataHelper.mockStationDto(1);
stationDto.setUuid(null);
when(stationRepository.findByG3efid(any(Long.class))).thenReturn(Optional.empty());
when(stationRepository.save(any(TblStation.class)))
.then((Answer<TblStation>) invocation -> {
Object[] args = invocation.getArguments();
return (TblStation) args[0];
});
StationDto stationDtoSaved = stationService.updateOrInsertStationByG3efid(stationDto);
assertEquals(stationDtoSaved.getLatitude(), stationDto.getLatitude());
assertEquals(stationDtoSaved.getLongitude(), stationDto.getLongitude());
assertNotNull(stationDtoSaved.getUuid());
}
@Test
public void shouldUpdateAdressesProperly() {
StationDto stationDto = MockDataHelper.mockStationDto(1);
TblStation tblStation = stationMapper.toTableStation(stationDto);
when(stationRepository.findByG3efid(any(Long.class))).thenReturn(Optional.of(tblStation));
when(stationRepository.save(any(TblStation.class)))
.then((Answer<TblStation>) invocation -> {
Object[] args = invocation.getArguments();
return (TblStation) args[0];
});
StationDto stationDtoSaved = stationService.updateOrInsertStationByG3efid(stationDto);
assertEquals(stationDtoSaved.getLatitude(), stationDto.getLatitude());
assertEquals(stationDtoSaved.getLongitude(), stationDto.getLongitude());
}
@Test
public void shouldThrowErrorOnUpdateAdresses() {
StationDto stationDto = MockDataHelper.mockStationDto(1);
TblStation tblStation = stationMapper.toTableStation(stationDto);
when(stationRepository.findByG3efid(any(Long.class))).thenReturn(Optional.of(tblStation));
when(stationRepository.save(any(TblStation.class)))
.thenThrow(new IllegalArgumentException());
StationDto stationDtoSaved = stationService.updateOrInsertStationByG3efid(stationDto);
assertNotNull(stationDtoSaved);
}
}
/*
*******************************************************************************
* 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.support;
import org.eclipse.openk.gridfailureinformation.importadresses.viewmodel.AddressDto;
import org.eclipse.openk.gridfailureinformation.importadresses.viewmodel.StationDto;
import java.math.BigDecimal;
import java.util.UUID;
public class MockDataHelper {
private MockDataHelper() {}
public static AddressDto mockAddressDto(int paramToChangeTestObject) {
AddressDto addressDto = new AddressDto();
addressDto.setUuid(UUID.randomUUID());
addressDto.setLatitude(new BigDecimal("12"));
addressDto.setLongitude(new BigDecimal("33"));
addressDto.setG3efid(1L);
addressDto.setDistrict("District_" + paramToChangeTestObject);
addressDto.setCommunity("Community_" + paramToChangeTestObject);
addressDto.setHousenumber("15");
addressDto.setPostcode("51132");
addressDto.setSdox1(new BigDecimal("1"