/*
 * Copyright (c) 2017-present, Facebook, Inc.
 * All rights reserved.
 *
 * This source code is licensed under the BSD-style license found in the
 * LICENSE file in the root directory of this source tree. An additional grant
 * of patent rights can be found in the PATENTS file in the same directory.
 */

package com.facebook.common.util;

import android.net.Uri;
import java.net.URL;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

/** Unit test for {@link UriUtilTest}. */
@RunWith(Parameterized.class)
public class UriUtilTest {

  private static final String FB_COM = "www.facebook.com";
  private static final List<String> PATHS_GIVEN = Arrays.asList("a", "b", "c");

  private static final List<String> KEYS_GIVEN = Arrays.asList("key1", "key2", "key3");
  private static final List<String> VALS_GIVEN = Arrays.asList("val1", "val2", "val3");

  private static final List<String> EMPTY_LIST = Collections.<String>emptyList();
  private static final List<String> NO_PATHS = EMPTY_LIST;
  private static final List<String> NO_KEYS = EMPTY_LIST;
  private static final List<String> NO_VALS = EMPTY_LIST;

  private String scheme;

  public UriUtilTest(String scheme) {
    this.scheme = scheme;
  }

  @Parameters
  public static Iterable<? extends Object> data() {
    return Arrays.asList("http", "https", "ftp", "file");
  }

  @Test
  public void testWithParams() {
    assertConversionFromUriToUrl(FB_COM, PATHS_GIVEN, KEYS_GIVEN, VALS_GIVEN);
    assertConversionFromUriToUrl(FB_COM, NO_PATHS, KEYS_GIVEN, VALS_GIVEN);
  }

  @Test
  public void testWithoutParams() {
    assertConversionFromUriToUrl(FB_COM, PATHS_GIVEN, NO_KEYS, NO_VALS);
    assertConversionFromUriToUrl(FB_COM, NO_PATHS, NO_KEYS, NO_VALS);
  }

  @Test
  public void testNull() {
    org.junit.Assert.assertNull(UriUtil.uriToUrl(null));
  }

  @Test
  public void testBadHostname() {
    assertConversionFromUriToUrl("www", NO_PATHS, NO_KEYS, NO_VALS);
    assertConversionFromUriToUrl(".www", NO_PATHS, NO_KEYS, NO_VALS);
    assertConversionFromUriToUrl("ww.w", NO_PATHS, NO_KEYS, NO_VALS);
    assertConversionFromUriToUrl("www.", NO_PATHS, NO_KEYS, NO_VALS);
    assertConversionFromUriToUrl("?k=v", NO_PATHS, NO_KEYS, NO_VALS);
  }

  @Test
  public void testListParameters() {
    assertConversionFromUriToUrl(
        FB_COM, PATHS_GIVEN, KEYS_GIVEN, Arrays.asList("[val11, val12]", "[val21, val22]"));
  }

  @Test
  public void testBadPaths() {
    assertConversionFromUriToUrl(FB_COM, Arrays.asList("a.b", "b//c"), NO_KEYS, NO_VALS);
    assertConversionFromUriToUrl(FB_COM, Arrays.asList("a?b", "b\\?c"), NO_KEYS, NO_VALS);
    assertConversionFromUriToUrl(FB_COM, Arrays.asList("{", "}"), NO_KEYS, NO_VALS);
  }

  private void assertConversionFromUriToUrl(
      String authority, List<String> paths, List<String> keys, List<String> values) {

    Uri.Builder builder = new Uri.Builder().scheme(scheme).authority(authority);

    for (String path : paths) {
      builder.appendPath(path);
    }

    Iterator<String> keyIter = keys.iterator();
    Iterator<String> valIter = values.iterator();
    while (keyIter.hasNext() && valIter.hasNext()) {
      String key = keyIter.next();
      String val = valIter.next();
      builder.appendQueryParameter(key, val);
    }

    Uri uri = builder.build();
    URL url = UriUtil.uriToUrl(uri);

    org.junit.Assert.assertEquals(uri.toString(), url.toString());
  }
}

@@ -407,16 +407,45 @@
 Arrays;%0A
+import java.util.Collection;%0A
 import j
@@ -579,37 +579,35 @@
 %0Aimport org.
-junit.runners
+robolectric
 .Parameteriz
@@ -612,48 +612,88 @@
 ized
-;%0Aimport org.junit.runners.Parameterized
+RobolectricTestRunner;%0Aimport org.robolectric.ParameterizedRobolectricTestRunner
 .Par
@@ -766,16 +766,37 @@
 eterized
+RobolectricTestRunner
 .class)%0A
@@ -1527,33 +1527,27 @@
 tic 
-Iterable%3C? extends 
+Collection%3C
 Object
+%5B%5D
 %3E da
@@ -1582,38 +1582,107 @@
 ist(
+%0A        new Object%5B%5D %7B
 %22http%22
+%7D
 , 
+new Object%5B%5D %7B
 %22https%22
+%7D
 , 
+new Object%5B%5D %7B
 %22ftp%22
+%7D
 , 
+new Object%5B%5D %7B
 %22file%22
+%7D
 );%0A 

fix oss build