Commit 153a4841 authored by Dimitrios Chalepakis's avatar Dimitrios Chalepakis
Browse files

SI-466 Unit Tests


Signed-off-by: Dimitrios Chalepakis's avatarDimitrios Chalepakis <dimitrios.chalepakis@pta.de>
parent a6212fff
...@@ -25,7 +25,6 @@ import org.eclipse.openk.gridfailureinformation.mapper.VersionMapper; ...@@ -25,7 +25,6 @@ import org.eclipse.openk.gridfailureinformation.mapper.VersionMapper;
import org.eclipse.openk.gridfailureinformation.mapper.VersionMapperImpl; import org.eclipse.openk.gridfailureinformation.mapper.VersionMapperImpl;
import org.eclipse.openk.gridfailureinformation.mapper.*; import org.eclipse.openk.gridfailureinformation.mapper.*;
import org.eclipse.openk.gridfailureinformation.service.*; import org.eclipse.openk.gridfailureinformation.service.*;
import org.mockito.Mockito;
import org.springframework.boot.autoconfigure.domain.EntityScan; import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.test.context.ConfigFileApplicationContextInitializer; import org.springframework.boot.test.context.ConfigFileApplicationContextInitializer;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
...@@ -33,9 +32,6 @@ import org.springframework.data.jpa.repository.config.EnableJpaRepositories; ...@@ -33,9 +32,6 @@ import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestPropertySource; import org.springframework.test.context.TestPropertySource;
import static org.mockito.Mockito.mock;
import static org.mockito.internal.util.MockUtil.createMock;
@EnableJpaRepositories(basePackages = "org.eclipse.openk.gridfailureinformation") @EnableJpaRepositories(basePackages = "org.eclipse.openk.gridfailureinformation")
@EntityScan(basePackageClasses = GridFailureInformationApplication.class) @EntityScan(basePackageClasses = GridFailureInformationApplication.class)
@ContextConfiguration( initializers = {ConfigFileApplicationContextInitializer.class}) @ContextConfiguration( initializers = {ConfigFileApplicationContextInitializer.class})
...@@ -79,6 +75,9 @@ public class TestConfiguration { ...@@ -79,6 +75,9 @@ public class TestConfiguration {
@Bean @Bean
ImportDataMapper importDataMapper() { return new ImportDataMapperImpl(); } ImportDataMapper importDataMapper() { return new ImportDataMapperImpl(); }
@Bean
AddressMapper addressMapper() { return new AddressMapperImpl(); }
@Bean @Bean
public VersionService myVersionService() { public VersionService myVersionService() {
return new VersionService(); return new VersionService();
...@@ -145,4 +144,7 @@ public class TestConfiguration { ...@@ -145,4 +144,7 @@ public class TestConfiguration {
@Bean @Bean
public ImportDataService myImportDataService() {return new ImportDataService(); } public ImportDataService myImportDataService() {return new ImportDataService(); }
@Bean
public AddressService myAddressService() { return new AddressService(); }
} }
/*
*******************************************************************************
* 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.AddressService;
import org.eclipse.openk.gridfailureinformation.support.MockDataHelper;
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.context.ActiveProfiles;
import org.springframework.test.web.servlet.MockMvc;
import java.util.List;
import java.util.Optional;
import static org.mockito.Mockito.*;
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;
@SpringBootTest(classes = GridFailureInformationApplication.class)
@AutoConfigureMockMvc()
@ActiveProfiles("test")
public class AddressControllerTest {
@MockBean
private AddressService addressService;
@Autowired
private MockMvc mockMvc;
@Test
public void shouldReturnPostCodes() throws Exception {
List<String> postcodes = MockDataHelper.mockPostCodes();
when(addressService.getPostcodes()).thenReturn(postcodes);
mockMvc.perform(get("/addresses/postcodes"))
.andExpect(status().is2xxSuccessful())
.andExpect(content().contentType(MediaType.APPLICATION_JSON));
verify(addressService, times(1)).getPostcodes();
}
@Test
public void shouldReturnCommunities() throws Exception {
String postcode = "71111";
List<String> strings = MockDataHelper.mockStringList();
when(addressService.getCommunities(postcode)).thenReturn(strings);
mockMvc.perform(get("/addresses/communities/" + postcode))
.andExpect(status().is2xxSuccessful())
.andExpect(content().contentType(MediaType.APPLICATION_JSON));
verify(addressService, times(1)).getCommunities(postcode);
}
@Test
public void shouldReturnDistricts() throws Exception {
String postcode = "71111";
String community = "com1";
List<String> strings = MockDataHelper.mockStringList();
when(addressService.getDistricts(postcode, community)).thenReturn(strings);
mockMvc.perform(
get("/addresses/districts")
.param("postcode", postcode)
.param("community", community)
)
.andExpect(status().is2xxSuccessful())
.andExpect(content().contentType(MediaType.APPLICATION_JSON));
verify(addressService, times(1)).getDistricts(postcode, community);
}
@Test
public void shouldReturnStreets() throws Exception {
String postcode = "71111";
String community = "com1";
Optional<String> district = Optional.of("district1");
List<String> strings = MockDataHelper.mockStringList();
when(addressService.getStreets(postcode, community, district)).thenReturn(strings);
mockMvc.perform(
get("/addresses/streets")
.param("postcode", postcode)
.param("community", community)
.param("district", "district1")
)
.andExpect(status().is2xxSuccessful())
.andExpect(content().contentType(MediaType.APPLICATION_JSON));
verify(addressService, times(1)).getStreets(postcode, community, district);
}
@Test
public void shouldReturnHousenumbers() throws Exception {
String postcode = "71111";
String community = "com1";
String street = "street1";
List<String> strings = MockDataHelper.mockStringList();
when(addressService.getHousenumbers(postcode, community, street)).thenReturn(strings);
mockMvc.perform(
get("/addresses/housenumbers")
.param("postcode", postcode)
.param("community", community)
.param("street", street)
)
.andExpect(status().is2xxSuccessful())
.andExpect(content().contentType(MediaType.APPLICATION_JSON));
verify(addressService, times(1)).getHousenumbers(postcode, community, street);
}
}
\ 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.model.TblAddress;
import org.eclipse.openk.gridfailureinformation.repository.AddressRepository;
import org.eclipse.openk.gridfailureinformation.support.MockDataHelper;
import org.eclipse.openk.gridfailureinformation.viewmodel.AddressDto;
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.Mockito.when;
@DataJpaTest
@ContextConfiguration(classes = {TestConfiguration.class})
public class AddressServiceTest {
@Qualifier("myAddressService")
@Autowired
private AddressService addressService;
@MockBean
private AddressRepository addressRepository;
@Test
public void shouldGetAllAddresses() {
List<TblAddress> addresses = MockDataHelper.mockAddressList();
when(addressRepository.findAll()).thenReturn(addresses);
List<AddressDto> addressesDtoList = addressService.getAddresses();
assertEquals(addressesDtoList.size(), addresses.size());
assertEquals(2, addressesDtoList.size());
assertEquals(addressesDtoList.get(1).getUuid(), addresses.get(1).getUuid());
}
@Test
public void shouldGetPostcodes() {
List<String> strings = MockDataHelper.mockStringList();
when(addressRepository.findAllPostcodes()).thenReturn(strings);
List<String> postCodes = addressService.getPostcodes();
assertEquals(postCodes.size(), strings.size());
assertEquals(2, postCodes.size());
assertEquals(postCodes.get(1), strings.get(1));
}
@Test
public void shouldGetCommunities() {
String postcode = "12345";
List<String> strings = MockDataHelper.mockStringList();
when(addressRepository.findCommunitiesByPostcode(postcode)).thenReturn(strings);
List<String> communities = addressService.getCommunities(postcode);
assertEquals(communities.size(), strings.size());
assertEquals(2, communities.size());
assertEquals(communities.get(1), strings.get(1));
}
@Test
public void shouldGetDistricts() {
String postcode = "12345";
String community = "com1";
List<String> strings = MockDataHelper.mockStringList();
when(addressRepository.findDistrictsByPostcodeAndCommunity(postcode, community)).thenReturn(strings);
List<String> districts = addressService.getDistricts(postcode, community);
assertEquals(districts.size(), strings.size());
assertEquals(2, districts.size());
assertEquals(districts.get(1), strings.get(1));
}
@Test
public void shouldGetHousenumbers() {
String postcode = "12345";
String community = "com1";
String street = "street1";
List<String> strings = MockDataHelper.mockStringList();
when(addressRepository.findHousenumbersByPostcodeAndCommunityAndStreet(postcode, community, street)).thenReturn(strings);
List<String> housenumbers = addressService.getHousenumbers(postcode, community, street);
assertEquals(housenumbers.size(), strings.size());
assertEquals(2, housenumbers.size());
assertEquals(housenumbers.get(1), strings.get(1));
}
@Test
public void shouldGetStreetsWithoutDistricts() {
String postcode = "12345";
String community = "com1";
Optional<String> district = Optional.empty();
List<String> strings = MockDataHelper.mockStringList();
when(addressRepository.findStreetsByPostcodeAndCommunity(postcode, community)).thenReturn(strings);
List<String> streets = addressService.getStreets(postcode, community, district);
assertEquals(streets.size(), strings.size());
assertEquals(2, streets.size());
assertEquals(streets.get(1), strings.get(1));
}
@Test
public void shouldGetStreetsWithDistricts() {
String postcode = "12345";
String community = "com1";
String district = "test1";
Optional<String> optionalDistrict = Optional.of("test1");
List<String> strings = MockDataHelper.mockStringList();
when(addressRepository.findStreetsByPostcodeAndCommunityAndDistrict(postcode, community, district)).thenReturn(strings);
List<String> streets = addressService.getStreets(postcode, community, optionalDistrict);
assertEquals(streets.size(), strings.size());
assertEquals(2, streets.size());
assertEquals(streets.get(1), strings.get(1));
}
}
...@@ -853,4 +853,42 @@ public class MockDataHelper { ...@@ -853,4 +853,42 @@ public class MockDataHelper {
return list; return list;
} }
public static List<String> mockPostCodes() {
List list = new ArrayList();
list.add("71111");
list.add("71122");
return list;
}
public static List<String> mockStringList() {
List list = new ArrayList();
list.add("test1");
list.add("test2");
return list;
}
public static List<TblAddress> mockAddressList() {
List<TblAddress> addressList = new ArrayList<>();
TblAddress address1 = new TblAddress();
address1.setUuid(UUID.randomUUID());
address1.setStreet("stree1");
address1.setPostcode("23443");
address1.setLongitude(BigDecimal.valueOf(443443));
TblAddress address2 = new TblAddress();
address2.setUuid(UUID.randomUUID());
address2.setStreet("stree1");
address2.setPostcode("45465");
address2.setLongitude(BigDecimal.valueOf(546765));
addressList.add(address1);
addressList.add(address2);
return addressList;
}
} }
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