<commit_before>// Copyright (C) 2017 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.api.project;

import static com.google.common.truth.Truth.assertThat;
import static com.google.gerrit.server.group.SystemGroupBackend.REGISTERED_USERS;

import com.google.gerrit.acceptance.AbstractDaemonTest;
import com.google.gerrit.acceptance.NoHttpd;
import com.google.gerrit.common.data.Permission;
import com.google.gerrit.extensions.api.projects.BranchInput;
import com.google.gerrit.extensions.api.projects.DashboardInfo;
import com.google.gerrit.extensions.restapi.BadRequestException;
import com.google.gerrit.extensions.restapi.ResourceConflictException;
import com.google.gerrit.extensions.restapi.ResourceNotFoundException;
import com.google.gerrit.server.project.DashboardsCollection;
import java.util.List;
import org.eclipse.jgit.junit.TestRepository;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;
import org.junit.Before;
import org.junit.Test;

@NoHttpd
public class DashboardIT extends AbstractDaemonTest {
  @Before
  public void setup() throws Exception {
    allow("refs/meta/dashboards/*", Permission.CREATE, REGISTERED_USERS);
  }

  @Test
  public void defaultDashboardDoesNotExist() throws Exception {
    exception.expect(ResourceNotFoundException.class);
    gApi.projects().name(project.get()).defaultDashboard().get();
  }

  @Test
  public void dashboardDoesNotExist() throws Exception {
    exception.expect(ResourceNotFoundException.class);
    gApi.projects().name(project.get()).dashboard("my:dashboard").get();
  }

  @Test
  public void getDashboard() throws Exception {
    assertThat(dashboards()).isEmpty();
    DashboardInfo info = createDashboard(DashboardsCollection.DEFAULT_DASHBOARD_NAME, "test");
    DashboardInfo result = gApi.projects().name(project.get()).dashboard(info.id).get();
    assertThat(result.id).isEqualTo(info.id);
    assertThat(result.path).isEqualTo(info.path);
    assertThat(result.ref).isEqualTo(info.ref);
    assertThat(result.project).isEqualTo(project.get());
    assertThat(result.definingProject).isEqualTo(project.get());
    assertThat(dashboards()).hasSize(1);
  }

  @Test
  public void setDefaultDashboard() throws Exception {
    DashboardInfo info = createDashboard(DashboardsCollection.DEFAULT_DASHBOARD_NAME, "test");
    assertThat(info.isDefault).isNull();
    gApi.projects().name(project.get()).dashboard(info.id).setDefault();
    assertThat(gApi.projects().name(project.get()).dashboard(info.id).get().isDefault).isTrue();
    assertThat(gApi.projects().name(project.get()).defaultDashboard().get().id).isEqualTo(info.id);
  }

  @Test
  public void setDefaultDashboardByProject() throws Exception {
    DashboardInfo info = createDashboard(DashboardsCollection.DEFAULT_DASHBOARD_NAME, "test");
    assertThat(info.isDefault).isNull();
    gApi.projects().name(project.get()).defaultDashboard(info.id);
    assertThat(gApi.projects().name(project.get()).dashboard(info.id).get().isDefault).isTrue();
    assertThat(gApi.projects().name(project.get()).defaultDashboard().get().id).isEqualTo(info.id);

    gApi.projects().name(project.get()).removeDefaultDashboard();
    assertThat(gApi.projects().name(project.get()).dashboard(info.id).get().isDefault).isNull();

    exception.expect(ResourceNotFoundException.class);
    gApi.projects().name(project.get()).defaultDashboard().get();
  }

  @Test
  public void replaceDefaultDashboard() throws Exception {
    DashboardInfo d1 = createDashboard(DashboardsCollection.DEFAULT_DASHBOARD_NAME, "test1");
    DashboardInfo d2 = createDashboard(DashboardsCollection.DEFAULT_DASHBOARD_NAME, "test2");
    assertThat(d1.isDefault).isNull();
    assertThat(d2.isDefault).isNull();
    gApi.projects().name(project.get()).dashboard(d1.id).setDefault();
    assertThat(gApi.projects().name(project.get()).dashboard(d1.id).get().isDefault).isTrue();
    assertThat(gApi.projects().name(project.get()).dashboard(d2.id).get().isDefault).isNull();
    assertThat(gApi.projects().name(project.get()).defaultDashboard().get().id).isEqualTo(d1.id);
    gApi.projects().name(project.get()).dashboard(d2.id).setDefault();
    assertThat(gApi.projects().name(project.get()).defaultDashboard().get().id).isEqualTo(d2.id);
    assertThat(gApi.projects().name(project.get()).dashboard(d1.id).get().isDefault).isNull();
    assertThat(gApi.projects().name(project.get()).dashboard(d2.id).get().isDefault).isTrue();
  }

