package io.norberg.rut;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.runners.MockitoJUnitRunner;

import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;

@RunWith(MockitoJUnitRunner.class)
public class RouterTest {

  @Test
  public void testRouting() {
    final Router.Builder<String> builder = Router.builder();
    final String target = "target";
    builder.route("GET", "/foo/<bar>/baz", target);

    final Router<String> router = builder.build();

    final Router.Result<String> result = router.result();

    final Router.Status status1 = router.route("GET", "/foo/bar-value/baz?q=a&w=b", result);
    assertThat(status1, is(Router.Status.SUCCESS));
    assertThat(result.status(), is(Router.Status.SUCCESS));
    assertThat(result.isSuccess(), is(true));
    assertThat(result.target(), is(target));
    assertThat(result.queryStart(), is(19));
    assertThat(result.query().toString(), is("q=a&w=b"));
    final String name = result.paramName(0);
    final CharSequence value = result.paramValue(0);
    assertThat(name, is("bar"));
    assertThat(value.toString(), is("bar-value"));

    final Router.Status status2 = router.route("DELETE", "/foo/bar/baz", result);
    assertThat(status2, is(Router.Status.METHOD_NOT_ALLOWED));
    assertThat(result.status(), is(Router.Status.METHOD_NOT_ALLOWED));
    assertThat(result.isSuccess(), is(false));

    final Router.Status status3 = router.route("GET", "/non/existent/path", result);
    assertThat(status3, is(Router.Status.NOT_FOUND));
    assertThat(result.status(), is(Router.Status.NOT_FOUND));
    assertThat(result.isSuccess(), is(false));
  }


}

@@ -35,20 +35,85 @@
 g.junit.
-Test
+Rule;%0Aimport org.junit.Test;%0Aimport org.junit.rules.ExpectedException
 ;%0Aimport
@@ -332,16 +332,88 @@
 Test %7B%0A%0A
+  @Rule public ExpectedException exception = ExpectedException.none();%0A%0A
   @Test%0A
@@ -1802,11 +1802,374 @@
 );%0A  %7D%0A%0A
+  @Test%0A  public void verifyResultTargetThrowsIfNotSuccessful() %7B%0A    final Router%3CString%3E router = Router.builder(String.class)%0A        .route(%22GET%22, %22/foo%22, %22foo%22)%0A        .build();%0A%0A    final Router.Result%3CString%3E result = router.result();%0A    router.route(%22GET%22, %22/bar%22, result);%0A%0A    exception.expect(IllegalStateException.class);%0A%0A    result.target();%0A  %7D%0A%0A
 %0A%7D%0A

add test