1 /*
2  * Copyright (C) 2017 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 junit.framework.Assert.assertEquals;
20 import static junit.framework.Assert.assertFalse;
21 import static junit.framework.Assert.assertTrue;
22 
23 import org.junit.Test;
24 import org.junit.runner.RunWith;
25 import org.junit.runners.JUnit4;
26 
27 import com.android.compatibility.common.util.BusinessLogic.BusinessLogicRule;
28 import com.android.compatibility.common.util.BusinessLogic.BusinessLogicRuleAction;
29 import com.android.compatibility.common.util.BusinessLogic.BusinessLogicRuleCondition;
30 import com.android.tradefed.util.FileUtil;
31 
32 import java.io.File;
33 import java.io.FileOutputStream;
34 import java.io.IOException;
35 import java.util.List;
36 
37 /**
38  * Unit tests for {@link BusinessLogic}
39  */
40 @RunWith(JUnit4.class)
41 public class BusinessLogicTest {
42 
43     private static final String CORRECT_LOGIC =
44             "{\n" +
45             "  \"name\": \"businessLogic/suites/gts\",\n" +
46             "  \"businessLogicRulesLists\": [\n" +
47             "    {\n" +
48             "      \"testName\": \"testCaseName1\",\n" +
49             "      \"description\": \"first test\",\n" +
50             "      \"businessLogicRules\": [\n" +
51             "        {\n" +
52             "          \"ruleConditions\": [\n" +
53             "            {\n" +
54             "              \"methodName\": \"conditionMethodName1\",\n" +
55             "              \"methodArgs\": [\n" +
56             "                \"arg1\"\n" +
57             "              ]\n" +
58             "            }\n" +
59             "          ],\n" +
60             "          \"ruleActions\": [\n" +
61             "            {\n" +
62             "              \"methodName\": \"actionMethodName1\",\n" +
63             "              \"methodArgs\": [\n" +
64             "                \"arg1\",\n" +
65             "                \"arg2\"\n" +
66             "              ]\n" +
67             "            }\n" +
68             "          ]\n" +
69             "        }\n" +
70             "      ]\n" +
71             "    },\n" +
72             "    {\n" +
73             "      \"testName\": \"testCaseName2\",\n" +
74             "      \"businessLogicRules\": [\n" +
75             "        {\n" +
76             "          \"ruleConditions\": [\n" +
77             "            {\n" +
78             "              \"methodName\": \"conditionMethodName1\",\n" +
79             "              \"methodArgs\": [\n" +
80             "                \"arg1\"\n" +
81             "              ]\n" +
82             "            }\n" +
83             "          ],\n" +
84             "          \"ruleActions\": [\n" +
85             "            {\n" +
86             "              \"methodName\": \"actionMethodName1\",\n" +
87             "              \"methodArgs\": [\n" +
88             "                \"arg1\",\n" +
89             "                \"arg2\"\n" +
90             "              ]\n" +
91             "            }\n" +
92             "          ]\n" +
93             "        },\n" +
94             "        {\n" +
95             "          \"ruleConditions\": [\n" +
96             "            {\n" +
97             "              \"methodName\": \"conditionMethodName1\",\n" +
98             "              \"methodArgs\": [\n" +
99             "                \"arg1\"\n" +
100             "              ]\n" +
101             "            },\n" +
102             "            {\n" +
103             "              \"methodName\": \"!conditionMethodName2\",\n" + // use negation
104             "              \"methodArgs\": [\n" +
105             "                \"arg2\"\n" +
106             "              ]\n" +
107             "            }\n" +
108             "          ],\n" +
109             "          \"ruleActions\": [\n" +
110             "            {\n" +
111             "              \"methodName\": \"actionMethodName1\",\n" +
112             "              \"methodArgs\": [\n" +
113             "                \"arg1\",\n" +
114             "                \"arg2\"\n" +
115             "              ]\n" +
116             "            },\n" +
117             "            {\n" +
118             "              \"methodName\": \"actionMethodName2\"\n" +
119             "            }\n" +
120             "          ]\n" +
121             "        }\n" +
122             "      ]\n" +
123             "    },\n" +
124             "    {\n" +
125             "      \"testName\": \"testCaseName3\"\n" +
126             "    }\n" +
127             "  ]\n" +
128             "}";
129 
130     @Test
testCorrectLogic()131     public void testCorrectLogic() throws Exception {
132         File file = createFileFromStr(CORRECT_LOGIC);
133         try {
134             BusinessLogic bl = BusinessLogicFactory.createFromFile(file);
135             assertEquals("Wrong number of business logic rule lists", 3, bl.mRules.size());
136             String description = bl.mRules.get("testCaseName1").get(0).getDescription();
137             assertEquals("Wrong or missing rule list description", "first test", description);
138             List<BusinessLogicRule> rulesList1 = bl.mRules.get("testCaseName1").get(0).getRules();
139             assertEquals("Wrong number of rules in first rule list", 1, rulesList1.size());
140             BusinessLogicRule rule1 = rulesList1.get(0);
141             List<BusinessLogicRuleCondition> rule1Conditions = rule1.mConditions;
142             assertEquals("Wrong number of conditions", 1, rule1Conditions.size());
143             BusinessLogicRuleCondition rule1Condition = rule1Conditions.get(0);
144             assertEquals("Wrong method name for business logic rule condition",
145                     "conditionMethodName1", rule1Condition.mMethodName);
146             assertFalse("Wrong negation value for business logic rule condition",
147                     rule1Condition.mNegated);
148             assertEquals("Wrong arg string count for business logic rule condition", 1,
149                     rule1Condition.mMethodArgs.size());
150             assertEquals("Wrong arg for business logic rule condition", "arg1",
151                     rule1Condition.mMethodArgs.get(0));
152             List<BusinessLogicRuleAction> rule1Actions = rule1.mActions;
153             assertEquals("Wrong number of actions", 1, rule1Actions.size());
154             BusinessLogicRuleAction rule1Action = rule1Actions.get(0);
155             assertEquals("Wrong method name for business logic rule action",
156                     "actionMethodName1", rule1Action.mMethodName);
157             assertEquals("Wrong arg string count for business logic rule action", 2,
158                     rule1Action.mMethodArgs.size());
159             assertEquals("Wrong arg for business logic rule action", "arg1",
160                     rule1Action.mMethodArgs.get(0));
161             assertEquals("Wrong arg for business logic rule action", "arg2",
162                     rule1Action.mMethodArgs.get(1));
163 
164             List<BusinessLogicRule> rulesList2 = bl.mRules.get("testCaseName2").get(0).getRules();
165             assertEquals("Wrong number of rules in second rule list", 2, rulesList2.size());
166             BusinessLogicRule rule2 = rulesList2.get(0);
167             List<BusinessLogicRuleCondition> rule2Conditions = rule2.mConditions;
168             assertEquals("Wrong number of conditions", 1, rule2Conditions.size());
169             BusinessLogicRuleCondition rule2Condition = rule2Conditions.get(0);
170             assertEquals("Wrong method name for business logic rule condition",
171                     "conditionMethodName1", rule2Condition.mMethodName);
172             assertFalse("Wrong negation value for business logic rule condition",
173                     rule2Condition.mNegated);
174             assertEquals("Wrong arg string count for business logic rule condition", 1,
175                     rule2Condition.mMethodArgs.size());
176             assertEquals("Wrong arg for business logic rule condition", "arg1",
177                     rule2Condition.mMethodArgs.get(0));
178             List<BusinessLogicRuleAction> rule2Actions = rule2.mActions;
179             assertEquals("Wrong number of actions", 1, rule2Actions.size());
180             BusinessLogicRuleAction rule2Action = rule2Actions.get(0);
181             assertEquals("Wrong method name for business logic rule action",
182                     "actionMethodName1", rule2Action.mMethodName);
183             assertEquals("Wrong arg string count for business logic rule action", 2,
184                     rule2Action.mMethodArgs.size());
185             assertEquals("Wrong arg for business logic rule action", "arg1",
186                     rule2Action.mMethodArgs.get(0));
187             assertEquals("Wrong arg for business logic rule action", "arg2",
188                     rule2Action.mMethodArgs.get(1));
189             BusinessLogicRule rule3 = rulesList2.get(1);
190             List<BusinessLogicRuleCondition> rule3Conditions = rule3.mConditions;
191             assertEquals("Wrong number of conditions", 2, rule3Conditions.size());
192             BusinessLogicRuleCondition rule3Condition1 = rule3Conditions.get(0);
193             assertEquals("Wrong method name for business logic rule condition",
194                     "conditionMethodName1", rule3Condition1.mMethodName);
195             assertFalse("Wrong negation value for business logic rule condition",
196                     rule3Condition1.mNegated);
197             assertEquals("Wrong arg string count for business logic rule condition", 1,
198                     rule3Condition1.mMethodArgs.size());
199             assertEquals("Wrong arg for business logic rule condition", "arg1",
200                     rule3Condition1.mMethodArgs.get(0));
201             BusinessLogicRuleCondition rule3Condition2 = rule3Conditions.get(1);
202             assertEquals("Wrong method name for business logic rule condition",
203                     "conditionMethodName2", rule3Condition2.mMethodName);
204             assertTrue("Wrong negation value for business logic rule condition",
205                     rule3Condition2.mNegated);
206             assertEquals("Wrong arg string count for business logic rule condition", 1,
207                     rule3Condition2.mMethodArgs.size());
208             assertEquals("Wrong arg for business logic rule condition", "arg2",
209                     rule3Condition2.mMethodArgs.get(0));
210             List<BusinessLogicRuleAction> rule3Actions = rule3.mActions;
211             assertEquals("Wrong number of actions", 2, rule3Actions.size());
212             BusinessLogicRuleAction rule3Action1 = rule3Actions.get(0);
213             assertEquals("Wrong method name for business logic rule action",
214                     "actionMethodName1", rule3Action1.mMethodName);
215             assertEquals("Wrong arg string count for business logic rule action", 2,
216                     rule3Action1.mMethodArgs.size());
217             assertEquals("Wrong arg for business logic rule action", "arg1",
218                     rule3Action1.mMethodArgs.get(0));
219             assertEquals("Wrong arg for business logic rule action", "arg2",
220                     rule3Action1.mMethodArgs.get(1));
221             BusinessLogicRuleAction rule3Action2 = rule3Actions.get(1);
222             assertEquals("Wrong method name for business logic rule action",
223                     "actionMethodName2", rule3Action2.mMethodName);
224             assertEquals("Wrong arg string count for business logic rule action", 0,
225                     rule3Action2.mMethodArgs.size());
226 
227             List<BusinessLogicRule> rulesList3 = bl.mRules.get("testCaseName3").get(0).getRules();
228             assertEquals("Wrong number of rules in third rule list", 0, rulesList3.size());
229         } finally {
230             FileUtil.deleteFile(file);
231         }
232     }
233 
234     @Test(expected = RuntimeException.class)
testLogicWithWrongNodeName()235     public void testLogicWithWrongNodeName() throws Exception {
236         File file = createFileFromStr(CORRECT_LOGIC.replace("testName", "testNam3"));
237         try {
238             BusinessLogic bl = BusinessLogicFactory.createFromFile(file);
239         } finally {
240             FileUtil.deleteFile(file);
241         }
242     }
243 
createFileFromStr(String blString)244     private static File createFileFromStr(String blString) throws IOException {
245         File file = File.createTempFile("test", "bl");
246         FileOutputStream stream = new FileOutputStream(file);
247         stream.write(blString.getBytes());
248         stream.flush();
249         stream.close();
250         return file;
251     }
252 }
253