Commit e5e11ea0 authored by Ina Curdt's avatar Ina Curdt
Browse files

SI-149 GET Branches service erstellt inkl. Unittests


Signed-off-by: Ina Curdt's avatarIna Curdt <Ina.Curdt@pta.de>
parent d689bacd
This source diff could not be displayed because it is too large. You can view the blob instead.
/*
*******************************************************************************
* 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.BranchService;
import org.eclipse.openk.gridfailureinformation.service.GridFailureInformationService;
import org.eclipse.openk.gridfailureinformation.viewmodel.BranchDto;
import org.eclipse.openk.gridfailureinformation.viewmodel.GridFailureInformationDto;
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 java.util.List;
@Log4j2
@RestController
@RequestMapping("/branches")
public class BranchController {
@Autowired
private BranchService branchService;
@ApiOperation(value = "Anzeigen aller Branches")
@ApiResponses(value = {@ApiResponse(code = 200, message = "Erfolgreich durchgeführt")})
@GetMapping
public List<BranchDto> findBranches() {
return branchService.getBranches();
}
}
/*
*******************************************************************************
* 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.mapper;
import org.eclipse.openk.gridfailureinformation.model.RefBranch;
import org.eclipse.openk.gridfailureinformation.model.TblFailureInformation;
import org.eclipse.openk.gridfailureinformation.viewmodel.BranchDto;
import org.eclipse.openk.gridfailureinformation.viewmodel.GridFailureInformationDto;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import org.mapstruct.ReportingPolicy;
@Mapper(componentModel = "spring", unmappedTargetPolicy = ReportingPolicy.IGNORE)
public interface BranchMapper {
BranchDto toBranchDto(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.RefBranch;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface BranchRepository extends JpaRepository<RefBranch, Long > {
List<RefBranch> findAll();
}
/*
*******************************************************************************
* 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.mapper.BranchMapper;
import org.eclipse.openk.gridfailureinformation.mapper.GridFailureInformationMapper;
import org.eclipse.openk.gridfailureinformation.repository.BranchRepository;
import org.eclipse.openk.gridfailureinformation.repository.FailureInformationRepository;
import org.eclipse.openk.gridfailureinformation.viewmodel.BranchDto;
import org.eclipse.openk.gridfailureinformation.viewmodel.GridFailureInformationDto;
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.stream.Collectors;
@Service
public class BranchService {
@Autowired
private BranchRepository branchRepository;
@Autowired
private BranchMapper branchMapper;
public List<BranchDto> getBranches() {
return branchRepository.findAll().stream()
.map( branchMapper::toBranchDto )
.collect(Collectors.toList());
}
}
/*
*******************************************************************************
* 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.viewmodel;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;
import java.io.Serializable;
import java.util.UUID;
@Data
public class BranchDto implements Serializable {
@JsonProperty("id")
private UUID uuid;
private String name;
private String colorCode;
}
......@@ -15,10 +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.VersionMapper;
import org.eclipse.openk.gridfailureinformation.mapper.VersionMapperImpl;
import org.eclipse.openk.gridfailureinformation.mapper.*;
import org.eclipse.openk.gridfailureinformation.service.BranchService;
import org.eclipse.openk.gridfailureinformation.service.GridFailureInformationService;
import org.eclipse.openk.gridfailureinformation.service.VersionService;
import org.springframework.boot.autoconfigure.domain.EntityScan;
......@@ -36,6 +34,9 @@ public class TestConfiguration {
@Bean
VersionMapper versionMapper() { return new VersionMapperImpl(); }
@Bean
BranchMapper branchMapper() { return new BranchMapperImpl(); }
@Bean
GridFailureInformationMapper gridFailureInformationMapper() { return new GridFailureInformationMapperImpl(); }
......@@ -44,6 +45,11 @@ public class TestConfiguration {
return new VersionService();
}
@Bean
public BranchService myBranchService() {
return new BranchService();
}
@Bean
public GridFailureInformationService myGridFailureInformationService() {
return new GridFailureInformationService();
......
/*
*******************************************************************************
* 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 org.eclipse.openk.gridfailureinformation.GridFailureInformationApplication;
import org.eclipse.openk.gridfailureinformation.service.BranchService;
import org.eclipse.openk.gridfailureinformation.service.VersionService;
import org.eclipse.openk.gridfailureinformation.support.MockDataHelper;
import org.eclipse.openk.gridfailureinformation.viewmodel.BranchDto;
import org.eclipse.openk.gridfailureinformation.viewmodel.VersionDto;
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.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import java.util.List;
import static org.hamcrest.Matchers.is;
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.*;
@SpringBootTest(classes = GridFailureInformationApplication.class)
@AutoConfigureMockMvc
public class BranchControllerTest {
@MockBean
private BranchService branchService;
@Autowired
private MockMvc mockMvc;
@Test
public void shouldReturnBranches() throws Exception {
List<BranchDto> branchDtoList = MockDataHelper.mockBranchDtoList();
when(branchService.getBranches()).thenReturn(branchDtoList);
mockMvc.perform(get("/branches"))
.andExpect(status().is2xxSuccessful())
.andExpect(content().contentType(MediaType.APPLICATION_JSON));
}
}
\ 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.constants.Constants;
import org.eclipse.openk.gridfailureinformation.model.RefBranch;
import org.eclipse.openk.gridfailureinformation.model.Version;
import org.eclipse.openk.gridfailureinformation.repository.BranchRepository;
import org.eclipse.openk.gridfailureinformation.repository.VersionRepository;
import org.eclipse.openk.gridfailureinformation.support.MockDataHelper;
import org.eclipse.openk.gridfailureinformation.viewmodel.BranchDto;
import org.eclipse.openk.gridfailureinformation.viewmodel.VersionDto;
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.ContextConfiguration;
import java.util.List;
import java.util.Optional;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;
//@RunWith(SpringRunner.class)
@DataJpaTest
@ContextConfiguration(classes = {TestConfiguration.class})
public class BranchServiceTest {
@Qualifier("myBranchService")
@Autowired
private BranchService branchService;
@MockBean
private BranchRepository branchRepository;
@Test
public void shouldGetBranchesProperly() {
List<RefBranch> mockRefBranchList = MockDataHelper.mockRefBranchList();
when(branchRepository.findAll()).thenReturn(mockRefBranchList);
List<BranchDto> listRefBranch = branchService.getBranches();
assertEquals(listRefBranch.size(), mockRefBranchList.size());
assertEquals(listRefBranch.size(), 2);
assertEquals(listRefBranch.get(1).getUuid(), mockRefBranchList.get(1).getUuid());
}
}
......@@ -14,9 +14,15 @@
*/
package org.eclipse.openk.gridfailureinformation.support;
import org.eclipse.openk.gridfailureinformation.model.RefBranch;
import org.eclipse.openk.gridfailureinformation.model.Version;
import org.eclipse.openk.gridfailureinformation.viewmodel.BranchDto;
import org.eclipse.openk.gridfailureinformation.viewmodel.VersionDto;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
public class MockDataHelper {
private MockDataHelper() {}
......@@ -33,4 +39,52 @@ public class MockDataHelper {
versionDto.setDbVersion("550");
return versionDto;
}
public static BranchDto mockBranchDto() {
BranchDto branchDto = new BranchDto();
branchDto.setUuid(UUID.fromString("14d4327c-594b-11ea-82b4-0242ac130003"));
branchDto.setName("Handel");
branchDto.setColorCode("#3ADF00");
return branchDto;
}
public static List<BranchDto> mockBranchDtoList() {
List<BranchDto> branchDtoList = new ArrayList<>();
BranchDto branchDto1 = mockBranchDto();
BranchDto branchDto2 = new BranchDto();
branchDto2.setUuid(UUID.fromString("f033c6d4-594b-11ea-8e2d-0242ac130003"));
branchDto2.setName("Diagnostik");
branchDto2.setColorCode("#D358F7");
branchDtoList.add(branchDto1);
branchDtoList.add(branchDto2);
return branchDtoList;
}
public static RefBranch mockRefBranch() {
RefBranch refBranch = new RefBranch();
refBranch.setUuid(UUID.fromString("319fe7ae-594e-11ea-82b4-0242ac130003"));
refBranch.setName("Pharma");
refBranch.setColorCode("#29088A");
return refBranch;
}
public static List<RefBranch> mockRefBranchList() {
List<RefBranch> refBranchList = new ArrayList<>();
RefBranch refBranch1 = mockRefBranch();
RefBranch refBranch2 = new RefBranch();
refBranch2.setUuid(UUID.fromString("d03814c2-594e-11ea-8e2d-0242ac130003"));
refBranch2.setName("Maschinenbau");
refBranch2.setColorCode("#FF8000");
refBranchList.add(refBranch1);
refBranchList.add(refBranch2);
return refBranchList;
}
}
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