diff options
Diffstat (limited to 'src/test/java/org/onap/a1pesimulator/service/pm')
7 files changed, 744 insertions, 0 deletions
diff --git a/src/test/java/org/onap/a1pesimulator/service/pm/CommonFileReady.java b/src/test/java/org/onap/a1pesimulator/service/pm/CommonFileReady.java new file mode 100644 index 0000000..aae5e53 --- /dev/null +++ b/src/test/java/org/onap/a1pesimulator/service/pm/CommonFileReady.java @@ -0,0 +1,163 @@ +/* + * Copyright (C) 2021 Samsung Electronics + * 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 + */ + +package org.onap.a1pesimulator.service.pm; + +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.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.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.UUID; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.mockito.InjectMocks; +import org.mockito.MockitoAnnotations; +import org.onap.a1pesimulator.data.fileready.EventMemoryHolder; +import org.onap.a1pesimulator.data.ves.VesEvent; +import org.onap.a1pesimulator.service.VesBrokerServiceImplTest; +import org.slf4j.LoggerFactory; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; + +import ch.qos.logback.classic.Logger; +import ch.qos.logback.classic.spi.ILoggingEvent; +import ch.qos.logback.core.read.ListAppender; + +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"; + public static final String TEST_CELL_ID = "Cell1"; + public static final Integer NO_OF_EVENTS = 3; + public static final Integer NO_OF_CELLS = 2; + + @InjectMocks + private ObjectMapper mapper; + + + @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; + } + } + + /** + * Generate NO_OF_EVENTS test EventMemoryHolder list + * + * @return EventMemoryHolder list + */ + protected List<EventMemoryHolder> getTestEvents() { + List<EventMemoryHolder> collectedEvents = new ArrayList<>(); + for (int i = 0; i < NO_OF_EVENTS; i++) { + EventMemoryHolder eventMemoryHolder = new EventMemoryHolder(TEST_CELL_ID, UUID.randomUUID().toString(), 10, ZonedDateTime.now(), + loadEventFromFile()); + collectedEvents.add(eventMemoryHolder); + } + return collectedEvents; + } + + /** + * Generate events by CellId + * + * @return Map by CellId and list of events + */ + protected Map<String, List<EventMemoryHolder>> getTestEventsByCells(List<EventMemoryHolder> eventList) { + Map<String, List<EventMemoryHolder>> collectedEventsByCell = new HashMap<>(); + for (int cellId = 0; cellId < NO_OF_CELLS; cellId++) { + collectedEventsByCell.put("Cell" + cellId, eventList); + } + return collectedEventsByCell; + } + + /** + * Converts json to VESEvent object + * + * @return created VESEvent + */ + protected 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; + } + } + + /** + * Create common log + * + * @return ListAppender<ILoggingEvent> + */ + protected ListAppender<ILoggingEvent> createCommonLog(Class clazz) { + Logger testLog = (Logger) LoggerFactory.getLogger(clazz); + ListAppender<ILoggingEvent> appender = new ListAppender<>(); + appender.start(); + testLog.addAppender(appender); + return appender; + } +} diff --git a/src/test/java/org/onap/a1pesimulator/service/pm/FileReadyEventServiceTest.java b/src/test/java/org/onap/a1pesimulator/service/pm/FileReadyEventServiceTest.java new file mode 100644 index 0000000..141d455 --- /dev/null +++ b/src/test/java/org/onap/a1pesimulator/service/pm/FileReadyEventServiceTest.java @@ -0,0 +1,91 @@ +/* + * Copyright (C) 2021 Samsung Electronics + * 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 + */ + +package org.onap.a1pesimulator.service.pm; + +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 java.time.ZonedDateTime; + +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() { + FileData testData = getTestFileData(); + Mono<FileData> fileMono = Mono.just(testData); + FileData expectedFileData = fileReadyEventService.createFileReadyEvent(testData); + 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)).startEventDate(ZonedDateTime.now()) + .endEventDate(ZonedDateTime.now().plusMinutes(5)).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/pm/FtpServerServiceTest.java b/src/test/java/org/onap/a1pesimulator/service/pm/FtpServerServiceTest.java new file mode 100644 index 0000000..4284e33 --- /dev/null +++ b/src/test/java/org/onap/a1pesimulator/service/pm/FtpServerServiceTest.java @@ -0,0 +1,156 @@ +/* + * Copyright (C) 2021 Samsung Electronics + * 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 + */ + +package org.onap.a1pesimulator.service.pm; + +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.spy; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.onap.a1pesimulator.service.pm.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.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.onap.a1pesimulator.data.fileready.FileData; +import org.onap.a1pesimulator.util.VnfConfigReader; +import org.springframework.test.util.ReflectionTestUtils; + +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 { + + private FtpServerService ftpServerService; + + @Mock + SSHClient sshClient; + + @Mock + SFTPClient sftpClient; + + @InjectMocks + VnfConfigReader vnfConfigReader; + + @BeforeEach + void setUp() { + super.setUp(); + ReflectionTestUtils.setField(vnfConfigReader, "vnfConfigFile", "src/test/resources/vnf.config"); + ftpServerService = spy(new FtpServerService(vnfConfigReader)); + } + + /** + * 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); + 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() { + ListAppender<ILoggingEvent> appender = createCommonLog(FtpServerService.class); + + 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("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/pm/PMBulkFileServiceTest.java b/src/test/java/org/onap/a1pesimulator/service/pm/PMBulkFileServiceTest.java new file mode 100644 index 0000000..ea9326e --- /dev/null +++ b/src/test/java/org/onap/a1pesimulator/service/pm/PMBulkFileServiceTest.java @@ -0,0 +1,51 @@ +/* + * Copyright (C) 2021 Samsung Electronics + * 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 + */ + +package org.onap.a1pesimulator.service.pm; + +import static org.junit.jupiter.api.Assertions.assertNotNull; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.InjectMocks; +import org.onap.a1pesimulator.data.fileready.FileData; +import org.onap.a1pesimulator.util.VnfConfigReader; +import org.springframework.test.util.ReflectionTestUtils; + +import reactor.core.publisher.Mono; + +class PMBulkFileServiceTest extends CommonFileReady { + + private PMBulkFileService pmBulkFileService; + + @InjectMocks + VnfConfigReader vnfConfigReader; + + + @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()); + } + + +}
\ No newline at end of file diff --git a/src/test/java/org/onap/a1pesimulator/service/pm/RanFileReadyHolderTest.java b/src/test/java/org/onap/a1pesimulator/service/pm/RanFileReadyHolderTest.java new file mode 100644 index 0000000..dd82065 --- /dev/null +++ b/src/test/java/org/onap/a1pesimulator/service/pm/RanFileReadyHolderTest.java @@ -0,0 +1,154 @@ +/* + * Copyright (C) 2021 Samsung Electronics + * 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 + */ + +package org.onap.a1pesimulator.service.pm; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.doThrow; +import static org.mockito.Mockito.spy; + +import java.util.List; +import java.util.Map; +import java.util.UUID; + +import org.apache.http.HttpStatus; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.Mock; +import org.onap.a1pesimulator.data.fileready.EventMemoryHolder; +import org.onap.a1pesimulator.data.fileready.FileData; +import org.onap.a1pesimulator.data.fileready.FileReadyEvent; +import org.onap.a1pesimulator.exception.VesBrokerException; +import org.onap.a1pesimulator.service.report.RanVesSender; + +import ch.qos.logback.classic.spi.ILoggingEvent; +import ch.qos.logback.core.read.ListAppender; +import reactor.core.publisher.Mono; + +class RanFileReadyHolderTest extends CommonFileReady { + + private RanFileReadyHolder ranFileReadyHolder; + + @Mock + PMBulkFileService pmBulkFileService; + + @Mock + RanVesSender ranVesSender; + + @Mock + FtpServerService ftpServerService; + + @Mock + FileReadyEventService fileReadyEventService; + + @BeforeEach + void setUp() { + super.setUp(); + ranFileReadyHolder = spy(new RanFileReadyHolder(ranVesSender, ftpServerService, pmBulkFileService, fileReadyEventService)); + } + + @Test + void createPMBulkFileAndSendFileReadyMessage() { + ListAppender<ILoggingEvent> appender = createCommonLogAndMock(); + + ranFileReadyHolder.createPMBulkFileAndSendFileReadyMessage(); + assertThat(appender.list).extracting(ILoggingEvent::getFormattedMessage) + .contains("PM Bulk file was generated, uploaded to FTP and File ready event was send to VES Collector"); + } + + @Test + void createPMBulkFileAndSendFileReadyMessageForOneCell() { + ListAppender<ILoggingEvent> appender = createCommonLogAndMock(); + + ranFileReadyHolder.createPMBulkFileAndSendFileReadyMessageForCellId(TEST_CELL_ID); + assertThat(appender.list).extracting(ILoggingEvent::getFormattedMessage) + .contains("PM Bulk file was generated, uploaded to FTP and File ready event was send to VES Collector"); + } + + @Test + void errorCreatePMBulkFileAndSendFileReadyMessage() { + ListAppender<ILoggingEvent> appender = createCommonLogAndMock(); + doReturn(Mono.error(new Exception("error"))).when(fileReadyEventService).createFileReadyEventAndDeleteTmpFile(any()); + + ranFileReadyHolder.createPMBulkFileAndSendFileReadyMessage(); + assertThat(appender.list).extracting(ILoggingEvent::getFormattedMessage).contains("File ready event was unsuccessful: error"); + } + + @Test + void saveEventToMemory() { + ranFileReadyHolder = spy(new RanFileReadyHolder(ranVesSender, ftpServerService, pmBulkFileService, fileReadyEventService)); + try { + ranFileReadyHolder.saveEventToMemory(loadEventFromFile(), TEST_CELL_ID, UUID.randomUUID().toString(), 30); + ranFileReadyHolder.saveEventToMemory(loadEventFromFile(), TEST_CELL_ID, UUID.randomUUID().toString(), 30); + } catch (VesBrokerException e) { + e.printStackTrace(); + } + assertThat(ranFileReadyHolder.getCollectedEventsByCell().get(TEST_CELL_ID)).hasSize(2); + } + + @Test + void errorSaveEventToMemory() throws VesBrokerException { + doThrow(new VesBrokerException("error")).when(ranFileReadyHolder).saveEventToMemory(any(), any(), any(), any()); + + Throwable exception = assertThrows(VesBrokerException.class, + () -> ranFileReadyHolder.saveEventToMemory(loadEventFromFile(), TEST_CELL_ID, UUID.randomUUID().toString(), 30)); + assertThat(exception.getMessage()).contains("error"); + assertThat(ranFileReadyHolder.getCollectedEventsByCell()).isEmpty(); + } + + @Test + void getCollectedEventsByCell() { + Map<String, List<EventMemoryHolder>> collectedEvents = ranFileReadyHolder.getCollectedEventsByCell(); + assertNotNull(collectedEvents); + } + + @Test + void getCollectedEvents() { + List<EventMemoryHolder> collectedEvents = ranFileReadyHolder.getCollectedEventsForCellId(TEST_CELL_ID); + assertNotNull(collectedEvents); + try { + ranFileReadyHolder.saveEventToMemory(loadEventFromFile(), TEST_CELL_ID, UUID.randomUUID().toString(), 30); + collectedEvents = ranFileReadyHolder.getCollectedEventsForCellId(TEST_CELL_ID); + assertNotNull(collectedEvents); + } catch (VesBrokerException e) { + e.printStackTrace(); + } + } + + /** + * Creates common Log and Mocks + * + * @return ListAppender<ILoggingEvent> + */ + private ListAppender<ILoggingEvent> createCommonLogAndMock() { + ListAppender<ILoggingEvent> appender = createCommonLog(RanFileReadyHolder.class); + + List<EventMemoryHolder> collectedEvents = getTestEvents(); + Map<String, List<EventMemoryHolder>> collectedEventsByCell = getTestEventsByCells(collectedEvents); + FileData testFileData = FileData.builder().pmBulkFile(createTempFile(PM_BULK_FILE)).build(); + + doReturn(collectedEventsByCell).when(ranFileReadyHolder).getCollectedEventsByCell(); + doReturn(collectedEvents).when(ranFileReadyHolder).getCollectedEventsForCellId(any()); + doReturn(Mono.just(testFileData)).when(pmBulkFileService).generatePMBulkFileXml(collectedEvents); + testFileData.setArchivedPmBulkFile(createTempFile(ARCHIVED_PM_BULK_FILE)); + doReturn(Mono.just(testFileData)).when(ftpServerService).uploadFileToFtp(any()); + testFileData.setFileReadyEvent(new FileReadyEvent()); + doReturn(Mono.just(testFileData)).when(fileReadyEventService).createFileReadyEventAndDeleteTmpFile(any()); + doReturn(Mono.just(HttpStatus.SC_ACCEPTED)).when(ranVesSender).send(any()); + return appender; + } +}
\ No newline at end of file diff --git a/src/test/java/org/onap/a1pesimulator/service/pm/RanSaveFileReadyRunnableTest.java b/src/test/java/org/onap/a1pesimulator/service/pm/RanSaveFileReadyRunnableTest.java new file mode 100644 index 0000000..198d46c --- /dev/null +++ b/src/test/java/org/onap/a1pesimulator/service/pm/RanSaveFileReadyRunnableTest.java @@ -0,0 +1,74 @@ +/* + * Copyright (C) 2021 Samsung Electronics + * 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 + */ + +package org.onap.a1pesimulator.service.pm; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.doThrow; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + +import java.util.Collections; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.Mock; +import org.onap.a1pesimulator.data.ves.VesEvent; +import org.onap.a1pesimulator.exception.VesBrokerException; +import org.onap.a1pesimulator.service.ue.RanUeHolder; +import org.onap.a1pesimulator.service.report.RanCellEventCustomizer; +import org.onap.a1pesimulator.service.report.RanEventCustomizerFactory; +import org.onap.a1pesimulator.service.report.RanEventCustomizerFactory.Mode; + +import ch.qos.logback.classic.spi.ILoggingEvent; +import ch.qos.logback.core.read.ListAppender; + +class RanSaveFileReadyRunnableTest extends CommonFileReady { + + private RanSaveFileReadyRunnable ranSaveFileReadyRunnable; + + @Mock + RanFileReadyHolder ranFileReadyHolder; + + @Mock + RanEventCustomizerFactory ranEventCustomizerFactory; + + @Mock + RanUeHolder ranUeHolder; + + @BeforeEach + void setUp() { + super.setUp(); + doReturn(new RanCellEventCustomizer(ranUeHolder)).when(ranEventCustomizerFactory).getEventCustomizer(any(), any()); + ranSaveFileReadyRunnable = spy( + new RanSaveFileReadyRunnable(ranFileReadyHolder, TEST_CELL_ID, loadEventFromFile(), ranEventCustomizerFactory.getEventCustomizer(new VesEvent(), + Mode.REGULAR), 60, Collections.emptyList())); + } + + @Test + void successfulRun() throws VesBrokerException { + ranSaveFileReadyRunnable.run(); + verify(ranFileReadyHolder, times(1)).saveEventToMemory(any(), any(), any(), any()); + } + + @Test + void errorRun() throws VesBrokerException { + ListAppender<ILoggingEvent> appender = createCommonLog(RanSaveFileReadyRunnable.class); + doThrow(new VesBrokerException("error")).when(ranFileReadyHolder).saveEventToMemory(any(), any(), any(), any()); + ranSaveFileReadyRunnable.run(); + assertThat(appender.list).extracting(ILoggingEvent::getFormattedMessage).containsExactly("Saving file ready event failed: error"); + } +}
\ No newline at end of file diff --git a/src/test/java/org/onap/a1pesimulator/service/pm/RanSendReportsRunnableTest.java b/src/test/java/org/onap/a1pesimulator/service/pm/RanSendReportsRunnableTest.java new file mode 100644 index 0000000..b7de25b --- /dev/null +++ b/src/test/java/org/onap/a1pesimulator/service/pm/RanSendReportsRunnableTest.java @@ -0,0 +1,55 @@ +/* + * Copyright (C) 2021 Samsung Electronics + * 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 + */ + +package org.onap.a1pesimulator.service.pm; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.Mock; +import org.onap.a1pesimulator.service.ue.RanUeHolder; +import org.onap.a1pesimulator.service.report.RanCellEventCustomizer; +import org.onap.a1pesimulator.service.report.RanEventCustomizerFactory; + +class RanSendReportsRunnableTest extends CommonFileReady { + + private RanSendReportsRunnable ranSendReportsRunnable; + + @Mock + RanFileReadyHolder ranFileReadyHolder; + + @Mock + RanEventCustomizerFactory ranEventCustomizerFactory; + + @Mock + RanUeHolder ranUeHolder; + + @BeforeEach + void setUp() { + super.setUp(); + doReturn(new RanCellEventCustomizer(ranUeHolder)).when(ranEventCustomizerFactory).getEventCustomizer(any(), any()); + ranSendReportsRunnable = spy( + new RanSendReportsRunnable(ranFileReadyHolder)); + } + + @Test + void successfulRun() { + ranSendReportsRunnable.run(); + verify(ranFileReadyHolder, times(1)).createPMBulkFileAndSendFileReadyMessage(); + } +}
\ No newline at end of file |