1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.compatibility.common.util;
18 
19 import static org.junit.Assert.assertTrue;
20 
21 import org.xmlpull.v1.XmlPullParser;
22 import org.xmlpull.v1.XmlPullParserException;
23 import org.xmlpull.v1.XmlPullParserFactory;
24 
25 import java.io.File;
26 import java.io.FileInputStream;
27 import java.io.FileNotFoundException;
28 import java.io.IOException;
29 import java.io.InputStreamReader;
30 import java.util.ArrayList;
31 import java.util.HashMap;
32 import java.util.List;
33 import java.util.Map;
34 import java.util.Set;
35 
36 /**
37  * Load dynamic config for test cases
38  */
39 public class DynamicConfig {
40 
41     //XML constant
42     public static final String NS = null;
43     public static final String CONFIG_TAG = "dynamicConfig";
44     public static final String ENTRY_TAG = "entry";
45     public static final String VALUE_TAG = "value";
46     public static final String KEY_ATTR = "key";
47 
48     public static final String REMOTE_CONFIG_REQUIRED_KEY = "remote_config_required";
49     public static final String REMOTE_CONFIG_RETRIEVED_KEY = "remote_config_retrieved";
50     public static final String CONFIG_FOLDER_ON_DEVICE = "/sdcard/dynamic-config-files/";
51 
52     protected Map<String, List<String>> mDynamicConfigMap = new HashMap<String, List<String>>();
53 
initializeConfig(File file)54     public void initializeConfig(File file) throws XmlPullParserException, IOException {
55         mDynamicConfigMap = createConfigMap(file);
56     }
57 
58     /** Init using directly a {@link FileInputStream} from the config file. */
initializeConfig(FileInputStream fileStream)59     public void initializeConfig(FileInputStream fileStream)
60             throws XmlPullParserException, IOException {
61         mDynamicConfigMap = createConfigMap(fileStream);
62     }
63 
getValue(String key)64     public String getValue(String key) {
65         assertRemoteConfigRequirementMet();
66         List<String> singleValue = mDynamicConfigMap.get(key);
67         if (singleValue == null || singleValue.size() == 0 || singleValue.size() > 1) {
68             // key must exist in the map, and map to a list containing exactly one string
69             return null;
70         }
71         return singleValue.get(0);
72     }
73 
getValues(String key)74     public List<String> getValues(String key) {
75         assertRemoteConfigRequirementMet();
76         return mDynamicConfigMap.get(key);
77     }
78 
keySet()79     public Set<String> keySet() {
80         assertRemoteConfigRequirementMet();
81         return mDynamicConfigMap.keySet();
82     }
83 
remoteConfigRequired()84     public boolean remoteConfigRequired() {
85         if (mDynamicConfigMap.containsKey(REMOTE_CONFIG_REQUIRED_KEY)) {
86             String val = mDynamicConfigMap.get(REMOTE_CONFIG_REQUIRED_KEY).get(0);
87             return Boolean.parseBoolean(val);
88         }
89         return true; // require remote configuration by default
90     }
91 
remoteConfigRetrieved()92     public boolean remoteConfigRetrieved() {
93         // assume config will always contain exactly one value, populated by DynamicConfigHandler
94         String val = mDynamicConfigMap.get(REMOTE_CONFIG_RETRIEVED_KEY).get(0);
95         return Boolean.parseBoolean(val);
96     }
97 
assertRemoteConfigRequirementMet()98     public void assertRemoteConfigRequirementMet() {
99         assertTrue("Remote connection to DynamicConfigService required for this test",
100                 !remoteConfigRequired() || remoteConfigRetrieved());
101     }
102 
getConfigFile(File configFolder, String moduleName)103     public static File getConfigFile(File configFolder, String moduleName)
104             throws FileNotFoundException {
105         File config = getConfigFileUnchecked(configFolder, moduleName);
106         if (!config.exists()) {
107             throw new FileNotFoundException(String.format("Cannot find %s.dynamic", moduleName));
108         }
109         return config;
110     }
111 
getConfigFileUnchecked(File configFolder, String moduleName)112     public static File getConfigFileUnchecked(File configFolder, String moduleName) {
113         return new File(configFolder, String.format("%s.dynamic", moduleName));
114     }
115 
createConfigMap(File file)116     public static Map<String, List<String>> createConfigMap(File file)
117             throws XmlPullParserException, IOException {
118         try (FileInputStream stream = new FileInputStream(file)) {
119             return createConfigMap(stream);
120         }
121     }
122 
createConfigMap(FileInputStream fileStream)123     public static Map<String, List<String>> createConfigMap(FileInputStream fileStream)
124             throws XmlPullParserException, IOException {
125 
126         Map<String, List<String>> dynamicConfigMap = new HashMap<String, List<String>>();
127         XmlPullParser parser = XmlPullParserFactory.newInstance().newPullParser();
128         parser.setInput(new InputStreamReader(fileStream));
129         parser.nextTag();
130         parser.require(XmlPullParser.START_TAG, NS, CONFIG_TAG);
131 
132         while (parser.nextTag() == XmlPullParser.START_TAG) {
133             parser.require(XmlPullParser.START_TAG, NS, ENTRY_TAG);
134             String key = parser.getAttributeValue(NS, KEY_ATTR);
135             List<String> valueList = new ArrayList<String>();
136             while (parser.nextTag() == XmlPullParser.START_TAG) {
137                 parser.require(XmlPullParser.START_TAG, NS, VALUE_TAG);
138                 valueList.add(parser.nextText());
139                 parser.require(XmlPullParser.END_TAG, NS, VALUE_TAG);
140             }
141             parser.require(XmlPullParser.END_TAG, NS, ENTRY_TAG);
142             if (key != null && !key.isEmpty()) {
143                 dynamicConfigMap.put(key, valueList);
144             }
145         }
146 
147         parser.require(XmlPullParser.END_TAG, NS, CONFIG_TAG);
148         return dynamicConfigMap;
149     }
150 }
151