Fix issue with filtering multiple measValue

Change-Id: I8f81510445edf276561645bdd4549a8c18207b57
Issue-ID: DCAEGEN2-1327
Signed-off-by: emartin <ephraim.martin@est.tech>
diff --git a/src/main/java/org/onap/dcaegen2/services/pmmapper/filtering/MeasFilterHandler.java b/src/main/java/org/onap/dcaegen2/services/pmmapper/filtering/MeasFilterHandler.java
index 6896d3c..2dc4ae9 100644
--- a/src/main/java/org/onap/dcaegen2/services/pmmapper/filtering/MeasFilterHandler.java
+++ b/src/main/java/org/onap/dcaegen2/services/pmmapper/filtering/MeasFilterHandler.java
@@ -22,6 +22,8 @@
 
 import java.math.BigInteger;
 import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Optional;
@@ -151,29 +153,36 @@
         return FilenameUtils.getExtension(fileName).equals(XML_EXTENSION);
     }
 
-    private void setMeasInfoFromMeasType(MeasInfo currentMeasInfo,  List<MeasInfo> filteredMeasInfos, Filter filter) {
-        MeasValue currentMeasValue = currentMeasInfo.getMeasValue()
-                .get(0);
-        List<R> measResultsRNodes = currentMeasValue.getR();
-        Map<BigInteger, R> mappedR = measResultsRNodes.stream()
-                .collect(Collectors.toMap(R::getP, Function.identity()));
-        List<R> filteredRs = new ArrayList<>();
-        List<MeasType> filteredMeasTypes = currentMeasInfo.getMeasType()
-                .stream().filter(mt -> {
-                    List<String> measTypeFilters = filter.getMeasTypes();
-                    if (measTypeFilters.contains(mt.getValue())) {
-                        filteredRs.add(mappedR.get(mt.getP()));
-                        return true;
-                    }
-                    return false;
-                })
+    private boolean hasMatchingResults(List<MeasType> filteredMeasTypes, MeasValue measValue ) {
+        List<R> filteredResults = new ArrayList<>();
+
+        filteredMeasTypes.forEach( mst ->
+            measValue.getR().stream()
+                .filter(r -> mst.getP().equals(r.getP()))
+                .findFirst()
+                .ifPresent(filteredResults::add)
+        );
+
+        boolean hasResults  = !filteredResults.isEmpty();
+        if(hasResults) {
+           measValue.replaceR(filteredResults);
+        }
+       return hasResults;
+    }
+
+    private void setMeasInfoFromMeasType(MeasInfo currentMeasInfo, List<MeasInfo> filteredMeasInfos, Filter filter) {
+        List<MeasType> filteredMeasTypes = currentMeasInfo.getMeasType().stream()
+                .filter(mt -> filter.getMeasTypes().contains(mt.getValue()))
                 .collect(Collectors.toList());
-        if (!filteredMeasTypes.isEmpty()) {
+
+        if(!filteredMeasTypes.isEmpty()) {
+            List<MeasValue> filteredMeasValues = currentMeasInfo.getMeasValue().stream()
+                    .filter( mv -> hasMatchingResults(filteredMeasTypes, mv))
+                    .collect(Collectors.toList());
             currentMeasInfo.replaceMeasType(filteredMeasTypes);
-            currentMeasValue.replaceR(filteredRs);
+            currentMeasInfo.replaceMeasValue(filteredMeasValues);
             filteredMeasInfos.add(currentMeasInfo);
         }
-
     }
 
     private void setMeasInfosFromMeasTypes(MeasInfo currentMeasInfo, List<MeasInfo> filteredMeasInfos, Filter filter) {
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 613fe63..6e9e254 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
@@ -1832,6 +1832,10 @@
                 this.measType = filteredMeasTypes;

             }

 

+            public void replaceMeasValue(List<MeasValue> filteredMeasValues) {

+                this.measValue = filteredMeasValues;

+            }

+

         }

 

 

diff --git a/src/test/java/org/onap/dcaegen2/services/pmmapper/filtering/MeasFilterHandlerTest.java b/src/test/java/org/onap/dcaegen2/services/pmmapper/filtering/MeasFilterHandlerTest.java
index 694b4e9..031a3c8 100644
--- a/src/test/java/org/onap/dcaegen2/services/pmmapper/filtering/MeasFilterHandlerTest.java
+++ b/src/test/java/org/onap/dcaegen2/services/pmmapper/filtering/MeasFilterHandlerTest.java
@@ -183,6 +183,20 @@
     }
 
     @Test
