// Copyright 2017 The Bazel Authors. All rights reserved.
//
// 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.build.workspace;

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

import com.beust.jcommander.JCommander;
import com.google.devtools.build.workspace.maven.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/** Tests for option parsing. */
@RunWith(JUnit4.class)
public class GenerateWorkspaceOptionsTest {
  @Test
  public void equalsSeparator() throws Exception {
    GenerateWorkspaceOptions options = new GenerateWorkspaceOptions();
    JCommander optionParser = JCommander.newBuilder().addObject(options).build();
    optionParser.parse("--artifact=foo:bar:1.2.3");
    assertThat(options.artifacts).contains("foo:bar:1.2.3");
  }

  @Test
  public void spaceSeparator() throws Exception {
    GenerateWorkspaceOptions options = new GenerateWorkspaceOptions();
    JCommander optionParser = JCommander.newBuilder().addObject(options).build();
    optionParser.parse("--artifact", "foo:bar:1.2.3");
    assertThat(options.artifacts).contains("foo:bar:1.2.3");
  }

  @Test
  public void noCommaDelimiter() throws Exception {
    GenerateWorkspaceOptions options = new GenerateWorkspaceOptions();
    JCommander optionParser = JCommander.newBuilder().addObject(options).build();
    optionParser.parse("--artifact", "foo:bar:[1.2.3,)");
    assertThat(options.artifacts).contains("foo:bar:[1.2.3,)");
  }

  @Test
  public void multipleArtifacts() throws Exception {
    GenerateWorkspaceOptions options = new GenerateWorkspaceOptions();
    JCommander optionParser = JCommander.newBuilder().addObject(options).build();
    optionParser.parse("--artifact", "a:b1:[1.2,2.0]", "--artifact", "a:b2:[1.0,2.0)");
    assertThat(options.artifacts).containsExactly("a:b1:[1.2,2.0]", "a:b2:[1.0,2.0)");
  }

  @Test
  public void repository() throws Exception {
    GenerateWorkspaceOptions options = new GenerateWorkspaceOptions();
    JCommander optionParser = JCommander.newBuilder().addObject(options).build();
    optionParser.parse("--repositories", "http://central.maven.org/maven2/");
    assertThat(options.repositories).contains("http://central.maven.org/maven2/");
  }

  @Test
  public void multipleRepositories() throws Exception {
    GenerateWorkspaceOptions options = new GenerateWorkspaceOptions();
    JCommander optionParser = JCommander.newBuilder().addObject(options).build();
    optionParser.parse(
        "--repositories", "http://central.maven.org/maven2/,http://repo.spring.io/libs-milestone/");
    assertThat(options.repositories)
        .containsExactly(
            "http://central.maven.org/maven2/", "http://repo.spring.io/libs-milestone/");
  }

  @Test
  public void alias() throws Exception {
    GenerateWorkspaceOptions options = new GenerateWorkspaceOptions();
    JCommander optionParser = JCommander.newBuilder().addObject(options).build();
    optionParser.parse("--declare=a:b=c");
    assertThat(options.aliases).hasSize(1);
    Rule aliasedRule = options.aliases.get(0);
    assertThat(aliasedRule.name()).isEqualTo("c");
    assertThat(aliasedRule.toMavenArtifactString()).isEqualTo("a:b:0");
  }

  @Test
  public void versionedAlias() throws Exception {
    GenerateWorkspaceOptions options = new GenerateWorkspaceOptions();
    JCommander optionParser = JCommander.newBuilder().addObject(options).build();
    optionParser.parse("--declare=a:b:1.2.3=c");
    assertThat(options.aliases).hasSize(1);
    Rule aliasedRule = options.aliases.get(0);
    assertThat(aliasedRule.name()).isEqualTo("c");
    assertThat(aliasedRule.toMavenArtifactString()).isEqualTo("a:b:1.2.3");
  }

  @Test
  public void declareDefault() throws Exception {
    GenerateWorkspaceOptions options = new GenerateWorkspaceOptions();
    JCommander optionParser = JCommander.newBuilder().addObject(options).build();
    optionParser.parse("--declare=a:b");
    assertThat(options.aliases).hasSize(1);
    Rule aliasedRule = options.aliases.get(0);
    assertThat(aliasedRule.name()).isEqualTo("a_b");
  }
}