// 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.server.mail;

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

import com.google.gerrit.acceptance.GerritConfig;
import com.google.gerrit.server.mail.send.EmailHeader;
import java.util.Map;
import org.junit.Test;

public class MailSenderIT extends AbstractMailIT {

  @Test
  @GerritConfig(name = "sendemail.replyToAddress", value = "custom@gerritcodereview.com")
  @GerritConfig(name = "receiveemail.protocol", value = "POP3")
  public void outgoingMailHasCustomReplyToHeader() throws Exception {
    createChangeWithReview(user);
    // Check that the custom address was added as Reply-To
    assertThat(sender.getMessages()).hasSize(1);
    Map<String, EmailHeader> headers = sender.getMessages().iterator().next().headers();
    assertThat(headerString(headers, "Reply-To")).isEqualTo("custom@gerritcodereview.com");
  }

  @Test
  public void outgoingMailHasUserEmailInReplyToHeader() throws Exception {
    createChangeWithReview(user);
    // Check that the user's email was added as Reply-To
    assertThat(sender.getMessages()).hasSize(1);
    Map<String, EmailHeader> headers = sender.getMessages().iterator().next().headers();
    assertThat(headerString(headers, "Reply-To")).contains(user.email);
  }

  private String headerString(Map<String, EmailHeader> headers, String name) {
    EmailHeader header = headers.get(name);
    assertThat(header).isInstanceOf(EmailHeader.String.class);
    return ((EmailHeader.String) header).getString();
  }
}

@@ -815,16 +815,37 @@
 Header;%0A
+import java.net.URI;%0A
 import j
@@ -1883,16 +1883,953 @@
 );%0A  %7D%0A%0A
+  @Test%0A  public void outgoingMailHasListHeaders() throws Exception %7B%0A    String changeId = createChangeWithReview(user);%0A    // Check that the mail has the expected headers%0A    assertThat(sender.getMessages()).hasSize(1);%0A    Map%3CString, EmailHeader%3E headers = sender.getMessages().iterator().next().headers();%0A    String hostname = URI.create(canonicalWebUrl.get()).getHost();%0A    String listId = String.format(%22%3Cgerrit-%25s.%25s%3E%22, project.get(), hostname);%0A    String unsubscribeLink = String.format(%22%3C%25ssettings%3E%22, canonicalWebUrl.get());%0A    String threadId =%0A        String.format(%0A            %22%3Cgerrit.%25s.%25s@%25s%3E%22,%0A            gApi.changes().id(changeId).get().created.getTime(), changeId, hostname);%0A    assertThat(headerString(headers, %22List-Id%22)).isEqualTo(listId);%0A    assertThat(headerString(headers, %22List-Unsubscribe%22)).isEqualTo(unsubscribeLink);%0A    assertThat(headerString(headers, %22In-Reply-To%22)).isEqualTo(threadId);%0A  %7D%0A%0A
   privat

Add test for mailing list headers