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 java.io.File;
19 import java.util.ArrayList;
20 import java.util.Collections;
21 import java.util.List;
22 import org.json.JSONArray;
23 import org.json.JSONException;
24 import org.json.JSONObject;
25 
26 /** A class to model a TestResource message returned by TFC API. */
27 public class TestResource {
28 
29     private static class TestResourceParameters {
30         private final List<String> mDecompressFiles;
31 
TestResourceParameters(List<String> decompressFiles)32         TestResourceParameters(List<String> decompressFiles) {
33             mDecompressFiles = decompressFiles != null ? decompressFiles : new ArrayList<>();
34         }
35 
toJson()36         JSONObject toJson() throws JSONException {
37             JSONObject json = new JSONObject();
38             json.put("decompress_files", new JSONArray(mDecompressFiles));
39             return json;
40         }
41 
fromJson(JSONObject json)42         static TestResourceParameters fromJson(JSONObject json) {
43             List<String> decompressFiles = new ArrayList<>();
44             if (json != null) {
45                 JSONArray jsonDecompressFiles = json.optJSONArray("decompress_files");
46                 if (jsonDecompressFiles != null) {
47                     for (int i = 0; i < jsonDecompressFiles.length(); i++) {
48                         decompressFiles.add(jsonDecompressFiles.optString(i));
49                     }
50                 }
51             }
52             return new TestResourceParameters(decompressFiles);
53         }
54     }
55 
56     private final String mName;
57     private final String mUrl;
58     private final boolean mDecompress;
59     private final String mDecompressDir;
60     private final boolean mMountZip;
61     private final TestResourceParameters mParams;
62 
TestResource(String name, String url)63     TestResource(String name, String url) {
64         this(name, url, false, null, false, (List<String>) null);
65     }
66 
TestResource( String name, String url, boolean decompress, String decompressDir, boolean mountZip, List<String> decompressFiles)67     TestResource(
68             String name,
69             String url,
70             boolean decompress,
71             String decompressDir,
72             boolean mountZip,
73             List<String> decompressFiles) {
74         this(
75                 name,
76                 url,
77                 decompress,
78                 decompressDir,
79                 mountZip,
80                 new TestResourceParameters(decompressFiles));
81     }
82 
TestResource( String name, String url, boolean decompress, String decompressDir, boolean mountZip, TestResourceParameters params)83     private TestResource(
84             String name,
85             String url,
86             boolean decompress,
87             String decompressDir,
88             boolean mountZip,
89             TestResourceParameters params) {
90         mName = name;
91         mUrl = url;
92         mDecompress = decompress;
93         mDecompressDir = decompressDir != null ? decompressDir : "";
94         mMountZip = mountZip;
95         mParams = params;
96     }
97 
getName()98     public String getName() {
99         return mName;
100     }
101 
getUrl()102     public String getUrl() {
103         return mUrl;
104     }
105 
getDecompress()106     public boolean getDecompress() {
107         return mDecompress;
108     }
109 
getDecompressDir()110     public String getDecompressDir() {
111         return mDecompressDir;
112     }
113 
getDecompressDir(File parentDir)114     public File getDecompressDir(File parentDir) {
115         return new File(parentDir, mDecompressDir);
116     }
117 
getFile(File parentDir)118     public File getFile(File parentDir) {
119         return new File(parentDir, mName);
120     }
121 
mountZip()122     public boolean mountZip() {
123         return mMountZip;
124     }
125 
getDecompressFiles()126     public List<String> getDecompressFiles() {
127         return Collections.unmodifiableList(mParams.mDecompressFiles);
128     }
129 
toJson()130     public JSONObject toJson() throws JSONException {
131         final JSONObject json = new JSONObject();
132         json.put("name", mName);
133         json.put("url", mUrl);
134         json.put("decompress", mDecompress);
135         json.put("decompress_dir", mDecompressDir);
136         json.put("mount_zip", mMountZip);
137         json.put("params", mParams.toJson());
138         return json;
139     }
140 
fromJson(JSONObject json)141     public static TestResource fromJson(JSONObject json) {
142         return new TestResource(
143                 json.optString("name"),
144                 json.optString("url"),
145                 json.optBoolean("decompress"),
146                 json.optString("decompress_dir"),
147                 json.optBoolean("mount_zip"),
148                 TestResourceParameters.fromJson(json.optJSONObject("params")));
149     }
150 
fromJsonArray(JSONArray jsonArray)151     public static List<TestResource> fromJsonArray(JSONArray jsonArray) throws JSONException {
152         final List<TestResource> objs = new ArrayList<>();
153         for (int i = 0; i < jsonArray.length(); i++) {
154             objs.add(TestResource.fromJson(jsonArray.getJSONObject(i)));
155         }
156         return objs;
157     }
158 }
159