  @Test
  public void cannotGetDashboardWithInheritedForNonDefault() throws Exception {
    DashboardInfo info = createDashboard(DashboardsCollection.DEFAULT_DASHBOARD_NAME, "test");
    exception.expect(BadRequestException.class);
    exception.expectMessage("inherited flag can only be used with default");
    gApi.projects().name(project.get()).dashboard(info.id).get(true);
  }

  private List<DashboardInfo> dashboards() throws Exception {
    return gApi.projects().name(project.get()).dashboards().get();
  }

  private DashboardInfo createDashboard(String ref, String path) throws Exception {
    DashboardInfo info = DashboardsCollection.newDashboardInfo(ref, path);
    String canonicalRef = DashboardsCollection.normalizeDashboardRef(info.ref);
    try {
      gApi.projects().name(project.get()).branch(canonicalRef).create(new BranchInput());
    } catch (ResourceConflictException e) {
      // The branch already exists if this method has already been called once.
      if (!e.getMessage().contains("already exists")) {
        throw e;
      }
    }
    try (Repository r = repoManager.openRepository(project)) {
      TestRepository<Repository>.CommitBuilder cb =
          new TestRepository<>(r).branch(canonicalRef).commit();
      String content =
          "[dashboard]\n"
              + "Description = Test\n"
              + "foreach = owner:self\n"
              + "[section \"Mine\"]\n"
              + "query = is:open";
      cb.add(info.path, content);
      RevCommit c = cb.create();
      gApi.projects().name(project.get()).commit(c.name());
    }
    return info;
  }
}<commit_msg>DashboardIT: Factor out getting the project API to a method<commit_after>// Copyright (C) 2017 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.api.project;

import static com.google.common.truth.Truth.assertThat;
import static com.google.gerrit.server.group.SystemGroupBackend.REGISTERED_USERS;

import com.google.gerrit.acceptance.AbstractDaemonTest;
import com.google.gerrit.acceptance.NoHttpd;
import com.google.gerrit.common.data.Permission;
import com.google.gerrit.extensions.api.projects.BranchInput;
import com.google.gerrit.extensions.api.projects.DashboardInfo;
import com.google.gerrit.extensions.api.projects.ProjectApi;
import com.google.gerrit.extensions.restapi.BadRequestException;
import com.google.gerrit.extensions.restapi.ResourceConflictException;
import com.google.gerrit.extensions.restapi.ResourceNotFoundException;
import com.google.gerrit.extensions.restapi.RestApiException;
import com.google.gerrit.server.project.DashboardsCollection;
import java.util.List;
import org.eclipse.jgit.junit.TestRepository;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;
import org.junit.Before;
import org.junit.Test;

@NoHttpd
public class DashboardIT extends AbstractDaemonTest {
  @Before
  public void setup() throws Exception {
    allow("refs/meta/dashboards/*", Permission.CREATE, REGISTERED_USERS);
  }

  @Test
  public void defaultDashboardDoesNotExist() throws Exception {
    exception.expect(ResourceNotFoundException.class);
    project().defaultDashboard().get();
  }

  @Test
  public void dashboardDoesNotExist() throws Exception {
    exception.expect(ResourceNotFoundException.class);
    project().dashboard("my:dashboard").get();
  }

  @Test
  public void getDashboard() throws Exception {
    assertThat(dashboards()).isEmpty();
    DashboardInfo info = createDashboard(DashboardsCollection.DEFAULT_DASHBOARD_NAME, "test");
    DashboardInfo result = project().dashboard(info.id).get();
    assertThat(result.id).isEqualTo(info.id);
    assertThat(result.path).isEqualTo(info.path);
    assertThat(result.ref).isEqualTo(info.ref);
    assertThat(result.project).isEqualTo(project.get());
    assertThat(result.definingProject).isEqualTo(project.get());
    assertThat(dashboards()).hasSize(1);
  }

