diff options
author | y.busko <y.busko@partner.samsung.com> | 2021-04-14 16:13:09 +0200 |
---|---|---|
committer | k.kedron <k.kedron@partner.samsung.com> | 2021-04-16 17:43:35 +0200 |
commit | 8f21c92d8104ea9bcaa49998cfb0787514a6ddc9 (patch) | |
tree | d6c30e8ee411ac1cdcbab8d303400eb895ace35d /datacollector/src/test | |
parent | 16c9954de0a4fdcb5324859b21ac928169792037 (diff) |
Initial code for DataCollector RAPP
Updated pom files
Added swagger templates at /doc/templates
Change-Id: I2fb7b26993fa13f07c3cf013aec9df06bd2dca8b
Issue-ID: INT-1887
Signed-off-by: Krystian Kedron <k.kedron@partner.samsung.com>
Signed-off-by: yauheni busko <y.busko3@partner.samsung.com>
Diffstat (limited to 'datacollector/src/test')
13 files changed, 1085 insertions, 0 deletions
diff --git a/datacollector/src/test/java/org/onap/rapp/datacollector/controller/PMControllerTest.java b/datacollector/src/test/java/org/onap/rapp/datacollector/controller/PMControllerTest.java new file mode 100644 index 0000000..1f23f48 --- /dev/null +++ b/datacollector/src/test/java/org/onap/rapp/datacollector/controller/PMControllerTest.java @@ -0,0 +1,303 @@ +/* + * 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.rapp.datacollector.controller; + +import com.fasterxml.jackson.databind.ObjectMapper; +import org.onap.rapp.datacollector.entity.pm.AggregatedPM; +import org.onap.rapp.datacollector.entity.pm.PMData; +import org.onap.rapp.datacollector.service.PMService; +import org.hamcrest.Matchers; +import org.hamcrest.core.IsNull; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.BDDMockito; +import org.mockito.internal.verification.VerificationModeFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.http.MediaType; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; +import org.springframework.test.web.servlet.result.MockMvcResultHandlers; +import org.springframework.test.web.servlet.result.MockMvcResultMatchers; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.time.OffsetDateTime; +import java.util.Collections; +import java.util.List; +import java.util.stream.Collectors; +import static org.hamcrest.collection.IsCollectionWithSize.hasSize; +import static org.mockito.Mockito.when; + +@RunWith(SpringRunner.class) +@WebMvcTest(PMController.class) +@ActiveProfiles("test") +public class PMControllerTest { + + private static final int SLOT = 10; + private static final int COUNT = 12; + private static final String startTime = OffsetDateTime.now().minusSeconds(SLOT * COUNT).toString(); + + @Autowired + private MockMvc mockMvc; + + @MockBean(name = "pmService") + private PMService pmService; + + @Autowired + private ObjectMapper mapper; + + protected <T> T mapFromJson(String json, Class<T> clazz) throws IOException { + return mapper.readValue(json, clazz); + } + + private List<PMData> pmDataList; + + @Before + public void setUp() throws Exception { + String testPmContent = getSamplePMData(); + pmDataList = Collections.singletonList(this.mapFromJson(testPmContent, PMData.class)); + } + + @Test + public void retrievePMData() throws Exception { + when(pmService.getAggregatedPMDataForTimeInterval(SLOT, COUNT, OffsetDateTime.parse(startTime))).thenReturn(new AggregatedPM(pmDataList, pmDataList.size())); + + BDDMockito + .given(pmService.getAggregatedPMDataForTimeInterval(SLOT, COUNT, OffsetDateTime.parse(startTime))) + .willReturn(new AggregatedPM(pmDataList, pmDataList.size())); + + mockMvc + // when + .perform( + MockMvcRequestBuilders + .get("/v1/pm/events/aggregatedmetrics") + .param("slot", String.valueOf(SLOT)) + .param("count", String.valueOf(COUNT)) + .param("startTime", startTime) + .accept(MediaType.APPLICATION_JSON) + ) + // then + .andDo(MockMvcResultHandlers.print()) + .andExpect( + MockMvcResultMatchers.status().isOk() + ) + .andExpect( + MockMvcResultMatchers + .content() + .contentTypeCompatibleWith(MediaType.APPLICATION_JSON) + ) + .andExpect( + MockMvcResultMatchers.jsonPath("$", Matchers.notNullValue()) + ) + .andExpect( + MockMvcResultMatchers.jsonPath("$.pm[0].cellId", Matchers.is("Cell1")) + ) + .andExpect( + MockMvcResultMatchers.jsonPath("$.itemsLength", Matchers.is(1)) + ) + .andExpect( + MockMvcResultMatchers.jsonPath("$.pm[0].performance[0].latency", Matchers.is(20)) + ) + .andExpect( + MockMvcResultMatchers.jsonPath("$.pm[0].performance[0].throughput", Matchers.is(80)) + ) + .andExpect( + MockMvcResultMatchers.jsonPath("$.pm[0].performance.*", hasSize(12)) + ) + ; + + // verify + BDDMockito + .verify(pmService, VerificationModeFactory.times(1)) + .getAggregatedPMDataForTimeInterval(SLOT, COUNT, OffsetDateTime.parse(startTime)); + } + + @Test + public void retrievePMDataWithEmptySlotOnBeginning() throws Exception { + pmDataList.get(0).getPerformance().get(0).setLatency(null); + pmDataList.get(0).getPerformance().get(0).setThroughput(null); + + when(pmService.getAggregatedPMDataForTimeInterval(SLOT, COUNT, OffsetDateTime.parse(startTime))).thenReturn(new AggregatedPM(pmDataList, pmDataList.size())); + + BDDMockito + .given(pmService.getAggregatedPMDataForTimeInterval(SLOT, COUNT, OffsetDateTime.parse(startTime))) + .willReturn(new AggregatedPM(pmDataList, pmDataList.size())); + + mockMvc + // when + .perform( + MockMvcRequestBuilders + .get("/v1/pm/events/aggregatedmetrics") + .param("slot", String.valueOf(SLOT)) + .param("count", String.valueOf(COUNT)) + .param("startTime", startTime) + .accept(MediaType.APPLICATION_JSON) + ) + // then + .andDo(MockMvcResultHandlers.print()) + .andExpect( + MockMvcResultMatchers.status().isOk() + ) + .andExpect( + MockMvcResultMatchers + .content() + .contentTypeCompatibleWith(MediaType.APPLICATION_JSON) + ) + .andExpect( + MockMvcResultMatchers.jsonPath("$.pm[0].cellId", Matchers.is("Cell1")) + ) + .andExpect( + MockMvcResultMatchers.jsonPath("$.itemsLength", Matchers.is(1)) + ) + .andExpect( + MockMvcResultMatchers.jsonPath("$.pm[0].performance[0].latency").value(IsNull.nullValue()) + ) + .andExpect( + MockMvcResultMatchers.jsonPath("$.pm[0].performance[0].throughput").value(IsNull.nullValue()) + ) + .andExpect( + MockMvcResultMatchers.jsonPath("$.pm[0].performance.*", hasSize(12)) + ) + ; + + // verify + BDDMockito + .verify(pmService, VerificationModeFactory.times(1)) + .getAggregatedPMDataForTimeInterval(SLOT, COUNT, OffsetDateTime.parse(startTime)); + } + + @Test + public void retrievePMDataWithEmptySlotOnEnd() throws Exception { + pmDataList.get(0).getPerformance().get(11).setLatency(null); + pmDataList.get(0).getPerformance().get(11).setThroughput(null); + + when(pmService.getAggregatedPMDataForTimeInterval(SLOT, COUNT, OffsetDateTime.parse(startTime))).thenReturn(new AggregatedPM(pmDataList, pmDataList.size())); + + BDDMockito + .given(pmService.getAggregatedPMDataForTimeInterval(SLOT, COUNT, OffsetDateTime.parse(startTime))) + .willReturn(new AggregatedPM(pmDataList, pmDataList.size())); + + mockMvc + // when + .perform( + MockMvcRequestBuilders + .get("/v1/pm/events/aggregatedmetrics") + .param("slot", String.valueOf(SLOT)) + .param("count", String.valueOf(COUNT)) + .param("startTime", startTime) + .accept(MediaType.APPLICATION_JSON) + ) + // then + .andDo(MockMvcResultHandlers.print()) + .andExpect( + MockMvcResultMatchers.status().isOk() + ) + .andExpect( + MockMvcResultMatchers + .content() + .contentTypeCompatibleWith(MediaType.APPLICATION_JSON) + ) + .andExpect( + MockMvcResultMatchers.jsonPath("$.pm[0].cellId", Matchers.is("Cell1")) + ) + .andExpect( + MockMvcResultMatchers.jsonPath("$.itemsLength", Matchers.is(1)) + ) + .andExpect( + MockMvcResultMatchers.jsonPath("$.pm[0].performance[11].latency").value(IsNull.nullValue()) + ) + .andExpect( + MockMvcResultMatchers.jsonPath("$.pm[0].performance[11].throughput").value(IsNull.nullValue()) + ) + .andExpect( + MockMvcResultMatchers.jsonPath("$.pm[0].performance.*", hasSize(12)) + ) + ; + + // verify + BDDMockito + .verify(pmService, VerificationModeFactory.times(1)) + .getAggregatedPMDataForTimeInterval(SLOT, COUNT, OffsetDateTime.parse(startTime)); + } + + @Test + public void retrievePMDataWithEmptySlotInMiddle() throws Exception { + pmDataList.get(0).getPerformance().get(5).setLatency(null); + pmDataList.get(0).getPerformance().get(5).setThroughput(null); + + when(pmService.getAggregatedPMDataForTimeInterval(SLOT, COUNT, OffsetDateTime.parse(startTime))).thenReturn(new AggregatedPM(pmDataList, pmDataList.size())); + + BDDMockito + .given(pmService.getAggregatedPMDataForTimeInterval(SLOT, COUNT, OffsetDateTime.parse(startTime))) + .willReturn(new AggregatedPM(pmDataList, pmDataList.size())); + + mockMvc + // when + .perform( + MockMvcRequestBuilders + .get("/v1/pm/events/aggregatedmetrics") + .param("slot", String.valueOf(SLOT)) + .param("count", String.valueOf(COUNT)) + .param("startTime", startTime) + .accept(MediaType.APPLICATION_JSON) + ) + // then + .andDo(MockMvcResultHandlers.print()) + .andExpect( + MockMvcResultMatchers.status().isOk() + ) + .andExpect( + MockMvcResultMatchers + .content() + .contentTypeCompatibleWith(MediaType.APPLICATION_JSON) + ) + .andExpect( + MockMvcResultMatchers.jsonPath("$.pm[0].cellId", Matchers.is("Cell1")) + ) + .andExpect( + MockMvcResultMatchers.jsonPath("$.itemsLength", Matchers.is(1)) + ) + .andExpect( + MockMvcResultMatchers.jsonPath("$.pm[0].performance[5].latency").value(IsNull.nullValue()) + ) + .andExpect( + MockMvcResultMatchers.jsonPath("$.pm[0].performance[5].throughput").value(IsNull.nullValue()) + ) + .andExpect( + MockMvcResultMatchers.jsonPath("$.pm[0].performance.*", hasSize(12)) + ) + ; + + // verify + BDDMockito + .verify(pmService, VerificationModeFactory.times(1)) + .getAggregatedPMDataForTimeInterval(SLOT, COUNT, OffsetDateTime.parse(startTime)); + } + + private String getSamplePMData() throws Exception { + String testPmContent; + InputStream in = this.getClass().getResourceAsStream("/sample-pm.json"); + try (in) { + BufferedReader inr = new BufferedReader(new InputStreamReader(in)); + testPmContent = inr.lines().collect(Collectors.joining(" ")); + } + return testPmContent; + } +}
\ No newline at end of file diff --git a/datacollector/src/test/java/org/onap/rapp/datacollector/entity/ves/AdditionalMeasurementValuesTest.java b/datacollector/src/test/java/org/onap/rapp/datacollector/entity/ves/AdditionalMeasurementValuesTest.java new file mode 100644 index 0000000..b1de446 --- /dev/null +++ b/datacollector/src/test/java/org/onap/rapp/datacollector/entity/ves/AdditionalMeasurementValuesTest.java @@ -0,0 +1,29 @@ +/* + * 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.rapp.datacollector.entity.ves; + +import static org.junit.Assert.assertEquals; +import org.junit.Test; + +public class AdditionalMeasurementValuesTest { + + @Test + public void of() { + AdditionalMeasurementValues actual = AdditionalMeasurementValues.of("test-name", "test-parameter", "test-value"); + assertEquals("test-name", actual.name); + assertEquals("test-parameter", actual.parameterName); + assertEquals("test-value", actual.parameterValue); + } +} diff --git a/datacollector/src/test/java/org/onap/rapp/datacollector/entity/ves/AdditionalMeasurementsTest.java b/datacollector/src/test/java/org/onap/rapp/datacollector/entity/ves/AdditionalMeasurementsTest.java new file mode 100644 index 0000000..14eacdc --- /dev/null +++ b/datacollector/src/test/java/org/onap/rapp/datacollector/entity/ves/AdditionalMeasurementsTest.java @@ -0,0 +1,53 @@ +/* + * 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.rapp.datacollector.entity.ves; + +import static org.junit.Assert.assertEquals; +import java.util.Map; +import org.junit.Test; + +public class AdditionalMeasurementsTest { + + final Map<String, String> m = Map.of("k1", "v1", "k2", "v2"); + + @Test + public void factories() { + AdditionalMeasurements actual = AdditionalMeasurements.of("test-measurements", m); + assertEquals("test-measurements", actual.name); + actual.values.forEach(v -> assertEquals(m.get(v.parameterName), v.parameterValue)); + + actual = AdditionalMeasurements.of(1234L, "test-measurements", m); + assertEquals("test-measurements", actual.name); + actual.values.forEach(v -> assertEquals(m.get(v.parameterName), v.parameterValue)); + assertEquals(1234L, actual.eventId.longValue()); + } + + @Test(expected = UnsupportedOperationException.class) + public void immutability() { + AdditionalMeasurements actual = AdditionalMeasurements.builder() + .withEventId(123L) + .withName("test-measurements") + .withHashMap(m) + .build(); + assertEquals(123L, actual.eventId.longValue()); + assertEquals("test-measurements", actual.name); + actual.values.forEach(v -> assertEquals(m.get(v.parameterName), v.parameterValue)); + + actual.values.add( + AdditionalMeasurementValues.of("test", "p1", "v1") + ); + } + +}
\ No newline at end of file diff --git a/datacollector/src/test/java/org/onap/rapp/datacollector/entity/ves/CommonEventHeaderTest.java b/datacollector/src/test/java/org/onap/rapp/datacollector/entity/ves/CommonEventHeaderTest.java new file mode 100644 index 0000000..873ce1b --- /dev/null +++ b/datacollector/src/test/java/org/onap/rapp/datacollector/entity/ves/CommonEventHeaderTest.java @@ -0,0 +1,89 @@ +/* + * 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.rapp.datacollector.entity.ves; + +import static org.junit.Assert.assertEquals; +import org.junit.Test; + +public class CommonEventHeaderTest { + + public static CommonEventHeader createDumyCommonEventHeader() { + return CommonEventHeader.builder() + .domain("domain") + .eventId("eventId") + .eventName("eventName") + .eventType("eventType") + .lastEpochMicrosec(12345L) + .nfcNamingCode("nfcNamingCode") + .nfNamingCode("nfNamingCode") + .priority("priority") + .reportingEntityId("entityId") + .reportingEntityName("reportingEntityName") + .sequence(567) + .sourceId("sourceId") + .sourceName("sourceName") + .startEpochMicrosec(123456789L) + .version("version") + .timeZoneOffset("UTC+2") + .build(); + + } + + public static CommonEventHeader createDumyCommonEventHeaderWithLastEpochMicro(Long lastEpochMicro) { + return CommonEventHeader.builder() + .domain("domain") + .eventId("eventId") + .eventName("eventName") + .eventType("eventType") + .lastEpochMicrosec(lastEpochMicro) + .nfcNamingCode("nfcNamingCode") + .nfNamingCode("nfNamingCode") + .priority("priority") + .reportingEntityId("entityId") + .reportingEntityName("reportingEntityName") + .sequence(567) + .sourceId("sourceId") + .sourceName("sourceName") + .startEpochMicrosec(123456789L) + .version("version") + .timeZoneOffset("UTC+2") + .build(); + + } + + @Test + public void builder() { + CommonEventHeader actual = createDumyCommonEventHeader(); + + assertEquals("version", actual.version); + assertEquals("domain", actual.domain); + assertEquals("eventId", actual.eventId); + assertEquals("eventName", actual.eventName); + assertEquals("eventType", actual.eventType); + assertEquals(12345L, actual.lastEpochMicrosec.longValue()); + assertEquals("nfcNamingCode", actual.nfcNamingCode); + assertEquals("nfNamingCode", actual.nfNamingCode); + assertEquals("priority", actual.priority); + assertEquals("entityId", actual.reportingEntityId); + assertEquals("reportingEntityName", actual.reportingEntityName); + assertEquals(567, actual.sequence.intValue()); + assertEquals("sourceId", actual.sourceId); + assertEquals("sourceName", actual.sourceName); + assertEquals(123456789L, actual.startEpochMicrosec.longValue()); + assertEquals("UTC+2", actual.timeZoneOffset); + + + } +}
\ No newline at end of file diff --git a/datacollector/src/test/java/org/onap/rapp/datacollector/entity/ves/EventTest.java b/datacollector/src/test/java/org/onap/rapp/datacollector/entity/ves/EventTest.java new file mode 100644 index 0000000..9b6700b --- /dev/null +++ b/datacollector/src/test/java/org/onap/rapp/datacollector/entity/ves/EventTest.java @@ -0,0 +1,62 @@ +/* + * 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.rapp.datacollector.entity.ves; + +import static org.junit.Assert.assertEquals; +import org.junit.Test; + +public class EventTest { + + public static Event createDumyEvent() { + CommonEventHeader header = + CommonEventHeaderTest.createDumyCommonEventHeader(); + MeasurementFields fields = + MeasurementFieldsTest.createDummy(MeasurementFieldsTest.createDummyAdditionalMeasurements()); + return Event.of(header, fields); + } + + public static Event createDumyEventWithUe() { + CommonEventHeader header = + CommonEventHeaderTest.createDumyCommonEventHeader(); + MeasurementFields fields = + MeasurementFieldsTest.createDummy(MeasurementFieldsTest.createDummyAdditionalMeasurementsWithTrafficModel()); + return Event.of(header, fields); + } + + @Test + public void of() { + CommonEventHeader header = + CommonEventHeaderTest.createDumyCommonEventHeader(); + MeasurementFields fields = + MeasurementFieldsTest.createDummy(MeasurementFieldsTest.createDummyAdditionalMeasurements()); + Event actual = Event.of(header, fields); + + assertEquals(header, actual.commonEventHeader); + assertEquals(fields, actual.measurementFields); + } + + @Test + public void testOf() { + CommonEventHeader header = + CommonEventHeaderTest.createDumyCommonEventHeader(); + MeasurementFields fields = + MeasurementFieldsTest.createDummy(MeasurementFieldsTest.createDummyAdditionalMeasurements()); + Event actual = Event.of(12345L, header, fields); + + assertEquals(12345L, actual.id.longValue()); + assertEquals(header, actual.commonEventHeader); + assertEquals(fields, actual.measurementFields); + } +}
\ No newline at end of file diff --git a/datacollector/src/test/java/org/onap/rapp/datacollector/entity/ves/MeasurementFieldsTest.java b/datacollector/src/test/java/org/onap/rapp/datacollector/entity/ves/MeasurementFieldsTest.java new file mode 100644 index 0000000..b3ca614 --- /dev/null +++ b/datacollector/src/test/java/org/onap/rapp/datacollector/entity/ves/MeasurementFieldsTest.java @@ -0,0 +1,54 @@ +/* + * 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.rapp.datacollector.entity.ves; + +import static org.junit.Assert.assertEquals; +import java.util.List; +import java.util.Map; +import org.junit.Test; + +public class MeasurementFieldsTest { + static AdditionalMeasurements createDummyAdditionalMeasurements() { + return AdditionalMeasurements.builder() + .withName("test-measurements") + .withHashMap(Map.of("k1", "v1", "k2", "v2", "k3", "v3")) + .build(); + } + + public static AdditionalMeasurements createDummyAdditionalMeasurementsWithTrafficModel() { + return AdditionalMeasurements.builder() + .withName("trafficModel") + .withHashMap(Map.of("emergency_samsung_01", "v1", "mobile_samsung_s10", "v2")) + .build(); + } + + static MeasurementFields createDummy(AdditionalMeasurements v) { + return MeasurementFields.builder() + .measurementInterval(1234567L) + .additionalMeasurements(List.of(v)) + .build(); + + } + + @Test + public void test() { + AdditionalMeasurements v = createDummyAdditionalMeasurements(); + MeasurementFields actual = createDummy(v); + + assertEquals(1234567L, actual.measurementInterval); + assertEquals("4.0", actual.measurementFieldsVersion); + assertEquals(List.of(v), actual.additionalMeasurements); + } +} diff --git a/datacollector/src/test/java/org/onap/rapp/datacollector/service/DataAggregationServiceTest.java b/datacollector/src/test/java/org/onap/rapp/datacollector/service/DataAggregationServiceTest.java new file mode 100644 index 0000000..01dbd0b --- /dev/null +++ b/datacollector/src/test/java/org/onap/rapp/datacollector/service/DataAggregationServiceTest.java @@ -0,0 +1,133 @@ +/* + * 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.rapp.datacollector.service; + +import org.onap.rapp.datacollector.entity.DataAggregationInfo; +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.CommonEventHeaderTest; +import org.onap.rapp.datacollector.entity.ves.Event; +import org.onap.rapp.datacollector.entity.ves.MeasurementFields; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.time.Instant; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Optional; + +public class DataAggregationServiceTest { + + public static final int MICRO_SECONDS_OF_SECOND = 1_000_000; + + List<Event> events; + DataAggregationInfo dataAggregationInfo; + + DataAggregationService dataAggregationService; + + @Before + public void init() { + events = new ArrayList<>(); + dataAggregationService = new DataAggregationService(); + dataAggregationInfo = createAggregationInfo(); + long startTime = dataAggregationInfo.getStartTime(); + + + for (int i = 0; i < 25; i++) { + CommonEventHeader header = CommonEventHeaderTest.createDumyCommonEventHeaderWithLastEpochMicro(startTime); + MeasurementFields measurements = createMeasurementFields(); + events.add(Event.of(header, measurements)); + startTime = startTime + MICRO_SECONDS_OF_SECOND; + } + } + + private DataAggregationInfo createAggregationInfo() { + long startTime = Instant.now().getEpochSecond() * MICRO_SECONDS_OF_SECOND; + return DataAggregationInfo.builder() + .slot(5 * MICRO_SECONDS_OF_SECOND) + .startTime(startTime) + .endTime(startTime + 5 * 5 * MICRO_SECONDS_OF_SECOND) + .build(); + } + + private MeasurementFields createMeasurementFields() { + AdditionalMeasurements latency = AdditionalMeasurements.of("latency", + Map.of("latency", "20")); + AdditionalMeasurements throughput = AdditionalMeasurements.of("throughput", + Map.of("throughput", "80")); + return MeasurementFields.builder() + .additionalMeasurements(List.of(latency, throughput)) + .build(); + } + + @Test + public void verifyAggregationData() { + PMData pmEntity = dataAggregationService.getAggregatedDataFromEventsForCell("Cell1", events, this.dataAggregationInfo); + + pmEntity.getPerformance().forEach(pm ->{ + Assert.assertEquals(Optional.of(pm.getLatency()), Optional.of(20)); + Assert.assertEquals(Optional.of(pm.getThroughput()), Optional.of(80)); + }); + } + + @Test + public void verifyAggregationDataWithEmptySlotOnBeginning() { + long startTime = this.dataAggregationInfo.getStartTime() - 10 * MICRO_SECONDS_OF_SECOND; + DataAggregationInfo dataAggregationInfo = DataAggregationInfo.builder() + .slot(5 * MICRO_SECONDS_OF_SECOND) + .startTime(startTime) + .endTime(startTime + 5 * 6 * MICRO_SECONDS_OF_SECOND) + .build(); + + PMData pmEntity = dataAggregationService.getAggregatedDataFromEventsForCell("Cell1", events, dataAggregationInfo); + + Assert.assertNull(pmEntity.getPerformance().get(0).getLatency()); + Assert.assertNull(pmEntity.getPerformance().get(0).getThroughput()); + Assert.assertEquals(pmEntity.getCellId(), "Cell1"); + } + + @Test + public void verifyAggregationDataWithEmptySlotOnEnd() { + long startTime = this.dataAggregationInfo.getStartTime(); + DataAggregationInfo dataAggregationInfo = DataAggregationInfo.builder() + .slot(5 * MICRO_SECONDS_OF_SECOND) + .startTime(this.dataAggregationInfo.getStartTime()) + .endTime(startTime + 5 * 6 * MICRO_SECONDS_OF_SECOND) + .build(); + + PMData pmEntity = dataAggregationService.getAggregatedDataFromEventsForCell("Cell1", events, dataAggregationInfo); + + Assert.assertNull(pmEntity.getPerformance().get(5).getLatency()); + Assert.assertNull(pmEntity.getPerformance().get(5).getThroughput()); + } + + @Test + public void verifyAggregationDataWithEmptySlotInMiddle() { + removeSecondSlotEvents(); + PMData pmEntity = dataAggregationService.getAggregatedDataFromEventsForCell("Cell1", events, dataAggregationInfo); + + Assert.assertNull(pmEntity.getPerformance().get(1).getLatency()); + Assert.assertNull(pmEntity.getPerformance().get(1).getThroughput()); + } + + private void removeSecondSlotEvents() { + for (int i = 0; i < 6; i++) { + events.remove(5); + } + } + +} diff --git a/datacollector/src/test/java/org/onap/rapp/datacollector/service/DmaapRestReaderConfigurationTest.java b/datacollector/src/test/java/org/onap/rapp/datacollector/service/DmaapRestReaderConfigurationTest.java new file mode 100644 index 0000000..5a593f8 --- /dev/null +++ b/datacollector/src/test/java/org/onap/rapp/datacollector/service/DmaapRestReaderConfigurationTest.java @@ -0,0 +1,57 @@ +/* + * 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.rapp.datacollector.service; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.onap.rapp.datacollector.service.configuration.DatabaseProperties; +import org.onap.rapp.datacollector.service.configuration.DmaapProperties; +import org.onap.rapp.datacollector.service.configuration.DmaapRestReaderConfiguration; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.TestPropertySource; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@ComponentScan("org.onap.rapp.datacollector.service.configuration") +@EnableConfigurationProperties({DmaapProperties.class, DatabaseProperties.class}) +@ContextConfiguration(classes = {DmaapRestReaderConfiguration.class}) +@TestPropertySource(properties = {"dmaap.host=localhost", + "dmaap.protocol=http", + "dmaap.port=8080", + "dmaap.measurements-topic=a-topic", + "database.url=jdbc:mysql://172.17.0.2:3306/ves?createDatabaseIfNotExist=true", + "database.username=root", + "database.password=mypass", + "database.driver-class-name=org.mariadb.jdbc.Driver" +}) + +public class DmaapRestReaderConfigurationTest { + + @Autowired + private DmaapRestReaderConfiguration config; + + @Test + public void testUrlConstruction() { + final String actual = config.getMeasurementsTopicUrl(); + final String expected = "http://localhost:8080/a-topic"; + + assertEquals(expected, actual); + } +} 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 new file mode 100644 index 0000000..f6cd0e5 --- /dev/null +++ b/datacollector/src/test/java/org/onap/rapp/datacollector/service/VesParserImplTest.java @@ -0,0 +1,56 @@ +/* + * 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.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 org.junit.Before; +import org.junit.Test; +import com.google.gson.JsonParseException; +import org.onap.rapp.datacollector.entity.ves.Event; + +public class VesParserImplTest { + String testVesContent; + 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(" ")); + } + } + + @Test + public void parse() { + Event actual = parser.parse(testVesContent); + assertEquals("4.0.1", actual.commonEventHeader.version); + assertEquals(1413378172000000L, (long) actual.commonEventHeader.lastEpochMicrosec); + assertEquals(1413378172000000L, (long) actual.commonEventHeader.startEpochMicrosec); + assertEquals(3, (int) actual.commonEventHeader.sequence); + assertEquals("measurement", actual.commonEventHeader.domain); + assertEquals("UTC-05:30", actual.commonEventHeader.timeZoneOffset); + } + + @Test(expected = JsonParseException.class) + public void parseEmpty() { + Event actual = parser.parse("{\"event\":{}}"); + } +}
\ No newline at end of file diff --git a/datacollector/src/test/java/org/onap/rapp/datacollector/service/VesPersisterSqlImplTest.java b/datacollector/src/test/java/org/onap/rapp/datacollector/service/VesPersisterSqlImplTest.java new file mode 100644 index 0000000..72b31c9 --- /dev/null +++ b/datacollector/src/test/java/org/onap/rapp/datacollector/service/VesPersisterSqlImplTest.java @@ -0,0 +1,47 @@ +/* + * 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.rapp.datacollector.service; + +import org.onap.rapp.datacollector.entity.ves.Event; +import org.onap.rapp.datacollector.entity.ves.EventTest; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.BDDMockito; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.internal.verification.VerificationModeFactory; +import org.mockito.junit.MockitoJUnitRunner; + +@RunWith(MockitoJUnitRunner.class) +public class VesPersisterSqlImplTest { + + @InjectMocks + private VesPersisterSqlImpl vesPersisterSql; + + @Mock + private SqlRepository repository; + + final Event event = EventTest.createDumyEvent(); + + @Test + public void persists() { + + vesPersisterSql.persists(event); + + // verify + BDDMockito + .verify(repository, VerificationModeFactory.times(1)) + .save(event); + } +}
\ 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 new file mode 100644 index 0000000..0126019 --- /dev/null +++ b/datacollector/src/test/java/org/onap/rapp/datacollector/service/VesRetrievalServiceTest.java @@ -0,0 +1,113 @@ +/* + * 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.rapp.datacollector.service; + +import java.util.Collection; +import java.util.HashSet; +import java.util.Set; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.junit.MockitoJUnitRunner; +import org.onap.rapp.datacollector.entity.ves.Event; +import org.onap.rapp.datacollector.service.configuration.DmaapRestReaderConfiguration; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.client.RestClientException; +import org.springframework.web.client.RestTemplate; +import org.onap.rapp.datacollector.entity.ves.EventTest; + +@RunWith(MockitoJUnitRunner.class) +public class VesRetrievalServiceTest { + + @Mock + private RestTemplate restTemplate; + + @Mock + private DmaapRestReaderConfiguration config; + + @Mock + private VesParser parser; + + @Mock + private VesPersister persister; + + @Mock + UEHolder ueHolder; + + private VesRetrievalService service; + + @Before + public void init() { + Mockito.when(config.getMeasurementsTopicUrl()).thenReturn("http://localhost/a-topic"); + String[] response = new String[]{"a", "b"}; + Mockito.when(restTemplate.getForEntity("http://localhost/a-topic", String[].class)) + .thenReturn(new ResponseEntity<>(response, HttpStatus.OK)); + + service = new VesRetrievalService(restTemplate, parser, persister, config, ueHolder); + } + + @Test + public void givenMockingIsDoneByMockRestServiceServer_whenGetIsCalled_thenReturnsMockedObject() { + HashSet<String> actual = new HashSet<>(service.retrieveEvents()); + Set<String> expected = Set.of("a", "b"); + Assert.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)) + .thenThrow(new RestClientException("An test exception")); + + service = new VesRetrievalService(restTemplate, parser, persister, config, ueHolder); + Collection<String> actual = service.retrieveEvents(); + Assert.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<String[]>(new String[]{"dead", "beef"}, HttpStatus.OK)); + Mockito.when(parser.parse(Mockito.any(String.class))) + .thenReturn(EventTest.createDumyEvent()); + + service = new VesRetrievalService(restTemplate, parser, persister, config, ueHolder); + service.retrieveAndStoreVesEvents(); + + Mockito.verify(persister, Mockito.times(2)).persists(Mockito.any(Event.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<String[]>(new String[]{"dead", "beef"}, HttpStatus.OK)); + Mockito.when(parser.parse(Mockito.any(String.class))) + .thenReturn(EventTest.createDumyEventWithUe()); + + UEHolder ueHolder = new UEHolder(); + + service = new VesRetrievalService(restTemplate, parser, persister, config, ueHolder); + service.retrieveAndStoreVesEvents(); + + Mockito.verify(persister, Mockito.times(2)).persists(Mockito.any(Event.class)); + Assert.assertEquals(ueHolder.getUes(), Set.of("emergency_samsung_01", "mobile_samsung_s10")); + } +} + diff --git a/datacollector/src/test/resources/sample-pm.json b/datacollector/src/test/resources/sample-pm.json new file mode 100644 index 0000000..d03c067 --- /dev/null +++ b/datacollector/src/test/resources/sample-pm.json @@ -0,0 +1,53 @@ +{ + "cellId": "Cell1", + "performance": [ + { + "latency": 20, + "throughput": 80 + }, + { + "latency": 20, + "throughput": 80 + }, + { + "latency": 20, + "throughput": 80 + }, + { + "latency": 20, + "throughput": 80 + }, + { + "latency": 20, + "throughput": 80 + }, + { + "latency": 20, + "throughput": 80 + }, + { + "latency": 20, + "throughput": 80 + }, + { + "latency": 20, + "throughput": 80 + }, + { + "latency": 20, + "throughput": 80 + }, + { + "latency": 20, + "throughput": 80 + }, + { + "latency": 20, + "throughput": 80 + }, + { + "latency": 20, + "throughput": 80 + } + ] +} diff --git a/datacollector/src/test/resources/sample-ves.json b/datacollector/src/test/resources/sample-ves.json new file mode 100644 index 0000000..114f592 --- /dev/null +++ b/datacollector/src/test/resources/sample-ves.json @@ -0,0 +1,36 @@ +{ + "event": { + "commonEventHeader": { + "version": "4.0.1", + "vesEventListenerVersion": "7.0.1", + "domain": "measurement", + "eventName": "Measurement_vIsbcMmc", + "eventId": "measurement0000259", + "sequence": 3, + "priority": "Normal", + "reportingEntityId": "cc305d54-75b4-431b-adb2-eb6b9e541234", + "reportingEntityName": "ibcx0001vm002oam001", + "sourceId": "de305d54-75b4-431b-adb2-eb6b9e546014", + "sourceName": "ibcx0001vm002ssc001", + "nfVendorName": "Samsung", + "nfNamingCode": "ibcx", + "nfcNamingCode": "ssc", + "startEpochMicrosec": 1413378172000000, + "lastEpochMicrosec": 1413378172000000, + "timeZoneOffset": "UTC-05:30" + }, + "measurementFields": { + "additionalMeasurements": [ + { + "name": "UE-1", + "hashMap": { + "latency": "20", + "throughput": "100" + } + } + ], + "measurementInterval": 5, + "measurementFieldsVersion": "4.0" + } + } +} |