Commit 4bac1aff authored by Noel Alonso's avatar Noel Alonso
Browse files

Elimina util y test de series temporales

parent 494b3eb1
Loading
Loading
Loading
Loading
+0 −123
Original line number Diff line number Diff line
package es.redmic.es.common.queryFactory.series;

/*-
 * #%L
 * ElasticSearch
 * %%
 * Copyright (C) 2019 REDMIC Project / Server
 * %%
 * 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.
 * #L%
 */

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.join.query.JoinQueryBuilders;
import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramInterval;

import es.redmic.es.common.queryFactory.geodata.GeoDataQueryUtils;
import es.redmic.exception.elasticsearch.ESHistogramIntervalQueryException;
import es.redmic.models.es.common.query.dto.GeoDataQueryDTO;

public abstract class SeriesQueryUtils extends GeoDataQueryUtils {

	// @FORMATTER:OFF

	public static final BoolQueryBuilder INTERNAL_QUERY = null;

	public static final String DEFAULT_FIELD = "value";
	public static final String DATETIME_FIELD = "date";
	public static final String PARENT = "geodata";
	public static final String GRANDPARENT = "activity";

	// @FORMATTER:ON

	public static BoolQueryBuilder getQuery(GeoDataQueryDTO queryDTO, QueryBuilder internalQuery,
			QueryBuilder partialQuery) {
		return getSeriesQuery(queryDTO, internalQuery, partialQuery);
	}

	protected static BoolQueryBuilder getSeriesQuery(GeoDataQueryDTO queryDTO, QueryBuilder internalQuery,
			QueryBuilder partialQuery) {

		BoolQueryBuilder query = getOrInitializeBaseQuery(getBaseQuery(queryDTO, internalQuery, partialQuery));

		addMustTermIfExist(query, getFlagQuery(queryDTO.getQFlags(), QFLAG_PROPERTY));
		addMustTermIfExist(query, getFlagQuery(queryDTO.getVFlags(), VFLAG_PROPERTY));
		addMustTermIfExist(query, getDateLimitsQuery(queryDTO.getDateLimits(), DATETIME_FIELD));
		addMustTermIfExist(query, getValueQuery(queryDTO.getValue(), VALUE_PROPERTY));
		addMustTermIfExist(query, getZQuery(Z_PROPERTY, SEARCH_BY_Z_RANGE_SCRIPT, queryDTO.getZ()));

		return getResultQuery(query);
	}

	public static BoolQueryBuilder getItemsQuery(String id, String parentId, String grandparentId,
			List<Long> accessibilityIds) {

		ArrayList<String> ids = new ArrayList<>();
		ids.add(id);
		return getItemsQuery(ids, parentId, grandparentId, accessibilityIds);
	}

	public static BoolQueryBuilder getItemsQuery(List<String> ids, String parentId, String grandparentId,
			List<Long> accessibilityIds) {

		BoolQueryBuilder query = QueryBuilders.boolQuery();

		if (accessibilityIds != null && !accessibilityIds.isEmpty() && grandparentId != null) {
			//TODO: Comprobar accessibilidad de actividades
		}

		if (grandparentId != null) {
			query.must(getQueryByActivity(grandparentId));
		}

		query.must(QueryBuilders.idsQuery().addIds(ids.toArray(new String[ids.size()])));

		return query;
	}

	public static DateHistogramInterval getInterval(String interval) {

		if (!interval.matches("^\\d+[smhdwMqy]$"))
			throw new ESHistogramIntervalQueryException(interval);
		return new DateHistogramInterval(interval);
	}

	public static QueryBuilder getHierarchicalQuery(GeoDataQueryDTO queryDTO, String parentId, String grandparentId) {

		// TODO: Comprobar accessibilidad de actividades
		return getQueryByActivity(grandparentId);
	}

	public static QueryBuilder getQueryByActivity (String activityId) {

		if (activityId == null)
			return null;

		return QueryBuilders.termQuery("activityId", activityId);
	}

	public static Set<String> getFieldsExcludedOnQuery() {

		HashSet<String> fieldsExcludedOnQuery = new HashSet<>();
		fieldsExcludedOnQuery.add(ZRANGE_QUERY_FIELD);
		return fieldsExcludedOnQuery;
	}
}
+0 −121
Original line number Diff line number Diff line
package es.redmic.test.unit.queryFactory.series;

