diff options
author | Stanislav Marszalek <s.marszalek2@partner.samsung.com> | 2021-07-19 12:59:13 +0200 |
---|---|---|
committer | Stanislav Marszalek <s.marszalek2@partner.samsung.com> | 2021-07-28 15:03:59 +0200 |
commit | 5995a778e7a5a7718c480a8099b9c2f47549530d (patch) | |
tree | 043c2400f820b2febc4a8331004aecdc59f387f8 | |
parent | c7bc54b6e81532ad906de88f5ad7ed63259ff586 (diff) |
O1 PM Bulk support - Unit test - II
Issue-ID: INT-1945
Signed-off-by: Stanislav Marszalek <s.marszalek2@partner.samsung.com>
Change-Id: I02d3684c98d563d7f386de2fdf032e930ac41b3f
8 files changed, 301 insertions, 76 deletions
diff --git a/src/main/java/org/onap/a1pesimulator/service/common/AbstractRanRunnable.java b/src/main/java/org/onap/a1pesimulator/service/common/AbstractRanRunnable.java index cfb1390..78de6df 100644 --- a/src/main/java/org/onap/a1pesimulator/service/common/AbstractRanRunnable.java +++ b/src/main/java/org/onap/a1pesimulator/service/common/AbstractRanRunnable.java @@ -22,9 +22,4 @@ public abstract class AbstractRanRunnable implements Runnable { public void updateEvent(VesEvent event) { this.event = event; } - - @Override - public void run() { - //will be implemented in subclasses - } } diff --git a/src/main/java/org/onap/a1pesimulator/service/fileready/RanFileReadyHolder.java b/src/main/java/org/onap/a1pesimulator/service/fileready/RanFileReadyHolder.java index 148c059..4191ae7 100644 --- a/src/main/java/org/onap/a1pesimulator/service/fileready/RanFileReadyHolder.java +++ b/src/main/java/org/onap/a1pesimulator/service/fileready/RanFileReadyHolder.java @@ -91,7 +91,7 @@ public class RanFileReadyHolder { * * @param fileData object with FileReadyEvent file */ - private void sendEventToVesCollector(FileData fileData) { + protected void sendEventToVesCollector(FileData fileData) { ranVesSender.send(fileData.getFileReadyEvent()); } diff --git a/src/test/java/org/onap/a1pesimulator/service/fileready/CommonFileReady.java b/src/test/java/org/onap/a1pesimulator/service/fileready/CommonFileReady.java index 75ce7a0..d65ad7c 100644 --- a/src/test/java/org/onap/a1pesimulator/service/fileready/CommonFileReady.java +++ b/src/test/java/org/onap/a1pesimulator/service/fileready/CommonFileReady.java @@ -5,20 +5,42 @@ 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.List; +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 Integer NO_OF_EVENTS = 3; + + @InjectMocks + private ObjectMapper mapper; + @BeforeEach void setUp() { @@ -49,4 +71,62 @@ public class CommonFileReady { 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("Cell1", UUID.randomUUID().toString(), 10, ZonedDateTime.now(), loadEventFromFile()); + collectedEvents.add(eventMemoryHolder); + } + return collectedEvents; + } + + /** + * 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/fileready/FtpServerServiceTest.java b/src/test/java/org/onap/a1pesimulator/service/fileready/FtpServerServiceTest.java index e8b77ef..d984b20 100644 --- a/src/test/java/org/onap/a1pesimulator/service/fileready/FtpServerServiceTest.java +++ b/src/test/java/org/onap/a1pesimulator/service/fileready/FtpServerServiceTest.java @@ -20,10 +20,8 @@ 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; @@ -99,10 +97,7 @@ class FtpServerServiceTest extends CommonFileReady { */ @Test void errorWhileDeletingFile() { - Logger testLog = (Logger) LoggerFactory.getLogger(FtpServerService.class); - ListAppender<ILoggingEvent> appender = new ListAppender<>(); - appender.start(); - testLog.addAppender(appender); + ListAppender<ILoggingEvent> appender = createCommonLog(FtpServerService.class); deletePMBulkFile(new File("test.txt")); assertThat(appender.list).extracting(ILoggingEvent::getFormattedMessage).containsExactly("Could not delete file: test.txt"); diff --git a/src/test/java/org/onap/a1pesimulator/service/fileready/PMBulkFileServiceTest.java b/src/test/java/org/onap/a1pesimulator/service/fileready/PMBulkFileServiceTest.java index 71c92ff..45d7cf6 100644 --- a/src/test/java/org/onap/a1pesimulator/service/fileready/PMBulkFileServiceTest.java +++ b/src/test/java/org/onap/a1pesimulator/service/fileready/PMBulkFileServiceTest.java @@ -2,42 +2,22 @@ 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; + private PMBulkFileService pmBulkFileService; @InjectMocks VnfConfigReader vnfConfigReader; - @InjectMocks - private ObjectMapper mapper; @BeforeEach void setUp() { @@ -54,48 +34,5 @@ class PMBulkFileServiceTest extends CommonFileReady { 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 diff --git a/src/test/java/org/onap/a1pesimulator/service/fileready/RanFileReadyHolderTest.java b/src/test/java/org/onap/a1pesimulator/service/fileready/RanFileReadyHolderTest.java new file mode 100644 index 0000000..10014c5 --- /dev/null +++ b/src/test/java/org/onap/a1pesimulator/service/fileready/RanFileReadyHolderTest.java @@ -0,0 +1,115 @@ +package org.onap.a1pesimulator.service.fileready; + +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.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.ves.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) + .containsExactly("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).containsExactly("File ready event was unsuccessful: error"); + } + + @Test + void saveEventToMemory() { + ranFileReadyHolder = spy(new RanFileReadyHolder(ranVesSender, ftpServerService, pmBulkFileService, fileReadyEventService)); + try { + ranFileReadyHolder.saveEventToMemory(loadEventFromFile(), "Cell1", UUID.randomUUID().toString(), 30); + } catch (VesBrokerException e) { + e.printStackTrace(); + } + assertThat(ranFileReadyHolder.getCollectedEvents()).hasSize(1); + } + + @Test + void errorSaveEventToMemory() throws VesBrokerException { + doThrow(new VesBrokerException("error")).when(ranFileReadyHolder).saveEventToMemory(any(), any(), any(), any()); + + Throwable exception = assertThrows(VesBrokerException.class, + () -> ranFileReadyHolder.saveEventToMemory(loadEventFromFile(), "Cell1", UUID.randomUUID().toString(), 30)); + assertThat(exception.getMessage()).contains("error"); + assertThat(ranFileReadyHolder.getCollectedEvents()).isEmpty(); + } + + @Test + void getCollectedEvents() { + List<EventMemoryHolder> collectedEvents = ranFileReadyHolder.getCollectedEvents(); + assertNotNull(collectedEvents); + } + + /** + * Creates common Log and Mocks + * + * @return ListAppender<ILoggingEvent> + */ + private ListAppender<ILoggingEvent> createCommonLogAndMock() { + ListAppender<ILoggingEvent> appender = createCommonLog(RanFileReadyHolder.class); + + List<EventMemoryHolder> collectedEvents = getTestEvents(); + FileData testFileData = FileData.builder().pmBulkFile(createTempFile(PM_BULK_FILE)).build(); + + doReturn(collectedEvents).when(ranFileReadyHolder).getCollectedEvents(); + 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/fileready/RanSaveFileReadyRunnableTest.java b/src/test/java/org/onap/a1pesimulator/service/fileready/RanSaveFileReadyRunnableTest.java new file mode 100644 index 0000000..4ee18de --- /dev/null +++ b/src/test/java/org/onap/a1pesimulator/service/fileready/RanSaveFileReadyRunnableTest.java @@ -0,0 +1,61 @@ +package org.onap.a1pesimulator.service.fileready; + +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.ves.RanCellEventCustomizer; +import org.onap.a1pesimulator.service.ves.RanEventCustomizerFactory; +import org.onap.a1pesimulator.service.ves.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, "Cell1", 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/fileready/RanSendReportsRunnableTest.java b/src/test/java/org/onap/a1pesimulator/service/fileready/RanSendReportsRunnableTest.java new file mode 100644 index 0000000..323e022 --- /dev/null +++ b/src/test/java/org/onap/a1pesimulator/service/fileready/RanSendReportsRunnableTest.java @@ -0,0 +1,42 @@ +package org.onap.a1pesimulator.service.fileready; + +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.ves.RanCellEventCustomizer; +import org.onap.a1pesimulator.service.ves.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 |