package fr.insee.pogues.rest.test;

import com.jayway.restassured.RestAssured;
import com.jayway.restassured.http.ContentType;
import org.apache.log4j.Logger;
import org.junit.BeforeClass;
import org.junit.Test;

import javax.ws.rs.core.MediaType;

import static com.jayway.restassured.RestAssured.expect;
import static org.junit.Assert.assertEquals;


/**
 * Test Class used to test the REST Web Service PoguesUser, called by Pogues UI
 *
 * @author I6VWID
 */
public class TestPoguesUser {

    private static String jUsername = "D5WQNO";
    private static String jPassword = "D5WQNO";

    final static Logger logger = Logger.getLogger(TestPoguesUser.class);

    /**
     * Setting up the RestAssured default URI
     */
    @BeforeClass
    public static void setUp() {
        logger.debug("Setting the RestAssured base Uri to http://localhost:8080 : for local tests");
        RestAssured.baseURI = "http://localhost:8080";
        /* All this boilerplate thing to handle Form auth with tomcat */
        String sessionId;
        sessionId = expect().statusCode(200).log().all()
                .when().get("/").sessionId();
        expect().statusCode(302).log().all()
                .given().param("j_username", jUsername)
                .param("j_password", jPassword).cookie("JSESSIONID", sessionId)
                .post("j_security_check");
        sessionId = expect()
                .statusCode(404)
                .log().all()
                .given().cookie("JSESSIONID", sessionId)
                .when()
                .get().sessionId();
        RestAssured.sessionId = sessionId;
    }

    /**
     * Dummy helloworld test, should return "Hello world"
     */
    @Test
    public void helloworldTest() {
        logger.debug(
                "Dummy helloworld test : trying to reach /Pogues-BO/pogues/user/helloworld with Status = 200");
        expect().statusCode(200).contentType(ContentType.TEXT).when()
                .get("/pogues/user/helloworld");

    }


    /**
     * getUserID test
     */
    @Test
    public void getUserID() {
        logger.debug(
                "Trying to reach /Pogues-BO/pogues/user/id with Status = 200");
        String id = expect().statusCode(200).contentType(MediaType.APPLICATION_JSON).when()
                .get("/pogues/user/id").body().jsonPath().get("id");
        assertEquals(id, jUsername);
    }

    /**
     * getPermissions test
     */
    @Test
    public void getPermissions() {
        logger.debug(
                "Trying to reach /Pogues-BO/pogues/user/permissions with Status = 200");
        expect().statusCode(200)
                .contentType(MediaType.APPLICATION_JSON)
                .when()
                .get("/pogues/user/permissions");

    }


}

@@ -344,16 +344,64 @@
 Equals;%0A
+import static org.junit.Assert.assertNotEquals;%0A
 %0A%0A/**%0A *
@@ -2246,32 +2246,66 @@
 expect()
-.statusCode(200)
+%0A                .statusCode(200)%0A                
 .content
@@ -2336,16 +2336,33 @@
 ON_JSON)
+%0A                
 .when()%0A
@@ -2404,26 +2404,77 @@
 id%22)
-.body().jsonPath()
+%0A                .body()%0A                .jsonPath()%0A                
 .get
@@ -2728,32 +2728,64 @@
 %22);%0A        
-expect()
+int listSize = expect()%0A                
 .statusCode(
@@ -2918,18 +2918,161 @@
 ssions%22)
-;
 %0A
+                .body()%0A                .jsonPath()%0A                .getList(%22$%22)%0A                .size();%0A        assertNotEquals(listSize, 0);
 %0A    %7D%0A%0A

improve get permissions test