diff options
author | Stanislav Marszalek <s.marszalek2@partner.samsung.com> | 2021-07-19 07:57:08 +0200 |
---|---|---|
committer | Stanislav Marszalek <s.marszalek2@partner.samsung.com> | 2021-07-28 12:24:54 +0000 |
commit | c7bc54b6e81532ad906de88f5ad7ed63259ff586 (patch) | |
tree | bbd9fdff15fb29176685b6a18161c58b0ae13318 | |
parent | 9a735bbccf18e984a9ae7c6bb81ee133ad911941 (diff) |
O1 PM Bulk support - Unit test - I
Issue-ID: INT-1945
Signed-off-by: Stanislav Marszalek <s.marszalek2@partner.samsung.com>
Change-Id: I02d3684c98d563d7f386de2fdf032e930ac41b2f
8 files changed, 393 insertions, 3 deletions
@@ -42,6 +42,11 @@ <artifactId>spring-webflux</artifactId> </dependency> <dependency> + <groupId>io.projectreactor</groupId> + <artifactId>reactor-test</artifactId> + <scope>test</scope> + </dependency> + <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-websocket</artifactId> </dependency> @@ -64,7 +69,6 @@ <artifactId>lombok</artifactId> <scope>provided</scope> </dependency> - <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> diff --git a/src/main/java/org/onap/a1pesimulator/data/ves/CommonEventHeader.java b/src/main/java/org/onap/a1pesimulator/data/ves/CommonEventHeader.java index baa8ab5..619f5ae 100644 --- a/src/main/java/org/onap/a1pesimulator/data/ves/CommonEventHeader.java +++ b/src/main/java/org/onap/a1pesimulator/data/ves/CommonEventHeader.java @@ -56,6 +56,8 @@ public class CommonEventHeader { private String reportingEntityId; + private String nfVendorName; + private String nfcNamingCode; private String nfNamingCode; diff --git a/src/main/java/org/onap/a1pesimulator/service/fileready/FtpServerService.java b/src/main/java/org/onap/a1pesimulator/service/fileready/FtpServerService.java index d71cdf0..a4145d5 100644 --- a/src/main/java/org/onap/a1pesimulator/service/fileready/FtpServerService.java +++ b/src/main/java/org/onap/a1pesimulator/service/fileready/FtpServerService.java @@ -142,7 +142,7 @@ public class FtpServerService { * * @return SSHClient */ - private SSHClient getSSHClient() { + protected SSHClient getSSHClient() { SSHClient client = new SSHClient(); try { client.addHostKeyVerifier(new PromiscuousVerifier()); @@ -191,7 +191,7 @@ public class FtpServerService { * @param fileData data about files which needs to be deleted * @return empty Mono object */ - private Mono<FileData> resumeError(Throwable throwable, FileData fileData) { + protected Mono<FileData> resumeError(Throwable throwable, FileData fileData) { log.error("Error occurs while uploading file to FTP server", throwable); deletePMBulkFile(fileData.getPmBulkFile()); deletePMBulkFile(fileData.getArchivedPmBulkFile()); diff --git a/src/test/java/org/onap/a1pesimulator/TestHelpers.java b/src/test/java/org/onap/a1pesimulator/TestHelpers.java index 449318d..f336340 100644 --- a/src/test/java/org/onap/a1pesimulator/TestHelpers.java +++ b/src/test/java/org/onap/a1pesimulator/TestHelpers.java @@ -17,6 +17,11 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.util.List; + import org.onap.a1pesimulator.data.cell.CellDetails; import org.onap.a1pesimulator.data.cell.CellWithStatus; import org.onap.a1pesimulator.data.ue.UserEquipment; @@ -68,6 +73,18 @@ public class TestHelpers { assertFalse(cellWithStatus.isVesEnabled()); } + public static void deleteTempFiles(List<File> files) { + files.forEach(file -> { + try { + if (Files.exists(file.toPath())) { + Files.delete(file.toPath()); + } + } catch (IOException e) { + e.printStackTrace(); + } + }); + } + private TestHelpers() { } } diff --git a/src/test/java/org/onap/a1pesimulator/service/fileready/CommonFileReady.java b/src/test/java/org/onap/a1pesimulator/service/fileready/CommonFileReady.java new file mode 100644 index 0000000..75ce7a0 --- /dev/null +++ b/src/test/java/org/onap/a1pesimulator/service/fileready/CommonFileReady.java @@ -0,0 +1,52 @@ +package org.onap.a1pesimulator.service.fileready; + +import static org.onap.a1pesimulator.TestHelpers.deleteTempFiles; +import static org.onap.a1pesimulator.util.Constants.TEMP_DIR; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.mockito.MockitoAnnotations; + +public class CommonFileReady { + + public List<File> filesToDelete; //we collect files created during testing and then delete them + public static final String PM_BULK_FILE = "pmBulkFile.xml"; + public static final String ARCHIVED_PM_BULK_FILE = "pmBulkFile.xml.gz"; + + @BeforeEach + void setUp() { + MockitoAnnotations.initMocks(this); + filesToDelete = Collections.synchronizedList(new ArrayList<>()); + } + + @AfterEach + void cleanUpFiles() { + deleteTempFiles(filesToDelete); + } + + /** + * Create temp file with simple text and adds it to filesToDelete list + * + * @param fileName name of file + * @return created file + */ + public File createTempFile(String fileName) { + try { + File tmpFile = new File(TEMP_DIR, fileName); + tmpFile.createNewFile(); + Files.write(tmpFile.toPath(), "sample text".getBytes()); + filesToDelete.add(tmpFile); + return tmpFile; + } catch (IOException e) { + e.printStackTrace(); + return null; + } + } +} diff --git a/src/test/java/org/onap/a1pesimulator/service/fileready/FileReadyEventServiceTest.java b/src/test/java/org/onap/a1pesimulator/service/fileready/FileReadyEventServiceTest.java new file mode 100644 index 0000000..11ae637 --- /dev/null +++ b/src/test/java/org/onap/a1pesimulator/service/fileready/FileReadyEventServiceTest.java @@ -0,0 +1,75 @@ +package org.onap.a1pesimulator.service.fileready; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.nio.file.InvalidPathException; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.onap.a1pesimulator.data.fileready.FileData; + +import reactor.core.publisher.Mono; +import reactor.test.StepVerifier; + +class FileReadyEventServiceTest extends CommonFileReady { + + @Mock + private FtpServerService ftpServerService; + + @InjectMocks + private FileReadyEventService fileReadyEventService; + + @BeforeEach + void setUp() { + super.setUp(); + when(ftpServerService.getFtpPath()).thenReturn(""); + } + + @Test + void createFileReadyEventAndDeleteTmpFile() { + Mono<FileData> fileMono = Mono.just(getTestFileData()); + FileData expectedFileData = fileReadyEventService.createFileReadyEvent(getTestFileData()); + StepVerifier.create(fileReadyEventService.createFileReadyEventAndDeleteTmpFile(fileMono)) + .expectNext(expectedFileData) + .verifyComplete(); + Mono<FileData> resultFileData = fileReadyEventService.createFileReadyEventAndDeleteTmpFile(fileMono); + assertFileDataResults(resultFileData.block()); + verify(ftpServerService, times(3)).getFtpPath(); + } + + @Test + void createFileReadyEvent() { + FileData resultFileData = fileReadyEventService.createFileReadyEvent(getTestFileData()); + assertFileDataResults(resultFileData); + verify(ftpServerService, times(1)).getFtpPath(); + } + + /** + * Common asserst for all tests here + */ + private void assertFileDataResults(FileData fileData) { + assertNotNull(fileData); + assertNotNull(fileData.getFileReadyEvent()); + assertEquals(ARCHIVED_PM_BULK_FILE, fileData.getFileReadyEvent().getNotificationFields().getArrayOfNamedHashMap().get(0).getHashMap().get("location")); + } + + /** + * Creates FileData object for test cases + * + * @return test FileData object + */ + private FileData getTestFileData() { + try { + return FileData.builder().pmBulkFile(createTempFile(PM_BULK_FILE)).archivedPmBulkFile(createTempFile(ARCHIVED_PM_BULK_FILE)).build(); + } catch (InvalidPathException e) { + e.printStackTrace(); + } + return null; + } +}
\ No newline at end of file diff --git a/src/test/java/org/onap/a1pesimulator/service/fileready/FtpServerServiceTest.java b/src/test/java/org/onap/a1pesimulator/service/fileready/FtpServerServiceTest.java new file mode 100644 index 0000000..e8b77ef --- /dev/null +++ b/src/test/java/org/onap/a1pesimulator/service/fileready/FtpServerServiceTest.java @@ -0,0 +1,139 @@ +package org.onap.a1pesimulator.service.fileready; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.doNothing; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.onap.a1pesimulator.service.fileready.FtpServerService.deletePMBulkFile; +import static org.onap.a1pesimulator.util.Constants.TEMP_DIR; + +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import org.junit.jupiter.api.Test; +import org.mockito.Mock; +import org.mockito.Spy; +import org.onap.a1pesimulator.data.fileready.FileData; +import org.slf4j.LoggerFactory; +import org.springframework.test.util.ReflectionTestUtils; + +import ch.qos.logback.classic.Logger; +import ch.qos.logback.classic.spi.ILoggingEvent; +import ch.qos.logback.core.read.ListAppender; +import net.schmizz.sshj.SSHClient; +import net.schmizz.sshj.sftp.SFTPClient; +import reactor.test.StepVerifier; + + +class FtpServerServiceTest extends CommonFileReady { + + @Spy + FtpServerService ftpServerService; + + @Mock + SSHClient sshClient; + + @Mock + SFTPClient sftpClient; + + /** + * Test to save archived PM Bulk File into specify directory + */ + @Test + void saveFileToFtp() { + ReflectionTestUtils.setField(ftpServerService, "xmlPmLocation", TEMP_DIR); + File archivedPmBulkFile = createTempFile(ARCHIVED_PM_BULK_FILE); + FileData testFileData = getTestFileData(); + FileData expectedFileData = FileData.builder().archivedPmBulkFile(archivedPmBulkFile).pmBulkFile(testFileData.getPmBulkFile()).build(); + expectedFileData.setArchivedPmBulkFile(archivedPmBulkFile); + + StepVerifier.create(ftpServerService.uploadFileToFtp(testFileData)) + .expectNext(expectedFileData) + .verifyComplete(); + } + + /** + * Test successful FTP upload + */ + @Test + void uploadFileToFtp() { + ReflectionTestUtils.setField(ftpServerService, "ftpServerUpload", true); + doReturn(sshClient).when(ftpServerService).getSSHClient(); + try { + doReturn(sftpClient).when(sshClient).newSFTPClient(); + doNothing().when(sftpClient).put(anyString(), anyString()); + } catch (IOException e) { + e.printStackTrace(); + } + + File archivedPmBulkFile = createTempFile(ARCHIVED_PM_BULK_FILE); + FileData testFileData = getTestFileData(); + FileData expectedFileData = FileData.builder().archivedPmBulkFile(archivedPmBulkFile).pmBulkFile(testFileData.getPmBulkFile()).build(); + expectedFileData.setArchivedPmBulkFile(archivedPmBulkFile); + + StepVerifier.create(ftpServerService.uploadFileToFtp(testFileData)) + .expectNext(expectedFileData).verifyComplete(); + } + + /** + * Test error while trying to upload archived PM Bulk File + */ + @Test + void errorWhileUploadingFileToFtp() { + ReflectionTestUtils.setField(ftpServerService, "ftpServerUpload", true); + ReflectionTestUtils.setField(ftpServerService, "ftpServerPort", "22"); + FileData testFileData = getTestFileData(); + StepVerifier.create(ftpServerService.uploadFileToFtp(testFileData)) + .verifyComplete(); + verify(ftpServerService, times(1)).resumeError(any(), any()); + } + + /** + * Test error while trying to delete not existing file + */ + @Test + void errorWhileDeletingFile() { + Logger testLog = (Logger) LoggerFactory.getLogger(FtpServerService.class); + ListAppender<ILoggingEvent> appender = new ListAppender<>(); + appender.start(); + testLog.addAppender(appender); + + deletePMBulkFile(new File("test.txt")); + assertThat(appender.list).extracting(ILoggingEvent::getFormattedMessage).containsExactly("Could not delete file: test.txt"); + } + + /** + * Test if path to FTP is created correctly + */ + @Test + void getFtpPath() { + List<String> ftpPathVars = new ArrayList<>(); + ftpPathVars.add("ftpServerProtocol"); + ftpPathVars.add("ftpServerUsername"); + ftpPathVars.add("ftpServerPassword"); + ftpPathVars.add("ftpServerUrl"); + ftpPathVars.add("ftpServerPort"); + ftpPathVars.add("ftpServerFilepath"); + + ftpPathVars.forEach(var -> ReflectionTestUtils.setField(ftpServerService, var, var)); + String ftpPath = ftpServerService.getFtpPath(); + + assertTrue(ftpPathVars.stream().allMatch(ftpPath::contains)); + } + + /** + * Creates FileData object for test cases + * + * @return test FileData object + */ + private FileData getTestFileData() { + return FileData.builder().pmBulkFile(createTempFile(PM_BULK_FILE)).build(); + } + +}
\ No newline at end of file diff --git a/src/test/java/org/onap/a1pesimulator/service/fileready/PMBulkFileServiceTest.java b/src/test/java/org/onap/a1pesimulator/service/fileready/PMBulkFileServiceTest.java new file mode 100644 index 0000000..71c92ff --- /dev/null +++ b/src/test/java/org/onap/a1pesimulator/service/fileready/PMBulkFileServiceTest.java @@ -0,0 +1,101 @@ +package org.onap.a1pesimulator.service.fileready; + +import static org.junit.jupiter.api.Assertions.assertNotNull; + +import java.io.IOException; +import java.net.URISyntaxException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.time.ZonedDateTime; +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.InjectMocks; +import org.onap.a1pesimulator.data.fileready.EventMemoryHolder; +import org.onap.a1pesimulator.data.fileready.FileData; +import org.onap.a1pesimulator.data.ves.VesEvent; +import org.onap.a1pesimulator.service.VesBrokerServiceImplTest; +import org.onap.a1pesimulator.util.VnfConfigReader; +import org.springframework.test.util.ReflectionTestUtils; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; + +import reactor.core.publisher.Mono; + +class PMBulkFileServiceTest extends CommonFileReady { + + private static final Integer NO_OF_EVENTS = 3; + + private PMBulkFileService pmBulkFileService; + + @InjectMocks + VnfConfigReader vnfConfigReader; + + @InjectMocks + private ObjectMapper mapper; + + @BeforeEach + void setUp() { + super.setUp(); + ReflectionTestUtils.setField(vnfConfigReader, "vnfConfigFile", "src/test/resources/vnf.config"); + pmBulkFileService = new PMBulkFileService(vnfConfigReader); + } + + @Test + void generatePMBulkFileXml() { + Mono<FileData> monoFileData = pmBulkFileService.generatePMBulkFileXml(getTestEvents()); + FileData fileData = monoFileData.block(); + assertNotNull(fileData); + assertNotNull(fileData.getPmBulkFile()); + } + + /** + * Generate NO_OF_EVENTS test EventMemoryHolder list + * + * @return EventMemoryHolder list + */ + private List<EventMemoryHolder> getTestEvents() { + List<EventMemoryHolder> collectedEvents = new ArrayList<>(); + for (int i = 0; i < NO_OF_EVENTS; i++) { + EventMemoryHolder eventMemoryHolder = new EventMemoryHolder("Cell1", UUID.randomUUID().toString(), 10, ZonedDateTime.now(), loadEventFromFile()); + collectedEvents.add(eventMemoryHolder); + } + return collectedEvents; + } + + /** + * Converts json to VESEvent object + * + * @return created VESEvent + */ + private VesEvent loadEventFromFile() { + try { + return mapper.readValue(loadFileContent("VesBrokerControllerTest_pm_ves.json"), VesEvent.class); + } catch (JsonProcessingException e) { + e.printStackTrace(); + return null; + } + } + + /** + * Get json string from specified json file + * + * @param fileName name of test json file + * @return json file as string + */ + private String loadFileContent(String fileName) { + Path path; + try { + path = Paths.get(VesBrokerServiceImplTest.class.getResource(fileName).toURI()); + return new String(Files.readAllBytes(path)); + } catch (URISyntaxException | IOException e) { + e.printStackTrace(); + return null; + } + } +}
\ No newline at end of file |