summaryrefslogtreecommitdiffstats
path: root/gerrit-acceptance-tests/src/test/java/com/google/gerrit/acceptance/rest/config/ListCachesIT.java
blob: 4d48bf41b14aa8376a77d109b36164cdec38bbcc (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
// Copyright (C) 2014 The Android Open Source Project
//
// 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.gerrit.acceptance.rest.config;

import static com.google.common.truth.Truth.assertThat;
import static java.nio.charset.StandardCharsets.UTF_8;

import com.google.common.collect.Ordering;
import com.google.gerrit.acceptance.AbstractDaemonTest;
import com.google.gerrit.acceptance.RestResponse;
import com.google.gerrit.server.config.ListCaches.CacheInfo;
import com.google.gerrit.server.config.ListCaches.CacheType;
import com.google.gson.reflect.TypeToken;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import org.eclipse.jgit.util.Base64;
import org.junit.Test;

public class ListCachesIT extends AbstractDaemonTest {

  @Test
  public void listCaches() throws Exception {
    RestResponse r = adminRestSession.get("/config/server/caches/");
    r.assertOK();
    Map<String, CacheInfo> result =
        newGson().fromJson(r.getReader(), new TypeToken<Map<String, CacheInfo>>() {}.getType());

    assertThat(result).containsKey("accounts");
    CacheInfo accountsCacheInfo = result.get("accounts");
    assertThat(accountsCacheInfo.type).isEqualTo(CacheType.MEM);
    assertThat(accountsCacheInfo.entries.mem).isAtLeast(1L);
    assertThat(accountsCacheInfo.averageGet).isNotNull();
    assertThat(accountsCacheInfo.averageGet).endsWith("s");
    assertThat(accountsCacheInfo.entries.disk).isNull();
    assertThat(accountsCacheInfo.entries.space).isNull();
    assertThat(accountsCacheInfo.hitRatio.mem).isAtLeast(0);
    assertThat(accountsCacheInfo.hitRatio.mem).isAtMost(100);
    assertThat(accountsCacheInfo.hitRatio.disk).isNull();

    userRestSession.get("/config/server/version").consume();
    r = adminRestSession.get("/config/server/caches/");
    r.assertOK();
    result =
        newGson().fromJson(r.getReader(), new TypeToken<Map<String, CacheInfo>>() {}.getType());
    assertThat(result.get("accounts").entries.mem).isEqualTo(2);
  }

  @Test
  public void listCaches_Forbidden() throws Exception {
    userRestSession.get("/config/server/caches/").assertForbidden();
  }

  @Test
  public void listCacheNames() throws Exception {
    RestResponse r = adminRestSession.get("/config/server/caches/?format=LIST");
    r.assertOK();
    List<String> result =
        newGson().fromJson(r.getReader(), new TypeToken<List<String>>() {}.getType());
    assertThat(result).contains("accounts");
    assertThat(result).contains("projects");
    assertThat(Ordering.natural().isOrdered(result)).isTrue();
  }

  @Test
  public void listCacheNamesTextList() throws Exception {
    RestResponse r = adminRestSession.get("/config/server/caches/?format=TEXT_LIST");
    r.assertOK();
    String result = new String(Base64.decode(r.getEntityContent()), UTF_8.name());
    List<String> list = Arrays.asList(result.split("\n"));
    assertThat(list).contains("accounts");
    assertThat(list).contains("projects");
    assertThat(Ordering.natural().isOrdered(list)).isTrue();
  }

  @Test
  public void listCaches_BadRequest() throws Exception {
    adminRestSession.get("/config/server/caches/?format=NONSENSE").assertBadRequest();
  }
}