<commit_before>/*
 * Copyright 2008-2013 Microarray Informatics Team, EMBL-European Bioinformatics Institute
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 *
 * For further details of the Gene Expression Atlas project, including source code,
 * downloads and documentation, please see:
 *
 * http://gxa.github.com/gxa
 */

package uk.ac.ebi.atlas.acceptance.rest.tests;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import uk.ac.ebi.atlas.acceptance.rest.fixtures.RestAssuredAuthenticatedFixture;

import static com.jayway.restassured.RestAssured.expect;
import static org.hamcrest.Matchers.*;

public class ExperimentLoaderIT extends RestAssuredAuthenticatedFixture {

    private static final String EXPERIMENT_ACCESSION = "E-MTAB-599";

    @Before
    public void init(){
    }

    @After
    public void cleanup() {
        expect().body(containsString(EXPERIMENT_ACCESSION)).
            when().get("updateExperiment?accession=" + EXPERIMENT_ACCESSION + "&private=false");
    }

    @Test
    public void testListExperiments() {
        expect().body(containsString(EXPERIMENT_ACCESSION)).when().get("listExperiments?accession=" + EXPERIMENT_ACCESSION);
    }

    @Test
    public void testListSelectedExperiments() {

        expect().body("experimentAccession", containsInAnyOrder(EXPERIMENT_ACCESSION, "E-MTAB-513")).when()
                .get("listExperiments?accession=" + EXPERIMENT_ACCESSION + ",E-MTAB-513");

    }

    @Test
    public void testDeleteAndLoadExperimentPublic() {

        expect().body(is("Experiment E-MTAB-599 successfully deleted.")).when()
                .get("deleteExperiment?accession=" + EXPERIMENT_ACCESSION);

        expect().body("experimentAccession", is(empty())).when().get("listExperiments?accession=" + EXPERIMENT_ACCESSION);

        expect().body(startsWith("Experiment " + EXPERIMENT_ACCESSION + " loaded, accessKey:")).when()
                .get("loadExperiment?accession=" + EXPERIMENT_ACCESSION + "&type=BASELINE&private=false");

        expect().body("experimentAccession", hasItem(EXPERIMENT_ACCESSION))
                .and().body("isPrivate", contains(false))
                .when().get("listExperiments?accession=" + EXPERIMENT_ACCESSION);

    }

    @Test
    public void testDeleteAndLoadExperimentPrivate() {

        expect().body(is("Experiment E-MTAB-599 successfully deleted.")).when()
                .get("deleteExperiment?accession=" + EXPERIMENT_ACCESSION);

        expect().body("experimentAccession", is(empty())).when().get("listExperiments?accession=" + EXPERIMENT_ACCESSION);

        expect().body(startsWith("Experiment " + EXPERIMENT_ACCESSION + " loaded, accessKey:")).when()
                .get("loadExperiment?accession=" + EXPERIMENT_ACCESSION + "&type=BASELINE&private=true");

        expect().body("experimentAccession", hasItem(EXPERIMENT_ACCESSION))
                .and().body("isPrivate", contains(true))
                .when().get("listExperiments?accession=" + EXPERIMENT_ACCESSION);

    }

    @Test
    public void testDeleteNonExisting() {
        String blablaExperimentAccession = "E-MTAB-BLA-BLA-BLA";

        expect().body(is("ResourceNotFoundException: Experiment not found for experiment accession: " + blablaExperimentAccession))
                .when().get("deleteExperiment?accession=" + blablaExperimentAccession);
    }

    @Test
    public void loadShouldFailWhenExperimentHasAlreadyBeenImported() {
        expect().body(is("IllegalStateException: Experiment with experimentAccession " + EXPERIMENT_ACCESSION + " has been already imported."))
                .when().get("loadExperiment?accession=" + EXPERIMENT_ACCESSION + "&type=BASELINE");
    }

    @Test
    public void testLoadInvalidExperiment() {
        String blablaExperimentAccession = "E-MTAB-BLA-BLA-BLA";

        expect().body(startsWith("IllegalStateException: Required file can not be read"))
                .when().get("loadExperiment?accession=" + blablaExperimentAccession + "&type=BASELINE");

        expect().body("experimentAccession", is(empty())).when().get("listExperiments?accession=" + blablaExperimentAccession);

    }

