SI-442 Unit Tests jobmanager, exception, controller added Signed-off-by: Dimitrios Chalepakis <dimitrios.chalepakis@pta.de>
diff --git a/addressImport/src/main/java/org/eclipse/openk/gridfailureinformation/importadresses/service/AddressService.java b/addressImport/src/main/java/org/eclipse/openk/gridfailureinformation/importadresses/service/AddressService.java index ae32be7..1b18272 100644 --- a/addressImport/src/main/java/org/eclipse/openk/gridfailureinformation/importadresses/service/AddressService.java +++ b/addressImport/src/main/java/org/eclipse/openk/gridfailureinformation/importadresses/service/AddressService.java
@@ -72,7 +72,6 @@ } catch (Exception ex) { - ex.printStackTrace(); log.warn("Adresse [sdo_x1= " + addressDto.getSdox1() + ", sdo_y1= " + addressDto.getSdoy1() + ", g3e_fid=" + addressDto.getG3efid()
diff --git a/addressImport/src/test/java/org/eclipse/openk/gridfailureinformation/importadresses/config/TestConfiguration.java b/addressImport/src/test/java/org/eclipse/openk/gridfailureinformation/importadresses/config/TestConfiguration.java index b4b2ff5..8bd1faf 100644 --- a/addressImport/src/test/java/org/eclipse/openk/gridfailureinformation/importadresses/config/TestConfiguration.java +++ b/addressImport/src/test/java/org/eclipse/openk/gridfailureinformation/importadresses/config/TestConfiguration.java
@@ -15,6 +15,7 @@ package org.eclipse.openk.gridfailureinformation.importadresses.config; import org.eclipse.openk.gridfailureinformation.importadresses.AddressImportApplication; +import org.eclipse.openk.gridfailureinformation.importadresses.jobs.JobManager; import org.eclipse.openk.gridfailureinformation.importadresses.mapper.AddressMapper; import org.eclipse.openk.gridfailureinformation.importadresses.mapper.AddressMapperImpl; import org.eclipse.openk.gridfailureinformation.importadresses.mapper.StationMapper; @@ -55,4 +56,7 @@ @Bean public StationService myStationService() { return new StationService(); } + + @Bean + public JobManager jobManager() { return new JobManager(); } }
diff --git a/addressImport/src/test/java/org/eclipse/openk/gridfailureinformation/importadresses/controller/AddressImportControllerTest.java b/addressImport/src/test/java/org/eclipse/openk/gridfailureinformation/importadresses/controller/AddressImportControllerTest.java new file mode 100644 index 0000000..5d467ee --- /dev/null +++ b/addressImport/src/test/java/org/eclipse/openk/gridfailureinformation/importadresses/controller/AddressImportControllerTest.java
@@ -0,0 +1,49 @@ +/* + ******************************************************************************* + * Copyright (c) 2018 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.controller; + +import org.eclipse.openk.gridfailureinformation.importadresses.AddressImportApplication; +import org.eclipse.openk.gridfailureinformation.importadresses.jobs.JobManager; +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.test.context.ActiveProfiles; +import org.springframework.test.web.servlet.MockMvc; +import static org.mockito.Mockito.verify; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +@SpringBootTest(classes = AddressImportApplication.class) +@AutoConfigureMockMvc +@ActiveProfiles("test") +public class AddressImportControllerTest { + + @MockBean + private JobManager jobManager; + + @Autowired + private MockMvc mockMvc; + + @Test + public void shouldTriggerStartImport() throws Exception { + + mockMvc.perform(post("/addresses/import")) + .andExpect(status().is2xxSuccessful()); + + verify(jobManager).triggerStartImport(); + } +}
diff --git a/addressImport/src/test/java/org/eclipse/openk/gridfailureinformation/importadresses/exceptions/InternalServerErrorExceptionTest.java b/addressImport/src/test/java/org/eclipse/openk/gridfailureinformation/importadresses/exceptions/InternalServerErrorExceptionTest.java new file mode 100644 index 0000000..c90dda0 --- /dev/null +++ b/addressImport/src/test/java/org/eclipse/openk/gridfailureinformation/importadresses/exceptions/InternalServerErrorExceptionTest.java
@@ -0,0 +1,29 @@ +/* + ******************************************************************************* + * Copyright (c) 2018 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.exceptions; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class InternalServerErrorExceptionTest { + @Test + public void testExceptionCreationForCoverage() { + new InternalServerErrorException("testOnly"); + Exception innerException = new InternalServerErrorException("inner Exception"); + assertEquals( innerException.getMessage(), "inner Exception"); + } +}
diff --git a/addressImport/src/test/java/org/eclipse/openk/gridfailureinformation/importadresses/jobs/JobManagerTest.java b/addressImport/src/test/java/org/eclipse/openk/gridfailureinformation/importadresses/jobs/JobManagerTest.java new file mode 100644 index 0000000..4f198e9 --- /dev/null +++ b/addressImport/src/test/java/org/eclipse/openk/gridfailureinformation/importadresses/jobs/JobManagerTest.java
@@ -0,0 +1,85 @@ +/* + ******************************************************************************* + * Copyright (c) 2018 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.jobs; + +import org.eclipse.openk.gridfailureinformation.importadresses.config.TestConfiguration; +import org.eclipse.openk.gridfailureinformation.importadresses.service.AddressImportService; +import org.junit.jupiter.api.Test; +import org.powermock.reflect.Whitebox; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.test.context.ContextConfiguration; +import static java.lang.Boolean.TRUE; +import static java.lang.Boolean.FALSE; +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.*; + +@DataJpaTest +@ContextConfiguration(classes = {TestConfiguration.class}) +public class JobManagerTest { + + @Autowired + JobManager jobManager; + + @MockBean + AddressImportService addressImportService; + + @Test + public void shouldTriggerStartImport() { + jobManager.triggerStartImport(); + Boolean startImport = Whitebox.getInternalState(jobManager, "startImport"); + assertEquals(startImport, TRUE); + } + + @Test + public void shouldImportOnTriggerAndSetStartImportBackToFalse() throws Exception { + Whitebox.setInternalState(jobManager, "startImport", TRUE); + + try { + Whitebox.invokeMethod(jobManager, "importOnTrigger"); + } catch (Exception e) { + e.printStackTrace(); + } + + Boolean startImport = Whitebox.getInternalState(jobManager, "startImport"); + assertEquals(startImport, FALSE); + } + + @Test + public void shouldImportOnTriggerAndDontChangeTheValueFromStartImport() { + Whitebox.setInternalState(jobManager, "startImport", FALSE); + + try { + Whitebox.invokeMethod(jobManager, "importOnTrigger"); + } catch (Exception e) { + e.printStackTrace(); + } + + Boolean startImport = Whitebox.getInternalState(jobManager, "startImport"); + assertEquals(startImport, FALSE); + } + + @Test + public void shouldImportAddressdataAndInvokeImportAddresses() { + jobManager.cleanUp = false; + try { + Whitebox.invokeMethod(jobManager, "importAddressdata"); + } catch (Exception e) { + e.printStackTrace(); + } + verify(addressImportService, times(1)).importAddresses(false); + } +}