Commit f1d39e81 authored by dietricf's avatar dietricf
Browse files

SI-67_GetSingle FailureInformation - Service incl. UnitTests

parent 7e6141d7
This diff is collapsed.
......@@ -23,11 +23,11 @@ import org.eclipse.openk.gridfailureinformation.viewmodel.FailureInformationDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.UUID;
@Log4j2
@RestController
......@@ -40,6 +40,17 @@ public class FailureInformationController {
@Autowired
private FailureInformationService failureInformationService;
@GetMapping("/{uuid}")
@ApiOperation(value = "Anzeigen einer Störungsinformation")
@ApiResponses(value = { @ApiResponse(code = 200, message = "Erfolgreich durchgeführt"),
@ApiResponse(code = 404, message = "Störungsinformation wurde nicht gefunden")})
@ResponseStatus(HttpStatus.OK)
public FailureInformationDto getFailureInformation(@PathVariable UUID uuid) {
return failureInformationService.findFailureInformation(uuid);
}
@ApiOperation(value = "Anzeigen aller Störungsinformationen")
@ApiResponses(value = {@ApiResponse(code = 200, message = "Erfolgreich durchgeführt")})
@GetMapping
......
......@@ -22,7 +22,7 @@ import org.mapstruct.Mappings;
import org.mapstruct.ReportingPolicy;
@Mapper(componentModel = "spring", unmappedTargetPolicy = ReportingPolicy.IGNORE)
public interface GridFailureInformationMapper {
public interface FailureInformationMapper {
@Mappings({
@Mapping(source = "refFailureClassification.uuid", target = "failureClassificationId"),
@Mapping(source = "refFailureClassification.classification", target = "failureClassification"),
......
......@@ -19,8 +19,11 @@ import org.eclipse.openk.gridfailureinformation.model.TblFailureInformation;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import java.util.Optional;
import java.util.UUID;
@Repository
public interface FailureInformationRepository extends JpaRepository<TblFailureInformation, Long > {
public Optional<TblFailureInformation> findByUuid(final UUID uuid);
}
......@@ -15,7 +15,8 @@
package org.eclipse.openk.gridfailureinformation.service;
import org.eclipse.openk.gridfailureinformation.mapper.GridFailureInformationMapper;
import org.eclipse.openk.gridfailureinformation.exceptions.NotFoundException;
import org.eclipse.openk.gridfailureinformation.mapper.FailureInformationMapper;
import org.eclipse.openk.gridfailureinformation.repository.FailureInformationRepository;
import org.eclipse.openk.gridfailureinformation.viewmodel.FailureInformationDto;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -23,6 +24,8 @@ import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import java.util.UUID;
@Service
public class FailureInformationService {
......@@ -31,10 +34,15 @@ public class FailureInformationService {
private FailureInformationRepository failureInformationRepository;
@Autowired
private GridFailureInformationMapper gridFailureInformationMapper;
private FailureInformationMapper failureInformationMapper;
public FailureInformationDto findFailureInformation( UUID uuid ) {
return failureInformationMapper.toGridFailureInformationDto(
failureInformationRepository.findByUuid( uuid )
.orElseThrow(NotFoundException::new));
}
public Page<FailureInformationDto> findFailureInformations(Pageable pageable) {
return failureInformationRepository.findAll(pageable).map(gridFailureInformationMapper::toGridFailureInformationDto);
return failureInformationRepository.findAll(pageable).map(failureInformationMapper::toGridFailureInformationDto);
}
}
......@@ -15,8 +15,8 @@
package org.eclipse.openk.gridfailureinformation.config;
import org.eclipse.openk.gridfailureinformation.GridFailureInformationApplication;
import org.eclipse.openk.gridfailureinformation.mapper.GridFailureInformationMapper;
import org.eclipse.openk.gridfailureinformation.mapper.GridFailureInformationMapperImpl;
import org.eclipse.openk.gridfailureinformation.mapper.FailureInformationMapper;
import org.eclipse.openk.gridfailureinformation.mapper.FailureInformationMapperImpl;
import org.eclipse.openk.gridfailureinformation.mapper.VersionMapper;
import org.eclipse.openk.gridfailureinformation.mapper.VersionMapperImpl;
import org.eclipse.openk.gridfailureinformation.service.FailureInformationService;
......@@ -37,7 +37,7 @@ public class TestConfiguration {
VersionMapper versionMapper() { return new VersionMapperImpl(); }
@Bean
GridFailureInformationMapper gridFailureInformationMapper() { return new GridFailureInformationMapperImpl(); }
FailureInformationMapper gridFailureInformationMapper() { return new FailureInformationMapperImpl(); }
@Bean
public VersionService myVersionService() {
......
......@@ -15,6 +15,7 @@
package org.eclipse.openk.gridfailureinformation.controller;
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.support.MockDataHelper;
import org.eclipse.openk.gridfailureinformation.viewmodel.FailureInformationDto;
......@@ -28,11 +29,13 @@ import org.springframework.data.domain.Pageable;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
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.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
@SpringBootTest(classes = GridFailureInformationApplication.class)
@AutoConfigureMockMvc
......@@ -47,7 +50,7 @@ public class FailureInformationControllerTest {
@Test
public void shouldFindFailureInfos() throws Exception {
Page<FailureInformationDto> page = MockDataHelper.mpckGridFailureInformationDtoPage();
Page<FailureInformationDto> page = MockDataHelper.mockGridFailureInformationDtoPage();
when(failureInformationService.findFailureInformations(any(Pageable.class))).thenReturn(page);
mockMvc.perform(get("/grid-failure-informations"))
......@@ -55,4 +58,23 @@ public class FailureInformationControllerTest {
.andExpect(content().contentType(MediaType.APPLICATION_JSON));
}
@Test
public void shouldFindFailureInfo() throws Exception {
FailureInformationDto dto = MockDataHelper.mockGridFailureInformationDto();
when(failureInformationService.findFailureInformation(any(UUID.class))).thenReturn(dto);
mockMvc.perform(get("/grid-failure-informations/"+UUID.randomUUID().toString()))
.andExpect(status().is2xxSuccessful())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("responsibility", is( dto.getResponsibility())));
}
@Test
public void shouldExceptionIfNotFoundFailureInfo() throws Exception {
when(failureInformationService.findFailureInformation(any(UUID.class))).thenThrow( new NotFoundException() );
mockMvc.perform(get("/grid-failure-informations/"+UUID.randomUUID().toString()))
.andExpect(status().isNotFound());
}
}
\ No newline at end of file
......@@ -15,6 +15,7 @@
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.TblFailureInformation;
import org.eclipse.openk.gridfailureinformation.repository.FailureInformationRepository;
import org.eclipse.openk.gridfailureinformation.support.MockDataHelper;
......@@ -28,7 +29,11 @@ import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.test.context.ContextConfiguration;
import java.util.Optional;
import java.util.UUID;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;
......@@ -54,4 +59,21 @@ public class FailureInformationServiceTest {
}
@Test
public void shouldFindASingleFailureInformation() {
TblFailureInformation mockFailure = MockDataHelper.mockTblFailureInformation();
when(failureInformationRepository.findByUuid(any(UUID.class))).thenReturn(Optional.of(mockFailure));
FailureInformationDto retDto = failureInformationService.findFailureInformation(UUID.randomUUID());
assertEquals( mockFailure.getResponsibility(), retDto.getResponsibility() );
assertEquals( mockFailure.getUuid(), retDto.getUuid());
}
@Test
public void shouldThrowExceptionWhenFailureNotFound() {
when(failureInformationRepository.findByUuid(any(UUID.class))).thenReturn(Optional.empty());
assertThrows(NotFoundException.class,
() -> failureInformationService.findFailureInformation(UUID.randomUUID()));
}
}
......@@ -112,7 +112,7 @@ public class MockDataHelper {
retList.get(1).setResponsibility("Lionel Lümmelprinz");
return retList;
}
public static Page<FailureInformationDto> mpckGridFailureInformationDtoPage() {
public static Page<FailureInformationDto> mockGridFailureInformationDtoPage() {
List<FailureInformationDto> dtos = mockGridFailureInformationDtos();
return new PageImpl<>(dtos, Pageable.unpaged(), dtos.size() );
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment