summaryrefslogtreecommitdiff
path: root/src/main/java/com/android/vts/servlet/ShowCoverageServlet.java
blob: 08e5db69b6ee66daf1faef716fe650aac6fbb52d (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
/*
 * Copyright (c) 2016 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.servlet;

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

import com.android.vts.entity.CoverageEntity;
import com.android.vts.entity.RoleEntity;
import com.android.vts.entity.TestEntity;
import com.android.vts.entity.TestRunEntity;
import com.android.vts.entity.UserEntity;
import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Query;
import com.google.gson.Gson;
import com.googlecode.objectify.Ref;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.logging.Level;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet for handling requests to show code coverage.
 */
public class ShowCoverageServlet extends BaseServlet {

  private static final String COVERAGE_JSP = "WEB-INF/jsp/show_coverage.jsp";
  private static final String TREE_JSP = "WEB-INF/jsp/show_tree.jsp";

  @Override
  public PageType getNavParentType() {
    return PageType.TOT;
  }

  @Override
  public List<Page> getBreadcrumbLinks(HttpServletRequest request) {
    List<Page> links = new ArrayList<>();
    String testName = request.getParameter("testName");
    links.add(new Page(PageType.TABLE, testName, "?testName=" + testName));

    String startTime = request.getParameter("startTime");
    links.add(new Page(PageType.COVERAGE, "?testName=" + testName + "&startTime=" + startTime));
    return links;
  }

  @Override
  public void doGetHandler(HttpServletRequest request, HttpServletResponse response)
      throws IOException {
    RequestDispatcher dispatcher = null;

    String test = request.getParameter("testName");
    String timeString = request.getParameter("startTime");

    Boolean isModerator = false;
    String currentUserEmail = request.getAttribute("email").toString();
    Optional<UserEntity> userEntityOptional = Optional
        .ofNullable(UserEntity.getUser(currentUserEmail));
    if (userEntityOptional.isPresent()) {
      Ref refRole = Ref.create(RoleEntity.getRole("coverage-moderator"));
      isModerator = userEntityOptional.get().getRoles().contains(refRole);
    }

    // Process the time key requested
    long time = -1;
    try {
      time = Long.parseLong(timeString);
    } catch (NumberFormatException e) {
      request.setAttribute("testName", test);
      dispatcher = request.getRequestDispatcher(TREE_JSP);
      return;
    }

    com.googlecode.objectify.Key testKey = com.googlecode.objectify.Key
        .create(TestEntity.class, test);
    com.googlecode.objectify.Key testRunKey = com.googlecode.objectify.Key
        .create(testKey, TestRunEntity.class, time);

    List<CoverageEntity> coverageEntityList = ofy().load().type(CoverageEntity.class)
        .ancestor(testRunKey).list();

    Collections.sort(coverageEntityList, CoverageEntity.isIgnoredComparator);

    request.setAttribute("isModerator", isModerator);
    request.setAttribute("testName", request.getParameter("testName"));
    request.setAttribute("gerritURI", new Gson().toJson(GERRIT_URI));
    request.setAttribute("gerritScope", new Gson().toJson(GERRIT_SCOPE));
    request.setAttribute("clientId", new Gson().toJson(CLIENT_ID));
    request.setAttribute("startTime", request.getParameter("startTime"));
    request.setAttribute("coverageEntityList", coverageEntityList);

    dispatcher = request.getRequestDispatcher(COVERAGE_JSP);
    try {
      dispatcher.forward(request, response);
    } catch (ServletException e) {
      logger.log(Level.SEVERE, "Servlet Exception caught : ", e);
    }
  }
}