    @Test
    public void testUpdate(){

        expect().body(is("Experiment E-MTAB-599 successfully updated.")).when()
                .get("updateExperiment?accession=" + EXPERIMENT_ACCESSION + "&private=true");

        expect().body("experimentAccession", contains(EXPERIMENT_ACCESSION))
                .and().body("isPrivate", contains(true))
                .when().get("listExperiments?accession=" + EXPERIMENT_ACCESSION);

        expect().body(is("Experiment E-MTAB-599 successfully updated.")).when()
                .get("updateExperiment?accession=" + EXPERIMENT_ACCESSION + "&private=false");

        expect().body("experimentAccession", contains(EXPERIMENT_ACCESSION))
                .and().body("isPrivate", contains(false))
                .when().get("listExperiments?accession=" + EXPERIMENT_ACCESSION);

    }

}<commit_msg>Extended REST IT tests to include assertions on conditionsIndex being updated on update, create and delete of experiment<commit_after>/*
 * Copyright 2008-2013 Microarray Informatics Team, EMBL-European Bioinformatics Institute
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 *
 * For further details of the Gene Expression Atlas project, including source code,
 * downloads and documentation, please see:
 *
 * http://gxa.github.com/gxa
 */

package uk.ac.ebi.atlas.acceptance.rest.tests;

import com.jayway.restassured.RestAssured;
import com.jayway.restassured.response.Response;
import org.apache.commons.lang.StringUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import uk.ac.ebi.atlas.acceptance.rest.fixtures.RestAssuredAuthenticatedFixture;

import static com.jayway.restassured.RestAssured.expect;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.assertThat;

public class ExperimentLoaderIT extends RestAssuredAuthenticatedFixture {

    private static final String EXPERIMENT_ACCESSION = "E-MTAB-599";
    private static final String DIFFERENTIAL_EXPERIMENT_ACCESSION = "E-GEOD-21860";

    @Before
    public void init(){

    }

    @After
    public void cleanup() {
        expect().body(containsString(EXPERIMENT_ACCESSION)).
            when().get("updateExperiment?accession=" + EXPERIMENT_ACCESSION + "&private=false");
    }

    @Test
    public void testListExperiments() {
        expect().body(containsString(EXPERIMENT_ACCESSION)).when().get("listExperiments?accession=" + EXPERIMENT_ACCESSION);
    }

    @Test
    public void testListSelectedExperiments() {

        expect().body("experimentAccession", containsInAnyOrder(EXPERIMENT_ACCESSION, "E-MTAB-513")).when()
                .get("listExperiments?accession=" + EXPERIMENT_ACCESSION + ",E-MTAB-513");

    }

    @Test
    public void testDeleteAndLoadBaselineExperimentPublic() {

        expect().body(is("Experiment E-MTAB-599 successfully deleted.")).when()
                .get("deleteExperiment?accession=" + EXPERIMENT_ACCESSION);

        expect().body("experimentAccession", is(empty())).when().get("listExperiments?accession=" + EXPERIMENT_ACCESSION);

        expect().body(startsWith("Experiment " + EXPERIMENT_ACCESSION + " loaded, accessKey:")).when()
                .get("loadExperiment?accession=" + EXPERIMENT_ACCESSION + "&type=BASELINE&private=false");

        expect().body("experimentAccession", hasItem(EXPERIMENT_ACCESSION))
                .and().body("isPrivate", contains(false))
                .when().get("listExperiments?accession=" + EXPERIMENT_ACCESSION);

    }

    @Test
    public void testDeleteAndLoadBaselineExperimentPrivate() {

        expect().body(is("Experiment E-MTAB-599 successfully deleted.")).when()
                .get("deleteExperiment?accession=" + EXPERIMENT_ACCESSION);

        expect().body("experimentAccession", is(empty())).when().get("listExperiments?accession=" + EXPERIMENT_ACCESSION);

        expect().body(startsWith("Experiment " + EXPERIMENT_ACCESSION + " loaded, accessKey:")).when()
                .get("loadExperiment?accession=" + EXPERIMENT_ACCESSION + "&type=BASELINE&private=true");

        expect().body("experimentAccession", hasItem(EXPERIMENT_ACCESSION))
                .and().body("isPrivate", contains(true))
                .when().get("listExperiments?accession=" + EXPERIMENT_ACCESSION);

    }

