package froop.rest.resource;

import org.glassfish.jersey.jackson.JacksonFeature;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.test.JerseyTest;
import org.glassfish.jersey.test.TestProperties;
import org.junit.Test;

import javax.json.JsonArray;
import javax.json.JsonValue;
import javax.ws.rs.client.Entity;
import javax.ws.rs.core.Application;
import javax.ws.rs.core.Response;

import java.util.Iterator;

import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;

public class SampleResourceTest extends JerseyTest {

  @Override
  protected Application configure() {
    enable(TestProperties.LOG_TRAFFIC);
    enable(TestProperties.DUMP_ENTITY);
    return new ResourceConfig(SampleResource.class).register(JacksonFeature.class);
  }

  @Test
  public void testGetText() throws Exception {
    String res = target("samples/text").request().get(String.class);

    assertThat(res, is("dummy text"));
  }

  @Test
  public void testGetList() throws Exception {
    JsonArray res = target("samples").request(APPLICATION_JSON).get(JsonArray.class);

    Iterator<JsonValue> it = res.iterator();
    assertItem(it.next().toString(), 1, "name1");
    assertItem(it.next().toString(), 2, "name2");
    assertFalse(it.hasNext());
  }

  @Test
  public void testGetItem() throws Exception {
    SampleBean res = target("samples/1").register(JacksonFeature.class).request(APPLICATION_JSON).get(SampleBean.class);

    assertThat(res, is(new SampleBean(1, "name1")));
  }

  private void assertItem(String json, int id, String name) {
    assertThat(json, is("{\"id\":" + id + ",\"name\":\"" + name + "\"}"));
  }

  @Test
  public void testCreate() throws Exception {
    SampleBean req = new SampleBean(null, "new name");

    Response res = target("samples").register(JacksonFeature.class).request().post(Entity.entity(req, APPLICATION_JSON));

    assertThat(res.getStatus(), is(204));
  }

  @Test
  public void testUpdate() throws Exception {
    SampleBean req = new SampleBean(1, "name1b");

    Response res = target("samples/1").register(JacksonFeature.class).request().put(Entity.entity(req, APPLICATION_JSON));

    assertThat(res.getStatus(), is(204));
  }

  @Test
  public void testDelete() throws Exception {
    Response res = target("samples/1").register(JacksonFeature.class).request().delete();

    assertThat(res.getStatus(), is(204));
  }
}
@@ -261,51 +261,27 @@
 vax.
-json.JsonArray;%0Aimport javax.json.JsonValue
+ws.rs.client.Entity
 ;%0Aim
@@ -294,36 +294,39 @@
 avax.ws.rs.c
-lient.Entity
+ore.Application
 ;%0Aimport jav
@@ -335,35 +335,35 @@
 .ws.rs.core.
-Application
+GenericType
 ;%0Aimport jav
@@ -404,24 +404,45 @@
 va.util.
-Iterator
+Arrays;%0Aimport java.util.List
 ;%0A%0Aimpor
@@ -543,52 +543,8 @@
 is;%0A
-import static org.junit.Assert.assertFalse;%0A
 impo
@@ -1088,268 +1088,291 @@
     
-JsonArray res = target(%22samples%22).request(APPLICATION_JSON).get(JsonArray.class);%0A%0A    Iterator%3CJsonValue%3E it = res.iterator();%0A    assertItem(it.next().toString(), 1, %22name1%22);%0A    assertItem(it.next().toString(), 2, %22name2%22);%0A    assertFalse(it.hasNext(
+GenericType%3CList%3CSampleBean%3E%3E listType = new GenericType%3CList%3CSampleBean%3E%3E() %7B%7D;%0A    List res = target(%22samples%22).register(JacksonFeature.class).request(APPLICATION_JSON).get(listType);%0A%0A    assertThat(res, is(Arrays.asList(new SampleBean(1, %22name1%22), new SampleBean(2, %22name2%22))
 ));%0A
- 
  %7D%0A%0A
@@ -1610,150 +1610,8 @@
  %7D%0A%0A
-  private void assertItem(String json, int id, String name) %7B%0A    assertThat(json, is(%22%7B%5C%22id%5C%22:%22 + id + %22,%5C%22name%5C%22:%5C%22%22 + name + %22%5C%22%7D%22));%0A  %7D%0A%0A
   @T

test refactoring