diff options
author | Stanislav Marszalek <s.marszalek2@partner.samsung.com> | 2021-08-03 12:58:05 +0200 |
---|---|---|
committer | Stanislav Marszalek <s.marszalek2@partner.samsung.com> | 2021-08-11 11:02:51 +0200 |
commit | f04c5084667975fabc849378af0702129fbf4de3 (patch) | |
tree | 682b5a649d424886af6634076b03e2eacf503029 | |
parent | 44b1873c0f84022d90769f46abda160b3ea06bd3 (diff) |
O1 PM Bulk support in DataCollector RAPP - Unit tests
Issue-ID: INT-1947
Signed-off-by: Stanislav Marszalek <s.marszalek2@partner.samsung.com>
Change-Id: I02d3684c98d563d7f386de2fdf032e930ac74b3f
7 files changed, 423 insertions, 39 deletions
diff --git a/datacollector/src/test/java/org/onap/rapp/datacollector/TestHelpers.java b/datacollector/src/test/java/org/onap/rapp/datacollector/TestHelpers.java new file mode 100644 index 0000000..bd13330 --- /dev/null +++ b/datacollector/src/test/java/org/onap/rapp/datacollector/TestHelpers.java @@ -0,0 +1,44 @@ +package org.onap.rapp.datacollector; + +import static java.util.Objects.nonNull; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.util.stream.Collectors; + +import lombok.experimental.UtilityClass; + +@UtilityClass +public class TestHelpers { + + /** + * Get empty string event + * + * @return empty event, without header and measurement data + */ + public static String getEmptyEvent() { + return "{\"event\":{}}"; + } + + /** + * Get Event as string from file + * + * @param fileName location of test file + * @return Event file as string + */ + public static String getTestEventFromFile(String fileName) { + InputStream in = TestHelpers.class.getResourceAsStream(fileName); + if (nonNull(in)) { + try (in) { + BufferedReader inr = new BufferedReader(new InputStreamReader(in)); + return inr.lines().collect(Collectors.joining(" ")); + } catch (IOException e) { + e.printStackTrace(); + } + } + return ""; + } + +} diff --git a/datacollector/src/test/java/org/onap/rapp/datacollector/service/FileReadyParserImplTest.java b/datacollector/src/test/java/org/onap/rapp/datacollector/service/FileReadyParserImplTest.java new file mode 100644 index 0000000..e5c9837 --- /dev/null +++ b/datacollector/src/test/java/org/onap/rapp/datacollector/service/FileReadyParserImplTest.java @@ -0,0 +1,41 @@ +package org.onap.rapp.datacollector.service; + +import static org.junit.Assert.assertEquals; +import static org.onap.rapp.datacollector.TestHelpers.getEmptyEvent; +import static org.onap.rapp.datacollector.TestHelpers.getTestEventFromFile; + +import java.util.List; + +import org.junit.Before; +import org.junit.Test; +import org.onap.rapp.datacollector.entity.ves.Event; + +import com.google.gson.JsonParseException; + +public class FileReadyParserImplTest { + + String testFileReadyContent; + VesParser parser = new FileReadyParserImpl(); + + @Before + public void setUp() { + testFileReadyContent = getTestEventFromFile("/sample-fileready.txt"); + } + + @Test + public void testParsing() { + List<Event> listOfEvents = parser.parse(testFileReadyContent); + assertEquals(4, listOfEvents.size()); + listOfEvents.forEach(event -> { + assertEquals("4.0", event.getCommonEventHeader().getVersion()); + assertEquals("perf3gpp", event.getCommonEventHeader().getDomain()); + assertEquals("perf3gpp_PE-Samsung_pmMeasResult", event.getCommonEventHeader().getEventName()); + assertEquals(3, event.getMeasurementFields().getAdditionalMeasurements().size()); + }); + } + + @Test(expected = JsonParseException.class) + public void parseEmpty() { + parser.parse(getEmptyEvent()); + } +}
\ No newline at end of file diff --git a/datacollector/src/test/java/org/onap/rapp/datacollector/service/PMServiceTest.java b/datacollector/src/test/java/org/onap/rapp/datacollector/service/PMServiceTest.java new file mode 100644 index 0000000..38d93bc --- /dev/null +++ b/datacollector/src/test/java/org/onap/rapp/datacollector/service/PMServiceTest.java @@ -0,0 +1,103 @@ +package org.onap.rapp.datacollector.service; + +import static java.util.Arrays.asList; +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyLong; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.spy; +import static org.onap.rapp.datacollector.TestHelpers.getTestEventFromFile; +import static org.onap.rapp.datacollector.service.PMService.CELL_FIELD_NAME; + +import java.time.OffsetDateTime; +import java.util.List; +import java.util.Map; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.onap.rapp.datacollector.entity.pm.AggregatedPM; +import org.onap.rapp.datacollector.entity.pm.PMData; +import org.onap.rapp.datacollector.entity.ves.AdditionalMeasurements; +import org.onap.rapp.datacollector.entity.ves.CommonEventHeader; +import org.onap.rapp.datacollector.entity.ves.Event; +import org.onap.rapp.datacollector.entity.ves.EventAPI; +import org.onap.rapp.datacollector.entity.ves.MeasurementFields; + +class PMServiceTest { + + public static final String TEST_CELL_ID = "Chn0000"; + + PMService pmService; + + @Mock + VesPersisterSqlImpl vesPersisterSql; + + @Mock + DataAggregationService aggregationService; + + @Mock + ParserFactory parser; + + @BeforeEach + public void setUp() { + MockitoAnnotations.initMocks(this); + pmService = spy(new PMService(vesPersisterSql, aggregationService, parser)); + } + + @Test + void testAggregatedPMDataForTimeInterval() { + doReturn(getTestApiEvents()).when(vesPersisterSql).findEventsByTimeWindow(anyLong(), anyLong()); + doReturn(new PMData()).when(aggregationService).getAggregatedDataFromEventsForCell(any(), any(), any()); + doReturn(getTestEvents()).when(parser).getParsedEvents(any()); + AggregatedPM aggregatedPM = pmService.getAggregatedPMDataForTimeInterval(10, 10, OffsetDateTime.now()); + assertThat(aggregatedPM.getItemsLength()).isOne(); + } + + /** + * List of EventApi to simulate query to sql + * + * @return List of EventApi + */ + private List<EventAPI> getTestApiEvents() { + return asList(EventAPI.builder().id(1L).rawdata(getTestEventFromFile("/sample-fileready.txt")).build(), + EventAPI.builder().id(2L).rawdata(getTestEventFromFile("/sample-fileready.txt")).build()); + } + + /** + * List of test Events returned by parser + * + * @return List of Events + */ + protected List<Event> getTestEvents() { + return asList(Event.of(CommonEventHeader.builder() + .build(), MeasurementFields.builder() + .additionalMeasurements(asList(AdditionalMeasurements.builder() + .withEventId(1L) + .withName(CELL_FIELD_NAME) + .withHashMap(Map.of(CELL_FIELD_NAME, TEST_CELL_ID)) + .build(), + AdditionalMeasurements.builder() + .withEventId(1L) + .withName("latency") + .withHashMap(Map.of("latency", "35")) + .build()) + ) + .build()), + Event.of(CommonEventHeader.builder() + .build(), MeasurementFields.builder() + .additionalMeasurements(asList(AdditionalMeasurements.builder() + .withEventId(1L) + .withName(CELL_FIELD_NAME) + .withHashMap(Map.of(CELL_FIELD_NAME, TEST_CELL_ID)) + .build(), + AdditionalMeasurements.builder() + .withEventId(1L) + .withName("latency") + .withHashMap(Map.of("latency", "59")) + .build()) + ) + .build())); + } +}
\ No newline at end of file diff --git a/datacollector/src/test/java/org/onap/rapp/datacollector/service/ParserFactoryTest.java b/datacollector/src/test/java/org/onap/rapp/datacollector/service/ParserFactoryTest.java new file mode 100644 index 0000000..61a88ee --- /dev/null +++ b/datacollector/src/test/java/org/onap/rapp/datacollector/service/ParserFactoryTest.java @@ -0,0 +1,51 @@ +package org.onap.rapp.datacollector.service; + +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.onap.rapp.datacollector.TestHelpers.getEmptyEvent; +import static org.onap.rapp.datacollector.TestHelpers.getTestEventFromFile; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +import com.google.gson.JsonParseException; + +class ParserFactoryTest { + + ParserFactory parserFactory; + + @Mock + VesParserImpl vesParser; + + @Mock + FileReadyParserImpl fileReadyParser; + + @BeforeEach + public void setUp() { + MockitoAnnotations.initMocks(this); + parserFactory = spy(new ParserFactory(vesParser, fileReadyParser)); + } + + @Test + void testParsedEventsForAllTypes() { + //File ready event + parserFactory.getParsedEvents(getTestEventFromFile("/sample-fileready.txt")); + verify(fileReadyParser, times(1)).parse(anyString()); + + //VES event + parserFactory.getParsedEvents(getTestEventFromFile("/sample-ves.json")); + verify(vesParser, times(1)).parse(anyString()); + } + + @Test + void testUnspportedJson() { + String emptyEvent = getEmptyEvent(); + assertThatExceptionOfType(JsonParseException.class).isThrownBy(() -> parserFactory.getParsedEvents(emptyEvent)); + } + +}
\ No newline at end of file diff --git a/datacollector/src/test/java/org/onap/rapp/datacollector/service/VesParserImplTest.java b/datacollector/src/test/java/org/onap/rapp/datacollector/service/VesParserImplTest.java index ba2a22f..3538d1d 100644 --- a/datacollector/src/test/java/org/onap/rapp/datacollector/service/VesParserImplTest.java +++ b/datacollector/src/test/java/org/onap/rapp/datacollector/service/VesParserImplTest.java @@ -15,11 +15,8 @@ package org.onap.rapp.datacollector.service; import static org.junit.Assert.assertEquals; - -import java.io.BufferedReader; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.util.stream.Collectors; +import static org.onap.rapp.datacollector.TestHelpers.getEmptyEvent; +import static org.onap.rapp.datacollector.TestHelpers.getTestEventFromFile; import org.junit.Before; import org.junit.Test; @@ -33,16 +30,12 @@ public class VesParserImplTest { VesParser parser = new VesParserImpl(); @Before - public void setUp() throws Exception { - InputStream in = this.getClass().getResourceAsStream("/sample-ves.json"); - try (in) { - BufferedReader inr = new BufferedReader(new InputStreamReader(in)); - testVesContent = inr.lines().collect(Collectors.joining(" ")); - } + public void setUp() { + testVesContent = getTestEventFromFile("/sample-ves.json"); } @Test - public void parse() { + public void testParsing() { Event actual = parser.parse(testVesContent).get(0); assertEquals("4.0.1", actual.commonEventHeader.getVersion()); assertEquals(1413378172000000L, (long) actual.commonEventHeader.getLastEpochMicrosec()); @@ -54,6 +47,6 @@ public class VesParserImplTest { @Test(expected = JsonParseException.class) public void parseEmpty() { - Event actual = parser.parse("{\"event\":{}}").get(0); + parser.parse(getEmptyEvent()); } }
\ No newline at end of file diff --git a/datacollector/src/test/java/org/onap/rapp/datacollector/service/VesRetrievalServiceTest.java b/datacollector/src/test/java/org/onap/rapp/datacollector/service/VesRetrievalServiceTest.java index e1a285c..206fc2f 100644 --- a/datacollector/src/test/java/org/onap/rapp/datacollector/service/VesRetrievalServiceTest.java +++ b/datacollector/src/test/java/org/onap/rapp/datacollector/service/VesRetrievalServiceTest.java @@ -13,27 +13,31 @@ package org.onap.rapp.datacollector.service; + +import static org.junit.jupiter.api.Assertions.assertEquals; + import java.util.Collection; import java.util.HashSet; import java.util.List; import java.util.Set; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.mockito.Mock; import org.mockito.Mockito; -import org.mockito.junit.MockitoJUnitRunner; +import org.mockito.MockitoAnnotations; import org.onap.rapp.datacollector.entity.ves.EventTest; +import org.onap.rapp.datacollector.service.configuration.DmaapProperties; import org.onap.rapp.datacollector.service.configuration.DmaapRestReaderConfiguration; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.client.RestClientException; import org.springframework.web.client.RestTemplate; -@RunWith(MockitoJUnitRunner.class) -public class VesRetrievalServiceTest { +class VesRetrievalServiceTest { @Mock private RestTemplate restTemplate; @@ -50,55 +54,52 @@ public class VesRetrievalServiceTest { @Mock UEHolder ueHolder; + private static final String TOPIC_URL = "http://localhost/a-topic"; + private VesRetrievalService service; - @Before + @BeforeEach public void init() { - Mockito.when(config.getMeasurementsTopicUrl()).thenReturn("http://localhost/a-topic"); + MockitoAnnotations.initMocks(this); + Mockito.when(config.getMeasurementsTopicUrl()).thenReturn(TOPIC_URL); + Mockito.when(config.getDmaapProperties()).thenReturn(getTestProperties()); String[] response = new String[]{"a", "b"}; - Mockito.when(restTemplate.getForEntity("http://localhost/a-topic", String[].class)) + + Mockito.when(restTemplate.exchange(TOPIC_URL, HttpMethod.GET, new HttpEntity<>(createTestHeaders()), String[].class)) .thenReturn(new ResponseEntity<>(response, HttpStatus.OK)); service = new VesRetrievalService(restTemplate, parser, persister, config, ueHolder); } @Test - public void givenMockingIsDoneByMockRestServiceServer_whenGetIsCalled_thenReturnsMockedObject() { + void givenMockingIsDoneByMockRestServiceServer_whenGetIsCalled_thenReturnsMockedObject() { HashSet<String> actual = new HashSet<>(service.retrieveEvents()); Set<String> expected = Set.of("a", "b"); - Assert.assertEquals(actual, expected); + assertEquals(actual, expected); } @Test - public void whenGetIsCalled_thenExceptionIsThrown() { - Mockito.when(config.getMeasurementsTopicUrl()).thenReturn("http://localhost/a-topic"); - Mockito.when(restTemplate.getForEntity("http://localhost/a-topic", String[].class)) + void whenGetIsCalled_thenExceptionIsThrown() { + Mockito.when(restTemplate.exchange(TOPIC_URL, HttpMethod.GET, new HttpEntity<>(createTestHeaders()), String[].class)) .thenThrow(new RestClientException("An test exception")); service = new VesRetrievalService(restTemplate, parser, persister, config, ueHolder); Collection<String> actual = service.retrieveEvents(); - Assert.assertEquals(0, actual.size()); + assertEquals(0, actual.size()); } @Test - public void whenRetrievedThenAlsoStored() { - Mockito.when(config.getMeasurementsTopicUrl()).thenReturn("http://localhost/a-topic"); - Mockito.when(restTemplate.getForEntity("http://localhost/a-topic", String[].class)) - .thenReturn(new ResponseEntity<>(new String[]{"dead", "beef"}, HttpStatus.OK)); + void whenRetrievedThenAlsoStored() { Mockito.when(parser.getParsedEvents(Mockito.any(String.class))) .thenReturn(EventTest.createDumyListOfEvents()); service = new VesRetrievalService(restTemplate, parser, persister, config, ueHolder); service.retrieveAndStoreVesEvents(); - Mockito.verify(persister, Mockito.times(2)).persistAll(Mockito.any(List.class)); } @Test - public void whenRetrievedThenAlsoStoredWithUE() { - Mockito.when(config.getMeasurementsTopicUrl()).thenReturn("http://localhost/a-topic"); - Mockito.when(restTemplate.getForEntity("http://localhost/a-topic", String[].class)) - .thenReturn(new ResponseEntity<>(new String[]{"dead", "beef"}, HttpStatus.OK)); + void whenRetrievedThenAlsoStoredWithUE() { Mockito.when(parser.getParsedEvents(Mockito.any(String.class))) .thenReturn(EventTest.createDumyListOfEventsWithUe()); @@ -108,7 +109,21 @@ public class VesRetrievalServiceTest { service.retrieveAndStoreVesEvents(); Mockito.verify(persister, Mockito.times(2)).persistAll(Mockito.any(List.class)); - Assert.assertEquals(ueHolder.getUes(), Set.of("emergency_samsung_01", "mobile_samsung_s10")); + assertEquals(ueHolder.getUes(), Set.of("emergency_samsung_01", "mobile_samsung_s10")); + } + + + private DmaapProperties getTestProperties() { + DmaapProperties dmaapProperties = new DmaapProperties(); + dmaapProperties.setPassword("password"); + dmaapProperties.setUsername("user name"); + return dmaapProperties; + } + + private HttpHeaders createTestHeaders() { + HttpHeaders headers = new HttpHeaders(); + headers.setBasicAuth(getTestProperties().getUsername(), getTestProperties().getPassword()); + return headers; } } diff --git a/datacollector/src/test/resources/sample-fileready.txt b/datacollector/src/test/resources/sample-fileready.txt new file mode 100644 index 0000000..944760a --- /dev/null +++ b/datacollector/src/test/resources/sample-fileready.txt @@ -0,0 +1,137 @@ +{ + "event": { + "commonEventHeader": { + "domain": "perf3gpp", + "eventId": "ddd48497-913c-451f-a396-2316a29e5975", + "sequence": 0, + "eventName": "perf3gpp_PE-Samsung_pmMeasResult", + "sourceName": "someVnfName", + "reportingEntityName": "", + "priority": "Normal", + "startEpochMicrosec": 1626352192000, + "lastEpochMicrosec": 1626352222000, + "version": "4.0", + "vesEventListenerVersion": "7.1", + "timeZoneOffset": "+02:00" + }, + "perf3gppFields": { + "perf3gppFieldsVersion": "1.0", + "measDataCollection": { + "granularityPeriod": 16262192000, + "measuredEntityUserName": "ORAN PE Sim", + "measuredEntityDn": "", + "measuredEntitySoftwareVersion": "", + "measInfoList": [ + { + "measInfoId": { + "sMeasInfoId": "" + }, + "measTypes": { + "sMeasTypesList": [ + "latency", + "throughput" + ] + }, + "measValuesList": [ + { + "measObjInstId": "Chn0000", + "suspectFlag": "false", + "measResults": [ + { + "p": 1, + "sValue": "110" + }, + { + "p": 2, + "sValue": "55" + } + ] + } + ] + }, + { + "measInfoId": { + "sMeasInfoId": "" + }, + "measTypes": { + "sMeasTypesList": [ + "latency", + "throughput" + ] + }, + "measValuesList": [ + { + "measObjInstId": "Chn0000", + "suspectFlag": "false", + "measResults": [ + { + "p": 1, + "sValue": "101" + }, + { + "p": 2, + "sValue": "78" + } + ] + } + ] + }, + { + "measInfoId": { + "sMeasInfoId": "" + }, + "measTypes": { + "sMeasTypesList": [ + "latency", + "throughput" + ] + }, + "measValuesList": [ + { + "measObjInstId": "Chn0000", + "suspectFlag": "false", + "measResults": [ + { + "p": 1, + "sValue": "135" + }, + { + "p": 2, + "sValue": "22" + } + ] + } + ] + }, + { + "measInfoId": { + "sMeasInfoId": "" + }, + "measTypes": { + "sMeasTypesList": [ + "latency", + "throughput" + ] + }, + "measValuesList": [ + { + "measObjInstId": "Chn0000", + "suspectFlag": "false", + "measResults": [ + { + "p": 1, + "sValue": "100" + }, + { + "p": 2, + "sValue": "35" + } + ] + } + ] + } + ] + } + } + } +}
\ No newline at end of file |