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

import com.google.common.collect.ImmutableSet;
import com.google.gerrit.extensions.registration.DynamicSet;
import com.google.gerrit.reviewdb.client.Account;
import com.google.gerrit.reviewdb.client.AccountGroup;
import com.google.gerrit.server.AuditEvent;
import com.google.gerrit.server.audit.AuditListener;
import com.google.gerrit.server.audit.group.GroupAuditListener;
import com.google.gerrit.server.audit.group.GroupMemberAuditEvent;
import com.google.gerrit.server.audit.group.GroupSubgroupAuditEvent;
import com.google.gerrit.server.group.GroupAuditService;
import com.google.gerrit.server.plugincontext.PluginSetContext;
import com.google.inject.AbstractModule;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

@Singleton
public class FakeGroupAuditService implements GroupAuditService {

  protected final PluginSetContext<GroupAuditListener> groupAuditListeners;
  protected final PluginSetContext<AuditListener> auditListeners;

  public final List<AuditEvent> auditEvents = new ArrayList<>();

  public static class Module extends AbstractModule {
    @Override
    public void configure() {
      DynamicSet.setOf(binder(), GroupAuditListener.class);
      DynamicSet.setOf(binder(), AuditListener.class);
      bind(GroupAuditService.class).to(FakeGroupAuditService.class);
    }
  }

  @Inject
  public FakeGroupAuditService(
      PluginSetContext<GroupAuditListener> groupAuditListeners,
      PluginSetContext<AuditListener> auditListeners) {
    this.groupAuditListeners = groupAuditListeners;
    this.auditListeners = auditListeners;
  }

  public void clearEvents() {
    auditEvents.clear();
  }

  @Override
  public void dispatch(AuditEvent action) {
    synchronized (auditEvents) {
      auditEvents.add(action);
      auditEvents.notifyAll();
    }
  }

  @Override
  public void dispatchAddMembers(
      Account.Id actor,
      AccountGroup.UUID updatedGroup,
      ImmutableSet<Account.Id> addedMembers,
      Timestamp addedOn) {
    GroupMemberAuditEvent event =
        GroupMemberAuditEvent.create(actor, updatedGroup, addedMembers, addedOn);
    groupAuditListeners.runEach(l -> l.onAddMembers(event));
  }

  @Override
  public void dispatchDeleteMembers(
      Account.Id actor,
      AccountGroup.UUID updatedGroup,
      ImmutableSet<Account.Id> deletedMembers,
      Timestamp deletedOn) {
    GroupMemberAuditEvent event =
        GroupMemberAuditEvent.create(actor, updatedGroup, deletedMembers, deletedOn);
    groupAuditListeners.runEach(l -> l.onDeleteMembers(event));
  }

  @Override
  public void dispatchAddSubgroups(
      Account.Id actor,
      AccountGroup.UUID updatedGroup,
      ImmutableSet<AccountGroup.UUID> addedSubgroups,
      Timestamp addedOn) {
    GroupSubgroupAuditEvent event =
        GroupSubgroupAuditEvent.create(actor, updatedGroup, addedSubgroups, addedOn);
    groupAuditListeners.runEach(l -> l.onAddSubgroups(event));
  }

  @Override
  public void dispatchDeleteSubgroups(
      Account.Id actor,
      AccountGroup.UUID updatedGroup,
      ImmutableSet<AccountGroup.UUID> deletedSubgroups,
      Timestamp deletedOn) {
    GroupSubgroupAuditEvent event =
        GroupSubgroupAuditEvent.create(actor, updatedGroup, deletedSubgroups, deletedOn);
    groupAuditListeners.runEach(l -> l.onDeleteSubgroups(event));
  }
}

@@ -660,201 +660,49 @@
 gle.
-common.collect.ImmutableSet;%0Aimport com.google.gerrit.extensions.registration.DynamicSet;%0Aimport com.google.gerrit.reviewdb.client.Account;%0Aimport com.google.gerrit.reviewdb.client.AccountGroup
+gerrit.extensions.registration.DynamicSet
 ;%0Aim
@@ -788,32 +788,84 @@
 .AuditListener;%0A
