/*
 * Copyright (c) 2011 Google, Inc.
 *
 * 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.devtools.moe.client.repositories;

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

import junit.framework.TestCase;

import org.joda.time.DateTime;

public class DescriptionMetadataScrubberTest extends TestCase {

  private static final RevisionMetadata REVISION_METADATA =
      RevisionMetadata.builder()
          .id("commit_number")
          .author("author@google.com")
          .date(new DateTime(1L))
          .description("some changes")
          .withParents(Revision.create("parentId1", "repo"), Revision.create("parentId2", "repo"))
          .build();

  public void testNonDescriptionFieldsUnaffacted() {
    RevisionMetadata rmExpected =
        RevisionMetadata.builder()
            .id("commit_number")
            .author("author@google.com")
            .date(new DateTime(1L))
            .description("some changes!!!")
            .withParents(Revision.create("parentId1", "repo"), Revision.create("parentId2", "repo"))
            .build();


    RevisionMetadata rmActual =
        new DescriptionMetadataScrubber()
            .scrub(
                REVISION_METADATA,
                new MetadataScrubberConfig() {
                  @Override
                  public String getLogFormat() {
                    return "{description}!!!";
                  }
                });
    assertThat(rmActual).isEqualTo(rmExpected);
  }

  public void testVariousScrubbingFormats() {
    assertFormatResults("{description} on {date}", "some changes on 1969/12/31");
    assertFormatResults(
        "{description} on {date}\nby: {author}",
        "some changes on 1969/12/31\nby: author@google.com");
    assertFormatResults(
        "{date} saw the birth of {id}", "1969/12/31 saw the birth of commit_number");
    assertFormatResults("my parents are ({parents})", "my parents are (parentId1, parentId2)");
  }

  private void assertFormatResults(final String format, String expectedOutput) {
    MetadataScrubberConfig config =
        new MetadataScrubberConfig() {
          @Override
          public String getLogFormat() {
            return format;
          }
        };
    assertWithMessage("Unexpected scrubbing output for format '%s'", format)
        .that(new DescriptionMetadataScrubber().scrub(REVISION_METADATA, config).description())
        .isEqualTo(expectedOutput);
  }
}

@@ -1079,34 +1079,54 @@
 te(new DateTime(
-1L
+2010, 1, 1, 0, 0, 0, 0
 ))%0A          .de
@@ -1502,10 +1502,30 @@
 ime(
-1L
+2010, 1, 1, 0, 0, 0, 0
 ))%0A 
@@ -2199,25 +2199,25 @@
 nges on 
-1969/12/3
+2010/01/0
 1%22);%0A   
@@ -2312,25 +2312,25 @@
 nges on 
-1969/12/3
+2010/01/0
 1%5Cnby: a
@@ -2419,17 +2419,17 @@
 %22, %22
-1969/12/3
+2010/01/0
 1 sa

Make a test work on all operating systems and java versions (apparently, a slight difference of what the instant 1 means exists, and is a problem, so use an explicit date). This fixes the test such that it passes internally, and also one external, open-source builds.