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

SI-442 Unit Tests

parent e46367e5
package org.eclipse.openk.gridfailureinformation.importadresses;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@EnableSwagger2
@SpringBootApplication
public class AddressImportApplication {
public static void main(String[] args) {
SpringApplication.run(AddressImportApplication.class, args);
}
}
package org.eclipse.openk.gridfailureinformation.importadresses.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.importadresses.service.AddressImportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@Log4j2
@RestController
@RequestMapping("/addresses")
public class AdressImportController {
@Autowired
private AddressImportService addressImportService;
@PostMapping
@ApiOperation(value = "Import von Adressen und Stromstationen")
@ApiResponses(value = {
@ApiResponse(code = 201, message = "Adressen erfolgreich importiert"),
@ApiResponse(code = 500, message = "Konnte nicht durchgeführt werden")
})
@RequestMapping("/import")
public void startImportAddresses(
@RequestParam( value="cleanup", required = false, defaultValue = "true") boolean cleanup ) {
addressImportService.importAddresses(cleanup);
}
}
\ No newline at end of file
package org.eclipse.openk.gridfailureinformation.importadresses.jobs;
import lombok.extern.log4j.Log4j2;
import org.eclipse.openk.gridfailureinformation.importadresses.service.AddressImportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@EnableScheduling
@Component
@Log4j2
public class JobManager {
@Value("${adressimport.cleanup}")
public boolean cleanUp;
@Autowired
AddressImportService addressImportService;
// job for impoerting address and station data
// @Scheduled(initialDelay = 1000, fixedRate = 900000000)
// public void importAddressdata() {
// boolean cleanUp = true;
// addressImportService.importAddresses(true);
// }
//
// }
@Scheduled(cron= "${adressimport.cron}")
public void importAddressdata() {
addressImportService.importAddresses(cleanUp);
}
}
/*
*******************************************************************************
* 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.mapper;
import org.eclipse.openk.gridfailureinformation.importadresses.model.TblAddress;
import org.eclipse.openk.gridfailureinformation.importadresses.viewmodel.AddressDto;
import org.mapstruct.Mapper;
import org.mapstruct.ReportingPolicy;
@Mapper(componentModel = "spring", unmappedTargetPolicy = ReportingPolicy.IGNORE)
public interface AddressMapper {
AddressDto toAddressDto(TblAddress tblAddress);
TblAddress toTableAddress(AddressDto addressDto);
}
/*
*******************************************************************************
* 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.mapper;
import org.eclipse.openk.gridfailureinformation.importadresses.model.TblStation;
import org.eclipse.openk.gridfailureinformation.importadresses.viewmodel.StationDto;
import org.mapstruct.Mapper;
import org.mapstruct.ReportingPolicy;
@Mapper(componentModel = "spring", unmappedTargetPolicy = ReportingPolicy.IGNORE)
public interface StationMapper {
StationDto toStationDto(TblStation tblStation);
TblStation toTableStation(StationDto stationDto);
}
/*
*******************************************************************************
* 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.model;
import lombok.Data;
import javax.persistence.*;
import java.math.BigDecimal;
import java.util.UUID;
@Data
@Entity
public class TblAddress {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "tbl_address_id_seq")
@SequenceGenerator(name = "tbl_address_id_seq", sequenceName = "tbl_address_id_seq", allocationSize = 1)
@Column(name = "id", updatable = false)
private Long id;
private UUID uuid;
private BigDecimal sdox1;
private BigDecimal sdoy1;
private Long g3efid;
private String postcode;
private String community;
private String district;
private String street;
private String housenumber;
private boolean waterConnection;
private String waterGroup;
private boolean gasConnection;
private String gasGroup;
private boolean powerConnection;
private String stationId;
private BigDecimal longitude;
private BigDecimal latitude;
}
/*
*******************************************************************************
* 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.model;
import lombok.Data;
import javax.persistence.*;
import java.math.BigDecimal;
import java.util.UUID;
@Data
@Entity
public class TblStation {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "tbl_station_id_seq")
@SequenceGenerator(name = "tbl_station_id_seq", sequenceName = "tbl_station_id_seq", allocationSize = 1)
@Column(name = "id", updatable = false)
private Long id;
private UUID uuid;
private BigDecimal sdox1;
private BigDecimal sdoy1;
private Long g3efid;
private String stationId;
private String stationName;
private BigDecimal longitude;
private BigDecimal latitude;
}
/*
*******************************************************************************
* 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.repository;
import org.eclipse.openk.gridfailureinformation.importadresses.model.TblAddress;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import java.util.Optional;
@Repository
public interface AddressRepository extends JpaRepository<TblAddress, Long > {
Optional<TblAddress> findByG3efid(Long g3efid);
}
/*
*******************************************************************************
* 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.repository;
import org.eclipse.openk.gridfailureinformation.importadresses.model.TblStation;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import java.util.List;
import java.util.Optional;
@Repository
public interface StationRepository extends JpaRepository<TblStation, Long > {
List<TblStation> findAll();
Optional<TblStation> findByG3efid(Long g3efid);
}
package org.eclipse.openk.gridfailureinformation.importadresses.service;
import com.opencsv.CSVParser;
import com.opencsv.CSVParserBuilder;
import com.opencsv.CSVReader;
import com.opencsv.CSVReaderBuilder;
import com.opencsv.exceptions.CsvValidationException;
import lombok.extern.log4j.Log4j2;
import org.eclipse.openk.gridfailureinformation.importadresses.util.UtmConverter;
import org.eclipse.openk.gridfailureinformation.importadresses.viewmodel.AddressDto;
import org.eclipse.openk.gridfailureinformation.importadresses.viewmodel.StationDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.io.FileReader;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;
@Service
@Log4j2
public class AddressImportService {
@Autowired
private AddressService addressService;
@Autowired
private StationService stationService;
@Autowired
private UtmConverter converter;
@Value("${adressimport.file.addresses}")
public String fileAddresses;
@Value("${adressimport.file.power-connections}")
public String filePowerConnections;
@Value("${adressimport.file.water-connections}")
public String fileWaterConnections;
@Value("${adressimport.file.gas-connections}")
public String fileGasConnections;
@Value("${adressimport.file.power-stations}")
public String filePowerStations;
public void importAddresses(Boolean cleanUp) {
if (cleanUp) {
log.info("CleanUp START");
addressService.deleteAllAddresses();
stationService.deleteAllStations();
log.info("CleanUp ENDE");
}
log.info("Adressimport START");
importAddressFile(cleanUp);
log.info("Import Power Connections START");
importPowerConnectionsAddressFile();
log.info("Import Water Connections START");
importWaterConnectionsAddressFile();
log.info("Import Gas Connections START");
importGasConnectionsAddressFile();
log.info("Adressimport ENDE");
log.info("Import Power Stations START");
importPowerStationFile(cleanUp);
log.info("Stationsimport ENDE");
}
public void importAddressFile(boolean isTableClean) {
String csvFile = fileAddresses;
CSVReader reader = null;
try {final CSVParser parser = new CSVParserBuilder()
.withSeparator(';')
.withIgnoreQuotations(true)
.build();
reader = new CSVReaderBuilder(new FileReader(csvFile))
.withSkipLines(1)
.withCSVParser(parser)
.build();
String[]line;
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(",", ".")));
addressDto.setG3efid(Long.parseLong(line[2]));
addressDto.setPostcode(line[3]);
addressDto.setCommunity(line[4]);
addressDto.setDistrict(line[5]);
addressDto.setStreet(line[6]);
addressDto.setHousenumber(line[7]);
List<BigDecimal> decimals = converter.convertUTMToDeg(addressDto.getSdox1().doubleValue(), addressDto.getSdoy1().doubleValue());
addressDto.setLongitude(decimals.get(0));
addressDto.setLatitude(decimals.get(1));
AddressDto savedAddressDto;
if (isTableClean) {
savedAddressDto = addressService.insertAddress(addressDto);
} else {
savedAddressDto = addressService.updateOrInsertAddressByG3efid(addressDto);
}
/* System.out.println("Adresse [sdo_x1= " + line[0]
+ ", sdo_y1= " + line[1]
+ ", g3e_fid=" + line[2]
+ ", long= " + addressDto.getLongitude()
+ ", lat= " + addressDto.getLatitude()
+ ", plz=" + line[3]
+ ", ort=" + line[4]
+ ", ortsteil=" + line[5]
+ ", strasse=" + line[6]
+ ", hausnummer=" + line[7]
+ "]");*/
}
} catch (IOException | CsvValidationException e) {
e.printStackTrace();
}
}
public void importPowerConnectionsAddressFile() {
String csvFile = filePowerConnections;
CSVReader reader = null;
try {final CSVParser parser = new CSVParserBuilder()
.withSeparator(';')
.withIgnoreQuotations(true)
.build();
reader = new CSVReaderBuilder(new FileReader(csvFile))
.withSkipLines(1)
.withCSVParser(parser)
.build();
String[]line;
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(",", ".")));
addressDto.setG3efid(Long.parseLong(line[2]));
addressDto.setPostcode(line[3]);
addressDto.setCommunity(line[4]);
addressDto.setDistrict(line[5]);
addressDto.setStreet(line[6]);
addressDto.setHousenumber(line[7]);
addressDto.setPowerConnection(true);
addressDto.setStationId(line[8]);
List<BigDecimal> decimals = converter.convertUTMToDeg( addressDto.getSdox1().doubleValue(), addressDto.getSdoy1().doubleValue());
addressDto.setLongitude(decimals.get(0));
addressDto.setLatitude(decimals.get(1));
AddressDto savedAddressDto = addressService.updateOrInsertAddressByG3efid(addressDto);
}
} catch (IOException | CsvValidationException e) {
e.printStackTrace();
}
}
public void importWaterConnectionsAddressFile() {
String csvFile = fileWaterConnections;
CSVReader reader = null;
try {final CSVParser parser = new CSVParserBuilder()
.withSeparator(';')
.withIgnoreQuotations(true)
.build();
reader = new CSVReaderBuilder(new FileReader(csvFile))
.withSkipLines(1)
.withCSVParser(parser)
.build();
String[]line;
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(",", ".")));
addressDto.setG3efid(Long.parseLong(line[2]));
addressDto.setPostcode(line[3]);
addressDto.setCommunity(line[4]);
addressDto.setDistrict(line[5]);
addressDto.setStreet(line[6]);
addressDto.setHousenumber(line[7]);
addressDto.setWaterConnection(true);
addressDto.setWaterGroup(line[8]);
List<BigDecimal> decimals = converter.convertUTMToDeg( addressDto.getSdox1().doubleValue(), addressDto.getSdoy1().doubleValue());
addressDto.setLongitude(decimals.get(0));
addressDto.setLatitude(decimals.get(1));
AddressDto savedAddressDto = addressService.updateOrInsertAddressByG3efid(addressDto);
}
} catch (IOException | CsvValidationException e) {
e.printStackTrace();
}
}
public void importGasConnectionsAddressFile() {
String csvFile = fileGasConnections;
CSVReader reader = null;
try {final CSVParser parser = new CSVParserBuilder()
.withSeparator(';')
.withIgnoreQuotations(true)
.build();
reader = new CSVReaderBuilder(new FileReader(csvFile))
.withSkipLines(1)
.withCSVParser(parser)
.build();
String[]line;
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(",", ".")));
addressDto.setG3efid(Long.parseLong(line[2]));
addressDto.setPostcode(line[3]);
addressDto.setCommunity(line[4]);
addressDto.setDistrict(line[5]);
addressDto.setStreet(line[6]);
addressDto.setHousenumber(line[7]);
addressDto.setGasConnection(true);
addressDto.setGasGroup(line[8]);
List<BigDecimal> decimals = converter.convertUTMToDeg(addressDto.getSdox1().doubleValue(), addressDto.getSdoy1().doubleValue());
addressDto.setLongitude(decimals.get(0));
addressDto.setLatitude(decimals.get(1));
AddressDto savedAddressDto = addressService.updateOrInsertAddressByG3efid(addressDto);
}
} catch (IOException | CsvValidationException e) {
e.printStackTrace();
}
}
public void importPowerStationFile(boolean isTableClean) {