    @Test
    public void testDeleteNonExisting() {
        String blablaExperimentAccession = "E-MTAB-BLA-BLA-BLA";

        expect().body(is("ResourceNotFoundException: Experiment not found for experiment accession: " + blablaExperimentAccession))
                .when().get("deleteExperiment?accession=" + blablaExperimentAccession);
    }

    @Test
    public void loadShouldFailWhenExperimentHasAlreadyBeenImported() {
        expect().body(is("IllegalStateException: Experiment with experimentAccession " + EXPERIMENT_ACCESSION + " has been already imported."))
                .when().get("loadExperiment?accession=" + EXPERIMENT_ACCESSION + "&type=BASELINE");
    }

    @Test
    public void testLoadInvalidExperiment() {
        String blablaExperimentAccession = "E-MTAB-BLA-BLA-BLA";

        expect().body(startsWith("IllegalStateException: Required file can not be read"))
                .when().get("loadExperiment?accession=" + blablaExperimentAccession + "&type=BASELINE");

        expect().body("experimentAccession", is(empty())).when().get("listExperiments?accession=" + blablaExperimentAccession);

    }

    @Test
    public void testUpdateDifferentialExperiment(){

        expect().body(is("Experiment E-GEOD-21860 successfully updated.")).when()
                .get("updateExperiment?accession=" + DIFFERENTIAL_EXPERIMENT_ACCESSION + "&private=true");

        expect().body("experimentAccession", contains(DIFFERENTIAL_EXPERIMENT_ACCESSION))
                .and().body("isPrivate", contains(true))
                .when().get("listExperiments?accession=" + DIFFERENTIAL_EXPERIMENT_ACCESSION);

        assertThat(countConditionProperties(DIFFERENTIAL_EXPERIMENT_ACCESSION), is(0));


        expect().body(is("Experiment E-GEOD-21860 successfully updated.")).when()
                .get("updateExperiment?accession=" + DIFFERENTIAL_EXPERIMENT_ACCESSION + "&private=false");

        expect().body("experimentAccession", contains(DIFFERENTIAL_EXPERIMENT_ACCESSION))
                .and().body("isPrivate", contains(false))
                .when().get("listExperiments?accession=" + DIFFERENTIAL_EXPERIMENT_ACCESSION);

        assertThat(countConditionProperties(DIFFERENTIAL_EXPERIMENT_ACCESSION), is(4));


    }

    @Test
    public void testDeleteAndLoadDifferentialExperimentPublic() {

        expect().body(is("Experiment E-GEOD-21860 successfully deleted.")).when()
                .get("deleteExperiment?accession=" + DIFFERENTIAL_EXPERIMENT_ACCESSION);

        expect().body("experimentAccession", is(empty())).when().get("listExperiments?accession=" + DIFFERENTIAL_EXPERIMENT_ACCESSION);

        assertThat(countConditionProperties(DIFFERENTIAL_EXPERIMENT_ACCESSION), is(0));

        expect().body(startsWith("Experiment " + DIFFERENTIAL_EXPERIMENT_ACCESSION + " loaded, accessKey:")).when()
                .get("loadExperiment?accession=" + DIFFERENTIAL_EXPERIMENT_ACCESSION + "&type=DIFFERENTIAL&private=false");

        expect().body("experimentAccession", hasItem(DIFFERENTIAL_EXPERIMENT_ACCESSION))
                .and().body("isPrivate", contains(false))
                .when().get("listExperiments?accession=" + DIFFERENTIAL_EXPERIMENT_ACCESSION);

        assertThat(countConditionProperties(DIFFERENTIAL_EXPERIMENT_ACCESSION), is(4));

    }

    private int countConditionProperties(String experimentAccession){
        Response conditionIndexResponse = queryConditionIndex(experimentAccession);

        String responseBody = conditionIndexResponse.body().asString();

        return Integer.parseInt(StringUtils.substringBetween(responseBody, "\"numFound\":", ","));
    }

    private Response queryConditionIndex(String experimentAccession){
        return RestAssured.get("http://lime:8983/solr/conditions/select?q=experiment_accession:"
                + experimentAccession + "&wt=json&indent=true");
    }


}
{'fraction_non_alphanumeric': 0.12428700090063044, 'fraction_numerical': 0.006604623236265386, 'mean_word_length': 3.9169741697416973, 'pattern_counts': {'":': 1, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5399154', 'n_tokens_mistral': 3918, 'n_tokens_neox': 3706, 'n_words': 677}