Split typeC XML to multiple Type A

*Split TypeC MeasFile to multiple TypeA by MeasData
*Create new Event for each TypeA

Change-Id: I22eb22c663c8491f3cae5e39e583ee2660647743
Issue-ID: DCAEGEN2-1281
Signed-off-by: emartin <ephraim.martin@est.tech>
diff --git a/src/main/java/org/onap/dcaegen2/services/pmmapper/model/MeasCollecFile.java b/src/main/java/org/onap/dcaegen2/services/pmmapper/model/MeasCollecFile.java
index 572b46b..613fe63 100644
--- a/src/main/java/org/onap/dcaegen2/services/pmmapper/model/MeasCollecFile.java
+++ b/src/main/java/org/onap/dcaegen2/services/pmmapper/model/MeasCollecFile.java
@@ -1841,4 +1841,9 @@
 

     }

 

+

+    public void replaceMeasData(List<MeasData> measDataList) {

+        this.measData = measDataList;

+    }

+

 }

diff --git a/src/main/java/org/onap/dcaegen2/services/pmmapper/utils/MeasSplitter.java b/src/main/java/org/onap/dcaegen2/services/pmmapper/utils/MeasSplitter.java
new file mode 100644
index 0000000..009e454
--- /dev/null
+++ b/src/main/java/org/onap/dcaegen2/services/pmmapper/utils/MeasSplitter.java
@@ -0,0 +1,75 @@
+/*-

+ * ============LICENSE_START=======================================================

+ *  Copyright (C) 2019 Nordix Foundation.

+ * ================================================================================

+ * 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.

+ *

+ * SPDX-License-Identifier: Apache-2.0

+ * ============LICENSE_END=========================================================

+ */

+

+package org.onap.dcaegen2.services.pmmapper.utils;

+

+import java.util.Arrays;

+import java.util.List;

+import java.util.Map;

+import java.util.stream.Collectors;

+

+import org.onap.dcaegen2.services.pmmapper.model.Event;

+import org.onap.dcaegen2.services.pmmapper.model.EventMetadata;

+import org.onap.dcaegen2.services.pmmapper.model.MeasCollecFile;

+import org.onap.dcaegen2.services.pmmapper.model.MeasCollecFile.MeasData;

+import org.onap.logging.ref.slf4j.ONAPLogAdapter;

+import org.slf4j.LoggerFactory;

+

+import io.undertow.server.HttpServerExchange;

+

+/**

+ * Splits the MeasCollecFile based on MeasData.

+ **/

+public class MeasSplitter {

+    private static final ONAPLogAdapter logger = new ONAPLogAdapter(LoggerFactory.getLogger(MeasSplitter.class));

+    private MeasConverter converter;

+

+    public MeasSplitter(MeasConverter converter) {

+        this.converter = converter;

+    }

+

+    public List<Event> split(Event event) {

+        logger.unwrap().debug("Splitting 3GPP xml MeasData to MeasCollecFile");

+        MeasCollecFile currentMeasurement = converter.convert(event.getBody());

+

+        return currentMeasurement.getMeasData().stream().map( measData -> {

+            Event newEvent  = generateNewEvent(event);

+            MeasCollecFile newMeasCollec = generateNewMeasCollec(newEvent,measData);

+            newEvent.setMeasCollecFile(newMeasCollec);

+            return newEvent;

+        }).collect(Collectors.toList());

+    }

+

+    private MeasCollecFile generateNewMeasCollec(Event event, MeasData measData) {

+        MeasCollecFile measCollec = new MeasCollecFile();

+        measCollec.replaceMeasData(Arrays.asList(measData));

+        measCollec.setFileHeader(event.getMeasCollecFile().getFileHeader());

+        measCollec.setFileFooter(event.getMeasCollecFile().getFileFooter());

+        return measCollec;

+    }

+

+    private Event generateNewEvent(Event event) {

+        Event modifiedEvent =  new Event(event.getHttpServerExchange(),

+                event.getBody(), event.getMetadata(), event.getMdc(),

+                event.getPublishIdentity());

+        modifiedEvent.setMeasCollecFile(event.getMeasCollecFile());

+        return modifiedEvent;

+    }

+}

