summaryrefslogtreecommitdiff
path: root/src/main/java/com/android/vts/api/TestRunRestServlet.java
blob: 90fd3f977e130557cd503ce3b67fcd88a7a0c05e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
 * Copyright (c) 2017 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you
 * may not use this file except in compliance with the License. You may
 * obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */

package com.android.vts.api;

import com.android.vts.entity.TestCaseRunEntity;
import com.android.vts.entity.TestEntity;
import com.android.vts.entity.TestRunEntity;
import com.android.vts.util.TestRunDetails;
import com.google.gson.Gson;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import static com.googlecode.objectify.ObjectifyService.ofy;

/** Servlet for handling requests to fetch test case results. */
public class TestRunRestServlet extends BaseApiServlet {
    private static final String LATEST = "latest";
    protected static final Logger logger = Logger.getLogger(TestRunRestServlet.class.getName());

    /**
     * Get the test case results for the specified run of the specified test.
     *
     * @param test The test whose test cases to get.
     * @param timeString The string representation of the test run timestamp (in microseconds).
     * @return A TestRunDetails object with the test case details for the specified run.
     */
    private TestRunDetails getTestRunDetails(String test, String timeString) {
        long timestamp;
        try {
            timestamp = Long.parseLong(timeString);
            if (timestamp <= 0) throw new NumberFormatException();
            timestamp = timestamp > 0 ? timestamp : null;
        } catch (NumberFormatException e) {
            return null;
        }

        TestRunEntity testRunEntity = TestRunEntity.getByTestNameId(test, timestamp);

        return getTestRunDetails(testRunEntity);
    }

    /**
     * Get the test case results for the latest run of the specified test.
     *
     * @param testName The test whose test cases to get.
     * @return A TestRunDetails object with the test case details for the latest run.
     */
    private TestRunDetails getLatestTestRunDetails(String testName) {
        com.googlecode.objectify.Key testKey =
                com.googlecode.objectify.Key.create(
                        TestEntity.class, testName);

        TestRunEntity testRun = ofy().load().type(TestRunEntity.class).ancestor(testKey)
                .filter("type", 2).orderKey(true).first().now();

        if (testRun == null) return null;

        return getTestRunDetails(testRun);
    }

    /**
     * Get TestRunDetails instance from codeCoverageEntity instance.
     *
     * @param testRunEntity The TestRunEntity to access testCaseId.
     * @return A TestRunDetails object with the test case details for the latest run.
     */
    private TestRunDetails getTestRunDetails(TestRunEntity testRunEntity) {
        TestRunDetails details = new TestRunDetails();
        List<com.googlecode.objectify.Key<TestCaseRunEntity>> testCaseKeyList = new ArrayList<>();
        if ( Objects.isNull(testRunEntity.getTestCaseIds()) ) {
            return details;
        } else {
            for (long testCaseId : testRunEntity.getTestCaseIds()) {
                testCaseKeyList.add(
                        com.googlecode.objectify.Key.create(TestCaseRunEntity.class, testCaseId));
            }
            Map<com.googlecode.objectify.Key<TestCaseRunEntity>, TestCaseRunEntity>
                    testCaseRunEntityKeyMap = ofy().load().keys(() -> testCaseKeyList.iterator());
            testCaseRunEntityKeyMap.forEach((key, value) -> details.addTestCase(value));
        }
        return details;
    }

    /**
     * Get the test case details for a test run.
     *
     * Expected format: (1) /api/test_run?test=[test name]&timestamp=[timestamp] to the details
     * for a specific run, or (2) /api/test_run?test=[test name]&timestamp=latest -- the details for
     * the latest test run.
     */
    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String test = request.getParameter("test");
        String timeString = request.getParameter("timestamp");
        TestRunDetails details = null;

        if (timeString != null && timeString.equals(LATEST)) {
            details = getLatestTestRunDetails(test);
        } else if (timeString != null) {
            details = getTestRunDetails(test, timeString);
        }

        if (details == null) {
            response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
        } else {
            response.setStatus(HttpServletResponse.SC_OK);
            response.setContentType("application/json");
            PrintWriter writer = response.getWriter();
            writer.print(new Gson().toJson(details.toJson()));
            writer.flush();
        }
    }
}