1 /*
2  * Copyright (C) 2019 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 package com.android.tradefed.cluster;
17 
18 import com.android.tradefed.log.LogUtil.CLog;
19 import java.util.ArrayList;
20 import java.util.Collections;
21 import java.util.List;
22 import java.util.Map;
23 import java.util.TreeMap;
24 import org.json.JSONArray;
25 import org.json.JSONException;
26 import org.json.JSONObject;
27 
28 /**
29  * A class to model a TestContext message of TFC API.
30  *
31  * <p>A TestContext message is used to store and retrieve contextual information being passed across
32  * multiple attempts of same test command.
33  */
34 public class TestContext {
35 
36     String mCommandLine = null;
37     final Map<String, String> mEnvVars = new TreeMap<>();
38     final List<TestResource> mTestResources = new ArrayList<>();
39 
getCommandLine()40     public String getCommandLine() {
41         return mCommandLine;
42     }
43 
setCommandLine(String commandLine)44     public void setCommandLine(String commandLine) {
45         mCommandLine = commandLine;
46     }
47 
getEnvVars()48     public Map<String, String> getEnvVars() {
49         return Collections.unmodifiableMap(mEnvVars);
50     }
51 
addEnvVars(Map<String, String> envVars)52     public void addEnvVars(Map<String, String> envVars) {
53         mEnvVars.putAll(envVars);
54     }
55 
getTestResources()56     public List<TestResource> getTestResources() {
57         return Collections.unmodifiableList(mTestResources);
58     }
59 
addTestResource(TestResource testResource)60     public void addTestResource(TestResource testResource) {
61         mTestResources.add(testResource);
62     }
63 
fromJson(JSONObject json)64     public static TestContext fromJson(JSONObject json) throws JSONException {
65         final TestContext obj = new TestContext();
66         obj.mCommandLine = json.optString("command_line");
67         final JSONArray envVars = json.optJSONArray("env_vars");
68         if (envVars != null) {
69             for (int i = 0; i < envVars.length(); i++) {
70                 final JSONObject pair = envVars.getJSONObject(i);
71                 obj.mEnvVars.put(pair.getString("key"), pair.getString("value"));
72             }
73         }
74         final JSONArray testResources = json.optJSONArray("test_resources");
75         if (testResources != null) {
76             obj.mTestResources.addAll(TestResource.fromJsonArray(testResources));
77         }
78         return obj;
79     }
80 
toJson()81     public JSONObject toJson() throws JSONException {
82         final JSONObject json = new JSONObject();
83         json.put("command_line", mCommandLine);
84         final JSONArray envVars = new JSONArray();
85         for (Map.Entry<String, String> entry : mEnvVars.entrySet()) {
86             final JSONObject pair = new JSONObject();
87             pair.put("key", entry.getKey());
88             pair.put("value", entry.getValue());
89             envVars.put(pair);
90         }
91         json.put("env_vars", envVars);
92         final JSONArray testResources = new JSONArray();
93         for (TestResource obj : mTestResources) {
94             testResources.put(obj.toJson());
95         }
96         json.put("test_resources", testResources);
97         return json;
98     }
99 
100     @Override
toString()101     public String toString() {
102         try {
103             return toJson().toString();
104         } catch (JSONException e) {
105             CLog.w("Failed to convert to JSON: %s", e);
106         }
107         return null;
108     }
109 
110     @Override
equals(Object o)111     public boolean equals(Object o) {
112         if (!(o instanceof TestContext)) {
113             return false;
114         }
115 
116         return toString().equals(o.toString());
117     }
118 }
119