diff options
author | elinuxhenrik <henrik.b.andersson@est.tech> | 2018-10-19 13:04:36 +0200 |
---|---|---|
committer | elinuxhenrik <henrik.b.andersson@est.tech> | 2018-11-15 10:40:09 +0100 |
commit | 598a0a928db52111744837e9d3daae4c3a5dbcdf (patch) | |
tree | bcf8099f333a7e47f06595c067ab47d6e0065944 /datafile-dmaap-client/src/test | |
parent | 7302b143f94d825f198def8c3b22c2920e6323a6 (diff) |
Clean up for Sonar
Change-Id: I5daa606cd974e43c899b7e35b14d268a255c7102
Issue-ID: DCAEGEN2-835
Signed-off-by: elinuxhenrik <henrik.b.andersson@est.tech>
Diffstat (limited to 'datafile-dmaap-client/src/test')
6 files changed, 125 insertions, 82 deletions
diff --git a/datafile-dmaap-client/src/test/java/org/onap/dcaegen2/collectors/datafile/ftp/ErrorDataTest.java b/datafile-dmaap-client/src/test/java/org/onap/dcaegen2/collectors/datafile/ftp/ErrorDataTest.java index e40d7d78..b4edf82c 100644 --- a/datafile-dmaap-client/src/test/java/org/onap/dcaegen2/collectors/datafile/ftp/ErrorDataTest.java +++ b/datafile-dmaap-client/src/test/java/org/onap/dcaegen2/collectors/datafile/ftp/ErrorDataTest.java @@ -1,57 +1,43 @@ /* * ============LICENSE_START====================================================================== - * Copyright (C) 2018 NOKIA Intellectual Property, 2018 Nordix Foundation. All rights reserved. + * Copyright (C) 2018 Nordix Foundation. All rights reserved. * =============================================================================================== - * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * - * Unless required by applicable law or agreed to in writing, software distributed under the License - * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express - * or implied. See the License for the specific language governing permissions and limitations under - * the License. + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. * ============LICENSE_END======================================================================== */ package org.onap.dcaegen2.collectors.datafile.ftp; -import static org.mockito.Mockito.mock; +import static org.junit.jupiter.api.Assertions.assertEquals; -import java.util.ArrayList; -import java.util.List; - -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; public class ErrorDataTest { - private List<String> errorMessages = new ArrayList<>(); - private List<Throwable> errorCauses = new ArrayList<>(); - private ErrorData errorData = new ErrorData(); - - @BeforeEach - protected void setUp() { - int testSize = 3; - for (int i = 0; i < testSize; i++) { - errorMessages.add("test"); - errorCauses.add(mock(Throwable.class)); - } - for (int i = 0; i < testSize; i++) { - errorData.addError(errorMessages.get(i), errorCauses.get(i)); - } - } - public String getMessageAsString() { - StringBuilder message = new StringBuilder(); - for (int i = 0; i < errorMessages.size(); i++) { - message.append(errorMessages.get(i)).append(" Cause: ").append(errorCauses.get(i)).append("\n"); - } - return message.toString(); + @Test + public void emptyData() { + ErrorData dataUnderTest = new ErrorData(); + + assertEquals("", dataUnderTest.toString()); } @Test - public void testToString_returnExpectedString() { - Assertions.assertEquals(getMessageAsString(), errorData.toString()); + public void withData() { + ErrorData dataUnderTest = new ErrorData(); + dataUnderTest.addError("Error", null); + dataUnderTest.addError("Null", new NullPointerException("Null")); + + assertEquals("Error\nNull Cause: java.lang.NullPointerException: Null", dataUnderTest.toString()); } } + diff --git a/datafile-dmaap-client/src/test/java/org/onap/dcaegen2/collectors/datafile/ftp/FileCollectResultTest.java b/datafile-dmaap-client/src/test/java/org/onap/dcaegen2/collectors/datafile/ftp/FileCollectResultTest.java new file mode 100644 index 00000000..083727e4 --- /dev/null +++ b/datafile-dmaap-client/src/test/java/org/onap/dcaegen2/collectors/datafile/ftp/FileCollectResultTest.java @@ -0,0 +1,45 @@ +/* + * ============LICENSE_START====================================================================== + * Copyright (C) 2018 Nordix Foundation. All rights reserved. + * =============================================================================================== + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END======================================================================== + */ + +package org.onap.dcaegen2.collectors.datafile.ftp; + +import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; + +import org.junit.jupiter.api.Test; + +public class FileCollectResultTest { + + @Test + public void successfulResult() { + FileCollectResult resultUnderTest = new FileCollectResult(); + assertTrue(resultUnderTest.downloadSuccessful()); + assertEquals("FileCollectResult: true Error data: ", resultUnderTest.toString()); + } + + @Test + public void unSuccessfulResult() { + ErrorData errorData = new ErrorData(); + errorData.addError("Error", null); + errorData.addError("Null", new NullPointerException()); + FileCollectResult resultUnderTest = new FileCollectResult(errorData); + assertFalse(resultUnderTest.downloadSuccessful()); + assertEquals("FileCollectResult: false Error data: " + errorData.toString(), resultUnderTest.toString()); + } +} diff --git a/datafile-dmaap-client/src/test/java/org/onap/dcaegen2/collectors/datafile/ftp/FtpsClientTest.java b/datafile-dmaap-client/src/test/java/org/onap/dcaegen2/collectors/datafile/ftp/FtpsClientTest.java index 66d3b00c..2157e176 100644 --- a/datafile-dmaap-client/src/test/java/org/onap/dcaegen2/collectors/datafile/ftp/FtpsClientTest.java +++ b/datafile-dmaap-client/src/test/java/org/onap/dcaegen2/collectors/datafile/ftp/FtpsClientTest.java @@ -135,7 +135,7 @@ public class FtpsClientTest { @Test public void collectFileFaultyOwnKey_shouldFail() throws Exception { - doThrow(new GeneralSecurityException()) + doThrow(new IKeyManagerUtils.KeyManagerException(new GeneralSecurityException())) .when(keyManagerUtilsMock).setCredentials(FTP_KEY_PATH, FTP_KEY_PASSWORD); ImmutableFileServerData fileServerData = ImmutableFileServerData.builder().serverAddress(XNF_ADDRESS) diff --git a/datafile-dmaap-client/src/test/java/org/onap/dcaegen2/collectors/datafile/ftp/SftpClientTest.java b/datafile-dmaap-client/src/test/java/org/onap/dcaegen2/collectors/datafile/ftp/SftpClientTest.java index 13f1fbb9..e9e68bb8 100644 --- a/datafile-dmaap-client/src/test/java/org/onap/dcaegen2/collectors/datafile/ftp/SftpClientTest.java +++ b/datafile-dmaap-client/src/test/java/org/onap/dcaegen2/collectors/datafile/ftp/SftpClientTest.java @@ -21,6 +21,8 @@ package org.onap.dcaegen2.collectors.datafile.ftp; import static java.nio.charset.StandardCharsets.UTF_8; import static org.apache.commons.io.IOUtils.toByteArray; import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; import com.github.stefanbirkner.fakesftpserver.rule.FakeSftpServerRule; import com.jcraft.jsch.ChannelSftp; @@ -63,6 +65,38 @@ public class SftpClientTest { assertThat(new String(localFile, UTF_8)).isEqualTo(DUMMY_CONTENT); } + @Test + public void collectFile_withWrongUserName_shouldFail() throws IOException, JSchException, SftpException { + SftpClient sftpClient = new SftpClient(); + sftpServer.putFile(REMOTE_DUMMY_FILE, DUMMY_CONTENT, UTF_8); + byte[] file = downloadFile(sftpServer, REMOTE_DUMMY_FILE); + FileServerData expectedFileServerData = ImmutableFileServerData.builder().serverAddress("127.0.0.1") + .userId("Wrong").password(PASSWORD).port(sftpServer.getPort()).build(); + FileCollectResult actualResult = sftpClient.collectFile(expectedFileServerData, REMOTE_DUMMY_FILE, + LOCAL_DUMMY_FILE); + + assertFalse(actualResult.downloadSuccessful()); + String expectedErrorMessage = "Unable to set up SFTP connection to xNF. Data: " + + "FileServerData{serverAddress=127.0.0.1, userId=Wrong, password=123, port="; + assertTrue(actualResult.getErrorData().toString().startsWith(expectedErrorMessage)); + } + + @Test + public void collectFile_withWrongFileName_shouldFail() throws IOException, JSchException, SftpException { + SftpClient sftpClient = new SftpClient(); + sftpServer.putFile(REMOTE_DUMMY_FILE, DUMMY_CONTENT, UTF_8); + byte[] file = downloadFile(sftpServer, REMOTE_DUMMY_FILE); + FileServerData expectedFileServerData = ImmutableFileServerData.builder().serverAddress("127.0.0.1") + .userId(USERNAME).password(PASSWORD).port(sftpServer.getPort()).build(); + FileCollectResult actualResult = sftpClient.collectFile(expectedFileServerData, "wrong", + LOCAL_DUMMY_FILE); + + assertFalse(actualResult.downloadSuccessful()); + String expectedErrorMessage = "Unable to get file from xNF. Data: FileServerData{serverAddress=127.0.0.1, " + + "userId=bob, password=123, port="; + assertTrue(actualResult.getErrorData().toString().startsWith(expectedErrorMessage)); + } + private static Session connectToServer(FakeSftpServerRule sftpServer) throws JSchException { return connectToServerAtPort(sftpServer.getPort()); } diff --git a/datafile-dmaap-client/src/test/java/org/onap/dcaegen2/collectors/datafile/service/producer/DmaapProducerReactiveHttpClientTest.java b/datafile-dmaap-client/src/test/java/org/onap/dcaegen2/collectors/datafile/service/producer/DmaapProducerReactiveHttpClientTest.java index ba424626..87c99f88 100644 --- a/datafile-dmaap-client/src/test/java/org/onap/dcaegen2/collectors/datafile/service/producer/DmaapProducerReactiveHttpClientTest.java +++ b/datafile-dmaap-client/src/test/java/org/onap/dcaegen2/collectors/datafile/service/producer/DmaapProducerReactiveHttpClientTest.java @@ -39,7 +39,7 @@ import org.onap.dcaegen2.collectors.datafile.config.DmaapPublisherConfiguration; import org.onap.dcaegen2.collectors.datafile.io.IFileSystemResource; import org.onap.dcaegen2.collectors.datafile.model.CommonFunctions; import org.onap.dcaegen2.collectors.datafile.model.ConsumerDmaapModel; -import org.onap.dcaegen2.collectors.datafile.model.ConsumerDmaapModelForUnitTest; +import org.onap.dcaegen2.collectors.datafile.model.ImmutableConsumerDmaapModel; import org.onap.dcaegen2.collectors.datafile.web.IRestTemplate; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; @@ -74,7 +74,7 @@ class DmaapProducerReactiveHttpClientTest { private DmaapProducerReactiveHttpClient dmaapProducerReactiveHttpClient; private DmaapPublisherConfiguration dmaapPublisherConfigurationMock = mock(DmaapPublisherConfiguration.class); - private ConsumerDmaapModel consumerDmaapModel = new ConsumerDmaapModelForUnitTest(); + private ConsumerDmaapModel consumerDmaapModel; private IFileSystemResource fileSystemResourceMock = mock(IFileSystemResource.class); private IRestTemplate restTemplateMock = mock(IRestTemplate.class); @@ -92,6 +92,10 @@ class DmaapProducerReactiveHttpClientTest { when(dmaapPublisherConfigurationMock.dmaapContentType()).thenReturn(APPLICATION_OCTET_STREAM_CONTENT_TYPE); when(dmaapPublisherConfigurationMock.dmaapTopicName()).thenReturn(PUBLISH_TOPIC); + consumerDmaapModel = ImmutableConsumerDmaapModel.builder().name(FILE_NAME) + .location("target/A20161224.1030-1045.bin.gz").compression("gzip") + .fileFormatType("org.3GPP.32.435#measCollec").fileFormatVersion("V10").build(); + dmaapProducerReactiveHttpClient = new DmaapProducerReactiveHttpClient(dmaapPublisherConfigurationMock); dmaapProducerReactiveHttpClient.setFileSystemResource(fileSystemResourceMock); dmaapProducerReactiveHttpClient.setRestTemplate(restTemplateMock); @@ -99,7 +103,7 @@ class DmaapProducerReactiveHttpClientTest { @Test void getHttpResponse_Success() throws Exception { - mockWebClientDependantObject(); + mockWebClientDependantObject(true); StepVerifier.create(dmaapProducerReactiveHttpClient.getDmaapProducerResponse(consumerDmaapModel)) .expectNext(HttpStatus.OK.toString()).verifyComplete(); @@ -130,11 +134,23 @@ class DmaapProducerReactiveHttpClientTest { verifyNoMoreInteractions(restTemplateMock); } - private void mockWebClientDependantObject() throws IOException { + @Test + void getHttpResponse_Fail() throws Exception { + mockWebClientDependantObject(false); + + StepVerifier.create(dmaapProducerReactiveHttpClient.getDmaapProducerResponse(consumerDmaapModel)) + .verifyComplete(); + } + + private void mockWebClientDependantObject(boolean success) throws IOException { fileStream = new ByteArrayInputStream(FILE_CONTENT.getBytes()); when(fileSystemResourceMock.getInputStream()).thenReturn(fileStream); - when(restTemplateMock.exchange(any(), any(), any(), any())).thenReturn(responseEntityMock); - when(responseEntityMock.getStatusCode()).thenReturn(HttpStatus.OK); + if (success) { + when(restTemplateMock.exchange(any(), any(), any(), any())).thenReturn(responseEntityMock); + when(responseEntityMock.getStatusCode()).thenReturn(HttpStatus.OK); + } else { + when(restTemplateMock.exchange(any(), any(), any(), any())).thenThrow(new RuntimeException()); + } } } diff --git a/datafile-dmaap-client/src/test/java/org/onap/dcaegen2/collectors/datafile/service/producer/PublishRedirectStrategyTest.java b/datafile-dmaap-client/src/test/java/org/onap/dcaegen2/collectors/datafile/service/producer/PublishRedirectStrategyTest.java deleted file mode 100644 index 74ad6722..00000000 --- a/datafile-dmaap-client/src/test/java/org/onap/dcaegen2/collectors/datafile/service/producer/PublishRedirectStrategyTest.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * ============LICENSE_START====================================================================== - * Copyright (C) 2018 Nordix Foundation. All rights reserved. - * =============================================================================================== - * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except - * in compliance with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software distributed under the License - * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express - * or implied. See the License for the specific language governing permissions and limitations under - * the License. - * ============LICENSE_END======================================================================== - */ - -package org.onap.dcaegen2.collectors.datafile.service.producer; - -import static org.junit.jupiter.api.Assertions.*; - -import org.apache.http.client.methods.HttpPut; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; - -class PublishRedirectStrategyTest { - - private PublishRedirectStrategy redirectStrategy; - - @BeforeEach - void setUp() { - redirectStrategy = new PublishRedirectStrategy(); - } - - @Test - void isRedirectable_shouldReturnTrue() { - assertTrue(redirectStrategy.isRedirectable(HttpPut.METHOD_NAME)); - } -}
\ No newline at end of file |