package me.dinowernli.grpc.polyglot.config;

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

import com.google.common.collect.ImmutableMultimap;
import me.dinowernli.junit.TestClass;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Optional;

import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

import com.google.common.collect.ImmutableList;
import org.junit.rules.TemporaryFolder;

/** Unit tests for {@link CommandLineArgs}. */
@TestClass
public class CommandLineArgsTest {
  @Rule public TemporaryFolder tempFolder = new TemporaryFolder();
  private Path tempFile1;
  private Path tempFile2;

  @Before
  public void setUp() throws Throwable {
    tempFile1 = Paths.get(tempFolder.newFile().toURI());
    tempFile2 = Paths.get(tempFolder.newFile().toURI());
  }

  @Test
  public void parsesAdditionalIncludesSingle() {
    CommandLineArgs params = parseArgs(ImmutableList.of(
        String.format("--add_protoc_includes=%s,%s", tempFile1.toString(), tempFile2.toString())));
    assertThat(params.additionalProtocIncludes()).hasSize(2);
  }

  @Test
  public void parsesAdditionalIncludesMulti() {
    CommandLineArgs params = parseArgs(ImmutableList.of(
        String.format("--add_protoc_includes=%s", tempFile1.toString())));
    assertThat(params.additionalProtocIncludes()).hasSize(1);
  }

  @Test
  public void parseMetadata() {
    CommandLineArgs params = parseArgs(ImmutableList.of(
        String.format("--metadata=%s:%s,%s:%s,%s:%s",
            "key1", "value1", "key1", "value2", "key2", "value2")));

    ImmutableMultimap<Object, Object> expectedResult =
        ImmutableMultimap.of("key1", "value1", "key1", "value2", "key2", "value2");
    assertThat(params.metadata()).isEqualTo(Optional.of(expectedResult));
  }

  @Test
  public void parseMetadataWithSpaces() {
    CommandLineArgs params = parseArgs(ImmutableList.of(
        String.format("--metadata=%s:%s,%s:%s,%s:%s",
            "key1", "value1 ", "key2", " value2", "key3", "value 3")));

    ImmutableMultimap<Object, Object> expectedResult =
        ImmutableMultimap.of("key1", "value1 ", "key2", " value2", "key3", "value 3");
    assertThat(params.metadata()).isEqualTo(Optional.of(expectedResult));
  }

  @Test(expected = IllegalArgumentException.class)
  public void parseMetadataWithKeyWithoutValue() {
    CommandLineArgs params = parseArgs(ImmutableList.of(
        String.format("--metadata=%s:%s,%s", "key1", "value1", "key2")));

    params.metadata();
  }

  @Test
  public void parseOutputFileEvenIfAbsent() {
    Path filePath = Paths.get(tempFolder.getRoot().getAbsolutePath(), "some-file.txt");
    CommandLineArgs params = parseArgs(ImmutableList.of(
        "--output_file_path=" + filePath.toAbsolutePath().toString()));
    assertThat(params.outputFilePath().isPresent()).isTrue();
  }

  private static CommandLineArgs parseArgs(ImmutableList<String> args) {
    ImmutableList<String> allArgs = ImmutableList.<String>builder()
        .addAll(args)
        .add("--endpoint=somehost:1234")
        .add("--full_method=some.package/Method")
        .add("--proto_discovery_root=.")
        .add("--use_reflection=true")
        .build();
    return CommandLineArgs.parse(allArgs.toArray(new String[0]));
  }
}

@@ -2895,16 +2895,465 @@
 );%0A  %7D%0A%0A
+  @Test(expected = IllegalArgumentException.class)%0A  public void parseOptionWithoutCommand() %7B%0A    CommandLineArgs.parse(new String%5B%5D%7B%22--endpoint=somehost:1234%22%7D);%0A  %7D%0A%0A  @Test%0A  public void usage() %7B%0A    assertThat(CommandLineArgs.getUsage()).startsWith(%22Usage: java -jar polyglot.jar %5Boptions%5D %5Bcommand%5D %5Bcommand options%5D%22);%0A  %7D%0A%0A  @Test%0A  public void help() %7B%0A    assertThat(CommandLineArgs.parse(new String%5B%5D%7B%22--help%22%7D).isHelp()).isTrue();%0A  %7D%0A%0A
   privat

Add more (failing) unit tests: help, usage, option without command