diff --git a/src/test/java/org/onap/dcaegen2/services/pmmapper/utils/MeasSplitterTest.java b/src/test/java/org/onap/dcaegen2/services/pmmapper/utils/MeasSplitterTest.java
new file mode 100644
index 0000000..962303c
--- /dev/null
+++ b/src/test/java/org/onap/dcaegen2/services/pmmapper/utils/MeasSplitterTest.java
@@ -0,0 +1,96 @@
+/*-

+ * ============LICENSE_START=======================================================

+ *  Copyright (C) 2019 Nordix Foundation.

+ * ================================================================================

+ * 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.

+ *

+ * SPDX-License-Identifier: Apache-2.0

+ * ============LICENSE_END=========================================================

+ */

+package org.onap.dcaegen2.services.pmmapper.utils;

+import static org.junit.Assert.assertEquals;

+

+import java.nio.file.Paths;

+import java.util.HashMap;

+import java.util.List;

+

+import javax.xml.bind.JAXBException;

+

+import org.junit.jupiter.api.Assertions;

+import org.junit.jupiter.api.BeforeEach;

+import org.junit.jupiter.api.Test;

+import org.junit.jupiter.api.extension.ExtendWith;

+import org.mockito.Mock;

+import org.mockito.Mockito;

+import org.mockito.junit.jupiter.MockitoExtension;

+import org.onap.dcaegen2.services.pmmapper.model.Event;

+import org.onap.dcaegen2.services.pmmapper.model.EventMetadata;

+import org.onap.dcaegen2.services.pmmapper.model.MeasCollecFile;

+

+import io.undertow.server.HttpServerExchange;

+import utils.EventUtils;

+

+@ExtendWith(MockitoExtension.class)

+public class MeasSplitterTest {

+    private static final String baseDir = "src/test/resources/split_test/";

+    private MeasSplitter objUnderTest;

+    private MeasConverter converter;

+    @Mock

+    HttpServerExchange exchange;

+    @Mock

+    EventMetadata meta;

+    @Mock

+    Event event;

+

+    @BeforeEach

+    public void setup() {

+        converter =  new MeasConverter();

+        objUnderTest = new MeasSplitter(converter);

+        Mockito.when(event.getHttpServerExchange()).thenReturn(exchange);

+        Mockito.when(event.getMetadata()).thenReturn(meta);

+        Mockito.when(event.getMdc()).thenReturn(new HashMap<String, String>());

+        Mockito.when(event.getMetadata()).thenReturn(meta);

+        Mockito.when(event.getPublishIdentity()).thenReturn("");

+    }

+

+    @Test

+    public void typeA_returns_only_one_event() throws JAXBException {

+        String inputPath = baseDir + "meas_results_typeA";

+        String inputXml = EventUtils.fileContentsToString(Paths.get(inputPath + ".xml"));

+        MeasCollecFile measToBeSplit = converter.convert(inputXml);

+        Mockito.when(event.getBody()).thenReturn(inputXml);

+        Mockito.when(event.getMeasCollecFile()).thenReturn(measToBeSplit);

+

+        List<Event> splitEvents = objUnderTest.split(event);

+

+        assertEquals(1,splitEvents.size());

+    }

+

+    @Test

+    public void typeC_returns_multiple_events() throws JAXBException {

+        String inputPath = baseDir + "meas_results_typeC";

+        String inputXml = EventUtils.fileContentsToString(Paths.get(inputPath + ".xml"));

+        Mockito.when(event.getBody()).thenReturn(inputXml);

+        MeasCollecFile measToBeSplit = converter.convert(inputXml);

+        Mockito.when(event.getMeasCollecFile()).thenReturn(measToBeSplit);

+

+        List<Event> splitEvents = objUnderTest.split(event);

+

+        assertEquals(3,splitEvents.size());

+        for (int i = 0; i < splitEvents.size(); i++) {

+          String measInfoId = splitEvents.get(i).getMeasCollecFile()

+                  .getMeasData().get(0).getMeasInfo().get(0).getMeasInfoId();

+          Assertions.assertTrue(measInfoId.equals("measInfoId"+(i+1)));

+        }

+    }

+}

