// Copyright (C) 2013 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 com.google.gerrit.acceptance.AbstractDaemonTest;
import com.google.gerrit.acceptance.NoHttpd;
import com.google.gerrit.extensions.restapi.AuthException;
import com.google.gerrit.extensions.restapi.ResourceConflictException;
import com.google.gerrit.extensions.restapi.UnprocessableEntityException;
import com.google.gerrit.reviewdb.client.Project;
import com.google.gerrit.server.config.AllProjectsNameProvider;
import org.junit.Test;

@NoHttpd
public class SetParentIT extends AbstractDaemonTest {
  @Test
  public void setParentNotAllowed() throws Exception {
    String parent = createProject("parent", null, true).get();
    setApiUser(user);
    exception.expect(AuthException.class);
    gApi.projects().name(project.get()).parent(parent);
  }

  @Test
  public void setParent() throws Exception {
    String parent = createProject("parent", null, true).get();

    gApi.projects().name(project.get()).parent(parent);
    assertThat(gApi.projects().name(project.get()).parent()).isEqualTo(parent);

    // When the parent name is not explicitly set, it should be
    // set to "All-Projects".
    gApi.projects().name(project.get()).parent(null);
    assertThat(gApi.projects().name(project.get()).parent())
        .isEqualTo(AllProjectsNameProvider.DEFAULT);
  }

  @Test
  public void setParentForAllProjectsNotAllowed() throws Exception {
    exception.expect(ResourceConflictException.class);
    exception.expectMessage("cannot set parent of " + AllProjectsNameProvider.DEFAULT);
    gApi.projects().name(allProjects.get()).parent(project.get());
  }

  @Test
  public void setParentToSelfNotAllowed() throws Exception {
    exception.expect(ResourceConflictException.class);
    exception.expectMessage("cannot set parent to self");
    gApi.projects().name(project.get()).parent(project.get());
  }

  @Test
  public void setParentToOwnChildNotAllowed() throws Exception {
    String child = createProject("child", project, true).get();
    exception.expect(ResourceConflictException.class);
    exception.expectMessage("cycle exists between");
    gApi.projects().name(project.get()).parent(child);
  }

  @Test
  public void setParentToGrandchildNotAllowed() throws Exception {
    Project.NameKey child = createProject("child", project, true);
    String grandchild = createProject("grandchild", child, true).get();
    exception.expect(ResourceConflictException.class);
    exception.expectMessage("cycle exists between");
    gApi.projects().name(project.get()).parent(grandchild);
  }

  @Test
  public void setParentToNonexistentProject() throws Exception {
    exception.expect(UnprocessableEntityException.class);
    exception.expectMessage("not found");
    gApi.projects().name(project.get()).parent("non-existing");
  }
}

@@ -862,32 +862,97 @@
 .AuthException;%0A
+import com.google.gerrit.extensions.restapi.BadRequestException;%0A
 import com.googl
@@ -1194,16 +1194,102 @@
 ovider;%0A
+import com.google.gerrit.server.config.AllUsersName;%0Aimport com.google.inject.Inject;%0A
 import o
@@ -1367,16 +1367,58 @@
 nTest %7B%0A
+  @Inject private AllUsersName allUsers;%0A%0A
   @Test%0A
@@ -3656,11 +3656,409 @@
 g%22);%0A  %7D
+%0A%0A  @Test%0A  public void setParentForAllUsersMustBeAllProjects() throws Exception %7B%0A    gApi.projects().name(allUsers.get()).parent(allProjects.get());%0A%0A    String parent = createProject(%22parent%22, null, true).get();%0A%0A    exception.expect(BadRequestException.class);%0A    exception.expectMessage(%22All-Users must inherit from All-Projects%22);%0A    gApi.projects().name(allUsers.get()).parent(parent);%0A  %7D
 %0A%7D%0A

Add test that would have caught bogus equals