Commit 0572dfae authored by Holger Rudolph's avatar Holger Rudolph
Browse files

[SI-89] Services für HistoryGridFailureInformation incl. Unit-Tests


Signed-off-by: Holger Rudolph's avatarHolger Rudolph <holger.rudolph@pta.de>
parent 8d76b299
/*
*******************************************************************************
* 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.service.HistFailureInformationService;
import org.eclipse.openk.gridfailureinformation.viewmodel.FailureInformationDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.UUID;
@Log4j2
@RestController
@RequestMapping("/hist-grid-failure-informations")
public class HistFailureInformationController {
@Value("${gridFailureInformation.maxListSize}")
int maxListSize;
@Autowired
private HistFailureInformationService histfailureInformationService;
@GetMapping("/{uuid}/versions")
@ApiOperation(value = "Anzeigen aller Versionen einer Störungsinformation")
@ApiResponses(value = { @ApiResponse(code = 200, message = "Erfolgreich durchgeführt"),
@ApiResponse(code = 404, message = "Störungsinformationen wurden nicht gefunden")})
@ResponseStatus(HttpStatus.OK)
public List<FailureInformationDto> getHistFailureInformationVersions(@PathVariable UUID uuid) {
return histfailureInformationService.getFailureInformationVersionsByUuid(uuid);
}
@GetMapping("/{uuid}/versions/{versionNumber}")
@ApiOperation(value = "Anzeigen einer bestimmten Version einer Störungsinformation")
@ApiResponses(value = { @ApiResponse(code = 200, message = "Erfolgreich durchgeführt"),
@ApiResponse(code = 404, message = "Störungsinformation wurden nicht gefunden")})
@ResponseStatus(HttpStatus.OK)
public FailureInformationDto readVersion(
@PathVariable UUID uuid,
@PathVariable Long versionNumber) {
return histfailureInformationService.getFailureInformationVersion(uuid, versionNumber);
}
}
/*
*******************************************************************************
* 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.model;
import lombok.Data;
import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedBy;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
import javax.persistence.*;
import java.util.Date;
import java.util.UUID;
@Data
@Entity
@EntityListeners(AuditingEntityListener.class)
public class HtblFailureInformation {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "htbl_failure_information_id_seq")
@SequenceGenerator(name = "htbl_failure_information_id_seq", sequenceName = "htbl_failure_information_id_seq", allocationSize = 1)
@Column(name = "hid", updatable = false)
private Long hid;
private Long haction;
private Date hdate;
private String huser;
private Long id;
private UUID uuid;
private Long versionNumber;
private String responsibility;
private String internExtern;
private String voltageLevel;
private String pressureLevel;
private Date failureBegin;
private Date failureEndPlanned;
private Date failureEndResupplied;
private String probableReason;
private String internalRemark;
private String postcode;
private String city;
private String district;
private String street;
private String housenumber;
private String stationDescription;
private String stationCoords;
private String radius;
@CreatedDate
@Column(name = "create_date")
private Date createDate;
@CreatedBy
@Column(name = "create_user")
private String createUser;
@LastModifiedDate
@Column(name = "mod_date")
private Date modDate;
@LastModifiedBy
@Column(name = "mod_user")
private String modUser;
@ManyToOne
@JoinColumn( name = "fk_ref_failure_classification")
private RefFailureClassification refFailureClassification;
@ManyToOne
@JoinColumn( name = "fk_ref_failure_type")
private RefFailureType refFailureType;
@ManyToOne
@JoinColumn( name = "fk_ref_status_intern")
private RefStatus refStatusIntern;
@ManyToOne
@JoinColumn( name = "fk_ref_status_extern")
private RefStatus refStatusExtern;
@ManyToOne
@JoinColumn( name = "fk_ref_branch")
private RefBranch refBranch;
}
/*
*******************************************************************************
* 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.repository;
import org.eclipse.openk.gridfailureinformation.model.HtblFailureInformation;
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;
import java.util.Optional;
import java.util.UUID;
@Repository
public interface HistFailureInformationRepository extends JpaRepository<HtblFailureInformation, Long > {
List<HtblFailureInformation> findByUuid(UUID uuid);
Optional<HtblFailureInformation> findByUuidAndVersionNumber(UUID uuid, Long versionNumber);
}
/*
*******************************************************************************
* 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.exceptions.NotFoundException;
import org.eclipse.openk.gridfailureinformation.mapper.HistFailureInformationMapper;
import org.eclipse.openk.gridfailureinformation.model.HtblFailureInformation;
import org.eclipse.openk.gridfailureinformation.repository.*;
import org.eclipse.openk.gridfailureinformation.viewmodel.FailureInformationDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;
@Service
public class HistFailureInformationService {
@Autowired
private HistFailureInformationRepository histFailureInformationRepository;
@Autowired
private HistFailureInformationMapper histFailureInformationMapper;
@Autowired
private BranchRepository branchRepository;
@Autowired
private FailureClassificationRepository failureClassificationRepository;
@Autowired
private FailureTypeRepository failureTypeRepository;
@Autowired
private StatusRepository statusRepository;
public List<FailureInformationDto> getFailureInformationVersionsByUuid(UUID uuid ) {
List<HtblFailureInformation> htblFailureInformationList = histFailureInformationRepository.findByUuid(uuid);
return htblFailureInformationList.stream().map(histFailureInformationMapper::toFailureInformationDto).collect(Collectors.toList());
}
public FailureInformationDto getFailureInformationVersion(UUID uuid, Long versionNumber ) {
HtblFailureInformation htblFailureInformation = histFailureInformationRepository.findByUuidAndVersionNumber(uuid, versionNumber)
.orElseThrow(NotFoundException::new);
return histFailureInformationMapper.toFailureInformationDto(htblFailureInformation);
}
}
......@@ -26,6 +26,7 @@ import java.util.UUID;
public class FailureInformationDto implements Serializable {
@JsonProperty("id")
private UUID uuid;
private Long versionNumber;
private String responsibility;
private String internExtern;
private String voltageLevel;
......
......@@ -39,6 +39,9 @@ public class TestConfiguration {
@Bean
FailureInformationMapper failureInformationMapper() { return new FailureInformationMapperImpl(); }
@Bean
HistFailureInformationMapper histFailureInformationMapper() { return new HistFailureInformationMapperImpl(); }
@Bean
BranchMapper branchMapper() { return new BranchMapperImpl(); }
......@@ -67,6 +70,11 @@ public class TestConfiguration {
return new FailureInformationService();
}
@Bean
public HistFailureInformationService myHistFailureInformationService() {
return new HistFailureInformationService();
}
@Bean
public BranchService myBranchService() {
return new BranchService();
......
/*
*******************************************************************************
* 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 com.fasterxml.jackson.databind.ObjectMapper;
import org.eclipse.openk.gridfailureinformation.GridFailureInformationApplication;
import org.eclipse.openk.gridfailureinformation.exceptions.NotFoundException;
import org.eclipse.openk.gridfailureinformation.service.FailureInformationService;
import org.eclipse.openk.gridfailureinformation.service.HistFailureInformationService;
import org.eclipse.openk.gridfailureinformation.support.MockDataHelper;
import org.eclipse.openk.gridfailureinformation.viewmodel.BranchDto;
import org.eclipse.openk.gridfailureinformation.viewmodel.FailureInformationDto;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.MediaType;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.web.servlet.MockMvc;
import java.util.List;
import java.util.Random;
import java.util.UUID;
import static org.hamcrest.Matchers.is;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
@SpringBootTest(classes = GridFailureInformationApplication.class)
@AutoConfigureMockMvc
@ActiveProfiles("test")
public class HistFailureInformationControllerTest {
@MockBean
private HistFailureInformationService histFailureInformationService;
@Autowired
private MockMvc mockMvc;
@Test
public void shouldFindHistFailureInfos() throws Exception {
List<FailureInformationDto> failureInformationDtoList = MockDataHelper.mockHistGridFailureInformationDtoList();
when(histFailureInformationService.getFailureInformationVersionsByUuid(any(UUID.class))).thenReturn(failureInformationDtoList);
mockMvc.perform(get("/hist-grid-failure-informations/{uuid}/versions", UUID.randomUUID()))
.andExpect(status().is2xxSuccessful())
.andExpect(content().contentType(MediaType.APPLICATION_JSON));
}
@Test
public void shouldFindHistFailureInfoVersion() throws Exception {
FailureInformationDto dto = MockDataHelper.mockFailureInformationDto();
when(histFailureInformationService.getFailureInformationVersion(
any(UUID.class),
any(Long.class)
)).thenReturn(dto);
String versionId = Long.toString(new Random().nextLong());
mockMvc.perform(get("/hist-grid-failure-informations/{uuid}/versions/{versionNumber}" ,UUID.randomUUID(),new Random().nextLong() ))
.andExpect(status().is2xxSuccessful())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("responsibility", is( dto.getResponsibility())));
}
}
\ No newline at end of file
/*
*******************************************************************************
* 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.config.TestConfiguration;
import org.eclipse.openk.gridfailureinformation.exceptions.NotFoundException;
import org.eclipse.openk.gridfailureinformation.model.*;
import org.eclipse.openk.gridfailureinformation.repository.*;
import org.eclipse.openk.gridfailureinformation.support.MockDataHelper;
import org.eclipse.openk.gridfailureinformation.viewmodel.BranchDto;
import org.eclipse.openk.gridfailureinformation.viewmodel.FailureInformationDto;
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.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.test.context.ContextConfiguration;
import java.util.List;
import java.util.Optional;
import java.util.Random;
import java.util.UUID;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;
@DataJpaTest
@ContextConfiguration(classes = {TestConfiguration.class})
public class HistFailureInformationServiceTest {
@Qualifier("myHistFailureInformationService")
@Autowired
private HistFailureInformationService histFailureInformationService;
@MockBean
private HistFailureInformationRepository histFailureInformationRepository;
@Test
public void shouldFindHistFailureInformationsByUuid() {
List<HtblFailureInformation> mockHistFailureList = MockDataHelper.mockHistTblFailureInformationList();
when(histFailureInformationRepository.findByUuid(any(UUID.class))).thenReturn(mockHistFailureList);
List<FailureInformationDto> retList = histFailureInformationService.getFailureInformationVersionsByUuid(UUID.randomUUID());
assertEquals(retList.size(), mockHistFailureList.size());
assertEquals(retList.size(), 2);
assertEquals(retList.get(1).getUuid(), mockHistFailureList.get(1).getUuid());
assertEquals(retList.get(0).getResponsibility(), mockHistFailureList.get(0).getResponsibility());
}
@Test
public void shouldFindASingleHistFailureInformationVersion() {
HtblFailureInformation mockHistFailure = MockDataHelper.mockHistTblFailureInformation();
when(histFailureInformationRepository.findByUuidAndVersionNumber(any(UUID.class), any(Long.class))).thenReturn(Optional.of(mockHistFailure));
FailureInformationDto retDto = histFailureInformationService.getFailureInformationVersion(UUID.randomUUID(), new Random().nextLong());
assertEquals( mockHistFailure.getResponsibility(), retDto.getResponsibility() );
assertEquals( mockHistFailure.getUuid(), retDto.getUuid());
}
}
......@@ -353,5 +353,147 @@ public class MockDataHelper {
return refStatusList;
}
public static HtblFailureInformation mockHistTblFailureInformation() {
HtblFailureInformation obj = new HtblFailureInformation();
obj.setUuid(UUID.randomUUID());
obj.setVersionNumber(1L);
obj.setResponsibility("Dudley Dursley");
obj.setInternExtern(INTERNAL_SHORT);
obj.setVoltageLevel(Constants.VOLTAGE_LEVEL_MS);
obj.setPressureLevel(Constants.PRESSURE_LEVEL_HD);
obj.setFailureBegin(Date.valueOf("2022-12-01"));
obj.setFailureEndPlanned(Date.valueOf("2022-12-02"));
obj.setFailureEndResupplied(Date.valueOf("2022-12-03"));
obj.setProbableReason("Über Kabel gestolpert");
obj.setCreateDate(Date.valueOf("2020-05-08"));
obj.setCreateUser("weizenkeimk");
obj.setModDate(Date.valueOf("2020-05-23"));
obj.setModUser("schlonzh");
obj.setRefStatusIntern(mockRefStatus());
obj.setRefStatusExtern(mockRefStatus());
obj.setRefFailureType(mockRefFailureType());
obj.setRefFailureClassification(mockRefFailureClassification());
obj.setRefBranch(mockRefBranch());
return obj;
}
public static HtblFailureInformation mockHistTblFailureInformation2() {
HtblFailureInformation obj = new HtblFailureInformation();
obj.setUuid(UUID.randomUUID());
obj.setVersionNumber(2L);
obj.setResponsibility("Donald Duck");
obj.setInternExtern(INTERNAL_SHORT);
obj.setVoltageLevel(Constants.VOLTAGE_LEVEL_MS);
obj.setPressureLevel(Constants.PRESSURE_LEVEL_HD);
obj.setFailureBegin(Date.valueOf("2022-12-01"));
obj.setFailureEndPlanned(Date.valueOf("2022-12-02"));
obj.setFailureEndResupplied(Date.valueOf("2022-12-03"));
obj.setProbableReason("Rohrbruch");
obj.setCreateDate(Date.valueOf("2020-05-08"));
obj.setCreateUser("Kleverk");
obj.setModDate(Date.valueOf("2020-05-23"));
obj.setModUser("Gansg");
obj.setRefStatusIntern(mockRefStatus());
obj.setRefStatusExtern(mockRefStatus());
obj.setRefFailureType(mockRefFailureType());
obj.setRefFailureClassification(mockRefFailureClassification());
obj.setRefBranch(mockRefBranch());
return obj;
}
public static List<HtblFailureInformation> mockHistTblFailureInformationList() {
List<HtblFailureInformation> retList = new LinkedList<>();
retList.add( mockHistTblFailureInformation() );
retList.add( mockHistTblFailureInformation2() );
return retList;
}
public static FailureInformationDto mockHistFailureInformationDto() {
FailureInformationDto dto = new FailureInformationDto();
dto.setUuid(UUID.randomUUID());
dto.setVersionNumber(1L);
dto.setResponsibility("Dudley Dursley");
dto.setInternExtern(INTERNAL_SHORT);
dto.setVoltageLevel(Constants.VOLTAGE_LEVEL_MS);
dto.setPressureLevel(Constants.PRESSURE_LEVEL_HD);
dto.setFailureBegin(Date.valueOf("2022-12-01"));
dto.setFailureEndPlanned(Date.valueOf("2022-12-02"));
dto.setFailureEndResupplied(Date.valueOf("2022-12-03"));
dto.setProbableReason("Über Kabel gestolpert");
dto.setFailureClassificationId(UUID.randomUUID());
dto.setFailureClassification("FailClazz");
dto.setFailureTypeId(UUID.randomUUID());
dto.setFailureType("FailTypo");