+    void multiple_measValues() {
+        String inputPath = baseDir + "meas_type_and_r_manyMeasvalue";
+        String filteredString = EventUtils.fileContentsToString(Paths.get(inputPath + "_filtered.xml"));
+        Event event = generateEvent(inputPath, generateValidFilter());
+
+        MeasCollecFile f = converter.convert(filteredString);
+        String expected = converter.convert(f);
+        objUnderTest.filterByMeasType(event);
+
+        String actual = converter.convert(event.getMeasCollecFile());
+        assertEquals(expected, actual);
+    }
+
+    @Test
     void valid_fileType() {
         Event event = mock(Event.class);
         when(event.getHttpServerExchange()).thenReturn(exchange);
diff --git a/src/test/resources/filter_test/meas_type_and_r_manyMeasvalue.xml b/src/test/resources/filter_test/meas_type_and_r_manyMeasvalue.xml
new file mode 100644
index 0000000..691882c
--- /dev/null
+++ b/src/test/resources/filter_test/meas_type_and_r_manyMeasvalue.xml
@@ -0,0 +1,36 @@
+<measCollecFile xmlns="http://www.3gpp.org/ftp/specs/archive/32_series/32.435#measCollec">

+    <fileHeader fileFormatVersion="32.435 V10.0" vendorName="FooBar Ltd" dnPrefix="some dnPrefix">

+        <fileSender localDn="Dublin"/>

+        <measCollec beginTime="2018-10-02T12:00:00+01:00"/>

+    </fileHeader>

+    <measData>

+        <managedElement localDn="Dublin" swVersion="r0.1"/>

+        <measInfo measInfoId="some measInfoId">

+            <job jobId="jobId"/>

+            <granPeriod duration="PT900S" endTime="2018-10-02T12:15:00Z"/>

+            <repPeriod duration="PT900S"/>

+            <measTypes></measTypes>

+            <measType p="1">a</measType>

+            <measType p="2">b</measType>

+            <measType p="3">c</measType>

+            <measValue measObjLdn="some measObjLdn 1">

+                <r p="1">1</r>

+                <r p="2">2</r>

+                <suspect>false</suspect>

+            </measValue>

+            <measValue measObjLdn="some measObjLdn 2">

+                <r p="88">88</r>

+                <r p="99">99</r>

+                <suspect>false</suspect>

+            </measValue>

+            <measValue measObjLdn="some measObjLdn 3">

+                <r p="1">111</r>

+                <r p="2">222</r>

+                <suspect>false</suspect>

+            </measValue>

+        </measInfo>

+    </measData>

+    <fileFooter>

+        <measCollec endTime="2018-10-02T12:15:00+01:00"/>

+    </fileFooter>

+</measCollecFile>
\ No newline at end of file
diff --git a/src/test/resources/filter_test/meas_type_and_r_manyMeasvalue_filtered.xml b/src/test/resources/filter_test/meas_type_and_r_manyMeasvalue_filtered.xml
new file mode 100644
index 0000000..9f185a4
--- /dev/null
+++ b/src/test/resources/filter_test/meas_type_and_r_manyMeasvalue_filtered.xml
@@ -0,0 +1,30 @@
+<measCollecFile xmlns="http://www.3gpp.org/ftp/specs/archive/32_series/32.435#measCollec">

+    <fileHeader fileFormatVersion="32.435 V10.0" vendorName="FooBar Ltd" dnPrefix="some dnPrefix">

+        <fileSender localDn="Dublin"/>

+        <measCollec beginTime="2018-10-02T12:00:00+01:00"/>

+    </fileHeader>

+    <measData>

+        <managedElement localDn="Dublin" swVersion="r0.1"/>

+        <measInfo measInfoId="some measInfoId">

+            <job jobId="jobId"/>

+            <granPeriod duration="PT900S" endTime="2018-10-02T12:15:00Z"/>

+            <repPeriod duration="PT900S"/>

+            <measTypes></measTypes>

+            <measType p="1">a</measType>

+            <measType p="2">b</measType>

+            <measValue measObjLdn="some measObjLdn 1">

+                <r p="1">1</r>

+                <r p="2">2</r>

+                <suspect>false</suspect>

+            </measValue>

+            <measValue measObjLdn="some measObjLdn 3">

+                <r p="1">111</r>

+                <r p="2">222</r>

+                <suspect>false</suspect>

+            </measValue>

+        </measInfo>

+    </measData>

+    <fileFooter>

+        <measCollec endTime="2018-10-02T12:15:00+01:00"/>

+    </fileFooter>

+</measCollecFile>
\ No newline at end of file