// Copyright (C) 2020 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.plugins.codeowners.backend;

import static com.google.common.truth.Truth.assertThat;
import static com.google.gerrit.testing.GerritJUnit.assertThrows;

import com.google.gerrit.plugins.codeowners.acceptance.AbstractCodeOwnersTest;
import java.util.Optional;
import org.junit.Test;

/** Tests for {@link CodeOwnerConfigReference}. */
public class CodeOwnerConfigReferenceTest extends AbstractCodeOwnersTest {
  @Test
  public void branchCannotBeSetToNull() throws Exception {
    NullPointerException npe =
        assertThrows(
            NullPointerException.class,
            () ->
                CodeOwnerConfigReference.builder(CodeOwnerConfigImportMode.ALL, "foo/OWNERS")
                    .setBranch((String) null));
    assertThat(npe).hasMessageThat().isEqualTo("branch");
  }

  @Test
  public void cannotCreateCodeOwnerConfigReferenceWithShortBranchName() throws Exception {
    IllegalStateException exception =
        assertThrows(
            IllegalStateException.class,
            () ->
                CodeOwnerConfigReference.builder(CodeOwnerConfigImportMode.ALL, "foo/OWNERS")
                    .setBranch(Optional.of("master"))
                    .build());
    assertThat(exception).hasMessageThat().isEqualTo("branch must be full name: master");
  }
}

@@ -699,13 +699,190 @@
 uth.
-Truth
+PathSubject.paths;%0Aimport static com.google.common.truth.Truth.assertThat;%0Aimport static com.google.common.truth.Truth.assertWithMessage;%0Aimport static com.google.common.truth.Truth8
 .ass
@@ -1036,16 +1036,71 @@
 rsTest;%0A
+import java.nio.file.Path;%0Aimport java.nio.file.Paths;%0A
 import j
@@ -2044,24 +2044,24 @@
  .build());%0A
-
     assertTh
@@ -2141,11 +2141,780 @@
 r%22);%0A  %7D
+%0A%0A  @Test%0A  public void absoluteFilePathCanBeSpecifiedInDifferentFormats() throws Exception %7B%0A    Path expectedPath = Paths.get(%22/foo/OWNERS%22);%0A    for (String inputPath : new String%5B%5D %7B%22/foo/OWNERS%22, %22//foo/OWNERS%22%7D) %7B%0A      Path path =%0A          CodeOwnerConfigReference.create(CodeOwnerConfigImportMode.ALL, inputPath).filePath();%0A      assertWithMessage(inputPath).about(paths()).that(path).isEqualTo(expectedPath);%0A      assertThat(path.isAbsolute()).isTrue();%0A    %7D%0A  %7D%0A%0A  @Test%0A  public void relativeFilePathCanBeSpecified() throws Exception %7B%0A    Path path =%0A        CodeOwnerConfigReference.create(CodeOwnerConfigImportMode.ALL, %22foo/OWNERS%22).filePath();%0A    assertThat(path).isEqualTo(Paths.get(%22foo/OWNERS%22));%0A    assertThat(path.isAbsolute()).isFalse();%0A  %7D
 %0A%7D%0A

Test different formats for paths to import code owner configs