package com.danisola.urlrestify;

import org.junit.Test;

import java.net.MalformedURLException;
import java.net.URL;

import static com.danisola.urlrestify.types.FloatVar.floatVar;
import static com.danisola.urlrestify.types.IntVar.intVar;
import static com.danisola.urlrestify.types.IntVar.posIntVar;
import static com.danisola.urlrestify.RestParser.parser;
import static com.danisola.urlrestify.types.StringVar.strVar;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;

public class RestUrlTest {

    @Test
    public void whenManyParametersRequestedThenValuesAreCorrect() throws MalformedURLException {
        RestParser parser = parser("/{}/{}?float={}&str={}&int={}",
                strVar("first"), strVar("second"), floatVar("float"), strVar("str"), intVar("int"));
        RestUrl restUrl = parser.parse("http://www.example.com/a/b?float=3.14&str=pi&int=3");
        assertThat((String) restUrl.variable("first"), is("a"));
        assertThat((String) restUrl.variable("second"), is("b"));
        assertThat((Float) restUrl.variable("float"), is(3.14f));
        assertThat((String) restUrl.variable("str"), is("pi"));
        assertThat((Integer) restUrl.variable("int"), is(3));
    }

    @Test
    public void whenParametersRequestedThenValuesAreCorrect() throws MalformedURLException {
        RestParser parser = parser("/{}?ts={}&to={}", strVar("userId"), posIntVar("ts"), strVar("to"));
        RestUrl restUrl = parser.parse("http://www.mail.com/john?to=ben&ts=3456");
        assertThat((String) restUrl.variable("userId"), is("john"));
        assertThat((Integer) restUrl.variable("ts"), is(3456));
        assertThat((String) restUrl.variable("to"), is("ben"));
    }

    @Test
    public void whenExtraParametersRequestedThenSpecifiedOnesAreCorrect() throws MalformedURLException {
        RestParser parser = parser("/users?id={}", strVar("userId"));
        RestUrl vars = parser.parse("http://www.mail.com/users?cache_buster=4f0ce72&id=ben");
        assertThat((String) vars.variable("userId"), is("ben"));
    }

    @Test(expected = NullPointerException.class)
    public void whenNoUrlIsPassedThenExceptionIsThrown() {
        RestParser parser = parser("/country/{}", strVar("countryId"));
        parser.parse((URL) null);
    }

    @Test(expected = IllegalStateException.class)
    public void whenUnexistantVariableIsRequestedThenExceptionIsThrown() {
        RestParser parser = parser("/countries/{}", strVar("countryId"));
        RestUrl restUrl = parser.parse("http://www.world.com/countries/uk");
        restUrl.variable("cityId");
    }

    @Test(expected = ClassCastException.class)
    public void whenWrongTypeIsRequestedThenExceptionIsThrown() {
        RestParser parser = parser("/countries/{}", strVar("countryId"));
        RestUrl restUrl = parser.parse("http://www.world.com/countries/uk");
        Integer countryId = restUrl.variable("countryId");
    }

    @Test(expected = IllegalArgumentException.class)
    public void whenNullKeyIsRequestedThenExceptionIsThrown() {
        RestParser parser = parser("/countries/{}", strVar("countryId"));
        RestUrl restUrl = parser.parse("http://www.world.com/countries/uk");
        restUrl.variable(null);
    }
}

@@ -112,16 +112,73 @@
 t.URL;%0A%0A
+import static com.danisola.urlrestify.RestParser.parser;%0A
 import s
@@ -295,127 +295,8 @@
 ar;%0A
-import static com.danisola.urlrestify.types.IntVar.posIntVar;%0Aimport static com.danisola.urlrestify.RestParser.parser;%0A
 impo
@@ -510,11 +510,15 @@
 henM
-any
+ultiple
 Para
@@ -515,32 +515,35 @@
 ltipleParameters
+Are
 RequestedThenVal
@@ -531,32 +531,35 @@
 AreRequestedThen
+Its
 ValuesAreCorrect
@@ -1189,502 +1189,8 @@
  %7D%0A%0A
-    @Test%0A    public void whenParametersRequestedThenValuesAreCorrect() throws MalformedURLException %7B%0A        RestParser parser = parser(%22/%7B%7D?ts=%7B%7D&to=%7B%7D%22, strVar(%22userId%22), posIntVar(%22ts%22), strVar(%22to%22));%0A        RestUrl restUrl = parser.parse(%22http://www.mail.com/john?to=ben&ts=3456%22);%0A        assertThat((String) restUrl.variable(%22userId%22), is(%22john%22));%0A        assertThat((Integer) restUrl.variable(%22ts%22), is(3456));%0A        assertThat((String) restUrl.variable(%22to%22), is(%22ben%22));%0A    %7D%0A%0A
     

Improve test name and remove a superfluous one