diff --git a/legend-engine-application-query/src/main/java/org/finos/legend/engine/application/query/api/ApplicationQuery.java b/legend-engine-application-query/src/main/java/org/finos/legend/engine/application/query/api/ApplicationQuery.java index 26de6a5607a..a0bb0aee276 100644 --- a/legend-engine-application-query/src/main/java/org/finos/legend/engine/application/query/api/ApplicationQuery.java +++ b/legend-engine-application-query/src/main/java/org/finos/legend/engine/application/query/api/ApplicationQuery.java @@ -118,26 +118,6 @@ public Response getQuery(@PathParam("queryId") String queryId) } } - @GET - @Path("/stats") - @ApiOperation(value = "Get Query Store Stats") - @Consumes({MediaType.APPLICATION_JSON}) - public Response getQueryCount() - { - try - { - return Response.ok(this.queryStoreManager.getQueryStoreStats()).build(); - } - catch (Exception e) - { - if (e instanceof ApplicationQueryException) - { - return ((ApplicationQueryException) e).toResponse(); - } - return ExceptionTool.exceptionManager(e, LoggingEventType.GET_QUERY_STATS_ERROR, null); - } - } - @POST @ApiOperation(value = "Create a new query") @Consumes({MediaType.APPLICATION_JSON}) @@ -251,6 +231,27 @@ public Response getQueryEvents(@QueryParam("queryId") @ApiParam("The query ID th } } + @GET + @Path("/stats") + @ApiOperation(value = "Get query store statistics") + @Consumes({MediaType.APPLICATION_JSON}) + public Response getQueryStoreStats() + { + try + { + return Response.ok(this.queryStoreManager.getQueryStoreStats()).build(); + } + catch (Exception e) + { + if (e instanceof ApplicationQueryException) + { + return ((ApplicationQueryException) e).toResponse(); + } + return ExceptionTool.exceptionManager(e, LoggingEventType.GET_QUERY_STATS_ERROR, null); + } + } + + // --------------------------------------------- DataCube Query --------------------------------------------- @POST @Path("dataCube/search") @@ -378,4 +379,49 @@ public Response deleteDataCubeQuery(@PathParam("queryId") String queryId, @ApiPa return ExceptionTool.exceptionManager(e, LoggingEventType.DELETE_QUERY_ERROR, identity.getName()); } } + + @GET + @Path("dataCube/events") + @ApiOperation(value = "Get DataCube query events") + @Consumes({MediaType.APPLICATION_JSON}) + public Response getDataCubeQueryEvents(@QueryParam("queryId") @ApiParam("The query ID the event is associated with") String queryId, + @QueryParam("eventType") @ApiParam("The type of event") QueryEvent.QueryEventType eventType, + @QueryParam("since") @ApiParam("Lower limit on the UNIX timestamp for the event creation time") Long since, + @QueryParam("until") @ApiParam("Upper limit on the UNIX timestamp for the event creation time") Long until, + @QueryParam("limit") @ApiParam("Limit the number of events returned") Integer limit, + @ApiParam(hidden = true) @Pac4JProfileManager ProfileManager profileManager) + { + try + { + return Response.ok().entity(this.dataCubeQueryStoreManager.getQueryEvents(queryId, eventType, since, until, limit)).build(); + } + catch (Exception e) + { + if (e instanceof ApplicationQueryException) + { + return ((ApplicationQueryException) e).toResponse(); + } + return ExceptionTool.exceptionManager(e, LoggingEventType.GET_QUERY_EVENTS_ERROR, null); + } + } + + @GET + @Path("dataCube/stats") + @ApiOperation(value = "Get DataCube query store statistics") + @Consumes({MediaType.APPLICATION_JSON}) + public Response getDataCubeQueryStoreStats() + { + try + { + return Response.ok(this.dataCubeQueryStoreManager.getQueryStoreStats()).build(); + } + catch (Exception e) + { + if (e instanceof ApplicationQueryException) + { + return ((ApplicationQueryException) e).toResponse(); + } + return ExceptionTool.exceptionManager(e, LoggingEventType.GET_QUERY_STATS_ERROR, null); + } + } } diff --git a/legend-engine-application-query/src/main/java/org/finos/legend/engine/application/query/api/DataCubeQueryStoreManager.java b/legend-engine-application-query/src/main/java/org/finos/legend/engine/application/query/api/DataCubeQueryStoreManager.java index 0cee796f942..d32e97444ec 100644 --- a/legend-engine-application-query/src/main/java/org/finos/legend/engine/application/query/api/DataCubeQueryStoreManager.java +++ b/legend-engine-application-query/src/main/java/org/finos/legend/engine/application/query/api/DataCubeQueryStoreManager.java @@ -27,10 +27,7 @@ import org.eclipse.collections.api.list.MutableList; import org.eclipse.collections.api.set.sorted.MutableSortedSet; import org.eclipse.collections.impl.utility.LazyIterate; -import org.finos.legend.engine.application.query.model.DataCubeQuery; -import org.finos.legend.engine.application.query.model.QuerySearchSortBy; -import org.finos.legend.engine.application.query.model.QuerySearchSpecification; -import org.finos.legend.engine.application.query.model.QuerySearchTermSpecification; +import org.finos.legend.engine.application.query.model.*; import org.finos.legend.engine.protocol.pure.v1.model.context.EngineErrorType; import org.finos.legend.engine.shared.core.operational.errorManagement.EngineException; import org.finos.legend.engine.shared.core.vault.Vault; @@ -45,11 +42,12 @@ public class DataCubeQueryStoreManager { private static final int MAX_NUMBER_OF_QUERIES = 100; + private static final int MAX_NUMBER_OF_EVENTS = 1000; private final ObjectMapper objectMapper = new ObjectMapper(); private static final Document EMPTY_FILTER = Document.parse("{}"); - private static final List LIGHT_QUERY_PROJECTION = Arrays.asList("id", "name", "createdAt", "lastUpdatedAt", "lastOpenAt"); + private static final List LIGHT_QUERY_PROJECTION = Arrays.asList("id", "name", "owner", "createdAt", "lastUpdatedAt", "lastOpenAt"); private static final int GET_QUERIES_LIMIT = 50; private final MongoClient mongoClient; @@ -59,24 +57,33 @@ public DataCubeQueryStoreManager(MongoClient mongoClient) this.mongoClient = mongoClient; } - private MongoDatabase getDataCubeQueryDatabase() + private MongoDatabase getQueryDatabase() { if (Vault.INSTANCE.hasValue("query.mongo.database")) { return this.mongoClient.getDatabase(Vault.INSTANCE.getValue("query.mongo.database")); } - throw new RuntimeException("DataCube Query MongoDB database has not been configured properly"); + throw new RuntimeException("DataCube query MongoDB database has not been configured properly"); } private MongoCollection getQueryCollection() { if (Vault.INSTANCE.hasValue("query.mongo.collection.dataCube")) { - return this.getDataCubeQueryDatabase().getCollection(Vault.INSTANCE.getValue("query.mongo.collection.dataCube")); + return this.getQueryDatabase().getCollection(Vault.INSTANCE.getValue("query.mongo.collection.dataCube")); } throw new RuntimeException("DataCube Query MongoDB collection has not been configured properly"); } + private MongoCollection getQueryEventCollection() + { + if (Vault.INSTANCE.hasValue("query.mongo.collection.dataCubeEvent")) + { + return this.getQueryDatabase().getCollection(Vault.INSTANCE.getValue("query.mongo.collection.dataCubeEvent")); + } + throw new RuntimeException("Query event MongoDB collection has not been configured properly"); + } + private T documentToClass(Document document, Class _class) { try @@ -99,6 +106,36 @@ private Document queryToDocument(DataCubeQuery query) throws JsonProcessingExcep return Document.parse(objectMapper.writeValueAsString(query)); } + private static QueryEvent createEvent(String queryId, QueryEvent.QueryEventType eventType) + { + QueryEvent event = new QueryEvent(); + event.queryId = queryId; + event.timestamp = Instant.now().toEpochMilli(); + event.eventType = eventType; + return event; + } + + private static QueryEvent documentToQueryEvent(Document document) + { + QueryEvent event = new QueryEvent(); + event.queryId = document.getString("queryId"); + try + { + event.timestamp = document.getLong("timestamp"); + } + catch (ClassCastException e) + { + event.timestamp = Long.valueOf(document.getInteger("timestamp")); + } + event.eventType = QueryEvent.QueryEventType.valueOf(document.getString("eventType")); + return event; + } + + private Document queryEventToDocument(QueryEvent event) throws JsonProcessingException + { + return Document.parse(objectMapper.writeValueAsString((event))); + } + private static void validate(boolean predicate, String message) { if (!predicate) @@ -112,17 +149,11 @@ private static void validateNonEmptyQueryField(String fieldValue, String message validate(fieldValue != null && !fieldValue.isEmpty(), message); } - private static void validateNonNullQueryField(Object fieldValue, String message) - { - validate(fieldValue != null, message); - } - public static void validateQuery(DataCubeQuery query) { validateNonEmptyQueryField(query.id, "Query ID is missing or empty"); validateNonEmptyQueryField(query.name, "Query name is missing or empty"); - validateNonNullQueryField(query.query, "Query is missing"); - validateNonNullQueryField(query.source, "Query source is missing"); + validate(query.content != null, "Query content is missing"); } public List searchQueries(QuerySearchSpecification searchSpecification, String currentUser) @@ -138,6 +169,10 @@ public List searchQueries(QuerySearchSpecification searchSpecific if (querySearchTermSpecification.exactMatchName != null && querySearchTermSpecification.exactMatchName) { Bson filter = Filters.eq("name", querySearchTermSpecification.searchTerm); + if (querySearchTermSpecification.includeOwner != null && querySearchTermSpecification.includeOwner) + { + filter = Filters.or(filter, Filters.eq("owner", querySearchTermSpecification.searchTerm)); + } filters.add(filter); } else @@ -145,13 +180,19 @@ public List searchQueries(QuerySearchSpecification searchSpecific Bson idFilter = Filters.eq("id", querySearchTermSpecification.searchTerm); Bson nameFilter = Filters.regex("name", Pattern.quote(querySearchTermSpecification.searchTerm), "i"); Bson filter = Filters.or(idFilter, nameFilter); + if (querySearchTermSpecification.includeOwner != null && querySearchTermSpecification.includeOwner) + { + filter = Filters.or(idFilter, nameFilter, Filters.regex("owner", Pattern.quote(querySearchTermSpecification.searchTerm), "i")); + } filters.add(filter); } } List queries = new ArrayList<>(); List aggregateLists = new ArrayList<>(); + aggregateLists.add(Aggregates.addFields(new Field<>("isCurrentUser", new Document("$eq", Arrays.asList("$owner", currentUser))))); aggregateLists.add(Aggregates.match(filters.isEmpty() ? EMPTY_FILTER : Filters.and(filters))); + aggregateLists.add(Aggregates.sort(Sorts.descending("isCurrentUser"))); if (searchSpecification.sortByOption != null) { aggregateLists.add(Aggregates.sort(Sorts.descending(getSortByField(searchSpecification.sortByOption)))); @@ -224,7 +265,7 @@ else if (count == 0) return matchingQueries; } - public DataCubeQuery getQuery(String queryId) throws JsonProcessingException + public DataCubeQuery getQuery(String queryId) { List matchingQueries = LazyIterate.collect(this.getQueryCollection().find(Filters.eq("id", queryId)), this::documentToQuery).toList(); if (matchingQueries.size() > 1) @@ -248,7 +289,9 @@ else if (matchingQueries.isEmpty()) public DataCubeQuery createQuery(DataCubeQuery query, String currentUser) throws JsonProcessingException { validateQuery(query); - // TODO: store ownership information + + // Force the current user as owner regardless of user input + query.owner = currentUser; List matchingQueries = LazyIterate.collect(this.getQueryCollection().find(Filters.eq("id", query.id)), this::documentToQuery).toList(); if (!matchingQueries.isEmpty()) @@ -259,6 +302,9 @@ public DataCubeQuery createQuery(DataCubeQuery query, String currentUser) throws query.lastUpdatedAt = query.createdAt; query.lastOpenAt = query.createdAt; this.getQueryCollection().insertOne(queryToDocument(query)); + QueryEvent createdEvent = createEvent(query.id, QueryEvent.QueryEventType.CREATED); + createdEvent.timestamp = query.createdAt; + this.getQueryEventCollection().insertOne(queryEventToDocument(createdEvent)); return query; } @@ -281,11 +327,20 @@ else if (matchingQueries.isEmpty()) } DataCubeQuery currentQuery = matchingQueries.get(0); - // TODO: check ownership + // Make sure only the owner can update the query + // NOTE: if the query is created by an anonymous user previously, set the current user as the owner + if (currentQuery.owner != null && !currentQuery.owner.equals(currentUser)) + { + throw new ApplicationQueryException("Only owner can update the query", Response.Status.FORBIDDEN); + } + query.owner = currentUser; query.createdAt = currentQuery.createdAt; query.lastUpdatedAt = Instant.now().toEpochMilli(); query.lastOpenAt = Instant.now().toEpochMilli(); this.getQueryCollection().findOneAndReplace(Filters.eq("id", queryId), queryToDocument(query)); + QueryEvent updatedEvent = createEvent(query.id, QueryEvent.QueryEventType.UPDATED); + updatedEvent.timestamp = query.lastUpdatedAt; + this.getQueryEventCollection().insertOne(queryEventToDocument(updatedEvent)); return query; } @@ -302,7 +357,45 @@ else if (matchingQueries.isEmpty()) } DataCubeQuery currentQuery = matchingQueries.get(0); - // TODO: check ownership + // Make sure only the owner can delete the query + if (currentQuery.owner != null && !currentQuery.owner.equals(currentUser)) + { + throw new ApplicationQueryException("Only owner can delete the query", Response.Status.FORBIDDEN); + } this.getQueryCollection().findOneAndDelete(Filters.eq("id", queryId)); + this.getQueryEventCollection().insertOne(queryEventToDocument(createEvent(queryId, QueryEvent.QueryEventType.DELETED))); + } + + public List getQueryEvents(String queryId, QueryEvent.QueryEventType eventType, Long since, Long until, Integer limit) + { + List filters = new ArrayList<>(); + if (queryId != null) + { + filters.add(Filters.eq("queryId", queryId)); + } + if (eventType != null) + { + filters.add(Filters.eq("eventType", eventType.toString())); + } + if (since != null) + { + filters.add(Filters.gte("timestamp", since)); + } + if (until != null) + { + filters.add(Filters.lte("timestamp", until)); + } + return LazyIterate.collect(this.getQueryEventCollection() + .find(filters.isEmpty() ? EMPTY_FILTER : Filters.and(filters)) + .limit(Math.min(MAX_NUMBER_OF_EVENTS, limit == null ? Integer.MAX_VALUE : limit)), DataCubeQueryStoreManager::documentToQueryEvent).toList(); + } + + public QueryStoreStats getQueryStoreStats() + { + Long count = this.getQueryCollection().countDocuments(); + QueryStoreStats storeStats = new QueryStoreStats(); + storeStats.setQueryCount(count); + storeStats.setQueryCreatedFromDataSpaceCount(0L); + return storeStats; } } diff --git a/legend-engine-application-query/src/main/java/org/finos/legend/engine/application/query/api/QueryStoreManager.java b/legend-engine-application-query/src/main/java/org/finos/legend/engine/application/query/api/QueryStoreManager.java index f59227252b4..56b986c14c8 100644 --- a/legend-engine-application-query/src/main/java/org/finos/legend/engine/application/query/api/QueryStoreManager.java +++ b/legend-engine-application-query/src/main/java/org/finos/legend/engine/application/query/api/QueryStoreManager.java @@ -20,12 +20,7 @@ import com.mongodb.client.MongoClient; import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoDatabase; -import com.mongodb.client.model.Aggregates; -import com.mongodb.client.model.Field; -import com.mongodb.client.model.Filters; -import com.mongodb.client.model.Updates; -import com.mongodb.client.model.Projections; -import com.mongodb.client.model.Sorts; +import com.mongodb.client.model.*; import org.bson.Document; import org.bson.conversions.Bson; import org.eclipse.collections.api.factory.SortedSets; @@ -64,6 +59,7 @@ public class QueryStoreManager private static final String QUERY_PROFILE_TAG_DATA_SPACE = "dataSpace"; private static final List LIGHT_QUERY_PROJECTION = Arrays.asList("id", "name", "versionId", "originalVersionId", "groupId", "artifactId", "owner", "createdAt", "lastUpdatedAt", "lastOpenAt"); private static final int GET_QUERIES_LIMIT = 50; + private final MongoClient mongoClient; public QueryStoreManager(MongoClient mongoClient) @@ -207,13 +203,13 @@ public List searchQueries(QuerySearchSpecification searchSpecification, S Bson filter = Filters.eq("name", querySearchTermSpecification.searchTerm); if (querySearchTermSpecification.includeOwner != null && querySearchTermSpecification.includeOwner) { - filter = Filters.or(filter,Filters.eq("owner", querySearchTermSpecification.searchTerm)); + filter = Filters.or(filter, Filters.eq("owner", querySearchTermSpecification.searchTerm)); } filters.add(filter); } else { - Bson idFilter = Filters.eq("id", querySearchTermSpecification.searchTerm); + Bson idFilter = Filters.eq("id", querySearchTermSpecification.searchTerm); Bson nameFilter = Filters.regex("name", Pattern.quote(querySearchTermSpecification.searchTerm), "i"); Bson filter = Filters.or(idFilter, nameFilter); if (querySearchTermSpecification.includeOwner != null && querySearchTermSpecification.includeOwner) @@ -259,7 +255,7 @@ public List searchQueries(QuerySearchSpecification searchSpecification, S ? searchSpecification.taggedValues.stream() .filter(taggedValue -> QUERY_PROFILE_PATH.equals(taggedValue.tag.profile) && QUERY_PROFILE_TAG_DATA_SPACE.equals(taggedValue.tag.value)) - .map(taggedValue -> taggedValue.value) + .map(taggedValue -> taggedValue.value) .collect(Collectors.toList()) : Collections.emptyList(); @@ -322,7 +318,7 @@ public String getSortByField(QuerySearchSortBy sortBy) } default: { - throw new EngineException("Fail to understand sort-by value", EngineErrorType.COMPILATION); + throw new EngineException("Unknown sort-by value", EngineErrorType.COMPILATION); } } } @@ -349,25 +345,25 @@ else if (count == 0) notFoundQueries.add(queryId); } }); - if (duplicatedQueries.size() != 0) + if (!duplicatedQueries.isEmpty()) { throw new IllegalStateException(duplicatedQueries.makeString("Found multiple queries with duplicated ID for the following ID(s):\\n", "\\n", "")); } - if (notFoundQueries.size() != 0) + if (!notFoundQueries.isEmpty()) { throw new ApplicationQueryException(notFoundQueries.makeString("Can't find queries for the following ID(s):\\n", "\\n", ""), Response.Status.INTERNAL_SERVER_ERROR); } return matchingQueries; } - public Query getQuery(String queryId) throws JsonProcessingException + public Query getQuery(String queryId) { List matchingQueries = LazyIterate.collect(this.getQueryCollection().find(Filters.eq("id", queryId)), this::documentToQuery).toList(); if (matchingQueries.size() > 1) { throw new IllegalStateException("Found multiple queries with ID '" + queryId + "'"); } - else if (matchingQueries.size() == 0) + else if (matchingQueries.isEmpty()) { throw new ApplicationQueryException("Can't find query with ID '" + queryId + "'", Response.Status.NOT_FOUND); } @@ -380,19 +376,6 @@ else if (matchingQueries.size() == 0) return query; } - public QueryStoreStats getQueryStoreStats() throws JsonProcessingException - { - Long count = this.getQueryCollection().countDocuments(); - QueryStoreStats storeStats = new QueryStoreStats(); - storeStats.setQueryCount(count); - List filters = new ArrayList<>(); - filters.add(Filters.and(Filters.eq("taggedValues.tag.profile", QUERY_PROFILE_PATH), Filters.eq("taggedValues.tag.value", QUERY_PROFILE_TAG_DATA_SPACE))); - storeStats.setQueryCreatedFromDataSpaceCount(this.getQueryCollection() - .countDocuments(Filters.and(filters))); - return storeStats; - } - - public Query createQuery(Query query, String currentUser) throws JsonProcessingException { validateQuery(query); @@ -401,7 +384,7 @@ public Query createQuery(Query query, String currentUser) throws JsonProcessingE query.owner = currentUser; List matchingQueries = LazyIterate.collect(this.getQueryCollection().find(Filters.eq("id", query.id)), this::documentToQuery).toList(); - if (matchingQueries.size() >= 1) + if (!matchingQueries.isEmpty()) { throw new ApplicationQueryException("Query with ID '" + query.id + "' already existed", Response.Status.BAD_REQUEST); } @@ -428,7 +411,7 @@ public Query updateQuery(String queryId, Query query, String currentUser) throws { throw new IllegalStateException("Found multiple queries with ID '" + queryId + "'"); } - else if (matchingQueries.size() == 0) + else if (matchingQueries.isEmpty()) { throw new ApplicationQueryException("Can't find query with ID '" + queryId + "'", Response.Status.NOT_FOUND); } @@ -496,7 +479,7 @@ public void deleteQuery(String queryId, String currentUser) throws JsonProcessin { throw new IllegalStateException("Found multiple queries with ID '" + queryId + "'"); } - else if (matchingQueries.size() == 0) + else if (matchingQueries.isEmpty()) { throw new ApplicationQueryException("Can't find query with ID '" + queryId + "'", Response.Status.NOT_FOUND); } @@ -534,4 +517,16 @@ public List getQueryEvents(String queryId, QueryEvent.QueryEventType .find(filters.isEmpty() ? EMPTY_FILTER : Filters.and(filters)) .limit(Math.min(MAX_NUMBER_OF_EVENTS, limit == null ? Integer.MAX_VALUE : limit)), QueryStoreManager::documentToQueryEvent).toList(); } + + public QueryStoreStats getQueryStoreStats() + { + Long count = this.getQueryCollection().countDocuments(); + QueryStoreStats storeStats = new QueryStoreStats(); + storeStats.setQueryCount(count); + List filters = new ArrayList<>(); + filters.add(Filters.and(Filters.eq("taggedValues.tag.profile", QUERY_PROFILE_PATH), Filters.eq("taggedValues.tag.value", QUERY_PROFILE_TAG_DATA_SPACE))); + storeStats.setQueryCreatedFromDataSpaceCount(this.getQueryCollection() + .countDocuments(Filters.and(filters))); + return storeStats; + } } diff --git a/legend-engine-application-query/src/main/java/org/finos/legend/engine/application/query/model/DataCubeQuery.java b/legend-engine-application-query/src/main/java/org/finos/legend/engine/application/query/model/DataCubeQuery.java index dd277601d85..2b4ccfcd0f6 100644 --- a/legend-engine-application-query/src/main/java/org/finos/legend/engine/application/query/model/DataCubeQuery.java +++ b/legend-engine-application-query/src/main/java/org/finos/legend/engine/application/query/model/DataCubeQuery.java @@ -25,12 +25,13 @@ public class DataCubeQuery public String name; public String description; - public Map query; - public Map source; + public Map content; public Long lastUpdatedAt; public Long createdAt; public Long lastOpenAt; - // TODO: ownership + // TODO: this is a very simple ownership mechanism + // where we only allow a single owner + public String owner; } diff --git a/legend-engine-application-query/src/main/java/org/finos/legend/engine/application/query/model/Query.java b/legend-engine-application-query/src/main/java/org/finos/legend/engine/application/query/model/Query.java index 2e2c0e50fae..a42ca66d01d 100644 --- a/legend-engine-application-query/src/main/java/org/finos/legend/engine/application/query/model/Query.java +++ b/legend-engine-application-query/src/main/java/org/finos/legend/engine/application/query/model/Query.java @@ -46,7 +46,8 @@ public class Query public List defaultParameterValues; - // We make it clear that we only allow a single owner + // TODO: this is a very simple ownership mechanism + // where we only allow a single owner public String owner; public Map gridConfig; diff --git a/legend-engine-application-query/src/test/java/org/finos/legend/engine/application/query/api/TestDataCubeQueryStoreManager.java b/legend-engine-application-query/src/test/java/org/finos/legend/engine/application/query/api/TestDataCubeQueryStoreManager.java index 5660f64c7a5..4ace791080e 100644 --- a/legend-engine-application-query/src/test/java/org/finos/legend/engine/application/query/api/TestDataCubeQueryStoreManager.java +++ b/legend-engine-application-query/src/test/java/org/finos/legend/engine/application/query/api/TestDataCubeQueryStoreManager.java @@ -17,15 +17,13 @@ import org.eclipse.collections.api.block.function.Function0; import org.eclipse.collections.api.factory.Lists; import org.eclipse.collections.api.factory.Maps; -import org.finos.legend.engine.application.query.model.DataCubeQuery; -import org.finos.legend.engine.application.query.model.QuerySearchSortBy; -import org.finos.legend.engine.application.query.model.QuerySearchSpecification; -import org.finos.legend.engine.application.query.model.QuerySearchTermSpecification; +import org.finos.legend.engine.application.query.model.*; import org.finos.legend.engine.application.query.utils.TestMongoClientProvider; import org.finos.legend.engine.shared.core.vault.TestVaultImplementation; import org.finos.legend.engine.shared.core.vault.Vault; import org.junit.*; +import java.time.Instant; import java.util.Arrays; import java.util.Collections; import java.util.List; @@ -34,14 +32,14 @@ public class TestDataCubeQueryStoreManager { - static class TestDataCubeQuerySearchSpecificationBuilder + static class TestQuerySearchSpecificationBuilder { public QuerySearchTermSpecification searchTermSpecification; public Integer limit; public Boolean showCurrentUserQueriesOnly; public QuerySearchSortBy sortByOption; - TestDataCubeQuerySearchSpecificationBuilder withSearchTerm(String searchTerm) + TestQuerySearchSpecificationBuilder withSearchTerm(String searchTerm) { if (this.searchTermSpecification == null) { @@ -51,19 +49,19 @@ TestDataCubeQuerySearchSpecificationBuilder withSearchTerm(String searchTerm) return this; } - TestDataCubeQuerySearchSpecificationBuilder withLimit(Integer limit) + TestQuerySearchSpecificationBuilder withLimit(Integer limit) { this.limit = limit; return this; } - TestDataCubeQuerySearchSpecificationBuilder withShowCurrentUserQueriesOnly(Boolean showCurrentUserQueriesOnly) + TestQuerySearchSpecificationBuilder withShowCurrentUserQueriesOnly(Boolean showCurrentUserQueriesOnly) { this.showCurrentUserQueriesOnly = showCurrentUserQueriesOnly; return this; } - TestDataCubeQuerySearchSpecificationBuilder withExactNameSearch(Boolean exactMatchName) + TestQuerySearchSpecificationBuilder withExactNameSearch(Boolean exactMatchName) { if (this.searchTermSpecification == null) { @@ -73,7 +71,17 @@ TestDataCubeQuerySearchSpecificationBuilder withExactNameSearch(Boolean exactMat return this; } - TestDataCubeQuerySearchSpecificationBuilder withSortByOption(QuerySearchSortBy sortByOption) + TestQuerySearchSpecificationBuilder withIncludeOwner(Boolean includeOwner) + { + if (this.searchTermSpecification == null) + { + this.searchTermSpecification = new QuerySearchTermSpecification(); + } + this.searchTermSpecification.includeOwner = includeOwner; + return this; + } + + TestQuerySearchSpecificationBuilder withSortByOption(QuerySearchSortBy sortByOption) { this.sortByOption = sortByOption; return this; @@ -94,15 +102,16 @@ static class TestQueryBuilder { public String id; public String name; + public String owner; public String description = "description"; - public Map query = Maps.mutable.empty(); - public Map source = Maps.mutable.empty(); + public Map content = Maps.mutable.empty(); - static TestQueryBuilder create(String id, String name) + static TestQueryBuilder create(String id, String name, String owner) { TestQueryBuilder queryBuilder = new TestQueryBuilder(); queryBuilder.id = id; queryBuilder.name = name; + queryBuilder.owner = owner; return queryBuilder; } @@ -112,14 +121,14 @@ DataCubeQuery build() query.id = this.id; query.name = this.name; query.description = this.description; - query.query = this.query; - query.source = this.source; + query.content = this.content; + query.owner = this.owner; return query; } } private TestMongoClientProvider testMongoClientProvider = new TestMongoClientProvider(); - private final DataCubeQueryStoreManager storeManager = new DataCubeQueryStoreManager(testMongoClientProvider.mongoClient); + private final DataCubeQueryStoreManager store = new DataCubeQueryStoreManager(testMongoClientProvider.mongoClient); private static final TestVaultImplementation testVaultImplementation = new TestVaultImplementation(); @BeforeClass @@ -127,6 +136,7 @@ public static void setupClass() { testVaultImplementation.setValue("query.mongo.database", "test"); testVaultImplementation.setValue("query.mongo.collection.dataCube", "dataCube"); + testVaultImplementation.setValue("query.mongo.collection.dataCubeEvent", "dataCube-event"); Vault.INSTANCE.registerImplementation(testVaultImplementation); } @@ -151,7 +161,8 @@ public void cleanUp() @Test public void testValidateQuery() { - Function0 _createTestQuery = () -> TestQueryBuilder.create("1", "query1").build(); + String currentUser = "testUser"; + Function0 _createTestQuery = () -> TestQueryBuilder.create("1", "query1", currentUser).build(); DataCubeQuery goodQuery = _createTestQuery.get(); DataCubeQueryStoreManager.validateQuery(goodQuery); @@ -169,22 +180,26 @@ public void testValidateQuery() queryWithInvalidId.name = ""; Assert.assertEquals("Query name is missing or empty", Assert.assertThrows(ApplicationQueryException.class, () -> DataCubeQueryStoreManager.validateQuery(queryWithInvalidName)).getMessage()); - // TODO?: validate content + // Content + DataCubeQuery queryWithInvalidContent = _createTestQuery.get(); + queryWithInvalidContent.content = null; + Assert.assertEquals("Query content is missing", Assert.assertThrows(ApplicationQueryException.class, () -> DataCubeQueryStoreManager.validateQuery(queryWithInvalidContent)).getMessage()); } @Test public void testSearchQueries() throws Exception { String currentUser = "testUser"; - DataCubeQuery newQuery = TestQueryBuilder.create("1", "query1").build(); - storeManager.createQuery(newQuery, currentUser); - List queries = storeManager.searchQueries(new TestDataCubeQuerySearchSpecificationBuilder().build(), currentUser); + DataCubeQuery newQuery = TestQueryBuilder.create("1", "query1", currentUser).build(); + store.createQuery(newQuery, currentUser); + List queries = store.searchQueries(new TestQuerySearchSpecificationBuilder().build(), currentUser); Assert.assertEquals(1, queries.size()); DataCubeQuery lightQuery = queries.get(0); Assert.assertEquals("1", lightQuery.id); Assert.assertEquals("query1", lightQuery.name); Assert.assertNotNull(lightQuery.createdAt); Assert.assertNotNull(lightQuery.lastUpdatedAt); + Assert.assertNull(lightQuery.content); Assert.assertNull(lightQuery.description); } @@ -192,15 +207,15 @@ public void testSearchQueries() throws Exception public void testMatchExactNameQuery() throws Exception { String currentUser = "testUser"; - DataCubeQuery newQuery = TestQueryBuilder.create("1", "Test Query 1").build(); - DataCubeQuery newQueryTwo = TestQueryBuilder.create("2", "Test Query 12").build(); - DataCubeQuery newQueryThree = TestQueryBuilder.create("3", "Test Query 13").build(); - storeManager.createQuery(newQuery, currentUser); - storeManager.createQuery(newQueryTwo, currentUser); - storeManager.createQuery(newQueryThree, currentUser); - List queriesGeneralSearch = storeManager.searchQueries(new TestDataCubeQuerySearchSpecificationBuilder().withSearchTerm("Test Query 1").build(), currentUser); + DataCubeQuery newQuery = TestQueryBuilder.create("1", "Test Query 1", currentUser).build(); + DataCubeQuery newQueryTwo = TestQueryBuilder.create("2", "Test Query 12", currentUser).build(); + DataCubeQuery newQueryThree = TestQueryBuilder.create("3", "Test Query 13", currentUser).build(); + store.createQuery(newQuery, currentUser); + store.createQuery(newQueryTwo, currentUser); + store.createQuery(newQueryThree, currentUser); + List queriesGeneralSearch = store.searchQueries(new TestQuerySearchSpecificationBuilder().withSearchTerm("Test Query 1").build(), currentUser); Assert.assertEquals(3, queriesGeneralSearch.size()); - List queriesExactSearch = storeManager.searchQueries(new TestDataCubeQuerySearchSpecificationBuilder().withSearchTerm("Test Query 1").withExactNameSearch(true).build(), currentUser); + List queriesExactSearch = store.searchQueries(new TestQuerySearchSpecificationBuilder().withSearchTerm("Test Query 1").withExactNameSearch(true).build(), currentUser); Assert.assertEquals(1, queriesExactSearch.size()); } @@ -208,66 +223,66 @@ public void testMatchExactNameQuery() throws Exception public void testGetQueriesWithLimit() throws Exception { String currentUser = "testUser"; - storeManager.createQuery(TestQueryBuilder.create("1", "query1").build(), currentUser); - storeManager.createQuery(TestQueryBuilder.create("2", "query2").build(), currentUser); - Assert.assertEquals(1, storeManager.searchQueries(new TestDataCubeQuerySearchSpecificationBuilder().withLimit(1).build(), currentUser).size()); - Assert.assertEquals(2, storeManager.searchQueries(new TestDataCubeQuerySearchSpecificationBuilder().build(), currentUser).size()); - Assert.assertEquals(0, storeManager.searchQueries(new TestDataCubeQuerySearchSpecificationBuilder().withLimit(0).build(), currentUser).size()); + store.createQuery(TestQueryBuilder.create("1", "query1", currentUser).build(), currentUser); + store.createQuery(TestQueryBuilder.create("2", "query2", currentUser).build(), currentUser); + Assert.assertEquals(1, store.searchQueries(new TestQuerySearchSpecificationBuilder().withLimit(1).build(), currentUser).size()); + Assert.assertEquals(2, store.searchQueries(new TestQuerySearchSpecificationBuilder().build(), currentUser).size()); + Assert.assertEquals(0, store.searchQueries(new TestQuerySearchSpecificationBuilder().withLimit(0).build(), currentUser).size()); } @Test public void testGetQueriesWithSortBy() throws Exception { String currentUser = "testUser"; - DataCubeQuery testQuery1 = TestQueryBuilder.create("1", "query1").build(); - DataCubeQuery testQuery2 = TestQueryBuilder.create("2", "query2").build(); - DataCubeQuery testQuery3 = TestQueryBuilder.create("3", "query3").build(); - DataCubeQuery testQuery4 = TestQueryBuilder.create("4", "query4").build(); + DataCubeQuery testQuery1 = TestQueryBuilder.create("1", "query1", currentUser).build(); + DataCubeQuery testQuery2 = TestQueryBuilder.create("2", "query2", currentUser).build(); + DataCubeQuery testQuery3 = TestQueryBuilder.create("3", "query3", currentUser).build(); + DataCubeQuery testQuery4 = TestQueryBuilder.create("4", "query4", currentUser).build(); // create in order 1 -> 4 -> 2 -> 3 - storeManager.createQuery(testQuery1, currentUser); + store.createQuery(testQuery1, currentUser); Thread.sleep(100); - storeManager.createQuery(testQuery4, currentUser); + store.createQuery(testQuery4, currentUser); Thread.sleep(100); - storeManager.createQuery(testQuery2, currentUser); + store.createQuery(testQuery2, currentUser); Thread.sleep(100); - storeManager.createQuery(testQuery3, currentUser); + store.createQuery(testQuery3, currentUser); - Assert.assertEquals(4, storeManager.searchQueries(new TestDataCubeQuerySearchSpecificationBuilder().withSortByOption(QuerySearchSortBy.SORT_BY_CREATE).build(), currentUser).size()); - Assert.assertEquals(Arrays.asList("3", "2", "4", "1"), storeManager.searchQueries(new TestDataCubeQuerySearchSpecificationBuilder().withSortByOption(QuerySearchSortBy.SORT_BY_CREATE).build(), currentUser).stream().map(q -> q.id).collect(Collectors.toList())); + Assert.assertEquals(4, store.searchQueries(new TestQuerySearchSpecificationBuilder().withSortByOption(QuerySearchSortBy.SORT_BY_CREATE).build(), currentUser).size()); + Assert.assertEquals(Arrays.asList("3", "2", "4", "1"), store.searchQueries(new TestQuerySearchSpecificationBuilder().withSortByOption(QuerySearchSortBy.SORT_BY_CREATE).build(), currentUser).stream().map(q -> q.id).collect(Collectors.toList())); - storeManager.updateQuery("2", TestQueryBuilder.create("2", "query2NewlyUpdated").build(), currentUser); - Assert.assertEquals(Arrays.asList("2", "3", "4", "1"), storeManager.searchQueries(new TestDataCubeQuerySearchSpecificationBuilder().withSortByOption(QuerySearchSortBy.SORT_BY_UPDATE).build(), currentUser).stream().map(q -> q.id).collect(Collectors.toList())); + store.updateQuery("2", TestQueryBuilder.create("2", "query2NewlyUpdated", currentUser).build(), currentUser); + Assert.assertEquals(Arrays.asList("2", "3", "4", "1"), store.searchQueries(new TestQuerySearchSpecificationBuilder().withSortByOption(QuerySearchSortBy.SORT_BY_UPDATE).build(), currentUser).stream().map(q -> q.id).collect(Collectors.toList())); - storeManager.getQuery("1"); - Assert.assertEquals(Arrays.asList("1", "2", "3", "4"), storeManager.searchQueries(new TestDataCubeQuerySearchSpecificationBuilder().withSortByOption(QuerySearchSortBy.SORT_BY_VIEW).build(), currentUser).stream().map(q -> q.id).collect(Collectors.toList())); + store.getQuery("1"); + Assert.assertEquals(Arrays.asList("1", "2", "3", "4"), store.searchQueries(new TestQuerySearchSpecificationBuilder().withSortByOption(QuerySearchSortBy.SORT_BY_VIEW).build(), currentUser).stream().map(q -> q.id).collect(Collectors.toList())); } @Test public void testGetQueriesWithSearchText() throws Exception { String currentUser = "testUser"; - storeManager.createQuery(TestQueryBuilder.create("1", "query1").build(), currentUser); - storeManager.createQuery(TestQueryBuilder.create("2", "query2").build(), currentUser); - storeManager.createQuery(TestQueryBuilder.create("3", "query2").build(), currentUser); - Assert.assertEquals(3, storeManager.searchQueries(new TestDataCubeQuerySearchSpecificationBuilder().build(), currentUser).size()); - Assert.assertEquals(1, storeManager.searchQueries(new TestDataCubeQuerySearchSpecificationBuilder().withSearchTerm("query1").build(), currentUser).size()); - Assert.assertEquals(2, storeManager.searchQueries(new TestDataCubeQuerySearchSpecificationBuilder().withSearchTerm("query2").build(), currentUser).size()); - Assert.assertEquals(3, storeManager.searchQueries(new TestDataCubeQuerySearchSpecificationBuilder().withSearchTerm("query").build(), currentUser).size()); + store.createQuery(TestQueryBuilder.create("1", "query1", currentUser).build(), currentUser); + store.createQuery(TestQueryBuilder.create("2", "query2", currentUser).build(), currentUser); + store.createQuery(TestQueryBuilder.create("3", "query2", currentUser).build(), currentUser); + Assert.assertEquals(3, store.searchQueries(new TestQuerySearchSpecificationBuilder().build(), currentUser).size()); + Assert.assertEquals(1, store.searchQueries(new TestQuerySearchSpecificationBuilder().withSearchTerm("query1").build(), currentUser).size()); + Assert.assertEquals(2, store.searchQueries(new TestQuerySearchSpecificationBuilder().withSearchTerm("query2").build(), currentUser).size()); + Assert.assertEquals(3, store.searchQueries(new TestQuerySearchSpecificationBuilder().withSearchTerm("query").build(), currentUser).size()); } @Test public void testGetNotFoundQuery() { - Assert.assertEquals("Can't find query with ID '1'", Assert.assertThrows(ApplicationQueryException.class, () -> storeManager.getQuery("1")).getMessage()); + Assert.assertEquals("Can't find query with ID '1'", Assert.assertThrows(ApplicationQueryException.class, () -> store.getQuery("1")).getMessage()); } @Test public void testCreateSimpleQuery() throws Exception { String currentUser = "testUser"; - DataCubeQuery newQuery = TestQueryBuilder.create("1", "query1").build(); - DataCubeQuery createdQuery = storeManager.createQuery(newQuery, currentUser); + DataCubeQuery newQuery = TestQueryBuilder.create("1", "query1", currentUser).build(); + DataCubeQuery createdQuery = store.createQuery(newQuery, currentUser); Assert.assertEquals("1", createdQuery.id); Assert.assertEquals("query1", createdQuery.name); Assert.assertEquals("description", createdQuery.description); @@ -280,62 +295,116 @@ public void testCreateSimpleQuery() throws Exception public void testCreateInvalidQuery() { String currentUser = "testUser"; - Assert.assertEquals("Query name is missing or empty", Assert.assertThrows(ApplicationQueryException.class, () -> storeManager.createQuery(TestQueryBuilder.create("1", null).build(), currentUser)).getMessage()); + Assert.assertEquals("Query name is missing or empty", Assert.assertThrows(ApplicationQueryException.class, () -> store.createQuery(TestQueryBuilder.create("1", null, currentUser).build(), currentUser)).getMessage()); } @Test public void testCreateQueryWithSameId() throws Exception { String currentUser = "testUser"; - storeManager.createQuery(TestQueryBuilder.create("1", "query1").build(), currentUser); - Assert.assertEquals("Query with ID '1' already existed", Assert.assertThrows(ApplicationQueryException.class, () -> storeManager.createQuery(TestQueryBuilder.create("1", "query1").build(), currentUser)).getMessage()); + store.createQuery(TestQueryBuilder.create("1", "query1", currentUser).build(), currentUser); + Assert.assertEquals("Query with ID '1' already existed", Assert.assertThrows(ApplicationQueryException.class, () -> store.createQuery(TestQueryBuilder.create("1", "query1", currentUser).build(), currentUser)).getMessage()); + } + + @Test + public void testForceCurrentUserToBeOwnerWhenCreatingQuery() throws Exception + { + String currentUser = "testUser"; + store.createQuery(TestQueryBuilder.create("1", "query1", null).build(), currentUser); + Assert.assertEquals(currentUser, store.getQuery("1").owner); + store.createQuery(TestQueryBuilder.create("2", "query2", "testUser2").build(), currentUser); + Assert.assertEquals(currentUser, store.getQuery("2").owner); + store.createQuery(TestQueryBuilder.create("3", "query1", "testUser2").build(), null); + Assert.assertNull(store.getQuery("3").owner); } @Test public void testUpdateQuery() throws Exception { String currentUser = "testUser"; - storeManager.createQuery(TestQueryBuilder.create("1", "query1").build(), currentUser); - storeManager.updateQuery("1", TestQueryBuilder.create("1", "query2").build(), currentUser); - Assert.assertEquals("query2", storeManager.getQuery("1").name); + store.createQuery(TestQueryBuilder.create("1", "query1", currentUser).build(), currentUser); + store.updateQuery("1", TestQueryBuilder.create("1", "query2", currentUser).build(), currentUser); + Assert.assertEquals("query2", store.getQuery("1").name); } @Test public void testUpdateWithInvalidQuery() { String currentUser = "testUser"; - Assert.assertEquals("Query name is missing or empty", Assert.assertThrows(ApplicationQueryException.class, () -> storeManager.updateQuery("1", TestQueryBuilder.create("1", null).build(), currentUser)).getMessage()); + Assert.assertEquals("Query name is missing or empty", Assert.assertThrows(ApplicationQueryException.class, () -> store.updateQuery("1", TestQueryBuilder.create("1", null, currentUser).build(), currentUser)).getMessage()); + } + + @Test + public void testPreventUpdateQueryId() throws Exception + { + String currentUser = "testUser"; + store.createQuery(TestQueryBuilder.create("1", "query1", null).build(), null); + Assert.assertEquals("Updating query ID is not supported", Assert.assertThrows(ApplicationQueryException.class, () -> store.updateQuery("1", TestQueryBuilder.create("2", "query1", "testUser2").build(), currentUser)).getMessage()); } @Test public void testUpdateNotFoundQuery() { String currentUser = "testUser"; - Assert.assertThrows(ApplicationQueryException.class, () -> storeManager.updateQuery("1", TestQueryBuilder.create("1", "query1").build(), currentUser)); + Assert.assertThrows(ApplicationQueryException.class, () -> store.updateQuery("1", TestQueryBuilder.create("1", "query1", currentUser).build(), currentUser)); + } + + @Test + public void testAllowUpdateQueryWithoutOwner() throws Exception + { + String currentUser = "testUser"; + store.createQuery(TestQueryBuilder.create("1", "query1", null).build(), null); + store.updateQuery("1", TestQueryBuilder.create("1", "query2", null).build(), currentUser); + Assert.assertEquals(currentUser, store.getQuery("1").owner); + } + + @Test + public void testForbidUpdateQueryOfAnotherUser() throws Exception + { + String currentUser = "testUser"; + store.createQuery(TestQueryBuilder.create("1", "query1", currentUser).build(), currentUser); + Assert.assertEquals("Only owner can update the query", Assert.assertThrows(ApplicationQueryException.class, () -> store.updateQuery("1", TestQueryBuilder.create("1", "query1", "testUser2").build(), "testUser2")).getMessage()); } @Test public void testDeleteQuery() throws Exception { String currentUser = "testUser"; - storeManager.createQuery(TestQueryBuilder.create("1", "query1").build(), currentUser); - storeManager.deleteQuery("1", currentUser); - Assert.assertEquals(0, storeManager.searchQueries(new TestDataCubeQuerySearchSpecificationBuilder().build(), currentUser).size()); + store.createQuery(TestQueryBuilder.create("1", "query1", currentUser).build(), currentUser); + store.deleteQuery("1", currentUser); + Assert.assertEquals(0, store.searchQueries(new TestQuerySearchSpecificationBuilder().build(), currentUser).size()); } @Test public void testDeleteNotFoundQuery() { String currentUser = "testUser"; - Assert.assertEquals("Can't find query with ID '1'", Assert.assertThrows(ApplicationQueryException.class, () -> storeManager.deleteQuery("1", currentUser)).getMessage()); + Assert.assertEquals("Can't find query with ID '1'", Assert.assertThrows(ApplicationQueryException.class, () -> store.deleteQuery("1", currentUser)).getMessage()); + } + + @Test + public void testAllowDeleteQueryWithoutOwner() throws Exception + { + String currentUser = "testUser"; + store.createQuery(TestQueryBuilder.create("1", "query1", null).build(), null); + store.deleteQuery("1", currentUser); + Assert.assertEquals(0, store.searchQueries(new TestQueryStoreManager.TestQuerySearchSpecificationBuilder().build(), currentUser).size()); + } + + @Test + public void testForbidDeleteQueryOfAnotherUser() throws Exception + { + String currentUser = "testUser"; + store.createQuery(TestQueryBuilder.create("1", "query1", currentUser).build(), currentUser); + Assert.assertEquals("Only owner can delete the query", Assert.assertThrows(ApplicationQueryException.class, () -> store.deleteQuery("1", "testUser2")).getMessage()); } @Test public void testCreateSimpleQueryContainsTimestamps() throws Exception { String currentUser = "testUser"; - DataCubeQuery newQuery = TestQueryBuilder.create("1", "query1").build(); - DataCubeQuery createdQuery = storeManager.createQuery(newQuery, currentUser); + DataCubeQuery newQuery = TestQueryBuilder.create("1", "query1", currentUser).build(); + DataCubeQuery createdQuery = store.createQuery(newQuery, currentUser); Assert.assertNotNull(createdQuery.lastUpdatedAt); Assert.assertNotNull(createdQuery.createdAt); } @@ -344,9 +413,9 @@ public void testCreateSimpleQueryContainsTimestamps() throws Exception public void testSearchQueriesContainTimestamps() throws Exception { String currentUser = "testUser"; - DataCubeQuery newQuery = TestQueryBuilder.create("1", "query1").build(); - storeManager.createQuery(newQuery, currentUser); - List queries = storeManager.searchQueries(new TestDataCubeQuerySearchSpecificationBuilder().build(), currentUser); + DataCubeQuery newQuery = TestQueryBuilder.create("1", "query1", currentUser).build(); + store.createQuery(newQuery, currentUser); + List queries = store.searchQueries(new TestQuerySearchSpecificationBuilder().build(), currentUser); Assert.assertEquals(1, queries.size()); DataCubeQuery lightQuery = queries.get(0); Assert.assertNotNull(lightQuery.lastUpdatedAt); @@ -357,32 +426,123 @@ public void testSearchQueriesContainTimestamps() throws Exception public void testSearchQueriesWithSearchByQueryId() throws Exception { String currentUser = "testUser"; - storeManager.createQuery(TestQueryBuilder.create("26929514-237c-11ed-861d-0242ac120002", "query_a").build(), currentUser); - storeManager.createQuery(TestQueryBuilder.create("26929515-237c-11bd-851d-0243ac120002", "query_b").build(), currentUser); - storeManager.createQuery(TestQueryBuilder.create("23929515-235c-11ad-851d-0143ac120002", "query_c").build(), currentUser); - Assert.assertEquals(3, storeManager.searchQueries(new TestDataCubeQuerySearchSpecificationBuilder().build(), currentUser).size()); - Assert.assertEquals(1, storeManager.searchQueries(new TestDataCubeQuerySearchSpecificationBuilder().withSearchTerm("23929515-235c-11ad-851d-0143ac120002").build(), currentUser).size()); - Assert.assertEquals(0, storeManager.searchQueries(new TestDataCubeQuerySearchSpecificationBuilder().withSearchTerm("23929515-235c-11ad").build(), currentUser).size()); + store.createQuery(TestQueryBuilder.create("26929514-237c-11ed-861d-0242ac120002", "query_a", currentUser).build(), currentUser); + store.createQuery(TestQueryBuilder.create("26929515-237c-11bd-851d-0243ac120002", "query_b", currentUser).build(), currentUser); + store.createQuery(TestQueryBuilder.create("23929515-235c-11ad-851d-0143ac120002", "query_c", currentUser).build(), currentUser); + Assert.assertEquals(3, store.searchQueries(new TestQuerySearchSpecificationBuilder().build(), currentUser).size()); + Assert.assertEquals(1, store.searchQueries(new TestQuerySearchSpecificationBuilder().withSearchTerm("23929515-235c-11ad-851d-0143ac120002").build(), currentUser).size()); + Assert.assertEquals(0, store.searchQueries(new TestQuerySearchSpecificationBuilder().withSearchTerm("23929515-235c-11ad").build(), currentUser).size()); } @Test public void testGetQueries() throws Exception { String currentUser = "testUser"; - DataCubeQuery testQuery1 = TestQueryBuilder.create("1", "query1").build(); - DataCubeQuery testQuery2 = TestQueryBuilder.create("2", "query2").build(); - DataCubeQuery testQuery3 = TestQueryBuilder.create("3", "query3").build(); - storeManager.createQuery(testQuery1, currentUser); - storeManager.createQuery(testQuery2, currentUser); - storeManager.createQuery(testQuery3, currentUser); + DataCubeQuery testQuery1 = TestQueryBuilder.create("1", "query1", currentUser).build(); + DataCubeQuery testQuery2 = TestQueryBuilder.create("2", "query2", currentUser).build(); + DataCubeQuery testQuery3 = TestQueryBuilder.create("3", "query3", currentUser).build(); + store.createQuery(testQuery1, currentUser); + store.createQuery(testQuery2, currentUser); + store.createQuery(testQuery3, currentUser); + + Assert.assertEquals(1, store.getQueries(Lists.fixedSize.of("2")).size()); + Assert.assertEquals(1, store.getQueries(Lists.fixedSize.of("3")).size()); + Assert.assertEquals(2, store.getQueries(Lists.fixedSize.of("2", "3")).size()); + + Assert.assertEquals("Can't find queries for the following ID(s):\\n4", Assert.assertThrows(ApplicationQueryException.class, () -> store.getQueries(Lists.fixedSize.of("4"))).getMessage()); + Assert.assertEquals("Can't find queries for the following ID(s):\\n4\\n6", Assert.assertThrows(ApplicationQueryException.class, () -> store.getQueries(Lists.fixedSize.of("4", "3", "6"))).getMessage()); + + Assert.assertEquals("Can't fetch more than 50 queries", Assert.assertThrows(ApplicationQueryException.class, () -> store.getQueries(Lists.fixedSize.ofAll(Collections.nCopies(51, "5")))).getMessage()); + } + + + @Test + public void testCreateQueryEvent() throws Exception + { + String currentUser = "testUser"; + store.createQuery(TestQueryBuilder.create("1", "query1", currentUser).build(), currentUser); + List events = store.getQueryEvents(null, null, null, null, null); + Assert.assertEquals(1, events.size()); + QueryEvent event = events.get(0); + Assert.assertEquals("1", event.queryId); + Assert.assertEquals(QueryEvent.QueryEventType.CREATED, event.eventType); + } - Assert.assertEquals(1, storeManager.getQueries(Lists.fixedSize.of("2")).size()); - Assert.assertEquals(1, storeManager.getQueries(Lists.fixedSize.of("3")).size()); - Assert.assertEquals(2, storeManager.getQueries(Lists.fixedSize.of("2", "3")).size()); + @Test + public void testUpdateQueryEvent() throws Exception + { + String currentUser = "testUser"; + DataCubeQuery query = TestQueryBuilder.create("1", "query1", currentUser).build(); + store.createQuery(query, currentUser); + store.updateQuery(query.id, query, currentUser); + List events = store.getQueryEvents(null, null, null, null, null); + Assert.assertEquals(2, events.size()); + QueryEvent event = events.get(1); + Assert.assertEquals("1", event.queryId); + Assert.assertEquals(QueryEvent.QueryEventType.UPDATED, event.eventType); + } - Assert.assertEquals("Can't find queries for the following ID(s):\\n4", Assert.assertThrows(ApplicationQueryException.class, () -> storeManager.getQueries(Lists.fixedSize.of("4"))).getMessage()); - Assert.assertEquals("Can't find queries for the following ID(s):\\n4\\n6", Assert.assertThrows(ApplicationQueryException.class, () -> storeManager.getQueries(Lists.fixedSize.of("4", "3", "6"))).getMessage()); + @Test + public void testDeleteQueryEvent() throws Exception + { + String currentUser = "testUser"; + DataCubeQuery query = TestQueryBuilder.create("1", "query1", currentUser).build(); + store.createQuery(query, currentUser); + store.deleteQuery(query.id, currentUser); + List events = store.getQueryEvents(null, null, null, null, null); + Assert.assertEquals(2, events.size()); + QueryEvent event = events.get(1); + Assert.assertEquals("1", event.queryId); + Assert.assertEquals(QueryEvent.QueryEventType.DELETED, event.eventType); + } - Assert.assertEquals("Can't fetch more than 50 queries", Assert.assertThrows(ApplicationQueryException.class, () -> storeManager.getQueries(Lists.fixedSize.ofAll(Collections.nCopies(51, "5")))).getMessage()); + @Test + public void testGetQueryEvents() throws Exception + { + String currentUser = "testUser"; + DataCubeQuery query1 = TestQueryBuilder.create("1", "query1", currentUser).build(); + + // NOTE: for this test to work well, we need leave a tiny window of time (10 ms) between each operation + // so the test for filter using timestamp can be correct + store.createQuery(query1, currentUser); + Thread.sleep(10); + store.updateQuery(query1.id, query1, currentUser); + Thread.sleep(10); + store.deleteQuery(query1.id, currentUser); + Thread.sleep(10); + DataCubeQuery query2 = TestQueryBuilder.create("2", "query2", currentUser).build(); + store.createQuery(query2, currentUser); + Thread.sleep(10); + store.updateQuery(query2.id, query2, currentUser); + Thread.sleep(10); + store.deleteQuery(query2.id, currentUser); + Thread.sleep(10); + + Assert.assertEquals(6, store.getQueryEvents(null, null, null, null, null).size()); + + // Query ID + Assert.assertEquals(3, store.getQueryEvents("1", null, null, null, null).size()); + Assert.assertEquals(3, store.getQueryEvents("2", null, null, null, null).size()); + + // Event Type + Assert.assertEquals(2, store.getQueryEvents(null, QueryEvent.QueryEventType.CREATED, null, null, null).size()); + Assert.assertEquals(2, store.getQueryEvents(null, QueryEvent.QueryEventType.UPDATED, null, null, null).size()); + Assert.assertEquals(2, store.getQueryEvents(null, QueryEvent.QueryEventType.DELETED, null, null, null).size()); + + // Limit + Assert.assertEquals(1, store.getQueryEvents(null, null, null, null, 1).size()); + Assert.assertEquals(5, store.getQueryEvents(null, null, null, null, 5).size()); + + Long now = Instant.now().toEpochMilli(); + Assert.assertEquals(0, store.getQueryEvents(null, null, now, null, null).size()); + Assert.assertEquals(6, store.getQueryEvents(null, null, null, now, null).size()); + + QueryEvent event1 = store.getQueryEvents("1", QueryEvent.QueryEventType.DELETED, null, null, null).get(0); + Assert.assertEquals(4, store.getQueryEvents(null, null, event1.timestamp, null, null).size()); + Assert.assertEquals(3, store.getQueryEvents(null, null, null, event1.timestamp, null).size()); + + QueryEvent event2 = store.getQueryEvents("2", QueryEvent.QueryEventType.CREATED, null, null, null).get(0); + Assert.assertEquals(3, store.getQueryEvents(null, null, event2.timestamp, null, null).size()); + Assert.assertEquals(4, store.getQueryEvents(null, null, null, event2.timestamp, null).size()); } } diff --git a/legend-engine-application-query/src/test/java/org/finos/legend/engine/application/query/api/TestQueryStoreManager.java b/legend-engine-application-query/src/test/java/org/finos/legend/engine/application/query/api/TestQueryStoreManager.java index 7403cc0cfae..c8f6dad40d7 100644 --- a/legend-engine-application-query/src/test/java/org/finos/legend/engine/application/query/api/TestQueryStoreManager.java +++ b/legend-engine-application-query/src/test/java/org/finos/legend/engine/application/query/api/TestQueryStoreManager.java @@ -14,9 +14,6 @@ package org.finos.legend.engine.application.query.api; -import com.fasterxml.jackson.databind.MapperFeature; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.SerializationFeature; import org.eclipse.collections.api.block.function.Function0; import org.eclipse.collections.api.factory.Lists; import org.finos.legend.engine.application.query.model.*; @@ -26,12 +23,7 @@ import org.finos.legend.engine.protocol.pure.v1.model.packageableElement.domain.TaggedValue; import org.finos.legend.engine.shared.core.vault.TestVaultImplementation; import org.finos.legend.engine.shared.core.vault.Vault; -import org.junit.After; -import org.junit.AfterClass; -import org.junit.Assert; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; +import org.junit.*; import java.time.Instant; import java.util.*; @@ -298,11 +290,8 @@ private static QueryParameterValue createTestParameterValue(String name, String return queryParameterValue; } - private static final ObjectMapper objectMapper = new ObjectMapper() - .configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true) - .configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true); private TestMongoClientProvider testMongoClientProvider = new TestMongoClientProvider(); - private final QueryStoreManager queryStoreManager = new QueryStoreManager(testMongoClientProvider.mongoClient); + private final QueryStoreManager store = new QueryStoreManager(testMongoClientProvider.mongoClient); private static final TestVaultImplementation testVaultImplementation = new TestVaultImplementation(); @BeforeClass @@ -335,7 +324,8 @@ public void cleanUp() @Test public void testValidateQuery() { - Function0 _createTestQuery = () -> TestQueryBuilder.create("1", "query1", "testUser").withLegacyExecutionContext().build(); + String currentUser = "testUser"; + Function0 _createTestQuery = () -> TestQueryBuilder.create("1", "query1", currentUser).withLegacyExecutionContext().build(); Query goodQuery = _createTestQuery.get(); QueryStoreManager.validateQuery(goodQuery); @@ -431,8 +421,8 @@ public void testSearchQueries() throws Exception { String currentUser = "testUser"; Query newQuery = TestQueryBuilder.create("1", "query1", currentUser).withLegacyExecutionContext().withGroupId("test.group").withArtifactId("test-artifact").build(); - queryStoreManager.createQuery(newQuery, currentUser); - List queries = queryStoreManager.searchQueries(new TestQuerySearchSpecificationBuilder().build(), currentUser); + store.createQuery(newQuery, currentUser); + List queries = store.searchQueries(new TestQuerySearchSpecificationBuilder().build(), currentUser); Assert.assertEquals(1, queries.size()); Query lightQuery = queries.get(0); Assert.assertEquals("test-artifact", lightQuery.artifactId); @@ -458,12 +448,12 @@ public void testMatchExactNameQuery() throws Exception Query newQuery = TestQueryBuilder.create("1", "Test Query 1", currentUser).withLegacyExecutionContext().withGroupId("test.group").withArtifactId("test-artifact").build(); Query newQueryTwo = TestQueryBuilder.create("2", "Test Query 12", currentUser).withLegacyExecutionContext().withGroupId("test.group").withArtifactId("test-artifact").build(); Query newQueryThree = TestQueryBuilder.create("3", "Test Query 13", currentUser).withLegacyExecutionContext().withGroupId("test.group").withArtifactId("test-artifact").build(); - queryStoreManager.createQuery(newQuery, currentUser); - queryStoreManager.createQuery(newQueryTwo, currentUser); - queryStoreManager.createQuery(newQueryThree, currentUser); - List queriesGeneralSearch = queryStoreManager.searchQueries(new TestQuerySearchSpecificationBuilder().withSearchTerm("Test Query 1").build(), currentUser); + store.createQuery(newQuery, currentUser); + store.createQuery(newQueryTwo, currentUser); + store.createQuery(newQueryThree, currentUser); + List queriesGeneralSearch = store.searchQueries(new TestQuerySearchSpecificationBuilder().withSearchTerm("Test Query 1").build(), currentUser); Assert.assertEquals(3, queriesGeneralSearch.size()); - List queriesExactSearch = queryStoreManager.searchQueries(new TestQuerySearchSpecificationBuilder().withSearchTerm("Test Query 1").withExactNameSearch(true).build(), currentUser); + List queriesExactSearch = store.searchQueries(new TestQuerySearchSpecificationBuilder().withSearchTerm("Test Query 1").withExactNameSearch(true).build(), currentUser); Assert.assertEquals(1, queriesExactSearch.size()); } @@ -471,10 +461,10 @@ public void testMatchExactNameQuery() throws Exception public void testGetQueryStats() throws Exception { String currentUser = "testUser"; - Assert.assertEquals(Long.valueOf(0), this.queryStoreManager.getQueryStoreStats().getQueryCount()); - queryStoreManager.createQuery(TestQueryBuilder.create("1", "query1", currentUser).withLegacyExecutionContext().build(), currentUser); - queryStoreManager.createQuery(TestQueryBuilder.create("2", "query2", currentUser).withLegacyExecutionContext().build(), currentUser); - Assert.assertEquals(Long.valueOf(2), this.queryStoreManager.getQueryStoreStats().getQueryCount()); + Assert.assertEquals(Long.valueOf(0), this.store.getQueryStoreStats().getQueryCount()); + store.createQuery(TestQueryBuilder.create("1", "query1", currentUser).withLegacyExecutionContext().build(), currentUser); + store.createQuery(TestQueryBuilder.create("2", "query2", currentUser).withLegacyExecutionContext().build(), currentUser); + Assert.assertEquals(Long.valueOf(2), this.store.getQueryStoreStats().getQueryCount()); } @Test @@ -482,21 +472,21 @@ public void testGetQueryStatsWithDataSpaceQueryCount() throws Exception { String currentUser = "testUser"; TaggedValue taggedValue1 = createTestTaggedValue("meta::pure::profiles::query", "dataSpace", "value1"); - Assert.assertEquals(Long.valueOf(0), this.queryStoreManager.getQueryStoreStats().getQueryCount()); - Query query = queryStoreManager.createQuery(TestQueryBuilder.create("1", "query1", currentUser).withTaggedValues(Lists.fixedSize.of(taggedValue1)).withLegacyExecutionContext().build(), currentUser); - queryStoreManager.createQuery(TestQueryBuilder.create("2", "query2", currentUser).withLegacyExecutionContext().build(), currentUser); - Assert.assertEquals(Long.valueOf(1), this.queryStoreManager.getQueryStoreStats().getQueryCreatedFromDataSpaceCount()); + Assert.assertEquals(Long.valueOf(0), this.store.getQueryStoreStats().getQueryCount()); + Query query = store.createQuery(TestQueryBuilder.create("1", "query1", currentUser).withTaggedValues(Lists.fixedSize.of(taggedValue1)).withLegacyExecutionContext().build(), currentUser); + store.createQuery(TestQueryBuilder.create("2", "query2", currentUser).withLegacyExecutionContext().build(), currentUser); + Assert.assertEquals(Long.valueOf(1), this.store.getQueryStoreStats().getQueryCreatedFromDataSpaceCount()); } @Test public void testGetQueriesWithLimit() throws Exception { String currentUser = "testUser"; - queryStoreManager.createQuery(TestQueryBuilder.create("1", "query1", currentUser).withLegacyExecutionContext().build(), currentUser); - queryStoreManager.createQuery(TestQueryBuilder.create("2", "query2", currentUser).withLegacyExecutionContext().build(), currentUser); - Assert.assertEquals(1, queryStoreManager.searchQueries(new TestQuerySearchSpecificationBuilder().withLimit(1).build(), currentUser).size()); - Assert.assertEquals(2, queryStoreManager.searchQueries(new TestQuerySearchSpecificationBuilder().build(), currentUser).size()); - Assert.assertEquals(0, queryStoreManager.searchQueries(new TestQuerySearchSpecificationBuilder().withLimit(0).build(), currentUser).size()); + store.createQuery(TestQueryBuilder.create("1", "query1", currentUser).withLegacyExecutionContext().build(), currentUser); + store.createQuery(TestQueryBuilder.create("2", "query2", currentUser).withLegacyExecutionContext().build(), currentUser); + Assert.assertEquals(1, store.searchQueries(new TestQuerySearchSpecificationBuilder().withLimit(1).build(), currentUser).size()); + Assert.assertEquals(2, store.searchQueries(new TestQuerySearchSpecificationBuilder().build(), currentUser).size()); + Assert.assertEquals(0, store.searchQueries(new TestQuerySearchSpecificationBuilder().withLimit(0).build(), currentUser).size()); } @Test @@ -507,28 +497,28 @@ public void testGetQueriesWithProjectCoordinates() throws Exception Query testQuery2 = TestQueryBuilder.create("2", "query2", currentUser).withGroupId("test").withArtifactId("test").withLegacyExecutionContext().build(); Query testQuery3 = TestQueryBuilder.create("3", "query3", currentUser).withGroupId("something").withArtifactId("something").withLegacyExecutionContext().build(); Query testQuery4 = TestQueryBuilder.create("4", "query4", currentUser).withGroupId("something.another").withArtifactId("something-another").withVersionId("1.0.0").withLegacyExecutionContext().build(); - queryStoreManager.createQuery(testQuery1, currentUser); - queryStoreManager.createQuery(testQuery2, currentUser); - queryStoreManager.createQuery(testQuery3, currentUser); - queryStoreManager.createQuery(testQuery4, currentUser); + store.createQuery(testQuery1, currentUser); + store.createQuery(testQuery2, currentUser); + store.createQuery(testQuery3, currentUser); + store.createQuery(testQuery4, currentUser); // When no projects specified, return all queries - Assert.assertEquals(4, queryStoreManager.searchQueries(new TestQuerySearchSpecificationBuilder().build(), currentUser).size()); + Assert.assertEquals(4, store.searchQueries(new TestQuerySearchSpecificationBuilder().build(), currentUser).size()); QueryProjectCoordinates coordinate1 = createTestQueryProjectCoordinate("notfound", "notfound", null); - Assert.assertEquals(0, queryStoreManager.searchQueries(new TestQuerySearchSpecificationBuilder().withProjectCoordinates(Lists.fixedSize.of(coordinate1)).build(), currentUser).size()); + Assert.assertEquals(0, store.searchQueries(new TestQuerySearchSpecificationBuilder().withProjectCoordinates(Lists.fixedSize.of(coordinate1)).build(), currentUser).size()); QueryProjectCoordinates coordinate2 = createTestQueryProjectCoordinate("test", "test", null); - Assert.assertEquals(2, queryStoreManager.searchQueries(new TestQuerySearchSpecificationBuilder().withProjectCoordinates(Lists.fixedSize.of(coordinate2)).build(), currentUser).size()); - Assert.assertEquals(2, queryStoreManager.searchQueries(new TestQuerySearchSpecificationBuilder().withProjectCoordinates(Lists.fixedSize.of(coordinate1, coordinate2)).build(), currentUser).size()); + Assert.assertEquals(2, store.searchQueries(new TestQuerySearchSpecificationBuilder().withProjectCoordinates(Lists.fixedSize.of(coordinate2)).build(), currentUser).size()); + Assert.assertEquals(2, store.searchQueries(new TestQuerySearchSpecificationBuilder().withProjectCoordinates(Lists.fixedSize.of(coordinate1, coordinate2)).build(), currentUser).size()); QueryProjectCoordinates coordinate3 = createTestQueryProjectCoordinate("something", "something", null); - Assert.assertEquals(1, queryStoreManager.searchQueries(new TestQuerySearchSpecificationBuilder().withProjectCoordinates(Lists.fixedSize.of(coordinate3)).build(), currentUser).size()); - Assert.assertEquals(3, queryStoreManager.searchQueries(new TestQuerySearchSpecificationBuilder().withProjectCoordinates(Lists.fixedSize.of(coordinate1, coordinate2, coordinate3)).build(), currentUser).size()); + Assert.assertEquals(1, store.searchQueries(new TestQuerySearchSpecificationBuilder().withProjectCoordinates(Lists.fixedSize.of(coordinate3)).build(), currentUser).size()); + Assert.assertEquals(3, store.searchQueries(new TestQuerySearchSpecificationBuilder().withProjectCoordinates(Lists.fixedSize.of(coordinate1, coordinate2, coordinate3)).build(), currentUser).size()); QueryProjectCoordinates coordinate4 = createTestQueryProjectCoordinate("something.another", "something-another", "1.0.0"); - Assert.assertEquals(1, queryStoreManager.searchQueries(new TestQuerySearchSpecificationBuilder().withProjectCoordinates(Lists.fixedSize.of(coordinate4)).build(), currentUser).size()); - Assert.assertEquals(4, queryStoreManager.searchQueries(new TestQuerySearchSpecificationBuilder().withProjectCoordinates(Lists.fixedSize.of(coordinate1, coordinate2, coordinate3, coordinate4)).build(), currentUser).size()); + Assert.assertEquals(1, store.searchQueries(new TestQuerySearchSpecificationBuilder().withProjectCoordinates(Lists.fixedSize.of(coordinate4)).build(), currentUser).size()); + Assert.assertEquals(4, store.searchQueries(new TestQuerySearchSpecificationBuilder().withProjectCoordinates(Lists.fixedSize.of(coordinate1, coordinate2, coordinate3, coordinate4)).build(), currentUser).size()); } @Test @@ -541,22 +531,22 @@ public void testGetQueriesWithSortBy() throws Exception Query testQuery4 = TestQueryBuilder.create("4", "query4", currentUser).withGroupId("something.another").withArtifactId("something-another").withVersionId("1.0.0").withLegacyExecutionContext().build(); // create in order 1 -> 4 -> 2 -> 3 - queryStoreManager.createQuery(testQuery1, currentUser); + store.createQuery(testQuery1, currentUser); Thread.sleep(100); - queryStoreManager.createQuery(testQuery4, currentUser); + store.createQuery(testQuery4, currentUser); Thread.sleep(100); - queryStoreManager.createQuery(testQuery2, currentUser); + store.createQuery(testQuery2, currentUser); Thread.sleep(100); - queryStoreManager.createQuery(testQuery3, currentUser); + store.createQuery(testQuery3, currentUser); - Assert.assertEquals(4, queryStoreManager.searchQueries(new TestQuerySearchSpecificationBuilder().withSortByOption(QuerySearchSortBy.SORT_BY_CREATE).build(), currentUser).size()); - Assert.assertEquals(Arrays.asList("3", "2", "4", "1"), queryStoreManager.searchQueries(new TestQuerySearchSpecificationBuilder().withSortByOption(QuerySearchSortBy.SORT_BY_CREATE).build(), currentUser).stream().map(q -> q.id).collect(Collectors.toList())); + Assert.assertEquals(4, store.searchQueries(new TestQuerySearchSpecificationBuilder().withSortByOption(QuerySearchSortBy.SORT_BY_CREATE).build(), currentUser).size()); + Assert.assertEquals(Arrays.asList("3", "2", "4", "1"), store.searchQueries(new TestQuerySearchSpecificationBuilder().withSortByOption(QuerySearchSortBy.SORT_BY_CREATE).build(), currentUser).stream().map(q -> q.id).collect(Collectors.toList())); - queryStoreManager.updateQuery("2", TestQueryBuilder.create("2", "query2NewlyUpdated", currentUser).withLegacyExecutionContext().build(), currentUser); - Assert.assertEquals(Arrays.asList("2", "3", "4", "1"), queryStoreManager.searchQueries(new TestQuerySearchSpecificationBuilder().withSortByOption(QuerySearchSortBy.SORT_BY_UPDATE).build(), currentUser).stream().map(q -> q.id).collect(Collectors.toList())); + store.updateQuery("2", TestQueryBuilder.create("2", "query2NewlyUpdated", currentUser).withLegacyExecutionContext().build(), currentUser); + Assert.assertEquals(Arrays.asList("2", "3", "4", "1"), store.searchQueries(new TestQuerySearchSpecificationBuilder().withSortByOption(QuerySearchSortBy.SORT_BY_UPDATE).build(), currentUser).stream().map(q -> q.id).collect(Collectors.toList())); - queryStoreManager.getQuery("1"); - Assert.assertEquals(Arrays.asList("1", "2", "3", "4"), queryStoreManager.searchQueries(new TestQuerySearchSpecificationBuilder().withSortByOption(QuerySearchSortBy.SORT_BY_VIEW).build(), currentUser).stream().map(q -> q.id).collect(Collectors.toList())); + store.getQuery("1"); + Assert.assertEquals(Arrays.asList("1", "2", "3", "4"), store.searchQueries(new TestQuerySearchSpecificationBuilder().withSortByOption(QuerySearchSortBy.SORT_BY_VIEW).build(), currentUser).stream().map(q -> q.id).collect(Collectors.toList())); } @Test @@ -570,18 +560,18 @@ public void testGetQueriesWithStereotypes() throws Exception Query testQuery2 = TestQueryBuilder.create("2", "query2", currentUser).withStereotypes(Lists.fixedSize.of(stereotype1)).withLegacyExecutionContext().build(); Query testQuery3 = TestQueryBuilder.create("3", "query3", currentUser).withStereotypes(Lists.fixedSize.of(stereotype2)).withLegacyExecutionContext().build(); Query testQuery4 = TestQueryBuilder.create("4", "query3", currentUser).withStereotypes(Lists.fixedSize.of(stereotype1, stereotype2)).withLegacyExecutionContext().build(); - queryStoreManager.createQuery(testQuery1, currentUser); - queryStoreManager.createQuery(testQuery2, currentUser); - queryStoreManager.createQuery(testQuery3, currentUser); - queryStoreManager.createQuery(testQuery4, currentUser); + store.createQuery(testQuery1, currentUser); + store.createQuery(testQuery2, currentUser); + store.createQuery(testQuery3, currentUser); + store.createQuery(testQuery4, currentUser); // When no stereotype provided, return all queries - Assert.assertEquals(4, queryStoreManager.searchQueries(new TestQuerySearchSpecificationBuilder().build(), currentUser).size()); - Assert.assertEquals(0, queryStoreManager.searchQueries(new TestQuerySearchSpecificationBuilder().withStereotypes(Lists.fixedSize.of(stereotype3)).build(), currentUser).size()); - Assert.assertEquals(2, queryStoreManager.searchQueries(new TestQuerySearchSpecificationBuilder().withStereotypes(Lists.fixedSize.of(stereotype1)).build(), currentUser).size()); - Assert.assertEquals(2, queryStoreManager.searchQueries(new TestQuerySearchSpecificationBuilder().withStereotypes(Lists.fixedSize.of(stereotype2)).build(), currentUser).size()); - Assert.assertEquals(3, queryStoreManager.searchQueries(new TestQuerySearchSpecificationBuilder().withStereotypes(Lists.fixedSize.of(stereotype1, stereotype2)).build(), currentUser).size()); - Assert.assertEquals(3, queryStoreManager.searchQueries(new TestQuerySearchSpecificationBuilder().withStereotypes(Lists.fixedSize.of(stereotype1, stereotype2, stereotype3)).build(), currentUser).size()); + Assert.assertEquals(4, store.searchQueries(new TestQuerySearchSpecificationBuilder().build(), currentUser).size()); + Assert.assertEquals(0, store.searchQueries(new TestQuerySearchSpecificationBuilder().withStereotypes(Lists.fixedSize.of(stereotype3)).build(), currentUser).size()); + Assert.assertEquals(2, store.searchQueries(new TestQuerySearchSpecificationBuilder().withStereotypes(Lists.fixedSize.of(stereotype1)).build(), currentUser).size()); + Assert.assertEquals(2, store.searchQueries(new TestQuerySearchSpecificationBuilder().withStereotypes(Lists.fixedSize.of(stereotype2)).build(), currentUser).size()); + Assert.assertEquals(3, store.searchQueries(new TestQuerySearchSpecificationBuilder().withStereotypes(Lists.fixedSize.of(stereotype1, stereotype2)).build(), currentUser).size()); + Assert.assertEquals(3, store.searchQueries(new TestQuerySearchSpecificationBuilder().withStereotypes(Lists.fixedSize.of(stereotype1, stereotype2, stereotype3)).build(), currentUser).size()); } @Test @@ -595,24 +585,24 @@ public void testGetQueriesWithParameterValues() throws Exception Query testQuery2 = TestQueryBuilder.create("2", "query2", currentUser).withParameterValues(Lists.fixedSize.of(param1)).withLegacyExecutionContext().build(); Query testQuery3 = TestQueryBuilder.create("3", "query3", currentUser).withParameterValues(Lists.fixedSize.of(param2)).withLegacyExecutionContext().build(); Query testQuery4 = TestQueryBuilder.create("4", "query4", currentUser).withParameterValues(Lists.fixedSize.of(param1, param2, param3)).withLegacyExecutionContext().build(); - queryStoreManager.createQuery(testQuery1, currentUser); - queryStoreManager.createQuery(testQuery2, currentUser); - queryStoreManager.createQuery(testQuery3, currentUser); - queryStoreManager.createQuery(testQuery4, currentUser); + store.createQuery(testQuery1, currentUser); + store.createQuery(testQuery2, currentUser); + store.createQuery(testQuery3, currentUser); + store.createQuery(testQuery4, currentUser); - Query query1 = queryStoreManager.getQuery("1"); + Query query1 = store.getQuery("1"); Assert.assertEquals(0, query1.defaultParameterValues.size()); Assert.assertNotNull(query1.lastOpenAt); Assert.assertNotEquals(query1.lastOpenAt, query1.createdAt); - Query query2 = queryStoreManager.getQuery("2"); + Query query2 = store.getQuery("2"); Assert.assertEquals(1, query2.defaultParameterValues.size()); Assert.assertEquals("booleanParam1", query2.defaultParameterValues.get(0).name); Assert.assertEquals("true", query2.defaultParameterValues.get(0).content); Assert.assertNotNull(query2.lastOpenAt); Assert.assertNotEquals(query2.lastOpenAt, query2.createdAt); - Query query4 = queryStoreManager.getQuery("4"); + Query query4 = store.getQuery("4"); Assert.assertEquals(3, query4.defaultParameterValues.size()); Assert.assertEquals("booleanParam1", query4.defaultParameterValues.get(0).name); Assert.assertEquals("stringParam2", query4.defaultParameterValues.get(1).name); @@ -630,21 +620,21 @@ public void testGetQueriesWithExecContext() throws Exception Query testQuery1 = TestQueryBuilder.create("1", "query1", currentUser).withExplicitExecution().build(); Query testQuery2 = TestQueryBuilder.create("2", "query2", currentUser).withDataSpaceExecution("my::dataSpace").build(); Query testQuery3 = TestQueryBuilder.create("3", "query3", currentUser).withDataSpaceExecution("my::dataSpace", "myKey").build(); - queryStoreManager.createQuery(testQuery1, currentUser); - queryStoreManager.createQuery(testQuery2, currentUser); - queryStoreManager.createQuery(testQuery3, currentUser); + store.createQuery(testQuery1, currentUser); + store.createQuery(testQuery2, currentUser); + store.createQuery(testQuery3, currentUser); - Query query1 = queryStoreManager.getQuery("1"); + Query query1 = store.getQuery("1"); Assert.assertTrue(query1.executionContext instanceof QueryExplicitExecutionContext); Assert.assertEquals(((QueryExplicitExecutionContext) query1.executionContext).runtime, "runtime"); Assert.assertEquals(((QueryExplicitExecutionContext) query1.executionContext).mapping, "mapping"); - Query query2 = queryStoreManager.getQuery("2"); + Query query2 = store.getQuery("2"); Assert.assertTrue(query2.executionContext instanceof QueryDataSpaceExecutionContext); Assert.assertEquals(((QueryDataSpaceExecutionContext) query2.executionContext).dataSpacePath, "my::dataSpace"); Assert.assertNull(((QueryDataSpaceExecutionContext) query2.executionContext).executionKey); - Query query3 = queryStoreManager.getQuery("3"); + Query query3 = store.getQuery("3"); Assert.assertTrue(query3.executionContext instanceof QueryDataSpaceExecutionContext); Assert.assertEquals(((QueryDataSpaceExecutionContext) query3.executionContext).dataSpacePath, "my::dataSpace"); Assert.assertEquals(((QueryDataSpaceExecutionContext) query3.executionContext).executionKey, "myKey"); @@ -662,18 +652,18 @@ public void testGetQueriesWithTaggedValues() throws Exception Query testQuery2 = TestQueryBuilder.create("2", "query2", currentUser).withTaggedValues(Lists.fixedSize.of(taggedValue1)).withLegacyExecutionContext().build(); Query testQuery3 = TestQueryBuilder.create("3", "query3", currentUser).withTaggedValues(Lists.fixedSize.of(taggedValue2)).withLegacyExecutionContext().build(); Query testQuery4 = TestQueryBuilder.create("4", "query3", currentUser).withTaggedValues(Lists.fixedSize.of(taggedValue1, taggedValue2)).withLegacyExecutionContext().build(); - queryStoreManager.createQuery(testQuery1, currentUser); - queryStoreManager.createQuery(testQuery2, currentUser); - queryStoreManager.createQuery(testQuery3, currentUser); - queryStoreManager.createQuery(testQuery4, currentUser); + store.createQuery(testQuery1, currentUser); + store.createQuery(testQuery2, currentUser); + store.createQuery(testQuery3, currentUser); + store.createQuery(testQuery4, currentUser); // When no tagged value provided, return all queries - Assert.assertEquals(4, queryStoreManager.searchQueries(new TestQuerySearchSpecificationBuilder().build(), currentUser).size()); - Assert.assertEquals(0, queryStoreManager.searchQueries(new TestQuerySearchSpecificationBuilder().withTaggedValues(Lists.fixedSize.of(taggedValue3)).build(), currentUser).size()); - Assert.assertEquals(2, queryStoreManager.searchQueries(new TestQuerySearchSpecificationBuilder().withTaggedValues(Lists.fixedSize.of(taggedValue1)).build(), currentUser).size()); - Assert.assertEquals(2, queryStoreManager.searchQueries(new TestQuerySearchSpecificationBuilder().withTaggedValues(Lists.fixedSize.of(taggedValue2)).build(), currentUser).size()); - Assert.assertEquals(3, queryStoreManager.searchQueries(new TestQuerySearchSpecificationBuilder().withTaggedValues(Lists.fixedSize.of(taggedValue1, taggedValue2)).build(), currentUser).size()); - Assert.assertEquals(3, queryStoreManager.searchQueries(new TestQuerySearchSpecificationBuilder().withTaggedValues(Lists.fixedSize.of(taggedValue1, taggedValue2, taggedValue3)).build(), currentUser).size()); + Assert.assertEquals(4, store.searchQueries(new TestQuerySearchSpecificationBuilder().build(), currentUser).size()); + Assert.assertEquals(0, store.searchQueries(new TestQuerySearchSpecificationBuilder().withTaggedValues(Lists.fixedSize.of(taggedValue3)).build(), currentUser).size()); + Assert.assertEquals(2, store.searchQueries(new TestQuerySearchSpecificationBuilder().withTaggedValues(Lists.fixedSize.of(taggedValue1)).build(), currentUser).size()); + Assert.assertEquals(2, store.searchQueries(new TestQuerySearchSpecificationBuilder().withTaggedValues(Lists.fixedSize.of(taggedValue2)).build(), currentUser).size()); + Assert.assertEquals(3, store.searchQueries(new TestQuerySearchSpecificationBuilder().withTaggedValues(Lists.fixedSize.of(taggedValue1, taggedValue2)).build(), currentUser).size()); + Assert.assertEquals(3, store.searchQueries(new TestQuerySearchSpecificationBuilder().withTaggedValues(Lists.fixedSize.of(taggedValue1, taggedValue2, taggedValue3)).build(), currentUser).size()); } @Test @@ -684,11 +674,11 @@ public void testGetDataSpaceQueriesWithExecutionContext() throws Exception TaggedValue taggedValue = createTestTaggedValue("meta::pure::profiles::query", "dataSpace", dataspacePath); Query testQuery1 = TestQueryBuilder.create("1", "query1", currentUser).withDataSpaceExecution(dataspacePath).build(); Query testQuery2 = TestQueryBuilder.create("2", "query2", currentUser).withTaggedValues(Lists.fixedSize.of(taggedValue)).withDataSpaceExecution(dataspacePath).build(); - queryStoreManager.createQuery(testQuery1, currentUser); - queryStoreManager.createQuery(testQuery2, currentUser); + store.createQuery(testQuery1, currentUser); + store.createQuery(testQuery2, currentUser); - Assert.assertEquals(2, queryStoreManager.searchQueries(new TestQuerySearchSpecificationBuilder().build(), currentUser).size()); - Assert.assertEquals(2, queryStoreManager.searchQueries(new TestQuerySearchSpecificationBuilder().withTaggedValues(Lists.fixedSize.of(taggedValue)).build(), currentUser).size()); + Assert.assertEquals(2, store.searchQueries(new TestQuerySearchSpecificationBuilder().build(), currentUser).size()); + Assert.assertEquals(2, store.searchQueries(new TestQuerySearchSpecificationBuilder().withTaggedValues(Lists.fixedSize.of(taggedValue)).build(), currentUser).size()); } @Test @@ -705,8 +695,8 @@ public void testGetQueriesWithGridConfigs() throws Exception inner.put("myCol", "val"); gridConfig.put("config", inner); Query testQuery1 = TestQueryBuilder.create("1", "query1", currentUser).withGridConfigs(gridConfig).withLegacyExecutionContext().build(); - queryStoreManager.createQuery(testQuery1, currentUser); - Query query = queryStoreManager.getQuery("1"); + store.createQuery(testQuery1, currentUser); + Query query = store.getQuery("1"); Map gridConfigs = query.gridConfig; Assert.assertNotNull(gridConfigs); Assert.assertEquals(gridConfigs.get("dummyValue"), "value"); @@ -720,13 +710,13 @@ public void testGetQueriesWithGridConfigs() throws Exception public void testGetQueriesWithSearchText() throws Exception { String currentUser = "testUser"; - queryStoreManager.createQuery(TestQueryBuilder.create("1", "query1", currentUser).withLegacyExecutionContext().build(), currentUser); - queryStoreManager.createQuery(TestQueryBuilder.create("2", "query2", currentUser).withLegacyExecutionContext().build(), currentUser); - queryStoreManager.createQuery(TestQueryBuilder.create("3", "query2", currentUser).withLegacyExecutionContext().build(), currentUser); - Assert.assertEquals(3, queryStoreManager.searchQueries(new TestQuerySearchSpecificationBuilder().build(), currentUser).size()); - Assert.assertEquals(1, queryStoreManager.searchQueries(new TestQuerySearchSpecificationBuilder().withSearchTerm("query1").build(), currentUser).size()); - Assert.assertEquals(2, queryStoreManager.searchQueries(new TestQuerySearchSpecificationBuilder().withSearchTerm("query2").build(), currentUser).size()); - Assert.assertEquals(3, queryStoreManager.searchQueries(new TestQuerySearchSpecificationBuilder().withSearchTerm("query").build(), currentUser).size()); + store.createQuery(TestQueryBuilder.create("1", "query1", currentUser).withLegacyExecutionContext().build(), currentUser); + store.createQuery(TestQueryBuilder.create("2", "query2", currentUser).withLegacyExecutionContext().build(), currentUser); + store.createQuery(TestQueryBuilder.create("3", "query2", currentUser).withLegacyExecutionContext().build(), currentUser); + Assert.assertEquals(3, store.searchQueries(new TestQuerySearchSpecificationBuilder().build(), currentUser).size()); + Assert.assertEquals(1, store.searchQueries(new TestQuerySearchSpecificationBuilder().withSearchTerm("query1").build(), currentUser).size()); + Assert.assertEquals(2, store.searchQueries(new TestQuerySearchSpecificationBuilder().withSearchTerm("query2").build(), currentUser).size()); + Assert.assertEquals(3, store.searchQueries(new TestQuerySearchSpecificationBuilder().withSearchTerm("query").build(), currentUser).size()); } @@ -735,31 +725,31 @@ public void testGetQueriesWithSearchTextSpec() throws Exception { String currentUser = "user1"; String user2 = "user2"; - queryStoreManager.createQuery(TestQueryBuilder.create("1", "query1", currentUser).withLegacyExecutionContext().build(), currentUser); - queryStoreManager.createQuery(TestQueryBuilder.create("2", "query2", currentUser).withLegacyExecutionContext().build(), currentUser); - queryStoreManager.createQuery(TestQueryBuilder.create("3", "query3", user2).withLegacyExecutionContext().build(), user2); - Assert.assertEquals(1, queryStoreManager.searchQueries(new TestQuerySearchSpecificationBuilder().withSearchTerm("user2").withIncludeOwner(true).build(), currentUser).size()); - Assert.assertEquals(2, queryStoreManager.searchQueries(new TestQuerySearchSpecificationBuilder().withSearchTerm("user1").withIncludeOwner(true).build(), currentUser).size()); - Assert.assertEquals(3, queryStoreManager.searchQueries(new TestQuerySearchSpecificationBuilder().withSearchTerm("user").withIncludeOwner(true).build(), currentUser).size()); - Assert.assertEquals(0, queryStoreManager.searchQueries(new TestQuerySearchSpecificationBuilder().withSearchTerm("user").withExactNameSearch(true).withIncludeOwner(true).build(), currentUser).size()); - Assert.assertEquals(0, queryStoreManager.searchQueries(new TestQuerySearchSpecificationBuilder().withSearchTerm("user").withIncludeOwner(false).build(), currentUser).size()); + store.createQuery(TestQueryBuilder.create("1", "query1", currentUser).withLegacyExecutionContext().build(), currentUser); + store.createQuery(TestQueryBuilder.create("2", "query2", currentUser).withLegacyExecutionContext().build(), currentUser); + store.createQuery(TestQueryBuilder.create("3", "query3", user2).withLegacyExecutionContext().build(), user2); + Assert.assertEquals(1, store.searchQueries(new TestQuerySearchSpecificationBuilder().withSearchTerm("user2").withIncludeOwner(true).build(), currentUser).size()); + Assert.assertEquals(2, store.searchQueries(new TestQuerySearchSpecificationBuilder().withSearchTerm("user1").withIncludeOwner(true).build(), currentUser).size()); + Assert.assertEquals(3, store.searchQueries(new TestQuerySearchSpecificationBuilder().withSearchTerm("user").withIncludeOwner(true).build(), currentUser).size()); + Assert.assertEquals(0, store.searchQueries(new TestQuerySearchSpecificationBuilder().withSearchTerm("user").withExactNameSearch(true).withIncludeOwner(true).build(), currentUser).size()); + Assert.assertEquals(0, store.searchQueries(new TestQuerySearchSpecificationBuilder().withSearchTerm("user").withIncludeOwner(false).build(), currentUser).size()); } @Test public void testGetQueriesForCurrentUser() throws Exception { String currentUser = "testUser"; - queryStoreManager.createQuery(TestQueryBuilder.create("1", "query1", currentUser).withLegacyExecutionContext().build(), "testUser1"); - queryStoreManager.createQuery(TestQueryBuilder.create("2", "query2", currentUser).withLegacyExecutionContext().build(), currentUser); - Assert.assertEquals(2, queryStoreManager.searchQueries(new TestQuerySearchSpecificationBuilder().build(), currentUser).size()); - Assert.assertEquals(2, queryStoreManager.searchQueries(new TestQuerySearchSpecificationBuilder().withShowCurrentUserQueriesOnly(false).build(), currentUser).size()); - Assert.assertEquals(1, queryStoreManager.searchQueries(new TestQuerySearchSpecificationBuilder().withShowCurrentUserQueriesOnly(true).build(), currentUser).size()); + store.createQuery(TestQueryBuilder.create("1", "query1", currentUser).withLegacyExecutionContext().build(), "testUser1"); + store.createQuery(TestQueryBuilder.create("2", "query2", currentUser).withLegacyExecutionContext().build(), currentUser); + Assert.assertEquals(2, store.searchQueries(new TestQuerySearchSpecificationBuilder().build(), currentUser).size()); + Assert.assertEquals(2, store.searchQueries(new TestQuerySearchSpecificationBuilder().withShowCurrentUserQueriesOnly(false).build(), currentUser).size()); + Assert.assertEquals(1, store.searchQueries(new TestQuerySearchSpecificationBuilder().withShowCurrentUserQueriesOnly(true).build(), currentUser).size()); } @Test public void testGetNotFoundQuery() { - Assert.assertEquals("Can't find query with ID '1'", Assert.assertThrows(ApplicationQueryException.class, () -> queryStoreManager.getQuery("1")).getMessage()); + Assert.assertEquals("Can't find query with ID '1'", Assert.assertThrows(ApplicationQueryException.class, () -> store.getQuery("1")).getMessage()); } @Test @@ -767,7 +757,7 @@ public void testCreateSimpleQuery() throws Exception { String currentUser = "testUser"; Query newQuery = TestQueryBuilder.create("1", "query1", currentUser).withLegacyExecutionContext().build(); - Query createdQuery = queryStoreManager.createQuery(newQuery, currentUser); + Query createdQuery = store.createQuery(newQuery, currentUser); Assert.assertEquals("test-artifact", createdQuery.artifactId); Assert.assertEquals("test.group", createdQuery.groupId); Assert.assertEquals("1", createdQuery.id); @@ -789,213 +779,123 @@ public void testCreateSimpleQuery() throws Exception public void testCreateInvalidQuery() { String currentUser = "testUser"; - Assert.assertEquals("Query name is missing or empty", Assert.assertThrows(ApplicationQueryException.class, () -> queryStoreManager.createQuery(TestQueryBuilder.create("1", null, currentUser).withLegacyExecutionContext().build(), currentUser)).getMessage()); + Assert.assertEquals("Query name is missing or empty", Assert.assertThrows(ApplicationQueryException.class, () -> store.createQuery(TestQueryBuilder.create("1", null, currentUser).withLegacyExecutionContext().build(), currentUser)).getMessage()); } @Test public void testCreateQueryWithSameId() throws Exception { String currentUser = "testUser"; - queryStoreManager.createQuery(TestQueryBuilder.create("1", "query1", currentUser).withLegacyExecutionContext().build(), currentUser); - Assert.assertEquals("Query with ID '1' already existed", Assert.assertThrows(ApplicationQueryException.class, () -> queryStoreManager.createQuery(TestQueryBuilder.create("1", "query1", currentUser).withLegacyExecutionContext().build(), currentUser)).getMessage()); + store.createQuery(TestQueryBuilder.create("1", "query1", currentUser).withLegacyExecutionContext().build(), currentUser); + Assert.assertEquals("Query with ID '1' already existed", Assert.assertThrows(ApplicationQueryException.class, () -> store.createQuery(TestQueryBuilder.create("1", "query1", currentUser).withLegacyExecutionContext().build(), currentUser)).getMessage()); } @Test public void testForceCurrentUserToBeOwnerWhenCreatingQuery() throws Exception { String currentUser = "testUser"; - queryStoreManager.createQuery(TestQueryBuilder.create("1", "query1", null).withLegacyExecutionContext().build(), currentUser); - Assert.assertEquals(currentUser, queryStoreManager.getQuery("1").owner); - queryStoreManager.createQuery(TestQueryBuilder.create("2", "query2", "testUser2").withLegacyExecutionContext().build(), currentUser); - Assert.assertEquals(currentUser, queryStoreManager.getQuery("2").owner); - queryStoreManager.createQuery(TestQueryBuilder.create("3", "query1", "testUser2").withLegacyExecutionContext().build(), null); - Assert.assertNull(queryStoreManager.getQuery("3").owner); + store.createQuery(TestQueryBuilder.create("1", "query1", null).withLegacyExecutionContext().build(), currentUser); + Assert.assertEquals(currentUser, store.getQuery("1").owner); + store.createQuery(TestQueryBuilder.create("2", "query2", "testUser2").withLegacyExecutionContext().build(), currentUser); + Assert.assertEquals(currentUser, store.getQuery("2").owner); + store.createQuery(TestQueryBuilder.create("3", "query1", "testUser2").withLegacyExecutionContext().build(), null); + Assert.assertNull(store.getQuery("3").owner); } @Test public void testUpdateQuery() throws Exception { String currentUser = "testUser"; - queryStoreManager.createQuery(TestQueryBuilder.create("1", "query1", currentUser).withLegacyExecutionContext().build(), currentUser); - queryStoreManager.updateQuery("1", TestQueryBuilder.create("1", "query2", currentUser).withLegacyExecutionContext().build(), currentUser); - Assert.assertEquals("query2", queryStoreManager.getQuery("1").name); + store.createQuery(TestQueryBuilder.create("1", "query1", currentUser).withLegacyExecutionContext().build(), currentUser); + store.updateQuery("1", TestQueryBuilder.create("1", "query2", currentUser).withLegacyExecutionContext().build(), currentUser); + Assert.assertEquals("query2", store.getQuery("1").name); } @Test public void testUpdateQueryVersion() throws Exception { String currentUser = "testUser"; - queryStoreManager.createQuery(TestQueryBuilder.create("1", "query1", currentUser).withLegacyExecutionContext().build(), currentUser); - queryStoreManager.updateQuery("1", TestQueryBuilder.create("1", "query2", currentUser).withLegacyExecutionContext().build(), currentUser); - Assert.assertEquals("query2", queryStoreManager.getQuery("1").name); + store.createQuery(TestQueryBuilder.create("1", "query1", currentUser).withLegacyExecutionContext().build(), currentUser); + store.updateQuery("1", TestQueryBuilder.create("1", "query2", currentUser).withLegacyExecutionContext().build(), currentUser); + Assert.assertEquals("query2", store.getQuery("1").name); Query queryWithSelectedFields = new Query(); queryWithSelectedFields.id = "1"; queryWithSelectedFields.versionId = "1.0.0"; - queryStoreManager.patchQuery("1", queryWithSelectedFields, currentUser); - Assert.assertEquals("1.0.0", queryStoreManager.getQuery("1").versionId); - Assert.assertEquals("0.0.0", queryStoreManager.getQuery("1").originalVersionId); + store.patchQuery("1", queryWithSelectedFields, currentUser); + Assert.assertEquals("1.0.0", store.getQuery("1").versionId); + Assert.assertEquals("0.0.0", store.getQuery("1").originalVersionId); } @Test public void testUpdateWithInvalidQuery() { String currentUser = "testUser"; - Assert.assertEquals("Query name is missing or empty", Assert.assertThrows(ApplicationQueryException.class, () -> queryStoreManager.updateQuery("1", TestQueryBuilder.create("1", null, currentUser).withLegacyExecutionContext().build(), currentUser)).getMessage()); + Assert.assertEquals("Query name is missing or empty", Assert.assertThrows(ApplicationQueryException.class, () -> store.updateQuery("1", TestQueryBuilder.create("1", null, currentUser).withLegacyExecutionContext().build(), currentUser)).getMessage()); } @Test public void testPreventUpdateQueryId() throws Exception { String currentUser = "testUser"; - queryStoreManager.createQuery(TestQueryBuilder.create("1", "query1", null).withLegacyExecutionContext().build(), null); - Assert.assertEquals("Updating query ID is not supported", Assert.assertThrows(ApplicationQueryException.class, () -> queryStoreManager.updateQuery("1", TestQueryBuilder.create("2", "query1", "testUser2").withLegacyExecutionContext().build(), currentUser)).getMessage()); + store.createQuery(TestQueryBuilder.create("1", "query1", null).withLegacyExecutionContext().build(), null); + Assert.assertEquals("Updating query ID is not supported", Assert.assertThrows(ApplicationQueryException.class, () -> store.updateQuery("1", TestQueryBuilder.create("2", "query1", "testUser2").withLegacyExecutionContext().build(), currentUser)).getMessage()); } @Test public void testUpdateNotFoundQuery() { String currentUser = "testUser"; - Assert.assertThrows(ApplicationQueryException.class, () -> queryStoreManager.updateQuery("1", TestQueryBuilder.create("1", "query1", currentUser).withLegacyExecutionContext().build(), currentUser)); + Assert.assertThrows(ApplicationQueryException.class, () -> store.updateQuery("1", TestQueryBuilder.create("1", "query1", currentUser).withLegacyExecutionContext().build(), currentUser)); } @Test public void testAllowUpdateQueryWithoutOwner() throws Exception { String currentUser = "testUser"; - queryStoreManager.createQuery(TestQueryBuilder.create("1", "query1", null).withLegacyExecutionContext().build(), null); - queryStoreManager.updateQuery("1", TestQueryBuilder.create("1", "query2", null).withLegacyExecutionContext().build(), currentUser); - Assert.assertEquals(currentUser, queryStoreManager.getQuery("1").owner); + store.createQuery(TestQueryBuilder.create("1", "query1", null).withLegacyExecutionContext().build(), null); + store.updateQuery("1", TestQueryBuilder.create("1", "query2", null).withLegacyExecutionContext().build(), currentUser); + Assert.assertEquals(currentUser, store.getQuery("1").owner); } @Test public void testForbidUpdateQueryOfAnotherUser() throws Exception { String currentUser = "testUser"; - queryStoreManager.createQuery(TestQueryBuilder.create("1", "query1", currentUser).withLegacyExecutionContext().build(), currentUser); - Assert.assertEquals("Only owner can update the query", Assert.assertThrows(ApplicationQueryException.class, () -> queryStoreManager.updateQuery("1", TestQueryBuilder.create("1", "query1", "testUser2").withLegacyExecutionContext().build(), "testUser2")).getMessage()); + store.createQuery(TestQueryBuilder.create("1", "query1", currentUser).withLegacyExecutionContext().build(), currentUser); + Assert.assertEquals("Only owner can update the query", Assert.assertThrows(ApplicationQueryException.class, () -> store.updateQuery("1", TestQueryBuilder.create("1", "query1", "testUser2").withLegacyExecutionContext().build(), "testUser2")).getMessage()); } @Test public void testDeleteQuery() throws Exception { String currentUser = "testUser"; - queryStoreManager.createQuery(TestQueryBuilder.create("1", "query1", currentUser).withLegacyExecutionContext().build(), currentUser); - queryStoreManager.deleteQuery("1", currentUser); - Assert.assertEquals(0, queryStoreManager.searchQueries(new TestQuerySearchSpecificationBuilder().build(), currentUser).size()); + store.createQuery(TestQueryBuilder.create("1", "query1", currentUser).withLegacyExecutionContext().build(), currentUser); + store.deleteQuery("1", currentUser); + Assert.assertEquals(0, store.searchQueries(new TestQuerySearchSpecificationBuilder().build(), currentUser).size()); } @Test public void testDeleteNotFoundQuery() { String currentUser = "testUser"; - Assert.assertEquals("Can't find query with ID '1'", Assert.assertThrows(ApplicationQueryException.class, () -> queryStoreManager.deleteQuery("1", currentUser)).getMessage()); + Assert.assertEquals("Can't find query with ID '1'", Assert.assertThrows(ApplicationQueryException.class, () -> store.deleteQuery("1", currentUser)).getMessage()); } @Test public void testAllowDeleteQueryWithoutOwner() throws Exception { String currentUser = "testUser"; - queryStoreManager.createQuery(TestQueryBuilder.create("1", "query1", null).withLegacyExecutionContext().build(), null); - queryStoreManager.deleteQuery("1", currentUser); - Assert.assertEquals(0, queryStoreManager.searchQueries(new TestQuerySearchSpecificationBuilder().build(), currentUser).size()); + store.createQuery(TestQueryBuilder.create("1", "query1", null).withLegacyExecutionContext().build(), null); + store.deleteQuery("1", currentUser); + Assert.assertEquals(0, store.searchQueries(new TestQuerySearchSpecificationBuilder().build(), currentUser).size()); } @Test public void testForbidDeleteQueryOfAnotherUser() throws Exception { String currentUser = "testUser"; - queryStoreManager.createQuery(TestQueryBuilder.create("1", "query1", currentUser).withLegacyExecutionContext().build(), currentUser); - Assert.assertEquals("Only owner can delete the query", Assert.assertThrows(ApplicationQueryException.class, () -> queryStoreManager.deleteQuery("1", "testUser2")).getMessage()); - } - - @Test - public void testCreateQueryEvent() throws Exception - { - String currentUser = "testUser"; - queryStoreManager.createQuery(TestQueryBuilder.create("1", "query1", currentUser).withLegacyExecutionContext().build(), currentUser); - List events = queryStoreManager.getQueryEvents(null, null, null, null, null); - Assert.assertEquals(1, events.size()); - QueryEvent event = events.get(0); - Assert.assertEquals("1", event.queryId); - Assert.assertEquals(QueryEvent.QueryEventType.CREATED, event.eventType); - } - - @Test - public void testUpdateQueryEvent() throws Exception - { - String currentUser = "testUser"; - Query query = TestQueryBuilder.create("1", "query1", currentUser).withLegacyExecutionContext().build(); - queryStoreManager.createQuery(query, currentUser); - queryStoreManager.updateQuery(query.id, query, currentUser); - List events = queryStoreManager.getQueryEvents(null, null, null, null, null); - Assert.assertEquals(2, events.size()); - QueryEvent event = events.get(1); - Assert.assertEquals("1", event.queryId); - Assert.assertEquals(QueryEvent.QueryEventType.UPDATED, event.eventType); - } - - @Test - public void testDeleteQueryEvent() throws Exception - { - String currentUser = "testUser"; - Query query = TestQueryBuilder.create("1", "query1", currentUser).withLegacyExecutionContext().build(); - queryStoreManager.createQuery(query, currentUser); - queryStoreManager.deleteQuery(query.id, currentUser); - List events = queryStoreManager.getQueryEvents(null, null, null, null, null); - Assert.assertEquals(2, events.size()); - QueryEvent event = events.get(1); - Assert.assertEquals("1", event.queryId); - Assert.assertEquals(QueryEvent.QueryEventType.DELETED, event.eventType); - } - - @Test - public void testGetQueryEvents() throws Exception - { - String currentUser = "testUser"; - Query query1 = TestQueryBuilder.create("1", "query1", currentUser).withLegacyExecutionContext().build(); - - // NOTE: for this test to work well, we need leave a tiny window of time (10 ms) between each operation - // so the test for filter using timestamp can be correct - queryStoreManager.createQuery(query1, currentUser); - Thread.sleep(10); - queryStoreManager.updateQuery(query1.id, query1, currentUser); - Thread.sleep(10); - queryStoreManager.deleteQuery(query1.id, currentUser); - Thread.sleep(10); - Query query2 = TestQueryBuilder.create("2", "query2", currentUser).withLegacyExecutionContext().build(); - queryStoreManager.createQuery(query2, currentUser); - Thread.sleep(10); - queryStoreManager.updateQuery(query2.id, query2, currentUser); - Thread.sleep(10); - queryStoreManager.deleteQuery(query2.id, currentUser); - Thread.sleep(10); - - Assert.assertEquals(6, queryStoreManager.getQueryEvents(null, null, null, null, null).size()); - - // Query ID - Assert.assertEquals(3, queryStoreManager.getQueryEvents("1", null, null, null, null).size()); - Assert.assertEquals(3, queryStoreManager.getQueryEvents("2", null, null, null, null).size()); - - // Event Type - Assert.assertEquals(2, queryStoreManager.getQueryEvents(null, QueryEvent.QueryEventType.CREATED, null, null, null).size()); - Assert.assertEquals(2, queryStoreManager.getQueryEvents(null, QueryEvent.QueryEventType.UPDATED, null, null, null).size()); - Assert.assertEquals(2, queryStoreManager.getQueryEvents(null, QueryEvent.QueryEventType.DELETED, null, null, null).size()); - - // Limit - Assert.assertEquals(1, queryStoreManager.getQueryEvents(null, null, null, null, 1).size()); - Assert.assertEquals(5, queryStoreManager.getQueryEvents(null, null, null, null, 5).size()); - - Long now = Instant.now().toEpochMilli(); - Assert.assertEquals(0, queryStoreManager.getQueryEvents(null, null, now, null, null).size()); - Assert.assertEquals(6, queryStoreManager.getQueryEvents(null, null, null, now, null).size()); - - QueryEvent event1 = queryStoreManager.getQueryEvents("1", QueryEvent.QueryEventType.DELETED, null, null, null).get(0); - Assert.assertEquals(4, queryStoreManager.getQueryEvents(null, null, event1.timestamp, null, null).size()); - Assert.assertEquals(3, queryStoreManager.getQueryEvents(null, null, null, event1.timestamp, null).size()); - - QueryEvent event2 = queryStoreManager.getQueryEvents("2", QueryEvent.QueryEventType.CREATED, null, null, null).get(0); - Assert.assertEquals(3, queryStoreManager.getQueryEvents(null, null, event2.timestamp, null, null).size()); - Assert.assertEquals(4, queryStoreManager.getQueryEvents(null, null, null, event2.timestamp, null).size()); + store.createQuery(TestQueryBuilder.create("1", "query1", currentUser).withLegacyExecutionContext().build(), currentUser); + Assert.assertEquals("Only owner can delete the query", Assert.assertThrows(ApplicationQueryException.class, () -> store.deleteQuery("1", "testUser2")).getMessage()); } @Test @@ -1003,7 +903,7 @@ public void testCreateSimpleQueryContainsTimestamps() throws Exception { String currentUser = "testUser"; Query newQuery = TestQueryBuilder.create("1", "query1", currentUser).withLegacyExecutionContext().build(); - Query createdQuery = queryStoreManager.createQuery(newQuery, currentUser); + Query createdQuery = store.createQuery(newQuery, currentUser); Assert.assertNotNull(createdQuery.lastUpdatedAt); Assert.assertNotNull(createdQuery.createdAt); } @@ -1013,8 +913,8 @@ public void testSearchQueriesContainTimestamps() throws Exception { String currentUser = "testUser"; Query newQuery = TestQueryBuilder.create("1", "query1", currentUser).withGroupId("test.group").withArtifactId("test-artifact").withLegacyExecutionContext().build(); - queryStoreManager.createQuery(newQuery, currentUser); - List queries = queryStoreManager.searchQueries(new TestQuerySearchSpecificationBuilder().build(), currentUser); + store.createQuery(newQuery, currentUser); + List queries = store.searchQueries(new TestQuerySearchSpecificationBuilder().build(), currentUser); Assert.assertEquals(1, queries.size()); Query lightQuery = queries.get(0); Assert.assertNotNull(lightQuery.lastUpdatedAt); @@ -1025,21 +925,21 @@ public void testSearchQueriesContainTimestamps() throws Exception public void testSearchQueriesWithSearchByQueryId() throws Exception { String currentUser = "testUser"; - queryStoreManager.createQuery(TestQueryBuilder.create("26929514-237c-11ed-861d-0242ac120002", "query_a", currentUser).withLegacyExecutionContext().build(), currentUser); - queryStoreManager.createQuery(TestQueryBuilder.create("26929515-237c-11bd-851d-0243ac120002", "query_b", currentUser).withLegacyExecutionContext().build(), currentUser); - queryStoreManager.createQuery(TestQueryBuilder.create("23929515-235c-11ad-851d-0143ac120002", "query_c", currentUser).withLegacyExecutionContext().build(), currentUser); - Assert.assertEquals(3, queryStoreManager.searchQueries(new TestQuerySearchSpecificationBuilder().build(), currentUser).size()); - Assert.assertEquals(1, queryStoreManager.searchQueries(new TestQuerySearchSpecificationBuilder().withSearchTerm("23929515-235c-11ad-851d-0143ac120002").build(), currentUser).size()); - Assert.assertEquals(0, queryStoreManager.searchQueries(new TestQuerySearchSpecificationBuilder().withSearchTerm("23929515-235c-11ad").build(), currentUser).size()); + store.createQuery(TestQueryBuilder.create("26929514-237c-11ed-861d-0242ac120002", "query_a", currentUser).withLegacyExecutionContext().build(), currentUser); + store.createQuery(TestQueryBuilder.create("26929515-237c-11bd-851d-0243ac120002", "query_b", currentUser).withLegacyExecutionContext().build(), currentUser); + store.createQuery(TestQueryBuilder.create("23929515-235c-11ad-851d-0143ac120002", "query_c", currentUser).withLegacyExecutionContext().build(), currentUser); + Assert.assertEquals(3, store.searchQueries(new TestQuerySearchSpecificationBuilder().build(), currentUser).size()); + Assert.assertEquals(1, store.searchQueries(new TestQuerySearchSpecificationBuilder().withSearchTerm("23929515-235c-11ad-851d-0143ac120002").build(), currentUser).size()); + Assert.assertEquals(0, store.searchQueries(new TestQuerySearchSpecificationBuilder().withSearchTerm("23929515-235c-11ad").build(), currentUser).size()); } @Test public void testSearchQueriesSortedByCurrentUserFirst() throws Exception { String currentUser = "testUser"; - queryStoreManager.createQuery(TestQueryBuilder.create("1", "query1", "testUser1").withLegacyExecutionContext().build(), "testUser1"); - queryStoreManager.createQuery(TestQueryBuilder.create("2", "query2", currentUser).withLegacyExecutionContext().build(), currentUser); - List queries = queryStoreManager.searchQueries(new TestQuerySearchSpecificationBuilder().build(), currentUser); + store.createQuery(TestQueryBuilder.create("1", "query1", "testUser1").withLegacyExecutionContext().build(), "testUser1"); + store.createQuery(TestQueryBuilder.create("2", "query2", currentUser).withLegacyExecutionContext().build(), currentUser); + List queries = store.searchQueries(new TestQuerySearchSpecificationBuilder().build(), currentUser); Assert.assertEquals(2, queries.size()); Assert.assertEquals(currentUser, queries.get(0).owner); } @@ -1055,18 +955,18 @@ public void testSearchQueriesWithCombineTaggedValuesCondition() throws Exception Query testQuery2 = TestQueryBuilder.create("2", "query2", currentUser).withTaggedValues(Lists.fixedSize.of(taggedValue1)).withLegacyExecutionContext().build(); Query testQuery3 = TestQueryBuilder.create("3", "query3", currentUser).withTaggedValues(Lists.fixedSize.of(taggedValue2)).withLegacyExecutionContext().build(); Query testQuery4 = TestQueryBuilder.create("4", "query3", currentUser).withTaggedValues(Lists.fixedSize.of(taggedValue1, taggedValue2)).withLegacyExecutionContext().build(); - queryStoreManager.createQuery(testQuery1, currentUser); - queryStoreManager.createQuery(testQuery2, currentUser); - queryStoreManager.createQuery(testQuery3, currentUser); - queryStoreManager.createQuery(testQuery4, currentUser); + store.createQuery(testQuery1, currentUser); + store.createQuery(testQuery2, currentUser); + store.createQuery(testQuery3, currentUser); + store.createQuery(testQuery4, currentUser); // When no tagged value provided, return all queries - Assert.assertEquals(4, queryStoreManager.searchQueries(new TestQuerySearchSpecificationBuilder().build(), currentUser).size()); - Assert.assertEquals(0, queryStoreManager.searchQueries(new TestQuerySearchSpecificationBuilder().withTaggedValues(Lists.fixedSize.of(taggedValue3)).withCombineTaggedValuesCondition(true).build(), currentUser).size()); - Assert.assertEquals(2, queryStoreManager.searchQueries(new TestQuerySearchSpecificationBuilder().withTaggedValues(Lists.fixedSize.of(taggedValue1)).withCombineTaggedValuesCondition(true).build(), currentUser).size()); - Assert.assertEquals(2, queryStoreManager.searchQueries(new TestQuerySearchSpecificationBuilder().withTaggedValues(Lists.fixedSize.of(taggedValue2)).withCombineTaggedValuesCondition(true).build(), currentUser).size()); - Assert.assertEquals(1, queryStoreManager.searchQueries(new TestQuerySearchSpecificationBuilder().withTaggedValues(Lists.fixedSize.of(taggedValue1, taggedValue2)).withCombineTaggedValuesCondition(true).build(), currentUser).size()); - Assert.assertEquals(0, queryStoreManager.searchQueries(new TestQuerySearchSpecificationBuilder().withTaggedValues(Lists.fixedSize.of(taggedValue1, taggedValue2, taggedValue3)).withCombineTaggedValuesCondition(true).build(), currentUser).size()); + Assert.assertEquals(4, store.searchQueries(new TestQuerySearchSpecificationBuilder().build(), currentUser).size()); + Assert.assertEquals(0, store.searchQueries(new TestQuerySearchSpecificationBuilder().withTaggedValues(Lists.fixedSize.of(taggedValue3)).withCombineTaggedValuesCondition(true).build(), currentUser).size()); + Assert.assertEquals(2, store.searchQueries(new TestQuerySearchSpecificationBuilder().withTaggedValues(Lists.fixedSize.of(taggedValue1)).withCombineTaggedValuesCondition(true).build(), currentUser).size()); + Assert.assertEquals(2, store.searchQueries(new TestQuerySearchSpecificationBuilder().withTaggedValues(Lists.fixedSize.of(taggedValue2)).withCombineTaggedValuesCondition(true).build(), currentUser).size()); + Assert.assertEquals(1, store.searchQueries(new TestQuerySearchSpecificationBuilder().withTaggedValues(Lists.fixedSize.of(taggedValue1, taggedValue2)).withCombineTaggedValuesCondition(true).build(), currentUser).size()); + Assert.assertEquals(0, store.searchQueries(new TestQuerySearchSpecificationBuilder().withTaggedValues(Lists.fixedSize.of(taggedValue1, taggedValue2, taggedValue3)).withCombineTaggedValuesCondition(true).build(), currentUser).size()); } @Test @@ -1076,17 +976,107 @@ public void testGetQueries() throws Exception Query testQuery1 = TestQueryBuilder.create("1", "query1", currentUser).withLegacyExecutionContext().build(); Query testQuery2 = TestQueryBuilder.create("2", "query2", currentUser).withLegacyExecutionContext().build(); Query testQuery3 = TestQueryBuilder.create("3", "query3", currentUser).withLegacyExecutionContext().build(); - queryStoreManager.createQuery(testQuery1, currentUser); - queryStoreManager.createQuery(testQuery2, currentUser); - queryStoreManager.createQuery(testQuery3, currentUser); + store.createQuery(testQuery1, currentUser); + store.createQuery(testQuery2, currentUser); + store.createQuery(testQuery3, currentUser); - Assert.assertEquals(1, queryStoreManager.getQueries(Lists.fixedSize.of("2")).size()); - Assert.assertEquals(1, queryStoreManager.getQueries(Lists.fixedSize.of("3")).size()); - Assert.assertEquals(2, queryStoreManager.getQueries(Lists.fixedSize.of("2", "3")).size()); + Assert.assertEquals(1, store.getQueries(Lists.fixedSize.of("2")).size()); + Assert.assertEquals(1, store.getQueries(Lists.fixedSize.of("3")).size()); + Assert.assertEquals(2, store.getQueries(Lists.fixedSize.of("2", "3")).size()); + + Assert.assertEquals("Can't find queries for the following ID(s):\\n4", Assert.assertThrows(ApplicationQueryException.class, () -> store.getQueries(Lists.fixedSize.of("4"))).getMessage()); + Assert.assertEquals("Can't find queries for the following ID(s):\\n4\\n6", Assert.assertThrows(ApplicationQueryException.class, () -> store.getQueries(Lists.fixedSize.of("4", "3", "6"))).getMessage()); + + Assert.assertEquals("Can't fetch more than 50 queries", Assert.assertThrows(ApplicationQueryException.class, () -> store.getQueries(Lists.fixedSize.ofAll(Collections.nCopies(51, "5")))).getMessage()); + } + + @Test + public void testCreateQueryEvent() throws Exception + { + String currentUser = "testUser"; + store.createQuery(TestQueryBuilder.create("1", "query1", currentUser).withLegacyExecutionContext().build(), currentUser); + List events = store.getQueryEvents(null, null, null, null, null); + Assert.assertEquals(1, events.size()); + QueryEvent event = events.get(0); + Assert.assertEquals("1", event.queryId); + Assert.assertEquals(QueryEvent.QueryEventType.CREATED, event.eventType); + } + + @Test + public void testUpdateQueryEvent() throws Exception + { + String currentUser = "testUser"; + Query query = TestQueryBuilder.create("1", "query1", currentUser).withLegacyExecutionContext().build(); + store.createQuery(query, currentUser); + store.updateQuery(query.id, query, currentUser); + List events = store.getQueryEvents(null, null, null, null, null); + Assert.assertEquals(2, events.size()); + QueryEvent event = events.get(1); + Assert.assertEquals("1", event.queryId); + Assert.assertEquals(QueryEvent.QueryEventType.UPDATED, event.eventType); + } + + @Test + public void testDeleteQueryEvent() throws Exception + { + String currentUser = "testUser"; + Query query = TestQueryBuilder.create("1", "query1", currentUser).withLegacyExecutionContext().build(); + store.createQuery(query, currentUser); + store.deleteQuery(query.id, currentUser); + List events = store.getQueryEvents(null, null, null, null, null); + Assert.assertEquals(2, events.size()); + QueryEvent event = events.get(1); + Assert.assertEquals("1", event.queryId); + Assert.assertEquals(QueryEvent.QueryEventType.DELETED, event.eventType); + } + + @Test + public void testGetQueryEvents() throws Exception + { + String currentUser = "testUser"; + Query query1 = TestQueryBuilder.create("1", "query1", currentUser).withLegacyExecutionContext().build(); + + // NOTE: for this test to work well, we need leave a tiny window of time (10 ms) between each operation + // so the test for filter using timestamp can be correct + store.createQuery(query1, currentUser); + Thread.sleep(10); + store.updateQuery(query1.id, query1, currentUser); + Thread.sleep(10); + store.deleteQuery(query1.id, currentUser); + Thread.sleep(10); + Query query2 = TestQueryBuilder.create("2", "query2", currentUser).withLegacyExecutionContext().build(); + store.createQuery(query2, currentUser); + Thread.sleep(10); + store.updateQuery(query2.id, query2, currentUser); + Thread.sleep(10); + store.deleteQuery(query2.id, currentUser); + Thread.sleep(10); + + Assert.assertEquals(6, store.getQueryEvents(null, null, null, null, null).size()); + + // Query ID + Assert.assertEquals(3, store.getQueryEvents("1", null, null, null, null).size()); + Assert.assertEquals(3, store.getQueryEvents("2", null, null, null, null).size()); + + // Event Type + Assert.assertEquals(2, store.getQueryEvents(null, QueryEvent.QueryEventType.CREATED, null, null, null).size()); + Assert.assertEquals(2, store.getQueryEvents(null, QueryEvent.QueryEventType.UPDATED, null, null, null).size()); + Assert.assertEquals(2, store.getQueryEvents(null, QueryEvent.QueryEventType.DELETED, null, null, null).size()); + + // Limit + Assert.assertEquals(1, store.getQueryEvents(null, null, null, null, 1).size()); + Assert.assertEquals(5, store.getQueryEvents(null, null, null, null, 5).size()); + + Long now = Instant.now().toEpochMilli(); + Assert.assertEquals(0, store.getQueryEvents(null, null, now, null, null).size()); + Assert.assertEquals(6, store.getQueryEvents(null, null, null, now, null).size()); - Assert.assertEquals("Can't find queries for the following ID(s):\\n4", Assert.assertThrows(ApplicationQueryException.class, () -> queryStoreManager.getQueries(Lists.fixedSize.of("4"))).getMessage()); - Assert.assertEquals("Can't find queries for the following ID(s):\\n4\\n6", Assert.assertThrows(ApplicationQueryException.class, () -> queryStoreManager.getQueries(Lists.fixedSize.of("4", "3", "6"))).getMessage()); + QueryEvent event1 = store.getQueryEvents("1", QueryEvent.QueryEventType.DELETED, null, null, null).get(0); + Assert.assertEquals(4, store.getQueryEvents(null, null, event1.timestamp, null, null).size()); + Assert.assertEquals(3, store.getQueryEvents(null, null, null, event1.timestamp, null).size()); - Assert.assertEquals("Can't fetch more than 50 queries", Assert.assertThrows(ApplicationQueryException.class, () -> queryStoreManager.getQueries(Lists.fixedSize.ofAll(Collections.nCopies(51, "5")))).getMessage()); + QueryEvent event2 = store.getQueryEvents("2", QueryEvent.QueryEventType.CREATED, null, null, null).get(0); + Assert.assertEquals(3, store.getQueryEvents(null, null, event2.timestamp, null, null).size()); + Assert.assertEquals(4, store.getQueryEvents(null, null, null, event2.timestamp, null).size()); } }