package org.slc.sli.shtick;

import org.apache.commons.io.IOUtils;
import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.map.ObjectMapper;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;

/**
 * @author jstokes
 */
public class StandardLevel2ClientManualTest {
    private Level2ClientManual client; // class under test

    private static final String BASE_URL = "http://local.slidev.org:8080/api/rest/v1";
    private static final Map<String, Object> EMPTY_QUERY_ARGS = Collections.emptyMap();

    @Before
    public void setup() {
        client = new StandardLevel2ClientManual(BASE_URL);
    }

    @Test
    @Ignore("Don't know if this works ATM")
    public void testCRUD() {
        try {
            // POST
            final String studentId = postStudent();
            assertNotNull(studentId);

            // GET
            final List<Entity> student = getStudent(studentId);
            assertNotNull(student);
            assertEquals(1, student.size());

            // PUT
            putStudent(studentId);
            final List<Entity> updatedStudent = getStudent(studentId);
            assertEquals(1, updatedStudent.size());
            assertEquals("900000012", updatedStudent.get(0).getData().get("studentUniqueStateId"));

            // DELETE
            client.deleteStudentById(TestingConstants.ROGERS_TOKEN, studentId);

            // GET (Should fail)
            try {
                getStudent(studentId);
            } catch (final StatusCodeException e) {
                assertEquals(404, e.getStatusCode());
            }
        } catch (final IOException e) {
            fail(e.getMessage());
        } catch (final StatusCodeException e) {
            fail(e.getMessage());
        } catch (final URISyntaxException e) {
            fail(e.getMessage());
        }
    }

    private List<Entity> getStudent(final String studentId) throws IOException, StatusCodeException {
        return client.getStudentsByStudentId(TestingConstants.ROGERS_TOKEN, Arrays.asList(studentId), EMPTY_QUERY_ARGS);
    }

    private void putStudent(final String studentId) throws IOException, StatusCodeException, URISyntaxException {
        final String studentData = readJsonFromFile("/testStudentUpdated.json");
        final Entity student = deserialize(studentData);
        Map<String, Object> dataCopy = MapHelper.deepCopy(student.getData());
        dataCopy.put("id", studentId);

        client.putStudent(TestingConstants.ROGERS_TOKEN, new RestEntity("student", dataCopy));
    }

    private String postStudent() throws IOException, StatusCodeException, URISyntaxException {
        final String studentData = readJsonFromFile("/testStudent.json");
        final Entity student = deserialize(studentData);

        return client.postStudent(TestingConstants.ROGERS_TOKEN, student);
    }

    private Entity deserialize(final String studentData) throws IOException {
        final ObjectMapper mapper = new ObjectMapper();
        final JsonNode studentJson = mapper.readValue(studentData, JsonNode.class);
        return mapper.readValue(studentJson, Entity.class);
    }

    private String readJsonFromFile(final String fileLoc) {
        InputStream in = this.getClass().getResourceAsStream(fileLoc);
        try {
            return IOUtils.toString(in);
        } catch (final IOException e) {
            fail(e.getMessage());
        }
        throw new RuntimeException();
    }
}

@@ -33,121 +33,44 @@
 ort 
-org.apache.commons.io.IOUtils;%0Aimport org.codehaus.jackson.JsonNode;%0Aimport org.codehaus.jackson.map.ObjectMapper
+static org.junit.Assert.assertEquals
 ;%0Aim
@@ -66,32 +66,39 @@
 tEquals;%0Aimport 
+static 
 org.junit.Before
@@ -95,39 +95,28 @@
 nit.
-Before;%0Aimport org.junit.Ignore
+Assert.assertNotNull
 ;%0Aim
@@ -120,16 +120,23 @@
 %0Aimport 
+static 
 org.juni
@@ -137,20 +137,27 @@
 g.junit.
-Test
+Assert.fail
 ;%0A%0Aimpor
@@ -355,44 +355,121 @@
 ort 
-static org.junit.Assert.assertEquals
+org.apache.commons.io.IOUtils;%0Aimport org.codehaus.jackson.JsonNode;%0Aimport org.codehaus.jackson.map.ObjectMapper
 ;%0Aim
@@ -465,39 +465,32 @@
 tMapper;%0Aimport 
-static 
 org.junit.Assert
@@ -483,36 +483,47 @@
 g.junit.
-Assert.assertNotNull
+Before;%0Aimport org.junit.Ignore
 ;%0Aimport
@@ -515,39 +515,32 @@
 .Ignore;%0Aimport 
-static 
 org.junit.Assert
@@ -533,27 +533,20 @@
 g.junit.
-Assert.fail
+Test
 ;%0A%0A/**%0A 
@@ -2836,12 +2836,8 @@
 new 
-Rest
 Enti

Fix merge bug with Level 2 test