  @Test
  public void setDefaultDashboard() throws Exception {
    DashboardInfo info = createDashboard(DashboardsCollection.DEFAULT_DASHBOARD_NAME, "test");
    assertThat(info.isDefault).isNull();
    project().dashboard(info.id).setDefault();
    assertThat(project().dashboard(info.id).get().isDefault).isTrue();
    assertThat(project().defaultDashboard().get().id).isEqualTo(info.id);
  }

  @Test
  public void setDefaultDashboardByProject() throws Exception {
    DashboardInfo info = createDashboard(DashboardsCollection.DEFAULT_DASHBOARD_NAME, "test");
    assertThat(info.isDefault).isNull();
    project().defaultDashboard(info.id);
    assertThat(project().dashboard(info.id).get().isDefault).isTrue();
    assertThat(project().defaultDashboard().get().id).isEqualTo(info.id);

    project().removeDefaultDashboard();
    assertThat(project().dashboard(info.id).get().isDefault).isNull();

    exception.expect(ResourceNotFoundException.class);
    project().defaultDashboard().get();
  }

  @Test
  public void replaceDefaultDashboard() throws Exception {
    DashboardInfo d1 = createDashboard(DashboardsCollection.DEFAULT_DASHBOARD_NAME, "test1");
    DashboardInfo d2 = createDashboard(DashboardsCollection.DEFAULT_DASHBOARD_NAME, "test2");
    assertThat(d1.isDefault).isNull();
    assertThat(d2.isDefault).isNull();
    project().dashboard(d1.id).setDefault();
    assertThat(project().dashboard(d1.id).get().isDefault).isTrue();
    assertThat(project().dashboard(d2.id).get().isDefault).isNull();
    assertThat(project().defaultDashboard().get().id).isEqualTo(d1.id);
    project().dashboard(d2.id).setDefault();
    assertThat(project().defaultDashboard().get().id).isEqualTo(d2.id);
    assertThat(project().dashboard(d1.id).get().isDefault).isNull();
    assertThat(project().dashboard(d2.id).get().isDefault).isTrue();
  }

  @Test
  public void cannotGetDashboardWithInheritedForNonDefault() throws Exception {
    DashboardInfo info = createDashboard(DashboardsCollection.DEFAULT_DASHBOARD_NAME, "test");
    exception.expect(BadRequestException.class);
    exception.expectMessage("inherited flag can only be used with default");
    project().dashboard(info.id).get(true);
  }

  private List<DashboardInfo> dashboards() throws Exception {
    return project().dashboards().get();
  }

  private ProjectApi project() throws RestApiException {
    return gApi.projects().name(project.get());
  }

  private DashboardInfo createDashboard(String ref, String path) throws Exception {
    DashboardInfo info = DashboardsCollection.newDashboardInfo(ref, path);
    String canonicalRef = DashboardsCollection.normalizeDashboardRef(info.ref);
    try {
      project().branch(canonicalRef).create(new BranchInput());
    } catch (ResourceConflictException e) {
      // The branch already exists if this method has already been called once.
      if (!e.getMessage().contains("already exists")) {
        throw e;
      }
    }
    try (Repository r = repoManager.openRepository(project)) {
      TestRepository<Repository>.CommitBuilder cb =
          new TestRepository<>(r).branch(canonicalRef).commit();
      String content =
          "[dashboard]\n"
              + "Description = Test\n"
              + "foreach = owner:self\n"
              + "[section \"Mine\"]\n"
              + "query = is:open";
      cb.add(info.path, content);
      RevCommit c = cb.create();
      project().commit(c.name());
    }
    return info;
  }
}
{'fraction_non_alphanumeric': 0.13581294852997916, 'fraction_numerical': 0.003549656609306274, 'mean_word_length': 4.719329214474845, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1896101', 'n_tokens_mistral': 3728, 'n_tokens_neox': 3819, 'n_words': 668}