package no.deichman.services.resources;

import com.fasterxml.jackson.core.JsonParseException;
import java.io.IOException;
import javax.ws.rs.core.Response;
import no.deichman.services.resources.WorkResource;
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.map.ObjectMapper;
import org.junit.After;
import org.junit.AfterClass;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

public class WorkResourceTest{


    @BeforeClass
    public static void setUpClass() throws Exception {
    }

    @AfterClass
    public static void tearDownClass() throws Exception {
    }

    @Before
    public void setUp() throws Exception {
    }

    @After
    public void tearDown() throws Exception {
    }

    /**
     * Test of listWork method, of class WorkResource.
     */
    @Test
    public void testListWork() {
        WorkResource instance = new WorkResource();
        Response expResult = null;
        Response result = instance.listWork();
        assertNotNull(result);
        assertEquals(200, result.getStatus());
        assertTrue(isValidJSON(result.getEntity().toString()));
    }

    /**
     * Test of getWork method, of class WorkResource.
     */
    @Test
    public void testGetWork() {
        String workId = "work_00001";
        WorkResource instance = new WorkResource();
        Response result = instance.getWorkJSON(workId);            
        assertNotNull(result);
        assertEquals(200, result.getStatus());
        assertTrue(isValidJSON(result.getEntity().toString()));

    }

    public boolean isValidJSON(final String json) {
        boolean valid = false;
        try {
            final JsonParser parser = new ObjectMapper().getJsonFactory()
                    .createJsonParser(json);
            while (parser.nextToken() != null) {
            }
            valid = true;
        } catch (JsonParseException jpe) {
            jpe.printStackTrace();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }

        return valid;
    }

}

@@ -108,32 +108,70 @@
 io.IOException;%0A
+import javax.ws.rs.NotFoundException;%0A
 import javax.ws.
@@ -188,16 +188,16 @@
 sponse;%0A
-
 import n
@@ -1012,16 +1012,45 @@
 oid 
-testList
+should_return_non_empty_json_list_of_
 Work
@@ -1437,16 +1437,36 @@
 oid 
-testGetW
+should_return_a_valid_json_w
 ork(
@@ -1769,16 +1769,371 @@
 ng()));%0A
+    %7D%0A%0A    /**%0A     * Test of getWork method, of class WorkResource.%0A     */%0A    @Test(expected = NotFoundException.class)%0A    public void should_throw_exception_when_work_is_not_found() %7B%0A        String workId = %22work_DOES_NOT_EXIST%22;%0A        WorkResource instance = new WorkResource();%0A        Response result = instance.getWorkJSON(workId);            
 %0A    %7D%0A%0A

Refactor test-method names