From be3127220aed103d1c1bb495080d62593d61b835 Mon Sep 17 00:00:00 2001 From: pwielebs Date: Tue, 4 Sep 2018 09:50:05 +0200 Subject: Maven resources refactoring Added resurces to java spring classpath directly Change-Id: I194748706f63cff1966111bbdb52538bd5a83855 Issue-ID: DCAEGEN2-606 Signed-off-by: pwielebs --- .../prh/configuration/PrhAppConfigTest.java | 39 +++++++++++++--------- 1 file changed, 23 insertions(+), 16 deletions(-) (limited to 'prh-app-server/src/test') diff --git a/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/configuration/PrhAppConfigTest.java b/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/configuration/PrhAppConfigTest.java index 8ad2a94f..01866309 100644 --- a/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/configuration/PrhAppConfigTest.java +++ b/prh-app-server/src/test/java/org/onap/dcaegen2/services/prh/configuration/PrhAppConfigTest.java @@ -41,6 +41,8 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.onap.dcaegen2.services.prh.integration.junit5.mockito.MockitoExtension; +import org.springframework.core.io.InputStreamResource; +import org.springframework.core.io.Resource; /** * @author Przemysław Wąsala on 4/9/18 @@ -78,8 +80,16 @@ class PrhAppConfigTest { private static PrhAppConfig prhAppConfig; private static AppConfig appConfig; - private static String filePath = Objects - .requireNonNull(PrhAppConfigTest.class.getClassLoader().getResource(PRH_ENDPOINTS)).getFile(); + private static InputStream inputStream; + + static { + try { + inputStream = Objects + .requireNonNull(PrhAppConfigTest.class.getClassLoader().getResource(PRH_ENDPOINTS)).openStream(); + } catch (IOException e) { + e.printStackTrace(); + } + } @BeforeEach void setUp() { @@ -88,17 +98,15 @@ class PrhAppConfigTest { } @Test - void whenApplicationWasStarted_FilePathIsSet() { + void whenApplicationWasStarted_FilePathIsSet() throws IOException { // // When // - prhAppConfig.setFilepath(filePath); + doReturn(inputStream).when(prhAppConfig).getInputStream(anyString()); // // Then // - verify(prhAppConfig, times(1)).setFilepath(anyString()); verify(prhAppConfig, times(0)).initFileStreamReader(); - Assertions.assertEquals(filePath, prhAppConfig.getFilepath()); } @Test @@ -112,7 +120,7 @@ class PrhAppConfigTest { // // When // - prhAppConfig.setFilepath(filePath); + prhAppConfig.setResourceFile(new InputStreamResource(inputStream)); doReturn(inputStream).when(prhAppConfig).getInputStream(any()); prhAppConfig.initFileStreamReader(); appConfig.dmaapConsumerConfiguration = prhAppConfig.getDmaapConsumerConfiguration(); @@ -121,7 +129,6 @@ class PrhAppConfigTest { // // Then // - verify(prhAppConfig, times(1)).setFilepath(anyString()); verify(prhAppConfig, times(1)).initFileStreamReader(); Assertions.assertNotNull(prhAppConfig.getAaiClientConfiguration()); Assertions.assertNotNull(prhAppConfig.getDmaapConsumerConfiguration()); @@ -136,12 +143,15 @@ class PrhAppConfigTest { } @Test - void whenFileIsNotExist_ThrowIoException() { + void whenFileIsNotExist_ThrowIoException() throws IOException { // // Given + InputStream inputStream = new ByteArrayInputStream((jsonString.getBytes( + StandardCharsets.UTF_8))); + Resource resource = spy(new InputStreamResource(inputStream)); // - filePath = "/temp.json"; - prhAppConfig.setFilepath(filePath); + when(resource.getInputStream()).thenThrow(new IOException()); + prhAppConfig.setResourceFile(resource); // // When // @@ -149,7 +159,6 @@ class PrhAppConfigTest { // // Then // - verify(prhAppConfig, times(1)).setFilepath(anyString()); verify(prhAppConfig, times(1)).initFileStreamReader(); Assertions.assertNull(prhAppConfig.getAaiClientConfiguration()); Assertions.assertNull(prhAppConfig.getDmaapConsumerConfiguration()); @@ -167,14 +176,13 @@ class PrhAppConfigTest { // // When // - prhAppConfig.setFilepath(filePath); + prhAppConfig.setResourceFile(new InputStreamResource(inputStream)); doReturn(inputStream).when(prhAppConfig).getInputStream(any()); prhAppConfig.initFileStreamReader(); // // Then // - verify(prhAppConfig, times(1)).setFilepath(anyString()); verify(prhAppConfig, times(1)).initFileStreamReader(); Assertions.assertNotNull(prhAppConfig.getAaiClientConfiguration()); Assertions.assertNotNull(prhAppConfig.getDmaapConsumerConfiguration()); @@ -190,7 +198,7 @@ class PrhAppConfigTest { InputStream inputStream = new ByteArrayInputStream((jsonString.getBytes( StandardCharsets.UTF_8))); // When - prhAppConfig.setFilepath(filePath); + prhAppConfig.setResourceFile(new InputStreamResource(inputStream)); doReturn(inputStream).when(prhAppConfig).getInputStream(any()); JsonElement jsonElement = mock(JsonElement.class); when(jsonElement.isJsonObject()).thenReturn(false); @@ -201,7 +209,6 @@ class PrhAppConfigTest { appConfig.aaiClientConfiguration = prhAppConfig.getAaiClientConfiguration(); // Then - verify(prhAppConfig, times(1)).setFilepath(anyString()); verify(prhAppConfig, times(1)).initFileStreamReader(); Assertions.assertNull(prhAppConfig.getAaiClientConfiguration()); Assertions.assertNull(prhAppConfig.getDmaapConsumerConfiguration()); -- cgit 1.2.3-korg