Support processing multiple files for matching key pattern.

Bug: b/154960556

Test: FilePullerDeviceMetricCollectorTest
Change-Id: Ie0f12110a0e27df39ad12fcb4292f7e555a9a41a
diff --git a/src/com/android/tradefed/device/metric/FilePullerDeviceMetricCollector.java b/src/com/android/tradefed/device/metric/FilePullerDeviceMetricCollector.java
index 3bbf52e..6b5fa18 100644
--- a/src/com/android/tradefed/device/metric/FilePullerDeviceMetricCollector.java
+++ b/src/com/android/tradefed/device/metric/FilePullerDeviceMetricCollector.java
@@ -28,6 +28,7 @@
 import java.io.IOException;
 import java.util.AbstractMap.SimpleEntry;
 import java.util.Arrays;
+import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Map;
 import java.util.Map.Entry;
@@ -121,9 +122,11 @@
             return;
         }
         for (String key : mKeys) {
-            Entry<String, File> pulledMetrics = pullMetricFile(key, currentMetrics);
-            if (pulledMetrics != null) {
-                processMetricFile(pulledMetrics.getKey(), pulledMetrics.getValue(), data);
+            Map<String, File> pulledMetrics = pullMetricFile(key, currentMetrics);
+
+            // Process all the metric files that matched the key pattern.
+            for (Map.Entry<String, File> entry : pulledMetrics.entrySet()) {
+                processMetricFile(entry.getKey(), entry.getValue(), data);
             }
         }
 
@@ -137,8 +140,9 @@
     }
 
 
-    private Entry<String, File> pullMetricFile(
+    private Map<String, File> pullMetricFile(
             String pattern, final Map<String, String> currentMetrics) {
+        Map<String, File> matchedFiles = new HashMap<>();
         Pattern p = Pattern.compile(pattern);
         for (Entry<String, String> entry : currentMetrics.entrySet()) {
             if (p.matcher(entry.getKey()).find()) {
@@ -153,8 +157,9 @@
                             if (mCleanUp) {
                                 device.deleteFile(entry.getValue());
                             }
-                            // Return the actual key and the file associated
-                            return new SimpleEntry<String, File>(entry.getKey(), attemptPull);
+                            // Store all the keys that matches the pattern and the corresponding
+                            // files pulled from the device.
+                            matchedFiles.put(entry.getKey(), attemptPull);
                         }
                     } catch (DeviceNotAvailableException e) {
                         CLog.e(
@@ -165,9 +170,13 @@
                 }
             }
         }
-        // Not a hard failure, just nice to know
-        CLog.d("Could not find a device file associated to pattern '%s'.", pattern);
-        return null;
+
+        if (matchedFiles.isEmpty()) {
+            // Not a hard failure, just nice to know
+            CLog.d("Could not find a device file associated to pattern '%s'.", pattern);
+
+        }
+        return matchedFiles;
     }
 
     /**
diff --git a/tests/src/com/android/tradefed/device/metric/FilePullerDeviceMetricCollectorTest.java b/tests/src/com/android/tradefed/device/metric/FilePullerDeviceMetricCollectorTest.java
index 27e9203..ec04f22 100644
--- a/tests/src/com/android/tradefed/device/metric/FilePullerDeviceMetricCollectorTest.java
+++ b/tests/src/com/android/tradefed/device/metric/FilePullerDeviceMetricCollectorTest.java
@@ -95,6 +95,35 @@
     }
 
     /**
+     * Test when a multiple file is found matching the matching key, then pulled and {@link
+     * FilePullerDeviceMetricCollector#processMetricFile(String, File, DeviceMetricData)} is called
+     * multiple times.
+     */
+    @Test
+    public void testPullMultipleMatchingKeyInMetrics() throws Exception {
+        OptionSetter setter = new OptionSetter(mFilePuller);
+        setter.setOptionValue("pull-pattern-keys", "coverageFile");
+        HashMap<String, Metric> currentMetrics = new HashMap<>();
+        currentMetrics.put("coverageFile", TfMetricProtoUtil.stringToMetric("/data/coverage1"));
+        currentMetrics.put("coverageFileAnother",
+                TfMetricProtoUtil.stringToMetric("/data/coverage2"));
+
+        Mockito.when(mMockDevice.pullFile(Mockito.eq("/data/coverage1")))
+                .thenReturn(new File("fake1"));
+        Mockito.when(mMockDevice.pullFile(Mockito.eq("/data/coverage2")))
+                .thenReturn(new File("fake2"));
+
+        mFilePuller.testRunStarted("fakeRun", 5);
+        mFilePuller.testRunEnded(500, currentMetrics);
+
+        Mockito.verify(mMockListener)
+                .testLog(Mockito.eq("coverageFile"), Mockito.eq(LogDataType.TEXT), Mockito.any());
+        Mockito.verify(mMockListener)
+                .testLog(Mockito.eq("coverageFileAnother"), Mockito.eq(LogDataType.TEXT),
+                        Mockito.any());
+    }
+
+    /**
      * Test when a file is found matching the key using a pattern matching, then pulled and {@link
      * FilePullerDeviceMetricCollector#processMetricFile(String, File, DeviceMetricData)} is called.
      */