Sync missing CL from qt-dev

Test: cts unit tests
Bug: 148692627
Change-Id: Icb892dd37c4baa5bc02fa50c38d01d7704730c09
Merged-In: I0f0f3b7b676a3782f06b7e6e18f3afee77daf75d
diff --git a/common/host-side/tradefed/src/com/android/compatibility/common/tradefed/targetprep/BusinessLogicPreparer.java b/common/host-side/tradefed/src/com/android/compatibility/common/tradefed/targetprep/BusinessLogicPreparer.java
index d59f0b6..a65b95b 100644
--- a/common/host-side/tradefed/src/com/android/compatibility/common/tradefed/targetprep/BusinessLogicPreparer.java
+++ b/common/host-side/tradefed/src/com/android/compatibility/common/tradefed/targetprep/BusinessLogicPreparer.java
@@ -375,11 +375,22 @@
             return extendedDeviceInfo;
         }
         File ediFile = null;
+        String[] fileAndKey = null;
         try{
             for (String ediEntry: requiredDeviceInfo) {
-                String[] fileAndKey = ediEntry.split(":");
+                fileAndKey = ediEntry.split(":");
+                if (fileAndKey.length <= 1) {
+                    CLog.e("Dynamic config Extended DeviceInfo key has problem.");
+                    return new ArrayList<>();
+                }
                 ediFile = FileUtil
                     .findFile(deviceInfoPath, fileAndKey[0] + ".deviceinfo.json");
+                if (ediFile == null) {
+                    CLog.e(
+                            "Could not find Extended DeviceInfo JSON file: %s.",
+                            deviceInfoPath + fileAndKey[0] + ".deviceinfo.json");
+                    return new ArrayList<>();
+                }
                 String jsonString = FileUtil.readStringFromFile(ediFile);
                 JSONObject jsonObj = new JSONObject(jsonString);
                 String value = jsonObj.getString(fileAndKey[1]);
@@ -387,8 +398,9 @@
                     .add(String.format("%s:%s:%s", fileAndKey[0], fileAndKey[1], value));
             }
         }catch(JSONException | IOException | RuntimeException e){
-            CLog.e("Failed to read or parse Extended DeviceInfo JSON file: %s. Error: %s",
-                ediFile.getAbsolutePath(), e);
+            CLog.e(
+                    "Failed to read or parse Extended DeviceInfo JSON file: %s. Error: %s",
+                    deviceInfoPath + fileAndKey[0] + ".deviceinfo.json", e);
             return new ArrayList<>();
         }
         return extendedDeviceInfo;
diff --git a/common/host-side/tradefed/tests/src/com/android/compatibility/common/tradefed/targetprep/BusinessLogicPreparerTest.java b/common/host-side/tradefed/tests/src/com/android/compatibility/common/tradefed/targetprep/BusinessLogicPreparerTest.java
index cbffc2f..a32218a 100644
--- a/common/host-side/tradefed/tests/src/com/android/compatibility/common/tradefed/targetprep/BusinessLogicPreparerTest.java
+++ b/common/host-side/tradefed/tests/src/com/android/compatibility/common/tradefed/targetprep/BusinessLogicPreparerTest.java
@@ -169,6 +169,17 @@
         testBuildRequestString(14, attributes);
     }
 
+    @Test
+    public void testBuildRequestString_noDeviceInfoJSONFileExists() throws Exception {
+        Map<String, String> attributes = new HashMap<>();
+        // Create a memory device info JSON file for test.
+        File jsonPath = createTestDeviceInfoTextFile("MemoryDeviceInfo");
+        mMockBuildInfo.setFile(DeviceInfoCollector.DEVICE_INFO_DIR, jsonPath, "v1");
+        // Setup BuildInfo attributes.
+        mMockBuildInfo.addBuildAttribute(CompatibilityBuildHelper.SUITE_VERSION, "v1");
+        testBuildRequestString(14, attributes);
+    }
+
     private void testBuildRequestString(int expectedParams, Map<String, String> attributes) throws Exception {
         for (String key: attributes.keySet()) {
             mMockBuildInfo.addBuildAttribute(key, attributes.get(key));
@@ -265,6 +276,12 @@
         return mTmpDir;
     }
 
+    private File createTestDeviceInfoTextFile(String DeviceInfoClassName)
+        throws IOException {
+        new File(mTmpDir, DeviceInfoClassName + ".deviceinfo.text");
+        return mTmpDir;
+    }
+
     private File createFileFromStr(String configStr) throws IOException {
         File file = File.createTempFile("test", "dynamic");
         FileOutputStream stream = new FileOutputStream(file);