// Copyright (C) 2018 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;

import static com.google.common.truth.Truth.assertThat;

import com.google.gerrit.extensions.registration.DynamicSet;
import com.google.gerrit.reviewdb.client.AccountGroup;
import com.google.gerrit.server.account.GroupBackend;
import com.google.gerrit.server.account.UniversalGroupBackend;
import com.google.gerrit.server.group.testing.TestGroupBackend;
import com.google.inject.Inject;
import org.junit.Test;

public class TestGroupBackendTest extends AbstractDaemonTest {
  @Inject private DynamicSet<GroupBackend> groupBackends;
  @Inject private UniversalGroupBackend universalGroupBackend;

  private final TestGroupBackend testGroupBackend = new TestGroupBackend();
  private final AccountGroup.UUID testUUID = new AccountGroup.UUID("testbackend:test");

  @Test
  public void handlesTestGroup() throws Exception {
    assertThat(testGroupBackend.handles(testUUID)).isTrue();
  }

  @Test
  public void universalGroupBackendHandlesTestGroup() throws Exception {
    groupBackends.add(testGroupBackend);
    assertThat(universalGroupBackend.handles(testUUID)).isTrue();
  }

  @Test
  public void doesNotHandleLDAP() throws Exception {
    assertThat(testGroupBackend.handles(new AccountGroup.UUID("ldap:1234"))).isFalse();
  }

  @Test
  public void doesNotHandleNull() throws Exception {
    assertThat(testGroupBackend.handles(null)).isFalse();
  }

  @Test
  public void returnsNullWhenGroupDoesNotExist() throws Exception {
    assertThat(testGroupBackend.get(testUUID)).isNull();
  }

  @Test
  public void returnsNullForNullGroup() throws Exception {
    assertThat(testGroupBackend.get(null)).isNull();
  }

  @Test
  public void returnsKnownGroup() throws Exception {
    testGroupBackend.create(testUUID);
    assertThat(testGroupBackend.get(testUUID)).isNotNull();
  }
}

@@ -759,16 +759,85 @@
 micSet;%0A
+import com.google.gerrit.extensions.registration.RegistrationHandle;%0A
 import c
@@ -1681,16 +1681,56 @@
 on %7B%0A   
+ RegistrationHandle registrationHandle =
  groupBa
@@ -1759,16 +1759,28 @@
 ckend);%0A
+    try %7B%0A  
     asse
@@ -1829,32 +1829,89 @@
 UID)).isTrue();%0A
+    %7D finally %7B%0A      registrationHandle.remove();%0A    %7D%0A
   %7D%0A%0A  @Test%0A  p

Remove TestGroupBackend from DynamicSet after test