+import com.google.gerrit.server.audit.AuditService;%0A
 import com.googl
@@ -916,144 +916,8 @@
 er;%0A
-import com.google.gerrit.server.audit.group.GroupMemberAuditEvent;%0Aimport com.google.gerrit.server.audit.group.GroupSubgroupAuditEvent;%0A
 impo
@@ -1147,35 +1147,8 @@
 on;%0A
-import java.sql.Timestamp;%0A
 impo
@@ -1245,24 +1245,16 @@
 ice 
-implement
+extend
 s 
-Group
 Audi
@@ -1268,152 +1268,8 @@
 e %7B%0A
-%0A  protected final PluginSetContext%3CGroupAuditListener%3E groupAuditListeners;%0A  protected final PluginSetContext%3CAuditListener%3E auditListeners;%0A%0A
   pu
@@ -1635,23 +1635,16 @@
 nject%0A  
-public 
 FakeGrou
@@ -1681,21 +1681,16 @@
 Context%3C
-Group
 AuditLis
@@ -1696,22 +1696,17 @@
 stener%3E 
-groupA
+a
 uditList
@@ -1727,32 +1727,37 @@
 luginSetContext%3C
+Group
 AuditListener%3E a
@@ -1747,33 +1747,38 @@
 pAuditListener%3E 
-a
+groupA
 uditListeners) %7B
@@ -1786,19 +1786,15 @@
     
-this.groupA
+super(a
 udit
@@ -1802,18 +1802,17 @@
 isteners
- =
+,
  groupAu
@@ -1827,50 +1827,9 @@
 ners
-;%0A    this.auditListeners = auditListeners
+)
 ;%0A  
@@ -1943,24 +1943,52 @@
 t action) %7B%0A
+    super.dispatch(action);%0A
     synchron
@@ -2070,16 +2070,16 @@
 yAll();%0A
+
     %7D%0A  
@@ -2083,1521 +2083,7 @@
 %0A  %7D
-%0A%0A  @Override%0A  public void dispatchAddMembers(%0A      Account.Id actor,%0A      AccountGroup.UUID updatedGroup,%0A      ImmutableSet%3CAccount.Id%3E addedMembers,%0A      Timestamp addedOn) %7B%0A    GroupMemberAuditEvent event =%0A        GroupMemberAuditEvent.create(actor, updatedGroup, addedMembers, addedOn);%0A    groupAuditListeners.runEach(l -%3E l.onAddMembers(event));%0A  %7D%0A%0A  @Override%0A  public void dispatchDeleteMembers(%0A      Account.Id actor,%0A      AccountGroup.UUID updatedGroup,%0A      ImmutableSet%3CAccount.Id%3E deletedMembers,%0A      Timestamp deletedOn) %7B%0A    GroupMemberAuditEvent event =%0A        GroupMemberAuditEvent.create(actor, updatedGroup, deletedMembers, deletedOn);%0A    groupAuditListeners.runEach(l -%3E l.onDeleteMembers(event));%0A  %7D%0A%0A  @Override%0A  public void dispatchAddSubgroups(%0A      Account.Id actor,%0A      AccountGroup.UUID updatedGroup,%0A      ImmutableSet%3CAccountGroup.UUID%3E addedSubgroups,%0A      Timestamp addedOn) %7B%0A    GroupSubgroupAuditEvent event =%0A        GroupSubgroupAuditEvent.create(actor, updatedGroup, addedSubgroups, addedOn);%0A    groupAuditListeners.runEach(l -%3E l.onAddSubgroups(event));%0A  %7D%0A%0A  @Override%0A  public void dispatchDeleteSubgroups(%0A      Account.Id actor,%0A      AccountGroup.UUID updatedGroup,%0A      ImmutableSet%3CAccountGroup.UUID%3E deletedSubgroups,%0A      Timestamp deletedOn) %7B%0A    GroupSubgroupAuditEvent event =%0A        GroupSubgroupAuditEvent.create(actor, updatedGroup, deletedSubgroups, deletedOn);%0A    groupAuditListeners.runEach(l -%3E l.onDeleteSubgroups(event));%0A  %7D
 %0A%7D%0A

Extend AuditService