<commit_before>package com.amee.integration

import groovyx.net.http.HttpResponseException
import org.junit.Test
import static groovyx.net.http.ContentType.JSON
import static groovyx.net.http.ContentType.XML
import static org.junit.Assert.*
import static org.restlet.data.Status.*

/**
 * Tests for the Search API.
 */
class SearchIT extends BaseApiTest {

    /**
     * Search using JSON response.
     *
     * Supported URL parameters are:
     * <ul>
     *     <li>q: the lucene query string (see searchable fields below)</li>
     *     <li>types: comma separated list of the types of entity to return. [DC, DI]</li>
     *     <li>tags: only include results with the given tags</li>
     *     <li>excTags: do not include results with the given tags</li>
     * </ul>
     *
     * Searchable fields are:
     * <ul>
     *     <li>name</li>
     *     <li>wikiName</li>
     *     <li>path</li>
     *     <li>provenance</li>
     *     <li>authority</li>
     *     <li>wikiDoc</li>
     *     <li>itemDefinitionName</li>
     *     <li>label</li>
     *     <li>tags</li>
     * </ul>
     */
    @Test
    void searchJson() {
        com.amee.integration.BaseApiTest.versions.each { version -> searchJson(version) }
    }

    def searchJson(version) {
        client.contentType = JSON
        def response = client.get(
                path: "/${version}/search",
                query: ['q': 'water', 'types': 'DC'])
        assertEquals SUCCESS_OK.code, response.status
        assertEquals 'application/json', response.contentType
        assertTrue response.data instanceof net.sf.json.JSON
        assertEquals 'OK', response.data.status
        assertEquals 2, response.data.results.size()
    }

    /**
     * Search using JSON response, excluding a tag.
     */
    @Test
    void searchJsonWithExcTags() {
        com.amee.integration.BaseApiTest.versions.each { version -> searchJsonWithExcTags(version) }
    }

    def searchJsonWithExcTags(version) {
        if (version >= 3.2) {
            client.contentType = JSON
            def response = client.get(
                    path: "/${version}/search",
                    query: ['q': 'water', 'excTags': 'ecoinvent', 'types': 'DC'])
            assertEquals SUCCESS_OK.code, response.status
            assertEquals 'application/json', response.contentType
            assertTrue response.data instanceof net.sf.json.JSON
            assertEquals 'OK', response.data.status
            assertEquals 1, response.data.results.size()
            assertFalse response.data.results.first().wikiName.toString().startsWith('Ecoinvent')
        }
    }

    /**
     * Search using JSON response for a particular tag.
     */
    @Test
    void searchJsonWithTags() {
        com.amee.integration.BaseApiTest.versions.each { version -> searchJsonWithTags(version) }
    }

    def searchJsonWithTags(version) {
        if (version >= 3.2) {
            client.contentType = JSON
            def response = client.get(
                    path: "/${version}/search",
                    query: ['q': 'water', 'tags': 'ecoinvent', 'types': 'DC'])
            assertEquals SUCCESS_OK.code, response.status
            assertEquals 'application/json', response.contentType
            assertTrue response.data instanceof net.sf.json.JSON
            assertEquals 'OK', response.data.status
            assertEquals 1, response.data.results.size()
            assertTrue response.data.results.first().wikiName.toString().startsWith('Ecoinvent')
        }
    }

    /**
     * Search (for DataItems only) using XML response.
     */
    @Test
    void searchXml() {
        com.amee.integration.BaseApiTest.versions.each { version -> searchXml(version) }
    }

