// 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.ssh;

import static com.google.common.truth.Truth.assert_;
import static org.junit.Assert.assertTrue;

import com.google.gerrit.acceptance.AbstractDaemonTest;
import com.google.gerrit.acceptance.PushOneCommit;
import com.google.gerrit.acceptance.UseSsh;
import org.junit.Test;

@UseSsh
public class SetReviewersIT extends AbstractDaemonTest {

  @Test
  public void addByCommitHash() throws Exception {
    PushOneCommit.Result change = createChange();
    adminSshSession.exec(
        "gerrit set-reviewers -a "
            + user.email
            + " "
            + change.getCommit().getId().toString().split("\\s+")[1]);
    assert_()
        .withFailureMessage(adminSshSession.getError())
        .that(adminSshSession.hasError())
        .isFalse();
    assertTrue(change.getChange().getReviewers().all().contains(user.id));
  }

  @Test
  public void addByChangeID() throws Exception {
    PushOneCommit.Result change = createChange();
    adminSshSession.exec("gerrit set-reviewers -a " + user.email + " " + change.getChangeId());
    assert_()
        .withFailureMessage(adminSshSession.getError())
        .that(adminSshSession.hasError())
        .isFalse();
    assertTrue(change.getChange().getReviewers().all().contains(user.id));
  }

  @Test
  public void removeReviewer() throws Exception {
    PushOneCommit.Result change = createChange();
    adminSshSession.exec("gerrit set-reviewers -a " + user.email + " " + change.getChangeId());
    assert_()
        .withFailureMessage(adminSshSession.getError())
        .that(adminSshSession.hasError())
        .isFalse();
    assertTrue(change.getChange().getReviewers().all().contains(user.id));
    adminSshSession.exec("gerrit set-reviewers -r " + user.email + " " + change.getChangeId());
    assert_()
        .withFailureMessage(adminSshSession.getError())
        .that(adminSshSession.hasError())
        .isFalse();
    assertTrue(change.getChange().getReviewers().all().asList().isEmpty());
  }
}

@@ -695,17 +695,20 @@
 h.assert
-_
+That
 ;%0Aimport
@@ -719,37 +719,94 @@
 tic 
-org.junit.Assert.assertTrue;%0A
+com.google.common.truth.Truth.assert_;%0A%0Aimport com.google.common.collect.ImmutableSet;
 %0Aimp
@@ -858,16 +858,61 @@
 onTest;%0A
+import com.google.gerrit.acceptance.NoHttpd;%0A
 import c
@@ -998,16 +998,94 @@
 UseSsh;%0A
+import com.google.gerrit.reviewdb.client.Account.Id;%0Aimport org.junit.Before;%0A
 import o
@@ -1108,16 +1108,25 @@
 @UseSsh%0A
+@NoHttpd%0A
 public c
@@ -1178,46 +1178,69 @@
 t %7B%0A
-%0A
   
-@Test%0A  public void addByCommitHash
+PushOneCommit.Result change;%0A%0A  @Before%0A  public void setUp
 () t
@@ -1261,37 +1261,16 @@
 n %7B%0A    
-PushOneCommit.Result 
 change =
@@ -1292,123 +1292,82 @@
 ;%0A  
-  adminSshSession.exec(%0A        %22gerrit set-reviewers -a %22%0A            + user.email%0A            + %22 %22%0A            +
+%7D%0A%0A  @Test%0A  public void byCommitHash() throws Exception %7B%0A    String id =
  cha
@@ -1421,216 +1421,53 @@
 )%5B1%5D
-)
 ;%0A    a
-ssert_()%0A        .withFailureMessage(adminSshSession.getError())%0A        .that(adminSshSession.hasError())%0A        .isFalse();%0A    assertTrue(change.getChange().getReviewers().all().contains(user.
+ddReviewer(id);%0A    removeReviewer(
 id)
-)
 ;%0A  
@@ -1495,12 +1495,9 @@
 oid 
-addB
+b
 yCha
@@ -1531,562 +1531,273 @@
     
-PushOneCommit.Result change = create
+addReviewer(change.get
 Change
-(
+Id()
 );%0A    
-adminSshSession.exec(%22gerrit set-reviewers -a %22 + user.email + %22 %22 + change.getChangeId());%0A    assert_()%0A        .withFailureMessage(adminSshSession.getError())%0A        .that(adminSshSession.hasError())%0A        .isFalse();%0A    assertTrue(change.getChange().getReviewers().all().contains(user.id));%0A  %7D%0A%0A  @Test%0A  public void removeReviewer() throws Exception %7B%0A    PushOneCommit.Result change = createChange();%0A    adminSshSession.exec(%22gerrit set-reviewers -a %22 + user.email + %22 %22 + change.getChangeId(
+removeReviewer(change.getChangeId());%0A  %7D%0A%0A  private void setReviewer(boolean add, String id) throws Exception %7B%0A    adminSshSession.exec(%0A        String.format(%22gerrit set-reviewers -%25s %25s %25s%22, add ? %22a%22 : %22r%22, user.email, id
 ));%0A
@@ -1928,35 +1928,53 @@
 alse();%0A    
-assertTrue(
+ImmutableSet%3CId%3E reviewers = 
 change.getCh
@@ -2004,330 +2004,323 @@
 ll()
-.contains(user.id));%0A    adminSshSession.exec(%22gerrit set-reviewers -r %22 + user.email + %22 %22 + change.getChangeId());%0A    assert_()%0A        .withFailureMessage(adminSshSession.getError())%0A        .that(adminSshSession.hasError())%0A        .isFalse();%0A    assertTrue(change.getChange().getReviewers().all().asList().isEmpty()
+;%0A    if (add) %7B%0A      assertThat(reviewers).contains(user.id);%0A    %7D else %7B%0A      assertThat(reviewers).doesNotContain(user.id);%0A    %7D%0A  %7D%0A%0A  private void addReviewer(String id) throws Exception %7B%0A    setReviewer(true, id);%0A  %7D%0A%0A  private void removeReviewer(String id) throws Exception %7B%0A    setReviewer(false, id
 );%0A 

Clean up and simplify