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 com.android.tradefed.util.FileUtil;
20 import com.android.tradefed.util.StreamUtil;
21 
22 import junit.framework.TestCase;
23 
24 import java.io.File;
25 import java.io.FileOutputStream;
26 import java.io.IOException;
27 
28 import java.util.List;
29 import java.util.Map;
30 
31 /**
32  * Unit tests for {@link DynamicConfigHandler}
33  */
34 public class DynamicConfigHandlerTest extends TestCase {
35 
36     private static final String LOCAL_CONFIG =
37             "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
38             "<dynamicConfig>\n" +
39             "    <entry key=\"test-config-1\">\n" +
40             "        <value>test config 1</value>\n" +
41             "    </entry>\n" +
42             "    <entry key=\"test-config-2\">\n" +
43             "        <value>test config 2</value>\n" +
44             "    </entry>\n" +
45             "    <entry key=\"override-config-2\">\n" +
46             "        <value>test config 3</value>\n" +
47             "    </entry>\n" +
48             "    <entry key=\"config-list\">\n" +
49             "        <value>config0</value>\n" +
50             "        <value>config1</value>\n" +
51             "        <value>config2</value>\n" +
52             "        <value>config3</value>\n" +
53             "        <value>config4</value>\n" +
54             "    </entry>\n" +
55             "    <entry key=\"override-config-list-2\">\n" +
56             "        <value>A</value>\n" +
57             "        <value>B</value>\n" +
58             "        <value>C</value>\n" +
59             "        <value>D</value>\n" +
60             "        <value>E</value>\n" +
61             "    </entry>\n" +
62             "</dynamicConfig>\n";
63 
64     private static final String OVERRIDE_JSON =
65             "{\n" +
66             "  \"dynamicConfigEntries\": {\n" +
67             "    \"override-config-1\": {\n" +
68             "      \"configValues\": [\n" +
69             "        \"override-config-val-1\"\n" +
70             "      ]\n" +
71             "    },\n" +
72             "    \"override-config-2\": {\n" +
73             "      \"configValues\": [\n" +
74             "        \"override-config-val-2\"\n" +
75             "      ]\n" +
76             "    },\n" +
77             "    \"override-config-list-1\": {\n" +
78             "      \"configValues\": [\n" +
79             "        \"override-config-list-val-1-1\",\n" +
80             "        \"override-config-list-val-1-2\"\n" +
81             "      ]\n" +
82             "    },\n" +
83             "    \"override-config-list-2\": {\n" +
84             "      \"configValues\": [\n" +
85             "        \"override-config-list-val-2-1\"\n" +
86             "      ]\n" +
87             "    },\n" +
88             "    \"override-config-list-3\": {\n" +
89             "      \"configValues\": [\n" +
90             "      ]\n" +
91             "    }\n" +
92             "  }\n" +
93             "}";
94 
testDynamicConfigHandler()95     public void testDynamicConfigHandler() throws Exception {
96         String module = "test1";
97         File localConfigFile = createFileFromStr(LOCAL_CONFIG, module);
98         File mergedFile = null;
99         try {
100             mergedFile = DynamicConfigHandler
101                     .getMergedDynamicConfigFile(localConfigFile, OVERRIDE_JSON, module);
102 
103             Map<String, List<String>> configMap = DynamicConfig.createConfigMap(mergedFile);
104 
105             assertEquals("override-config-val-1", configMap.get("override-config-1").get(0));
106             assertTrue(configMap.get("override-config-list-1")
107                     .contains("override-config-list-val-1-1"));
108             assertTrue(configMap.get("override-config-list-1")
109                     .contains("override-config-list-val-1-2"));
110             assertTrue(configMap.get("override-config-list-3").size() == 0);
111 
112             assertEquals("test config 1", configMap.get("test-config-1").get(0));
113             assertTrue(configMap.get("config-list").contains("config2"));
114 
115             assertEquals("override-config-val-2", configMap.get("override-config-2").get(0));
116             assertEquals(1, configMap.get("override-config-list-2").size());
117             assertTrue(configMap.get("override-config-list-2")
118                     .contains("override-config-list-val-2-1"));
119         } finally {
120             FileUtil.deleteFile(localConfigFile);
121             FileUtil.recursiveDelete(mergedFile.getParentFile());
122         }
123     }
124 
createFileFromStr(String configStr, String module)125     private File createFileFromStr(String configStr, String module) throws IOException {
126         File file = File.createTempFile(module, "dynamic");
127         FileOutputStream stream = null;
128         try {
129             stream = new FileOutputStream(file);
130             stream.write(configStr.getBytes());
131             stream.flush();
132         } finally {
133             StreamUtil.close(stream);
134         }
135         return file;
136     }
137 }
138