    def searchXml(version) {
        client.contentType = XML
        def response = client.get(
                path: "/${version}/search",
                query: ['q': 'water', 'types': 'DI'])<commit_msg>Add type to search integration test. PL-11243.<commit_after>package com.amee.integration

import groovyx.net.http.HttpResponseException
import org.junit.Test
import static groovyx.net.http.ContentType.JSON
import static groovyx.net.http.ContentType.XML
import static org.junit.Assert.*
import static org.restlet.data.Status.*

/**
 * Tests for the Search API.
 */
class SearchIT extends BaseApiTest {

    /**
     * Search using JSON response.
     *
     * Supported URL parameters are:
     * <ul>
     *     <li>q: the lucene query string (see searchable fields below)</li>
     *     <li>types: comma separated list of the types of entity to return. [DC, DI]</li>
     *     <li>tags: only include results with the given tags</li>
     *     <li>excTags: do not include results with the given tags</li>
     * </ul>
     *
     * Searchable fields are:
     * <ul>
     *     <li>name</li>
     *     <li>wikiName</li>
     *     <li>path</li>
     *     <li>provenance</li>
     *     <li>authority</li>
     *     <li>wikiDoc</li>
     *     <li>itemDefinitionName</li>
     *     <li>label</li>
     *     <li>tags</li>
     * </ul>
     */
    @Test
    void searchJson() {
        com.amee.integration.BaseApiTest.versions.each { version -> searchJson(version) }
    }

    def searchJson(version) {
        client.contentType = JSON
        def response = client.get(
                path: "/${version}/search",
                query: ['q': 'water', 'types': 'DC'])
        assertEquals SUCCESS_OK.code, response.status
        assertEquals 'application/json', response.contentType
        assertTrue response.data instanceof net.sf.json.JSON
        assertEquals 'OK', response.data.status
        assertEquals 2, response.data.results.size()
        assert Collections.nCopies(2, 'category') == response.data.results.collect { it.type }
    }

    /**
     * Search using JSON response, excluding a tag.
     */
    @Test
    void searchJsonWithExcTags() {
        com.amee.integration.BaseApiTest.versions.each { version -> searchJsonWithExcTags(version) }
    }

    def searchJsonWithExcTags(version) {
        if (version >= 3.2) {
            client.contentType = JSON
            def response = client.get(
                    path: "/${version}/search",
                    query: ['q': 'water', 'excTags': 'ecoinvent', 'types': 'DC'])
            assertEquals SUCCESS_OK.code, response.status
            assertEquals 'application/json', response.contentType
            assertTrue response.data instanceof net.sf.json.JSON
            assertEquals 'OK', response.data.status
            assertEquals 1, response.data.results.size()
            assertFalse response.data.results.first().wikiName.toString().startsWith('Ecoinvent')
            assertEquals 'category', response.data.results.first().type
        }
    }

    /**
     * Search using JSON response for a particular tag.
     */
    @Test
    void searchJsonWithTags() {
        com.amee.integration.BaseApiTest.versions.each { version -> searchJsonWithTags(version) }
    }

    def searchJsonWithTags(version) {
        if (version >= 3.2) {
            client.contentType = JSON
            def response = client.get(
                    path: "/${version}/search",
                    query: ['q': 'water', 'tags': 'ecoinvent', 'types': 'DC'])
            assertEquals SUCCESS_OK.code, response.status
            assertEquals 'application/json', response.contentType
            assertTrue response.data instanceof net.sf.json.JSON
            assertEquals 'OK', response.data.status
            assertEquals 1, response.data.results.size()
            assertTrue response.data.results.first().wikiName.toString().startsWith('Ecoinvent')
            assertEquals 'category', response.data.results.first().type
        }
    }

    /**
     * Search (for DataItems only) using XML response.
     */
    @Test
    void searchXml() {
        com.amee.integration.BaseApiTest.versions.each { version -> searchXml(version) }
    }

    def searchXml(version) {
        client.contentType = XML
        def response = client.get(
                path: "/${version}/search",
                query: ['q': 'water', 'types': 'DI'])
{'fraction_non_alphanumeric': 0.1250613647520864, 'fraction_numerical': 0.002454590083456063, 'mean_word_length': 2.172051381860646, 'pattern_counts': {'":': 0, '<': 63, '<?xml version=': 0, '>': 75, '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': '4627289', 'n_tokens_mistral': 2323, 'n_tokens_neox': 2269, 'n_words': 524}