diff --git a/src/test/resources/split_test/meas_results_typeA.xml b/src/test/resources/split_test/meas_results_typeA.xml
new file mode 100644
index 0000000..bc87b79
--- /dev/null
+++ b/src/test/resources/split_test/meas_results_typeA.xml
@@ -0,0 +1,23 @@
+<measCollecFile xmlns="http://www.3gpp.org/ftp/specs/archive/32_series/32.435#measCollec">
+    <fileHeader dnPrefix="some dnPrefix" vendorName="FooBar Ltd"
+                fileFormatVersion="32.435 V10.0">
+        <fileSender localDn="Dublin"/>
+        <measCollec beginTime="2018-10-02T12:00:00+01:00"/>
+    </fileHeader>
+    <measData>
+        <managedElement swVersion="r0.1" localDn="Dublin"/>
+        <measInfo measInfoId="some measInfoId">
+            <job jobId="jobId"/>
+            <granPeriod endTime="2018-10-02T12:15:00Z" duration="PT900S"/>
+            <repPeriod duration="PT900S"/>
+            <measTypes>z a zz b</measTypes>
+            <measValue measObjLdn="objLdn">
+                <measResults>99 1 27 2</measResults>
+                <suspect>false</suspect>
+            </measValue>
+        </measInfo>
+    </measData>
+    <fileFooter>
+        <measCollec endTime="2018-10-02T12:15:00+01:00"/>
+    </fileFooter>
+</measCollecFile>
diff --git a/src/test/resources/split_test/meas_results_typeC.xml b/src/test/resources/split_test/meas_results_typeC.xml
new file mode 100644
index 0000000..b6a3b43
--- /dev/null
+++ b/src/test/resources/split_test/meas_results_typeC.xml
@@ -0,0 +1,49 @@
+<measCollecFile xmlns="http://www.3gpp.org/ftp/specs/archive/32_series/32.435#measCollec">
+    <fileHeader dnPrefix="some dnPrefix" vendorName="FooBar Ltd"
+                fileFormatVersion="32.435 V10.0">
+        <fileSender localDn="Dublin"/>
+        <measCollec beginTime="2018-10-02T12:00:00+01:00"/>
+    </fileHeader>
+    <measData>
+        <managedElement swVersion="r0.1" localDn="Dublin1"/>
+        <measInfo measInfoId="measInfoId1">
+            <job jobId="jobId1"/>
+            <granPeriod endTime="2001-10-02T12:15:00Z" duration="PT100S"/>
+            <repPeriod duration="PT100S"/>
+            <measTypes>z1 a1 zz1 b1</measTypes>
+            <measValue measObjLdn="objLdn">
+                <measResults>1 11 111 1111</measResults>
+                <suspect>false</suspect>
+            </measValue>
+        </measInfo>
+    </measData>
+    <measData>
+        <managedElement swVersion="r0.2" localDn="Dublin2"/>
+        <measInfo measInfoId="measInfoId2">
+            <job jobId="jobId"/>
+            <granPeriod endTime="2002-10-02T12:15:00Z" duration="PT200S"/>
+            <repPeriod duration="PT200S"/>
+            <measTypes>z2 a2 zz2 b2</measTypes>
+            <measValue measObjLdn="objLdn">
+                <measResults>2 22 222 2222</measResults>
+                <suspect>false</suspect>
+            </measValue>
+        </measInfo>
+    </measData>
+    <measData>
+        <managedElement swVersion="r0.3" localDn="Dublin3"/>
+        <measInfo measInfoId="measInfoId3">
+            <job jobId="jobId"/>
+            <granPeriod endTime="2003-10-02T12:15:00Z" duration="PT300S"/>
+            <repPeriod duration="PT300S"/>
+            <measTypes>z3 a3 zz3 b3</measTypes>
+            <measValue measObjLdn="objLdn">
+                <measResults>3 33 333 3333</measResults>
+                <suspect>false</suspect>
+            </measValue>
+        </measInfo>
+    </measData>
+    <fileFooter>
+        <measCollec endTime="2018-10-02T12:15:00+01:00"/>
+    </fileFooter>
+</measCollecFile>