<commit_before>package fr.insee.pogues.rest.test;

import com.jayway.restassured.RestAssured;
import fr.insee.pogues.rest.test.mock.QuestionnaireMocks;
import fr.insee.pogues.rest.test.utils.RestAssuredConfig;
import org.apache.log4j.Logger;
import org.json.simple.JSONObject;
import org.junit.AfterClass;
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.assertNotEquals;


/**
 * Test Class used to test the REST Web Service PoguesPersistence, called by
 * Pogues-BO
 *
 * @author I6VWID
 */
public class TestPoguesPersistence {


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

    /**
     * Setting up the RestAssured default URI and Authentication
     */
    @BeforeClass
    public static void setUp() {
        RestAssuredConfig.configure();
    }

    @AfterClass
    public static void tearDown() {
        RestAssured.reset();
    }

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

    }

    /**
     * Post Questionnaire Test
     */
    @Test
    public void postQuestionnaireThenDeleteWithSuccess() {
        logger.debug("Trying to post on /pogues/persistence/questionnaires with Created = 201");
        JSONObject questionnaire = QuestionnaireMocks.createMockQuestionnaire();
        postQuestionnaireWithSuccess(questionnaire);
        deleteQuestionnaireWithSuccess(questionnaire.get("id").toString());

    }

    /**
     * Post Questionnaire Test
     */
    @Test
    public void postQuestionnaireThenRePostWithError() {
        JSONObject questionnaire = QuestionnaireMocks.createMockQuestionnaire();
        postQuestionnaireWithSuccess(questionnaire);
        expect()
                .statusCode(400)
                .when()
                .given()
                .contentType(MediaType.APPLICATION_JSON)
                .body(questionnaire.toJSONString())
                .when()
                .post("/pogues/persistence/questionnaires");
        deleteQuestionnaireWithSuccess(questionnaire.get("id").toString());
    }

    /**
     * Put Questionnaire Test
     */
    @Test
    public void putQuestionnaireWithNotFoundError() {
        logger.debug("Trying to post on /pogues/persistence/questionnaires with Created = 201");
        JSONObject questionnaire = QuestionnaireMocks.createMockQuestionnaire();
        expect()
                .statusCode(404)
                .when()
                .given()
                .contentType(MediaType.APPLICATION_JSON)
                .body(questionnaire.toJSONString())
                .when()
                .put("/pogues/persistence/questionnaire/notfound");

    }

    /**
     * Put Questionnaire Test
     */
    @Test
    public void postQuestionnaireThenUpdateWithNoContent() {
        JSONObject questionnaire = QuestionnaireMocks.createMockQuestionnaire();
        postQuestionnaireWithSuccess(questionnaire);
        expect()
                .statusCode(204)
                .when()
                .given()
                .contentType(MediaType.APPLICATION_JSON)
                .body(questionnaire.toJSONString())
                .when()
                .put("/pogues/persistence/questionnaire/" + questionnaire.get("id"));
        deleteQuestionnaireWithSuccess(questionnaire.get("id").toString());
    }

    @Test
    public void postQuestionnaireThenUpdateWithUnauthorized() {
        JSONObject questionnaire = QuestionnaireMocks.createMockQuestionnaire();
        questionnaire.put("owner", "DR31-DIR");
        postQuestionnaireWithSuccess(questionnaire);
        expect()
                .statusCode(403)
                .when()
                .given()
                .contentType(MediaType.APPLICATION_JSON)
                .body(questionnaire.toJSONString())
                .when()
                .put("/pogues/persistence/questionnaire/" + questionnaire.get("id"));
        deleteQuestionnaireWithSuccess(questionnaire.get("id").toString());
    }

    /**
     * getQuestionnaireById test
     */
    @Test
    public void postQuestionnaireThenGetWithSuccess() {
        logger.debug("Trying to reach /pogues/persistence/17 with Status = 200");
        JSONObject questionnaire = QuestionnaireMocks.createMockQuestionnaire();
        postQuestionnaireWithSuccess(questionnaire);
        expect()
                .statusCode(200)
                .when()
                .get("/pogues/persistence/questionnaire/" + questionnaire.get("id"));
        deleteQuestionnaireWithSuccess(questionnaire.get("id").toString());

    }

    @Test
    public void getQuestionnaireByIdWithNotFound() {
        logger.debug("Trying to reach /pogues/persistence/notfound with Status = 404");
        expect()
                .statusCode(404)
                .when()
                .get("/pogues/persistence/questionnaire/notfound");
    }

    @Test
    public void postQuestionnaireThenGetByOwnerWithSuccess() {
        JSONObject questionnaire = QuestionnaireMocks.createMockQuestionnaire();
        questionnaire.put("owner", "DR31-DIR");
        postQuestionnaireWithSuccess(questionnaire);
        int listSize = expect()
                .statusCode(200)
                .when()<commit_msg>Update put test to match concrete scenarios<commit_after>package fr.insee.pogues.rest.test;

import com.jayway.restassured.RestAssured;
import fr.insee.pogues.rest.test.mock.QuestionnaireMocks;
import fr.insee.pogues.rest.test.utils.RestAssuredConfig;
import org.apache.log4j.Logger;
import org.json.simple.JSONObject;
import org.junit.AfterClass;
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.assertNotEquals;


/**
 * Test Class used to test the REST Web Service PoguesPersistence, called by
 * Pogues-BO
 *
 * @author I6VWID
 */
public class TestPoguesPersistence {


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

    /**
     * Setting up the RestAssured default URI and Authentication
     */
    @BeforeClass
    public static void setUp() {
        RestAssuredConfig.configure();
    }

    @AfterClass
    public static void tearDown() {
        RestAssured.reset();
    }

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

    }

    /**
     * Post Questionnaire Test
     */
    @Test
    public void postQuestionnaireThenDeleteWithSuccess() {
        JSONObject questionnaire = QuestionnaireMocks.createMockQuestionnaire();
        postQuestionnaireWithSuccess(questionnaire);
        deleteQuestionnaireWithSuccess(questionnaire.get("id").toString());

    }

    /**
     * Post Questionnaire Test
     */
    @Test
    public void postQuestionnaireThenRePostWithError() {
        JSONObject questionnaire = QuestionnaireMocks.createMockQuestionnaire();
        postQuestionnaireWithSuccess(questionnaire);
        expect()
                .statusCode(400)
                .when()
                .given()
                .contentType(MediaType.APPLICATION_JSON)
                .body(questionnaire.toJSONString())
                .when()
                .post("/pogues/persistence/questionnaires");
        deleteQuestionnaireWithSuccess(questionnaire.get("id").toString());
    }

    /**
     * Put Questionnaire Test
     */
    @Test
    public void putQuestionnaireWithNotFoundError() {
        JSONObject questionnaire = QuestionnaireMocks.createMockQuestionnaire();
        questionnaire.put("id", "notfound");
        expect()
                .statusCode(404)
                .when()
                .given()
                .contentType(MediaType.APPLICATION_JSON)
                .body(questionnaire.toJSONString())
                .when()
                .put("/pogues/persistence/questionnaire/notfound");

    }

    /**
     * Put Questionnaire Test
     */
    @Test
    public void postQuestionnaireThenUpdateWithNoContent() {
        JSONObject questionnaire = QuestionnaireMocks.createMockQuestionnaire();
        postQuestionnaireWithSuccess(questionnaire);
        expect()
                .statusCode(204)
                .when()
                .given()
                .contentType(MediaType.APPLICATION_JSON)
                .body(questionnaire.toJSONString())
                .when()
                .put("/pogues/persistence/questionnaire/" + questionnaire.get("id"));
        deleteQuestionnaireWithSuccess(questionnaire.get("id").toString());
    }

    @Test
    public void postQuestionnaireThenUpdateWithUnauthorized() {
        JSONObject questionnaire = QuestionnaireMocks.createMockQuestionnaire();
        questionnaire.put("owner", "DR31-DIR");
        postQuestionnaireWithSuccess(questionnaire);
        expect()
                .statusCode(403)
                .when()
                .given()
                .contentType(MediaType.APPLICATION_JSON)
                .body(questionnaire.toJSONString())
                .when()
                .put("/pogues/persistence/questionnaire/" + questionnaire.get("id"));
        deleteQuestionnaireWithSuccess(questionnaire.get("id").toString());
    }

    /**
     * getQuestionnaireById test
     */
    @Test
    public void postQuestionnaireThenGetWithSuccess() {
        logger.debug("Trying to reach /pogues/persistence/17 with Status = 200");
        JSONObject questionnaire = QuestionnaireMocks.createMockQuestionnaire();
        postQuestionnaireWithSuccess(questionnaire);
        expect()
                .statusCode(200)
                .when()
                .get("/pogues/persistence/questionnaire/" + questionnaire.get("id"));
        deleteQuestionnaireWithSuccess(questionnaire.get("id").toString());

    }

    @Test
    public void getQuestionnaireByIdWithNotFound() {
        expect()
                .statusCode(404)
                .when()
                .get("/pogues/persistence/questionnaire/notfound");
    }

    @Test
    public void postQuestionnaireThenGetByOwnerWithSuccess() {
        JSONObject questionnaire = QuestionnaireMocks.createMockQuestionnaire();
        questionnaire.put("owner", "DR31-DIR");
        postQuestionnaireWithSuccess(questionnaire);
        int listSize = expect()
                .statusCode(200)
                .when()
{'fraction_non_alphanumeric': 0.10334762382559519, 'fraction_numerical': 0.007753352184620998, 'mean_word_length': 2.3103864734299515, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3436364', 'n_tokens_mistral': 3005, 'n_tokens_neox': 2788, 'n_words': 494}