<commit_before>package datasearch

import groovymvc.Controller
import org.openforis.sepal.component.datasearch.LatLng
import org.openforis.sepal.component.datasearch.Polygon
import org.openforis.sepal.component.datasearch.SceneArea
import org.openforis.sepal.component.datasearch.SceneMetaData
import org.openforis.sepal.component.datasearch.api.GoogleEarthEngineGateway
import org.openforis.sepal.component.datasearch.api.SceneQuery
import org.openforis.sepal.component.datasearch.endpoint.DataSearchEndpoint
import org.openforis.sepal.component.datasearch.query.FindBestScenes
import org.openforis.sepal.component.datasearch.query.FindSceneAreasForAoi
import org.openforis.sepal.component.datasearch.query.FindScenesForSceneArea
import org.openforis.sepal.util.DateTime
import util.AbstractEndpointTest

import static org.openforis.sepal.component.datasearch.MetaDataSource.USGS
import static org.openforis.sepal.util.DateTime.parseDateString
import static org.openforis.sepal.util.DateTime.toDateTimeString

@SuppressWarnings("GroovyAssignabilityCheck")
class DataSearchEndpointTest extends AbstractEndpointTest {
    def geeGateway = Mock GoogleEarthEngineGateway

    void registerEndpoint(Controller controller) {
        new DataSearchEndpoint(queryDispatcher, commandDispatcher, geeGateway).registerWith(controller)
    }

    def 'GET /data/sceneareas/?countryIso= returns sceneareas'() {
        when:
        def response = get(path: 'data/sceneareas', query: [countryIso: 'aa'])

        then:
        1 * queryDispatcher.submit({ it.aoi.keyValue == 'aa' } as FindSceneAreasForAoi) >> [
                new SceneArea(
                        id: 'scene area id',
                        polygon: new Polygon([new LatLng(1d, 1d), new LatLng(2d, 2d), new LatLng(3d, 3d), new LatLng(1d, 1d)]))
        ]
        sameJson(response.data, [
                [
                        sceneAreaId: 'scene area id',
                        polygon    : [[1d, 1d], [2d, 2d], [3d, 3d], [1d, 1d]]
                ]
        ])
        response.status == 200
    }

    def 'GET /data/sceneareas/{sceneAreaId} returns scenes'() {
        def query = [fromDate: '2016-01-01', toDate: '2016-02-01', targetDayOfYear: 365]
        def expectedSceneQuery = new SceneQuery(
                sceneAreaId: 'someSceneAreaId',
                fromDate: parseDateString(query.fromDate),
                toDate: parseDateString(query.toDate),
                targetDayOfYear: query.targetDayOfYear
        )
        def expectedScene = scene(expectedSceneQuery.fromDate)

        when:
        def response = get(path: 'data/sceneareas/someSceneAreaId', query: query)

        then:
        1 * queryDispatcher.submit({ it.sceneQuery == expectedSceneQuery } as FindScenesForSceneArea) >> [expectedScene]
        sameJson(response.data, [
                [
                        sceneId          : expectedScene.id,
                        sensor           : expectedScene.sensorId,
                        browseUrl        : expectedScene.browseUrl as String,
                        acquisitionDate  : DateTime.toDateString(expectedScene.acquisitionDate),
                        cloudCover       : expectedScene.cloudCover,
                        sunAzimuth       : expectedScene.sunAzimuth,
                        sunElevation     : expectedScene.sunElevation,
                        daysFromTargetDay: 1
                ]
        ])
        response.status == 200
    }

