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

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

import com.google.gerrit.acceptance.Sandboxed;
import com.google.gerrit.server.AuditEvent;
import com.google.gerrit.server.audit.HttpAuditEvent;
import com.google.gerrit.testing.FakeGroupAuditService;
import java.util.Collections;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jgit.transport.CredentialsProvider;
import org.eclipse.jgit.transport.RefSpec;
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
import org.junit.Before;
import org.junit.Test;

public class GitOverHttpServletIT extends AbstractPushForReview {
  private static final long AUDIT_EVENT_TIMEOUT = 500L;

  @Before
  public void beforeEach() throws Exception {
    CredentialsProvider.setDefault(
        new UsernamePasswordCredentialsProvider(admin.username, admin.httpPassword));
    selectProtocol(AbstractPushForReview.Protocol.HTTP);
  }

  @Test
  @Sandboxed
  public void receivePackAuditEventLog() throws Exception {
    FakeGroupAuditService auditService = clearAuditService();
    testRepo
        .git()
        .push()
        .setRemote("origin")
        .setRefSpecs(new RefSpec("HEAD:refs/for/master"))
        .call();
    waitForAudit(auditService);

    // Git smart protocol makes two requests:
    // https://github.com/git/git/blob/master/Documentation/technical/http-protocol.txt
    assertThat(auditService.auditEvents.size()).isEqualTo(2);

    AuditEvent e = auditService.auditEvents.get(1);
    assertThat(e.who.getAccountId()).isEqualTo(admin.id);
    assertThat(e.what).endsWith("/git-receive-pack");
    assertThat(e.params).isEmpty();
    assertThat(((HttpAuditEvent) e).httpStatus).isEqualTo(HttpServletResponse.SC_OK);
  }

  @Test
  @Sandboxed
  public void uploadPackAuditEventLog() throws Exception {
    FakeGroupAuditService auditService = clearAuditService();
    testRepo.git().fetch().call();
    waitForAudit(auditService);

    assertThat(auditService.auditEvents.size()).isEqualTo(1);

    AuditEvent e = auditService.auditEvents.get(0);
    assertThat(e.who.toString()).isEqualTo("ANONYMOUS");
    assertThat(e.params.get("service"))
        .containsExactlyElementsIn(Collections.singletonList("git-upload-pack"));
    assertThat(e.what).endsWith("service=git-upload-pack");
    assertThat(((HttpAuditEvent) e).httpStatus).isEqualTo(HttpServletResponse.SC_OK);
  }

  private FakeGroupAuditService clearAuditService() {
    FakeGroupAuditService auditService =
        server.getTestInjector().getInstance(FakeGroupAuditService.class);
    auditService.clearEvents();
    return auditService;
  }

  private void waitForAudit(FakeGroupAuditService auditService) throws InterruptedException {
    synchronized (auditService.auditEvents) {
      auditService.auditEvents.wait(AUDIT_EVENT_TIMEOUT);
    }
  }
}

@@ -903,16 +903,49 @@
 ervice;%0A
+import com.google.inject.Inject;%0A
 import j
@@ -966,16 +966,16 @@
 ctions;%0A
-
 import j
@@ -1354,16 +1354,71 @@
  500L;%0A%0A
+  @Inject private FakeGroupAuditService auditService;%0A%0A
   @Befor
@@ -1730,34 +1730,25 @@
 ption %7B%0A    
-FakeGroupA
+a
 uditService 
@@ -1750,41 +1750,20 @@
 vice
- auditService = clearAuditService
+.clearEvents
 ();%0A
@@ -2511,34 +2511,25 @@
 ption %7B%0A    
-FakeGroupA
+a
 uditService 
@@ -2531,41 +2531,20 @@
 vice
- auditService = clearAuditService
+.clearEvents
 ();%0A
@@ -2957,32 +2957,32 @@
 -upload-pack%22);%0A
+
     assertThat((
@@ -3060,240 +3060,8 @@
  %7D%0A%0A
-  private FakeGroupAuditService clearAuditService() %7B%0A    FakeGroupAuditService auditService =%0A        server.getTestInjector().getInstance(FakeGroupAuditService.class);%0A    auditService.clearEvents();%0A    return auditService;%0A  %7D%0A%0A
   pr

Make FakeGroupAuditService a field