/*
 * Copyright (c) 2018, Inversoft Inc., 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 org.primeframework.mvc.parameter.convert.converters;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import org.primeframework.mvc.PrimeBaseTest;
import org.testng.annotations.Test;

import com.google.inject.Inject;
import static org.testng.Assert.assertEquals;
import sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl;

/**
 * @author Daniel DeGroff
 */
public class CollectionsConverterTest extends PrimeBaseTest {
  @Inject
  public CollectionConverter converter;

  @Test
  public void fromStrings() {
    withValues("foo", "bar").assertConversion();
    withValues("foo", "bar,baz").assertConversion();
    withValues(",foo,", "bar,baz").assertConversion();

    withValues(",foo,").assertConversion();
    withValues("foo,bar").assertConversion();

    withValues("").withExpectedResult(new String[0]).assertConversion();
    withValues("foo", "").withExpectedResult(new String[]{"foo"}).assertConversion();
  }

  private Builder withValues(String... values) {
    return new Builder(values);
  }

  private class Builder {
    public String[] values;

    public Object[] expected;

    public Builder(String... values) {
      this.values = values;
    }

    public Builder withExpectedResult(Object[] expected) {
      this.expected = expected;
      return this;
    }

    @SuppressWarnings("unchecked")
    private void assertConversion() {
      if (expected == null) {
        expected = values;
      }

      // --> java.util.Set
      Set<String> set = (Set<String>) converter.convertFromStrings(ParameterizedTypeImpl.make(Set.class, new Type[]{String.class}, null), null, "variations", values);
      assertEquals(new TreeSet(set), new TreeSet<>(Arrays.asList(expected)));

      // Without a parameterized type
      set = (Set<String>) converter.convertFromStrings(HashSet.class, null, "variations", values);
      assertEquals(new TreeSet(set), new TreeSet<>(Arrays.asList(expected)));

      // --> java.util.TreeSet
      TreeSet<String> sortedSet = (TreeSet<String>) converter.convertFromStrings(ParameterizedTypeImpl.make(TreeSet.class, new Type[]{String.class}, null), null, "variations", values);
      assertEquals(sortedSet, new TreeSet<>(Arrays.asList(expected)));

      // Without a parameterized type
      sortedSet = (TreeSet<String>) converter.convertFromStrings(TreeSet.class, null, "variations", values);
      assertEquals(sortedSet, new TreeSet<>(Arrays.asList(expected)));

      // --> java.util.List
      List<String> list = (List<String>) converter.convertFromStrings(ParameterizedTypeImpl.make(List.class, new Type[]{String.class}, null), null, "variations", values);
      assertEquals(list, new ArrayList<>(Arrays.asList(expected)));

      // Without a parameterized type
      list = (List<String>) converter.convertFromStrings(ParameterizedTypeImpl.make(List.class, new Type[]{String.class}, null), null, "variations", values);
      assertEquals(list, new ArrayList<>(Arrays.asList(expected)));
    }
  }
}

@@ -900,24 +900,140 @@
 meBaseTest;%0A
+import org.primeframework.mvc.TestBuilder;%0Aimport org.primeframework.mvc.parameter.convert.ConverterStateException;%0A
 import org.t
@@ -1796,32 +1796,297 @@
 version();%0A  %7D%0A%0A
+  @Test%0A  public void toStrings() %7B%0A    TestBuilder.expectException(ConverterStateException.class,%0A        () -%3E converter.convertToString(ParameterizedTypeImpl.make(Set.class, new Type%5B%5D%7BString.class%7D, null), null, %22variations%22, new String%5B%5D%7B%22foo%22, %22bar%22%7D));%0A  %7D%0A%0A
   private Builde

Test for objectToString