    def 'GET /data/best-scenes returns scenes'() {
        def expectedQuery = new FindBestScenes(
                sceneAreaIds: ['some-area', 'another-area'],
                sensorIds: ['some-sensor', 'another-sensor'],
                fromDate: parseDateString('2015-01-01'),
                toDate: parseDateString('2016-01-01'),
                targetDayOfYear: 22,
                targetDayOfYearWeight: 0.12,
                cloudCoverTarget: 0.001
        )
        def expectedScene = scene(parseDateString('2015-01-01'))

        when:
        def response = get(path: 'data/best-scenes', query: [
                sceneAreaIds         : 'some-area, another-area',
                sensorIds            : 'some-sensor, another-sensor',
                fromDate             : toDateTimeString(expectedQuery.fromDate),
                toDate               : toDateTimeString(expectedQuery.toDate),
                targetDayOfYear      : expectedQuery.targetDayOfYear,
                targetDayOfYearWeight: expectedQuery.targetDayOfYearWeight,
                cloudCoverTarget     : expectedQuery.cloudCoverTarget
        ])

        then:
        1 * queryDispatcher.submit(expectedQuery) >> [
                'some-area': [expectedScene]
        ]
        sameJson(response.data, [
                'some-area': [[
                                      sceneId          : expectedScene.id,
                                      sensor           : expectedScene.sensorId,
                                      browseUrl        : expectedScene.browseUrl as String,
                                      acquisitionDate  : DateTime.toDateString(expectedScene.acquisitionDate),
                                      cloudCover       : expectedScene.cloudCover,<commit_msg>Fixed broken test.<commit_after>package datasearch

import groovymvc.Controller
import org.openforis.sepal.component.datasearch.LatLng
import org.openforis.sepal.component.datasearch.Polygon
import org.openforis.sepal.component.datasearch.SceneArea
import org.openforis.sepal.component.datasearch.SceneMetaData
import org.openforis.sepal.component.datasearch.api.GoogleEarthEngineGateway
import org.openforis.sepal.component.datasearch.api.SceneQuery
import org.openforis.sepal.component.datasearch.endpoint.DataSearchEndpoint
import org.openforis.sepal.component.datasearch.query.FindBestScenes
import org.openforis.sepal.component.datasearch.query.FindSceneAreasForAoi
import org.openforis.sepal.component.datasearch.query.FindScenesForSceneArea
import org.openforis.sepal.util.DateTime
import util.AbstractEndpointTest

import static org.openforis.sepal.component.datasearch.MetaDataSource.USGS
import static org.openforis.sepal.util.DateTime.parseDateString
import static org.openforis.sepal.util.DateTime.toDateTimeString

@SuppressWarnings("GroovyAssignabilityCheck")
class DataSearchEndpointTest extends AbstractEndpointTest {
    def geeGateway = Mock GoogleEarthEngineGateway

    void registerEndpoint(Controller controller) {
        new DataSearchEndpoint(queryDispatcher, commandDispatcher, geeGateway).registerWith(controller)
    }

    def 'GET /data/sceneareas/?countryIso= returns sceneareas'() {
        when:
        def response = get(path: 'data/sceneareas', query: [countryIso: 'aa'])
        assert response.status == 200

        then:
        1 * queryDispatcher.submit({ it.aoi.keyValue == 'aa' } as FindSceneAreasForAoi) >> [
                new SceneArea(
                        id: 'scene area id',
                        polygon: new Polygon([new LatLng(1d, 1d), new LatLng(2d, 2d), new LatLng(3d, 3d), new LatLng(1d, 1d)]))
        ]
        sameJson(response.data, [
                [
                        sceneAreaId: 'scene area id',
                        polygon    : [[1d, 1d], [2d, 2d], [3d, 3d], [1d, 1d]]
                ]
        ])
        response.status == 200
    }

    def 'GET /data/sceneareas/{sceneAreaId} returns scenes'() {
        def query = [fromDate: '2016-01-01', toDate: '2016-02-01', targetDayOfYear: 365]
        def expectedSceneQuery = new SceneQuery(
                sceneAreaId: 'someSceneAreaId',
                fromDate: parseDateString(query.fromDate),
                toDate: parseDateString(query.toDate),
                targetDayOfYear: query.targetDayOfYear
        )
        def expectedScene = scene(expectedSceneQuery.fromDate)

        when:
        def response = get(path: 'data/sceneareas/someSceneAreaId', query: query)
        assert response.status == 200

        then:
        1 * queryDispatcher.submit({ it.sceneQuery == expectedSceneQuery } as FindScenesForSceneArea) >> [expectedScene]
        sameJson(response.data, [
                [
                        sceneId          : expectedScene.id,
                        sensor           : expectedScene.sensorId,
                        browseUrl        : expectedScene.browseUrl as String,
                        acquisitionDate  : DateTime.toDateString(expectedScene.acquisitionDate),
                        cloudCover       : expectedScene.cloudCover,
                        sunAzimuth       : expectedScene.sunAzimuth,
                        sunElevation     : expectedScene.sunElevation,
                        daysFromTargetDay: 1
                ]
        ])
        response.status == 200
    }

    def 'GET /data/best-scenes returns scenes'() {
        def expectedQuery = new FindBestScenes(
                sceneAreaIds: ['some-area', 'another-area'],
                sensorIds: ['some-sensor', 'another-sensor'],
                fromDate: parseDateString('2015-01-01'),
                toDate: parseDateString('2016-01-01'),
                targetDayOfYear: 22,
                targetDayOfYearWeight: 0.12,
                cloudCoverTarget: 0.001
        )
        def expectedScene = scene(parseDateString('2015-01-01'))

        when:
        def response = post(path: 'data/best-scenes', query: [
                sceneAreaIds         : 'some-area, another-area',
                sensorIds            : 'some-sensor, another-sensor',
                fromDate             : toDateTimeString(expectedQuery.fromDate),
                toDate               : toDateTimeString(expectedQuery.toDate),
                targetDayOfYear      : expectedQuery.targetDayOfYear,
                targetDayOfYearWeight: expectedQuery.targetDayOfYearWeight,
                cloudCoverTarget     : expectedQuery.cloudCoverTarget
        ])
        assert response.status == 200

        then:
        1 * queryDispatcher.submit(expectedQuery) >> [
                'some-area': [expectedScene]
        ]
        sameJson(response.data, [
                'some-area': [[
                                      sceneId          : expectedScene.id,
                                      sensor           : expectedScene.sensorId,
                                      browseUrl        : expectedScene.browseUrl as String,
                                      acquisitionDate  : DateTime.toDateString(expectedScene.acquisitionDate),
                                      cloudCover       : expectedScene.cloudCover,
{'fraction_non_alphanumeric': 0.08867924528301886, 'fraction_numerical': 0.015566037735849057, 'mean_word_length': 1.9904090267983074, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2407529', 'n_tokens_mistral': 3018, 'n_tokens_neox': 2766, 'n_words': 494}