aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/tools/dash/src/test/java/com/google/devtools/dash/DashRequestTest.java
blob: 02adc72890cedd777e10c56348ef6ab6f483de63 (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
// Copyright 2015 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.google.devtools.dash;

import static com.google.common.truth.Truth.assertThat;
import static org.easymock.EasyMock.createMock;
import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.replay;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

import com.google.appengine.api.datastore.Blob;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.tools.development.testing.LocalDatastoreServiceTestConfig;
import com.google.appengine.tools.development.testing.LocalServiceTestHelper;
import com.google.devtools.build.lib.bazel.dash.DashProtos;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

import javax.servlet.http.HttpServletRequest;

/**
 * Tests for {@link DashRequest}.
 */
@RunWith(JUnit4.class)
public class DashRequestTest {

  private final LocalServiceTestHelper helper =
      new LocalServiceTestHelper(new LocalDatastoreServiceTestConfig());
  private HttpServletRequest request;

  @Before
  public void setUp() {
    helper.setUp();
    request = createMock(HttpServletRequest.class);
  }

  @After
  public void tearDown() {
    helper.tearDown();
  }

  @Test
  public void testUriParsing() throws Exception {
    final String buildId = "3b9a81d9-0ed3-48d2-84c6-6296aecc21e6";
    final DashProtos.BuildData data = DashProtos.BuildData.newBuilder().setBuildId(buildId).build();
    expect(request.getRequestURI()).andReturn("/result/3b9a81d9-0ed3-48d2-84c6-6296aecc21e6");
    expect(request.getInputStream()).andReturn(new ProtoInputStream(data));
    replay(request);

    DashRequest dashRequest = new DashRequest(request);
    assertEquals(buildId, dashRequest.getBuildId());
    Entity entity = dashRequest.getEntity();
    assertEquals(buildId, entity.getProperty(DashRequest.BUILD_ID));
    assertEquals("result", entity.getProperty(DashRequest.PAGE_NAME));
    assertEquals(data, DashProtos.BuildData.parseFrom(
        ((Blob) entity.getProperty(DashRequest.BUILD_DATA)).getBytes()));
  }

  private void uriError(String uri) {
    expect(request.getRequestURI()).andReturn(uri).times(2);
    replay(request);

    try {
      new DashRequest(request);
      fail("Should have thrown");
    } catch (DashRequest.DashRequestException e) {
      assertThat(e.getMessage()).contains("Invalid URI pattern: " + uri);
    }
  }

  @Test
  public void testInvalidUuid() {
    uriError("/result/3b9a81d9-0ed3-48d2");
  }

  @Test
  public void testMissingPageName() {
    uriError("/3b9a81d9-0ed3-48d2-84c6-6296aecc21e6");
  }

}