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 
17 package com.android.tradefed.testtype.mobly;
18 
19 import com.android.tradefed.testtype.mobly.MoblyYamlResultHandlerFactory.Type;
20 
21 import java.util.Map;
22 
23 /** Mobly yaml result 'Record' element handler. */
24 public class MoblyYamlResultRecordHandler implements IMoblyYamlResultHandler {
25 
26     private static final String BEGIN_TIME = "Begin Time";
27     private static final String END_TIME = "End Time";
28     private static final String RESULT = "Result";
29     private static final String STACKTRACE = "Stacktrace";
30     private static final String TEST_CLASS = "Test Class";
31     private static final String TEST_NAME = "Test Name";
32 
33     @Override
handle(Map<String, Object> docMap)34     public Record handle(Map<String, Object> docMap) {
35         Record.Builder builder = Record.builder();
36         builder.setTestClass(String.valueOf(docMap.get(TEST_CLASS)));
37         builder.setTestName(String.valueOf(docMap.get(TEST_NAME)));
38         builder.setResult(RecordResult.valueOf((String) docMap.get(RESULT)));
39         builder.setBeginTime(String.valueOf(docMap.get(BEGIN_TIME)));
40         builder.setEndTime(String.valueOf(docMap.get(END_TIME)));
41         builder.setStackTrace(String.valueOf(docMap.get(STACKTRACE)));
42         return builder.build();
43     }
44 
45     public static class Record implements ITestResult {
46         private final String mTestClass;
47         private final String mTestName;
48         private final RecordResult mResult;
49         private final long mBeginTime;
50         private final long mEndTime;
51         private final String mStacktrace;
52 
Record( String testClass, String testName, RecordResult result, String beginTime, String endTime, String stacktrace)53         private Record(
54                 String testClass,
55                 String testName,
56                 RecordResult result,
57                 String beginTime,
58                 String endTime,
59                 String stacktrace) {
60             mTestClass = testClass;
61             mTestName = testName;
62             mResult = result;
63             mBeginTime = Long.parseLong(beginTime);
64             mEndTime = Long.parseLong(endTime);
65             mStacktrace = stacktrace;
66         }
67 
68         @Override
getType()69         public Type getType() {
70             return Type.RECORD;
71         }
72 
getBeginTime()73         public long getBeginTime() {
74             return mBeginTime;
75         }
76 
getTestClass()77         public String getTestClass() {
78             return mTestClass;
79         }
80 
getTestName()81         public String getTestName() {
82             return mTestName;
83         }
84 
getStackTrace()85         public String getStackTrace() {
86             return mStacktrace;
87         }
88 
getEndTime()89         public long getEndTime() {
90             return mEndTime;
91         }
92 
getResult()93         public RecordResult getResult() {
94             return mResult;
95         }
96 
builder()97         public static Builder builder() {
98             return new Builder();
99         }
100 
101         public static class Builder {
102             private String mTestClass;
103             private String mTestName;
104             private RecordResult mResult;
105             private String mBeginTime;
106             private String mEndTime;
107             private String mStacktrace;
108 
Builder()109             public Builder() {}
110 
setTestClass(String testClass)111             public Builder setTestClass(String testClass) {
112                 mTestClass = testClass;
113                 return this;
114             }
115 
setTestName(String testName)116             public Builder setTestName(String testName) {
117                 mTestName = testName;
118                 return this;
119             }
120 
setResult(RecordResult result)121             public Builder setResult(RecordResult result) {
122                 mResult = result;
123                 return this;
124             }
125 
setBeginTime(String beginTime)126             public Builder setBeginTime(String beginTime) {
127                 mBeginTime = beginTime;
128                 return this;
129             }
130 
setEndTime(String endTime)131             public Builder setEndTime(String endTime) {
132                 mEndTime = endTime;
133                 return this;
134             }
135 
setStackTrace(String stacktrace)136             public Builder setStackTrace(String stacktrace) {
137                 mStacktrace = stacktrace;
138                 return this;
139             }
140 
build()141             public Record build() {
142                 return new Record(
143                         mTestClass, mTestName, mResult, mBeginTime, mEndTime, mStacktrace);
144             }
145         }
146     }
147 
148     public enum RecordResult {
149         PASS,
150         FAIL,
151         ERROR
152     }
153 }
154