/*-
 * #%L
 * ElasticSearch
 * %%
 * Copyright (C) 2019 REDMIC Project / Server
 * %%
 * 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.
 * #L%
 */

import java.io.IOException;

import org.elasticsearch.index.query.BoolQueryBuilder;
import org.json.JSONException;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.junit.MockitoJUnitRunner;
import org.skyscreamer.jsonassert.JSONAssert;

import es.redmic.es.common.queryFactory.series.SeriesQueryUtils;
import es.redmic.test.unit.queryFactory.common.GeoDataQueryTest;

@RunWith(MockitoJUnitRunner.class)
public class SeriesQueryTest extends GeoDataQueryTest {

	protected String parentId = "AS65565sWEWEsd";
	protected String grandparentId = "239";

	@Test
	public void getQuery_ReturnDefaultQuery_IfIsDefaultQueryDTO() throws IOException, JSONException {

		BoolQueryBuilder query = SeriesQueryUtils.getQuery(geoDataQueryDTO, SeriesQueryUtils.INTERNAL_QUERY,
				SeriesQueryUtils.getHierarchicalQuery(geoDataQueryDTO, parentId, grandparentId));

		String queryExpected = getExpectedQuery("/queryfactory/series/internalQuery.json");

		JSONAssert.assertEquals(queryExpected, query.toString(), false);
	}

	@Test
	public void getQuery_ReturnDateLimitsQuery_IfQueryDTOHasDateLimisQueryDTO() throws IOException, JSONException {

		createDateLimitsQuery();

		BoolQueryBuilder query = SeriesQueryUtils.getQuery(geoDataQueryDTO, SeriesQueryUtils.INTERNAL_QUERY,
				SeriesQueryUtils.getHierarchicalQuery(geoDataQueryDTO, parentId, grandparentId));

		String queryExpected = getExpectedQuery("/queryfactory/series/dateLimitsQuery.json");

		JSONAssert.assertEquals(queryExpected, query.toString(), false);
	}

	@Test
	public void getQuery_ReturnFlagsQuery_IfQueryDTOHasFlagsQueryDTO() throws IOException, JSONException {

		createFlagsQuery();

		BoolQueryBuilder query = SeriesQueryUtils.getQuery(geoDataQueryDTO, SeriesQueryUtils.INTERNAL_QUERY,
				SeriesQueryUtils.getHierarchicalQuery(geoDataQueryDTO, parentId, grandparentId));

		String queryExpected = getExpectedQuery("/queryfactory/series/flagsQuery.json");

		JSONAssert.assertEquals(queryExpected, query.toString(), false);
	}

	@Test
	public void getQuery_ReturnValueQuery_IfQueryDTOHasValueQueryDTO() throws IOException, JSONException {

		createValueQuery();

		BoolQueryBuilder query = SeriesQueryUtils.getQuery(geoDataQueryDTO, SeriesQueryUtils.INTERNAL_QUERY,
				SeriesQueryUtils.getHierarchicalQuery(geoDataQueryDTO, parentId, grandparentId));

		String queryExpected = getExpectedQuery("/queryfactory/series/valueQuery.json");

		JSONAssert.assertEquals(queryExpected, query.toString(), false);
	}

	@Test
	public void getQuery_ReturnZRangeQuery_IfQueryDTOHasZRangeQueryDTO() throws IOException, JSONException {

		createZRangeQuery();

		BoolQueryBuilder query = SeriesQueryUtils.getQuery(geoDataQueryDTO, SeriesQueryUtils.INTERNAL_QUERY,
				SeriesQueryUtils.getHierarchicalQuery(geoDataQueryDTO, parentId, grandparentId));

		String queryExpected = getExpectedQuery("/queryfactory/series/zRangeQuery.json");

		System.out.println(queryExpected);
		System.out.println(query.toString());

		JSONAssert.assertEquals(queryExpected, query.toString(), false);
	}

	@Test
	public void getQuery_ReturnComplexQuery_IfQueryDTOIsFull() throws IOException, JSONException {

		createDateLimitsQuery();
		createFlagsQuery();
		createValueQuery();
		createZRangeQuery();

		BoolQueryBuilder query = SeriesQueryUtils.getQuery(geoDataQueryDTO, null, null);

		String queryExpected = getExpectedQuery("/queryfactory/series/fullQuery.json");

		JSONAssert.assertEquals(queryExpected, query.toString(), false);
	}
}