/* (c) Copyright 2017, tranSMART Foundation, Inc. */

package tests.rest.v1

import annotations.RequiresStudy
import annotations.RequiresV1ApiSupport
import base.RESTSpec

import static base.ContentTypeFor.JSON
import static config.Config.GSE8581_ID
import static config.Config.V1_PATH_STUDIES

@RequiresV1ApiSupport(true)
@RequiresStudy(GSE8581_ID)
class StudySpec extends RESTSpec {

    /**
     *  given: "several studies are loaded"
     *  when: "I request all studies"
     *  then: "I get all studies I have access to"
     */
    def "v1 all studies"() {
        given: "several studies are loaded"

        when: "I request all studies"
        def responseData = get([
                path      : V1_PATH_STUDIES,
                acceptType: JSON
        ])

        then: "I get several studies"
        def studies = responseData.studies as List
        def studyIds = studies*.id as List

        assert studyIds.contains(GSE8581_ID)
        responseData.studies.each {
            assert it.ontologyTerm != null
        }
    }

    /**
     *  given: "a study with id EHR is loaded"
     *  when: "I request studies with id EHR"
     *  then: "only the EHR study is returned"
     */
    def "v1 single study"() {
        given: "study EHR is loaded"
        def studieId = GSE8581_ID

        when: "I request studies with id EHR"
        def responseData = get([
                path      : V1_PATH_STUDIES + "/${studieId}",
                acceptType: JSON
        ])

        then: "only the EHR study is returned"
        assert responseData.id == GSE8581_ID
        assert responseData.ontologyTerm.fullName == "\\Public Studies\\${studieId}\\"
        assert responseData.ontologyTerm.key == "\\\\Public Studies\\Public Studies\\${studieId}\\"
        assert responseData.ontologyTerm.name == studieId
        assert responseData.ontologyTerm.type == 'STUDY'
    }
}
