/*
 * Copyright (c) 2015, 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 com.inversoft.rest.text;

import com.inversoft.rest.ClientResponse;
import org.testng.annotations.Test;

import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.UUID;

import static org.testng.Assert.*;

/**
 * @author Brian Pontarelli
 */
@Test(groups = "unit")
public class RESTClientTest {
  @Test(enabled = false)
  public void get_CleanSpeak_Version() {
    ClientResponse<String, String> clientResponse = new RESTClient().url("https://www.inversoft.com/latest-clean-speak-version").get().go();
    assertTrue(clientResponse.wasSuccessful());
    assertEquals(clientResponse.successResponse, "3.1.8");
  }

  @Test
  public void get_useVoidClassInsteadOfVoidTYPE() {
    // success response
    expectException(() -> new com.inversoft.rest.json.RESTClient<>(Void.class, Void.TYPE).url("https://www.inversoft.com/latest-clean-speak-version").get().go(),
        IllegalArgumentException.class);

    // error response
    expectException(() -> new com.inversoft.rest.json.RESTClient<>(Void.TYPE, Void.class).url("https://www.inversoft.com/does-not-exist").get().go(),
        IllegalArgumentException.class);
  }

  private void expectException(Runnable runnable, Class<? extends Throwable> expected) {
    try {
      runnable.run();
    } catch (Throwable e) {
      if (!e.getClass().equals(expected)) {
        fail("Expected exception [" + expected + "], but caught [" + e.getClass() + "].", e);
      }
      return;
    }

    fail("Expected exception [" + expected + "].");
  }

  @Test
  public void get_parameters() {
    ZonedDateTime now = ZonedDateTime.now();

    // Test null segment, null parameter, ZoneDateTime parameter, and a collection parameter
    RESTClient client = new RESTClient()
        .url("https://www.inversoft.com")
        .urlSegment(null)
        .urlSegment("latest-clean-speak-version")
        .urlParameter("time", now)
        .urlParameter("foo", "bar")
        .urlParameter("baz", null)
        .urlParameter("ids", new ArrayList<>(Arrays.asList(new UUID(1, 0), new UUID(2, 0))))
        .get();

    assertEquals(client.url.toString(), "https://www.inversoft.com/latest-clean-speak-version");

    assertEquals(client.parameters.get("time").size(), 1);
    assertEquals(client.parameters.get("time").get(0), now.toInstant().toEpochMilli());

    assertEquals(client.parameters.get("foo").size(), 1);
    assertEquals(client.parameters.get("foo").get(0), "bar");

    assertNull(client.parameters.get("baz"));

    client.go(); // finish building the final URL
    assertEquals(client.url.toString(), "https://www.inversoft.com/latest-clean-speak-version?time="
        + now.toInstant().toEpochMilli() + "&foo=bar&ids=" + new UUID(1, 0).toString() + "&ids=" + new UUID(2, 0).toString());
  }
}

@@ -813,16 +813,38 @@
 Arrays;%0A
+import java.util.Map;%0A
 import j
@@ -1295,32 +1295,628 @@
  %223.1.8%22);%0A  %7D%0A%0A
+  @Test%0A  public void get_missing() %7B%0A    ClientResponse%3CMap, Map%3E clientResponse = new com.inversoft.rest.json.RESTClient%3C%3E(Map.class, Map.class)%0A        .url(%22http://passport-admin.inversoft.com:9011/api/user/%22 + %2200000000-0000-0000-0000-0000000909f6%22)%0A        .authorization(%222772f072-677a-463b-b709-5075903387ce%22)%0A        .get()%0A        .go();%0A%0A    System.out.println(clientResponse.exception);%0A    System.out.println(clientResponse.errorResponse);%0A    System.out.println(clientResponse.status);%0A    System.out.println(clientResponse.successResponse);%0A    System.out.println(%22Success%22);%0A  %7D%0A%0A
   @Test%0A  public

Fix predicate for removing projects