diff options
author | PatrikBuhr <patrik.buhr@est.tech> | 2019-03-14 14:27:30 +0000 |
---|---|---|
committer | PatrikBuhr <patrik.buhr@est.tech> | 2019-03-14 14:27:30 +0000 |
commit | ad4a3a514bd943df22a2e27d78f0706d412ebe9f (patch) | |
tree | e13648b6efbb335bf8e4f5f7a853615f8626d30e /datafile-app-server/src/test | |
parent | a89e09eecabd035f1c227b1ae3f5fa59eff36be4 (diff) |
Thread safety issues
The TrustManager is now loaded and initialized once
in a thread safe way (instead of each time it is used).
Removed some unneeded wrappers.
Using AutoCloseable for FTP clients to make
sure they are closed in case of exceptions.
Made AppConfig thread safe.
Change-Id: Ia6a2c8a76bf960013180fdd7c53ae0ff17b26505
Issue-ID: DCAEGEN2-1118
Signed-off-by: PatrikBuhr <patrik.buhr@est.tech>
Diffstat (limited to 'datafile-app-server/src/test')
-rw-r--r-- | datafile-app-server/src/test/java/org/onap/dcaegen2/collectors/datafile/configuration/DatafileAppConfigTest.java | 42 | ||||
-rw-r--r-- | datafile-app-server/src/test/java/org/onap/dcaegen2/collectors/datafile/tasks/FileCollectorTest.java (renamed from datafile-app-server/src/test/java/org/onap/dcaegen2/collectors/datafile/tasks/XnfCollectorTaskImplTest.java) | 83 | ||||
-rw-r--r-- | datafile-app-server/src/test/java/org/onap/dcaegen2/collectors/datafile/tasks/ScheduledTasksTest.java | 2 |
3 files changed, 56 insertions, 71 deletions
diff --git a/datafile-app-server/src/test/java/org/onap/dcaegen2/collectors/datafile/configuration/DatafileAppConfigTest.java b/datafile-app-server/src/test/java/org/onap/dcaegen2/collectors/datafile/configuration/DatafileAppConfigTest.java index 443ddae7..2c136304 100644 --- a/datafile-app-server/src/test/java/org/onap/dcaegen2/collectors/datafile/configuration/DatafileAppConfigTest.java +++ b/datafile-app-server/src/test/java/org/onap/dcaegen2/collectors/datafile/configuration/DatafileAppConfigTest.java @@ -47,16 +47,15 @@ import org.onap.dcaegen2.collectors.datafile.integration.junit5.mockito.MockitoE */ @ExtendWith({MockitoExtension.class}) class AppConfigTest { - + private static final String DATAFILE_ENDPOINTS = "datafile_endpoints.json"; private static final boolean CORRECT_JSON = true; private static final boolean INCORRECT_JSON = false; private static AppConfig appConfigUnderTest; - - private static String filePath = Objects - .requireNonNull(AppConfigTest.class.getClassLoader().getResource(DATAFILE_ENDPOINTS)).getFile(); + private static String filePath = + Objects.requireNonNull(AppConfigTest.class.getClassLoader().getResource(DATAFILE_ENDPOINTS)).getFile(); @BeforeEach public void setUp() { @@ -70,7 +69,7 @@ class AppConfigTest { // Then verify(appConfigUnderTest, times(1)).setFilepath(anyString()); - verify(appConfigUnderTest, times(0)).initFileStreamReader(); + verify(appConfigUnderTest, times(0)).loadConfigurationFromFile(); Assertions.assertEquals(filePath, appConfigUnderTest.getFilepath()); } @@ -82,15 +81,12 @@ class AppConfigTest { // When appConfigUnderTest.setFilepath(filePath); - doReturn(inputStream).when(appConfigUnderTest).getInputStream(any()); - appConfigUnderTest.initFileStreamReader(); - appConfigUnderTest.dmaapConsumerConfiguration = appConfigUnderTest.getDmaapConsumerConfiguration(); - appConfigUnderTest.dmaapPublisherConfiguration = appConfigUnderTest.getDmaapPublisherConfiguration(); - appConfigUnderTest.ftpesConfig = appConfigUnderTest.getFtpesConfiguration(); + doReturn(inputStream).when(appConfigUnderTest).createInputStream(any()); + appConfigUnderTest.loadConfigurationFromFile(); // Then verify(appConfigUnderTest, times(1)).setFilepath(anyString()); - verify(appConfigUnderTest, times(1)).initFileStreamReader(); + verify(appConfigUnderTest, times(1)).loadConfigurationFromFile(); Assertions.assertNotNull(appConfigUnderTest.getDmaapConsumerConfiguration()); Assertions.assertNotNull(appConfigUnderTest.getDmaapPublisherConfiguration()); Assertions.assertEquals(appConfigUnderTest.getDmaapPublisherConfiguration(), @@ -98,7 +94,6 @@ class AppConfigTest { Assertions.assertEquals(appConfigUnderTest.getDmaapConsumerConfiguration(), appConfigUnderTest.getDmaapConsumerConfiguration()); Assertions.assertEquals(appConfigUnderTest.getFtpesConfiguration(), appConfigUnderTest.getFtpesConfiguration()); - } @Test @@ -108,11 +103,11 @@ class AppConfigTest { appConfigUnderTest.setFilepath(filePath); // When - appConfigUnderTest.initFileStreamReader(); + appConfigUnderTest.loadConfigurationFromFile(); // Then verify(appConfigUnderTest, times(1)).setFilepath(anyString()); - verify(appConfigUnderTest, times(1)).initFileStreamReader(); + verify(appConfigUnderTest, times(1)).loadConfigurationFromFile(); Assertions.assertNull(appConfigUnderTest.getDmaapConsumerConfiguration()); Assertions.assertNull(appConfigUnderTest.getDmaapPublisherConfiguration()); Assertions.assertNull(appConfigUnderTest.getFtpesConfiguration()); @@ -127,15 +122,15 @@ class AppConfigTest { // When appConfigUnderTest.setFilepath(filePath); - doReturn(inputStream).when(appConfigUnderTest).getInputStream(any()); - appConfigUnderTest.initFileStreamReader(); + doReturn(inputStream).when(appConfigUnderTest).createInputStream(any()); + appConfigUnderTest.loadConfigurationFromFile(); // Then verify(appConfigUnderTest, times(1)).setFilepath(anyString()); - verify(appConfigUnderTest, times(1)).initFileStreamReader(); - Assertions.assertNotNull(appConfigUnderTest.getDmaapConsumerConfiguration()); + verify(appConfigUnderTest, times(1)).loadConfigurationFromFile(); + Assertions.assertNull(appConfigUnderTest.getDmaapConsumerConfiguration()); Assertions.assertNull(appConfigUnderTest.getDmaapPublisherConfiguration()); - Assertions.assertNotNull(appConfigUnderTest.getFtpesConfiguration()); + Assertions.assertNull(appConfigUnderTest.getFtpesConfiguration()); } @@ -147,18 +142,15 @@ class AppConfigTest { new ByteArrayInputStream((getJsonConfig(CORRECT_JSON).getBytes(StandardCharsets.UTF_8))); // When appConfigUnderTest.setFilepath(filePath); - doReturn(inputStream).when(appConfigUnderTest).getInputStream(any()); + doReturn(inputStream).when(appConfigUnderTest).createInputStream(any()); JsonElement jsonElement = mock(JsonElement.class); when(jsonElement.isJsonObject()).thenReturn(false); doReturn(jsonElement).when(appConfigUnderTest).getJsonElement(any(JsonParser.class), any(InputStream.class)); - appConfigUnderTest.initFileStreamReader(); - appConfigUnderTest.dmaapConsumerConfiguration = appConfigUnderTest.getDmaapConsumerConfiguration(); - appConfigUnderTest.dmaapPublisherConfiguration = appConfigUnderTest.getDmaapPublisherConfiguration(); - appConfigUnderTest.ftpesConfig = appConfigUnderTest.getFtpesConfiguration(); + appConfigUnderTest.loadConfigurationFromFile(); // Then verify(appConfigUnderTest, times(1)).setFilepath(anyString()); - verify(appConfigUnderTest, times(1)).initFileStreamReader(); + verify(appConfigUnderTest, times(1)).loadConfigurationFromFile(); Assertions.assertNull(appConfigUnderTest.getDmaapConsumerConfiguration()); Assertions.assertNull(appConfigUnderTest.getDmaapPublisherConfiguration()); Assertions.assertNull(appConfigUnderTest.getFtpesConfiguration()); diff --git a/datafile-app-server/src/test/java/org/onap/dcaegen2/collectors/datafile/tasks/XnfCollectorTaskImplTest.java b/datafile-app-server/src/test/java/org/onap/dcaegen2/collectors/datafile/tasks/FileCollectorTest.java index b5d3c159..c266d50e 100644 --- a/datafile-app-server/src/test/java/org/onap/dcaegen2/collectors/datafile/tasks/XnfCollectorTaskImplTest.java +++ b/datafile-app-server/src/test/java/org/onap/dcaegen2/collectors/datafile/tasks/FileCollectorTest.java @@ -16,16 +16,21 @@ package org.onap.dcaegen2.collectors.datafile.tasks; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.spy; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verifyNoMoreInteractions; import static org.mockito.Mockito.when; + import java.nio.file.Path; import java.time.Duration; import java.util.HashMap; import java.util.Map; + import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import org.onap.dcaegen2.collectors.datafile.configuration.AppConfig; @@ -40,13 +45,14 @@ import org.onap.dcaegen2.collectors.datafile.model.ImmutableConsumerDmaapModel; import org.onap.dcaegen2.collectors.datafile.model.ImmutableFileData; import org.onap.dcaegen2.collectors.datafile.model.ImmutableMessageMetaData; import org.onap.dcaegen2.collectors.datafile.model.MessageMetaData; + import reactor.test.StepVerifier; /** * @author <a href="mailto:henrik.b.andersson@est.tech">Henrik Andersson</a> * */ -public class XnfCollectorTaskImplTest { +public class FileCollectorTest { private static final String PRODUCT_NAME = "NrRadio"; private static final String VENDOR_NAME = "Ericsson"; private static final String LAST_EPOCH_MICROSEC = "8745745764578"; @@ -70,7 +76,7 @@ public class XnfCollectorTaskImplTest { private static final String FTPES_LOCATION_NO_PORT = FTPES_SCHEME + USER + ":" + PWD + "@" + SERVER_ADDRESS + REMOTE_FILE_LOCATION; private static final String SFTP_LOCATION = SFTP_SCHEME + SERVER_ADDRESS + ":" + PORT_22 + REMOTE_FILE_LOCATION; - private static final String SFTP_LOCATION_NO_PORT = SFTP_SCHEME + SERVER_ADDRESS + REMOTE_FILE_LOCATION; + private static final String SFTP_LOCATION_NO_PORT = SFTP_SCHEME + SERVER_ADDRESS + REMOTE_FILE_LOCATION; private static final String GZIP_COMPRESSION = "gzip"; private static final String MEAS_COLLECT_FILE_FORMAT_TYPE = "org.3GPP.32.435#measCollec"; @@ -104,7 +110,7 @@ public class XnfCollectorTaskImplTest { // @formatter:on } - private FileData createFileData(String location) { + private FileData createFileData(String location, Scheme scheme) { // @formatter:off return ImmutableFileData.builder() .name(PM_FILE_NAME) @@ -112,7 +118,7 @@ public class XnfCollectorTaskImplTest { .compression(GZIP_COMPRESSION) .fileFormatType(MEAS_COLLECT_FILE_FORMAT_TYPE) .fileFormatVersion(FILE_FORMAT_VERSION) - .scheme(Scheme.FTPS) + .scheme(scheme) .build(); // @formatter:on } @@ -147,10 +153,10 @@ public class XnfCollectorTaskImplTest { @Test public void whenFtpesFile_returnCorrectResponse() throws Exception { - FileCollector collectorUndetTest = - new FileCollector(appConfigMock, ftpsClientMock, sftpClientMock); + FileCollector collectorUndetTest = spy(new FileCollector(appConfigMock)); + doReturn(ftpsClientMock).when(collectorUndetTest).createFtpsClient(any()); - FileData fileData = createFileData(FTPES_LOCATION_NO_PORT); + FileData fileData = createFileData(FTPES_LOCATION_NO_PORT, Scheme.FTPS); ConsumerDmaapModel expectedConsumerDmaapModel = createExpectedConsumerDmaapModel(FTPES_LOCATION_NO_PORT); @@ -159,56 +165,43 @@ public class XnfCollectorTaskImplTest { .expectNext(expectedConsumerDmaapModel).verifyComplete(); verify(ftpsClientMock, times(1)).collectFile(REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION); - verify(ftpsClientMock).setKeyCertPath(FTP_KEY_PATH); - verify(ftpsClientMock).setKeyCertPassword(FTP_KEY_PASSWORD); - verify(ftpsClientMock).setTrustedCAPath(TRUSTED_CA_PATH); - verify(ftpsClientMock).setTrustedCAPassword(TRUSTED_CA_PASSWORD); + verify(ftpsClientMock, times(1)).close(); + verifyNoMoreInteractions(ftpsClientMock); } @Test public void whenSftpFile_returnCorrectResponse() throws Exception { - FileCollector collectorUndetTest = - new FileCollector(appConfigMock, ftpsClientMock, sftpClientMock); - // @formatter:off - FileData fileData = ImmutableFileData.builder() - .name(PM_FILE_NAME) - .location(SFTP_LOCATION_NO_PORT) - .compression(GZIP_COMPRESSION) - .fileFormatType(MEAS_COLLECT_FILE_FORMAT_TYPE) - .fileFormatVersion(FILE_FORMAT_VERSION) - .scheme(Scheme.SFTP) - .build(); + FileCollector collectorUndetTest = spy(new FileCollector(appConfigMock)); + doReturn(sftpClientMock).when(collectorUndetTest).createSftpClient(any()); - ConsumerDmaapModel expectedConsumerDmaapModel = ImmutableConsumerDmaapModel.builder() - .productName(PRODUCT_NAME) - .vendorName(VENDOR_NAME) - .lastEpochMicrosec(LAST_EPOCH_MICROSEC) - .sourceName(SOURCE_NAME) - .startEpochMicrosec(START_EPOCH_MICROSEC) - .timeZoneOffset(TIME_ZONE_OFFSET) - .name(PM_FILE_NAME) - .location(SFTP_LOCATION_NO_PORT) - .internalLocation(LOCAL_FILE_LOCATION.toString()) - .compression(GZIP_COMPRESSION) - .fileFormatType(MEAS_COLLECT_FILE_FORMAT_TYPE) - .fileFormatVersion(FILE_FORMAT_VERSION) - .build(); - // @formatter:on + FileData fileData = createFileData(SFTP_LOCATION_NO_PORT, Scheme.SFTP); + ConsumerDmaapModel expectedConsumerDmaapModel = createExpectedConsumerDmaapModel(SFTP_LOCATION_NO_PORT); Map<String, String> contextMap = new HashMap<>(); + StepVerifier.create(collectorUndetTest.execute(fileData, createMessageMetaData(), 3, Duration.ofSeconds(0), contextMap)) + .expectNext(expectedConsumerDmaapModel) // + .verifyComplete(); + + // The same again, but with port + fileData = createFileData(SFTP_LOCATION, Scheme.SFTP); + expectedConsumerDmaapModel = createExpectedConsumerDmaapModel(SFTP_LOCATION); + StepVerifier.create(collectorUndetTest.execute(fileData, createMessageMetaData(), 3, Duration.ofSeconds(0), contextMap)) - .expectNext(expectedConsumerDmaapModel).verifyComplete(); + .expectNext(expectedConsumerDmaapModel) // + .verifyComplete(); - verify(sftpClientMock, times(1)).collectFile(REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION); + verify(sftpClientMock, times(2)).collectFile(REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION); + verify(sftpClientMock, times(2)).close(); verifyNoMoreInteractions(sftpClientMock); } @Test public void whenFtpesFileAlwaysFail_retryAndFail() throws Exception { - FileCollector collectorUndetTest = - new FileCollector(appConfigMock, ftpsClientMock, sftpClientMock); - FileData fileData = createFileData(FTPES_LOCATION); + FileCollector collectorUndetTest = spy(new FileCollector(appConfigMock)); + doReturn(ftpsClientMock).when(collectorUndetTest).createFtpsClient(any()); + + FileData fileData = createFileData(FTPES_LOCATION, Scheme.FTPS); doThrow(new DatafileTaskException("Unable to collect file.")).when(ftpsClientMock) .collectFile(REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION); @@ -221,14 +214,14 @@ public class XnfCollectorTaskImplTest { @Test public void whenFtpesFileFailOnce_retryAndReturnCorrectResponse() throws Exception { - FileCollector collectorUndetTest = - new FileCollector(appConfigMock, ftpsClientMock, sftpClientMock); + FileCollector collectorUndetTest = spy(new FileCollector(appConfigMock)); + doReturn(ftpsClientMock).when(collectorUndetTest).createFtpsClient(any()); doThrow(new DatafileTaskException("Unable to collect file.")).doNothing().when(ftpsClientMock) .collectFile(REMOTE_FILE_LOCATION, LOCAL_FILE_LOCATION); ConsumerDmaapModel expectedConsumerDmaapModel = createExpectedConsumerDmaapModel(FTPES_LOCATION_NO_PORT); - FileData fileData = createFileData(FTPES_LOCATION_NO_PORT); + FileData fileData = createFileData(FTPES_LOCATION_NO_PORT, Scheme.FTPS); Map<String, String> contextMap = new HashMap<>(); StepVerifier.create(collectorUndetTest.execute(fileData, createMessageMetaData(), 3, Duration.ofSeconds(0), contextMap)) .expectNext(expectedConsumerDmaapModel).verifyComplete(); diff --git a/datafile-app-server/src/test/java/org/onap/dcaegen2/collectors/datafile/tasks/ScheduledTasksTest.java b/datafile-app-server/src/test/java/org/onap/dcaegen2/collectors/datafile/tasks/ScheduledTasksTest.java index 8a572be4..8c4b3891 100644 --- a/datafile-app-server/src/test/java/org/onap/dcaegen2/collectors/datafile/tasks/ScheduledTasksTest.java +++ b/datafile-app-server/src/test/java/org/onap/dcaegen2/collectors/datafile/tasks/ScheduledTasksTest.java @@ -82,7 +82,7 @@ public class ScheduledTasksTest { doReturn(dmaapPublisherConfiguration).when(appConfig).getDmaapPublisherConfiguration(); doReturn(consumerMock).when(testedObject).createConsumerTask(); - doReturn(fileCollectorMock).when(testedObject).createFileCollector(notNull()); + doReturn(fileCollectorMock).when(testedObject).createFileCollector(); doReturn(dataRouterMock).when(testedObject).createDataRouterPublisher(); } |