Search in sources :

Example 1 with FindContentIdsByQueryResult

use of com.enonic.xp.content.FindContentIdsByQueryResult in project xp by enonic.

the class QueryContentHandlerTest method setUpForMetricsAggregations.

private void setUpForMetricsAggregations(final SingleValueMetricAggregation aggregation) {
    final List<Content> toAddContents = new ArrayList<>();
    for (int i = 1; i <= 5; i++) {
        final PropertyTree data = new PropertyTree();
        data.addString("category", "books");
        data.addString("productName", "product " + i);
        data.addDouble("price", 10.0 * i);
        final Content content = Content.create().id(ContentId.from("id" + i)).name("name" + i).displayName("My Content " + i).parentPath(ContentPath.from("/a/b")).modifier(PrincipalKey.from("user:system:admin")).modifiedTime(Instant.ofEpochSecond(0)).creator(PrincipalKey.from("user:system:admin")).createdTime(Instant.ofEpochSecond(0)).data(data).build();
        toAddContents.add(content);
    }
    final Buckets bucket = Buckets.create().add(Bucket.create().key("books").docCount(5).addAggregations(Aggregations.from(aggregation)).build()).build();
    final Contents contents = Contents.from(toAddContents);
    final FindContentIdsByQueryResult findResult = FindContentIdsByQueryResult.create().hits(contents.getSize()).totalHits(5).contents(contents.getIds()).aggregations(Aggregations.from(BucketAggregation.bucketAggregation("products").buckets(bucket).build())).build();
    Mockito.when(this.contentService.find(Mockito.isA(ContentQuery.class))).thenReturn(findResult);
    Mockito.when(this.contentService.getByIds(Mockito.isA(GetContentByIdsParams.class))).thenReturn(contents);
}
Also used : FindContentIdsByQueryResult(com.enonic.xp.content.FindContentIdsByQueryResult) Contents(com.enonic.xp.content.Contents) GetContentByIdsParams(com.enonic.xp.content.GetContentByIdsParams) ContentQuery(com.enonic.xp.content.ContentQuery) Content(com.enonic.xp.content.Content) PropertyTree(com.enonic.xp.data.PropertyTree) ArrayList(java.util.ArrayList) Buckets(com.enonic.xp.aggregation.Buckets)

Example 2 with FindContentIdsByQueryResult

use of com.enonic.xp.content.FindContentIdsByQueryResult in project xp by enonic.

the class QueryContentHandlerTest method testExecute.

@Test
public void testExecute() throws Exception {
    FindContentIdsByQueryResult queryResult = FindContentIdsByQueryResult.create().contents(ContentIds.from("contentId")).sort(Collections.singletonMap(ContentId.from("contentId"), SortValuesProperty.create().values(10).build())).build();
    Contents contents = Contents.create().add(Content.create().id(ContentId.from("contentId")).name("name").parentPath(ContentPath.ROOT).build()).build();
    Mockito.when(contentService.find(Mockito.any(ContentQuery.class))).thenReturn(queryResult);
    Mockito.when(contentService.getByIds(Mockito.any(GetContentByIdsParams.class))).thenReturn(contents);
    QueryContentHandler instance = new QueryContentHandler();
    instance.initialize(newBeanContext(ResourceKey.from("myapp:/test")));
    final ScriptValue sort = Mockito.mock(ScriptValue.class);
    final ScriptValue query = Mockito.mock(ScriptValue.class);
    Mockito.when(sort.getValue(String.class)).thenReturn("getDistance(\"location\", \"83,80\", \"km\")");
    Mockito.when(query.getValue(String.class)).thenReturn("_name = \"cityName\"");
    Mockito.when(query.isValue()).thenReturn(true);
    Mockito.when(sort.isValue()).thenReturn(true);
    instance.setSort(sort);
    instance.setQuery(query);
    JsonMapGenerator generator = new JsonMapGenerator();
    ContentsResultMapper resultMapper = (ContentsResultMapper) instance.execute();
    resultMapper.serialize(generator);
    final JsonNode actualJson = (JsonNode) generator.getRoot();
    Assertions.assertEquals(1, actualJson.path("count").asInt());
    Assertions.assertTrue(actualJson.path("hits").get(0).path("_sort").isArray());
    Assertions.assertEquals(10, actualJson.path("hits").get(0).path("_sort").get(0).asInt());
}
Also used : FindContentIdsByQueryResult(com.enonic.xp.content.FindContentIdsByQueryResult) ScriptValue(com.enonic.xp.script.ScriptValue) Contents(com.enonic.xp.content.Contents) GetContentByIdsParams(com.enonic.xp.content.GetContentByIdsParams) ContentsResultMapper(com.enonic.xp.lib.content.mapper.ContentsResultMapper) ContentQuery(com.enonic.xp.content.ContentQuery) JsonMapGenerator(com.enonic.xp.script.serializer.JsonMapGenerator) JsonNode(com.fasterxml.jackson.databind.JsonNode) Test(org.junit.jupiter.api.Test)

Example 3 with FindContentIdsByQueryResult

use of com.enonic.xp.content.FindContentIdsByQueryResult in project xp by enonic.

the class ReprocessRunnableTaskTest method reprocess_incl_children.

@Test
public void reprocess_incl_children() throws Exception {
    Content content = createContent("content-id", ContentPath.from("/path/to/content"));
    Content reprocessedContent = Content.create(content).displayName("new name").build();
    Content childContent = createContent("child-id", ContentPath.from(content.getPath(), "child"));
    Content reprocessedChildContent = Content.create(childContent).displayName("new name").build();
    FindContentIdsByQueryResult countResult = FindContentIdsByQueryResult.create().totalHits(1).build();
    Mockito.when(this.contentService.getByPath(content.getPath())).thenReturn(content);
    Mockito.when(this.contentService.reprocess(content.getId())).thenReturn(reprocessedContent);
    Mockito.when(this.contentService.reprocess(childContent.getId())).thenReturn(reprocessedChildContent);
    Mockito.when(this.contentService.find(Mockito.isA(ContentQuery.class))).thenReturn(countResult);
    Mockito.when(this.contentService.findByParent(FindContentByParentParams.create().parentId(content.getId()).from(0).size(5).build())).thenReturn(FindContentByParentResult.create().contents(Contents.create().add(childContent).build()).build());
    Mockito.when(this.contentService.findByParent(FindContentByParentParams.create().parentId(childContent.getId()).from(0).size(5).build())).thenReturn(FindContentByParentResult.create().contents(Contents.create().build()).build());
    final ReprocessRunnableTask task = createAndRunTask(new ReprocessContentRequestJson("branch:/path/to/content", false));
    task.createTaskResult();
    Mockito.verify(progressReporter, Mockito.times(1)).info(contentQueryArgumentCaptor.capture());
    Mockito.verify(taskService, Mockito.times(1)).submitTask(Mockito.isA(RunnableTask.class), Mockito.eq("reprocess"));
    final String result = contentQueryArgumentCaptor.getAllValues().get(0);
    jsonTestHelper.assertJsonEquals(jsonTestHelper.loadTestJson("incl_children_result.json"), jsonTestHelper.stringToJson(result));
}
Also used : FindContentIdsByQueryResult(com.enonic.xp.content.FindContentIdsByQueryResult) ContentQuery(com.enonic.xp.content.ContentQuery) Content(com.enonic.xp.content.Content) RunnableTask(com.enonic.xp.task.RunnableTask) ReprocessContentRequestJson(com.enonic.xp.impl.server.rest.model.ReprocessContentRequestJson) Test(org.junit.jupiter.api.Test) AbstractRunnableTaskTest(com.enonic.xp.task.AbstractRunnableTaskTest)

Example 4 with FindContentIdsByQueryResult

use of com.enonic.xp.content.FindContentIdsByQueryResult in project xp by enonic.

the class ReprocessRunnableTaskTest method reprocess_root_incl_children.

@Test
public void reprocess_root_incl_children() throws Exception {
    Content content = createContent("content-id", ContentPath.from("/content"));
    Content reprocessedContent = Content.create(content).displayName("new name").build();
    Content childContent = createContent("child-id", ContentPath.from(content.getPath(), "child"));
    Content reprocessedChildContent = Content.create(childContent).displayName("new name").build();
    FindContentIdsByQueryResult countResult = FindContentIdsByQueryResult.create().totalHits(1).build();
    Mockito.when(this.contentService.getByPath(ContentPath.ROOT)).thenReturn(content);
    Mockito.when(this.contentService.reprocess(content.getId())).thenReturn(reprocessedContent);
    Mockito.when(this.contentService.reprocess(childContent.getId())).thenReturn(reprocessedChildContent);
    Mockito.when(this.contentService.find(Mockito.isA(ContentQuery.class))).thenReturn(countResult);
    Mockito.when(this.contentService.findByParent(FindContentByParentParams.create().parentId(content.getId()).from(0).size(5).build())).thenReturn(FindContentByParentResult.create().contents(Contents.create().add(childContent).build()).build());
    Mockito.when(this.contentService.findByParent(FindContentByParentParams.create().parentId(childContent.getId()).from(0).size(5).build())).thenReturn(FindContentByParentResult.create().contents(Contents.create().build()).build());
    final ReprocessRunnableTask task = createAndRunTask(new ReprocessContentRequestJson("branch:/", false));
    task.createTaskResult();
    Mockito.verify(progressReporter, Mockito.times(1)).info(contentQueryArgumentCaptor.capture());
    Mockito.verify(taskService, Mockito.times(1)).submitTask(Mockito.isA(RunnableTask.class), Mockito.eq("reprocess"));
    final String result = contentQueryArgumentCaptor.getAllValues().get(0);
    jsonTestHelper.assertJsonEquals(jsonTestHelper.loadTestJson("root_incl_children_result.json"), jsonTestHelper.stringToJson(result));
}
Also used : FindContentIdsByQueryResult(com.enonic.xp.content.FindContentIdsByQueryResult) ContentQuery(com.enonic.xp.content.ContentQuery) Content(com.enonic.xp.content.Content) RunnableTask(com.enonic.xp.task.RunnableTask) ReprocessContentRequestJson(com.enonic.xp.impl.server.rest.model.ReprocessContentRequestJson) Test(org.junit.jupiter.api.Test) AbstractRunnableTaskTest(com.enonic.xp.task.AbstractRunnableTaskTest)

Example 5 with FindContentIdsByQueryResult

use of com.enonic.xp.content.FindContentIdsByQueryResult in project xp by enonic.

the class ContentDependenciesResolver method resolveInboundDependenciesAggregation.

private Collection<ContentDependenciesAggregation> resolveInboundDependenciesAggregation(final ContentId contentId) {
    if (contentId == null) {
        return new HashSet<>();
    }
    final FindContentIdsByQueryResult result = this.contentService.find(ContentQuery.create().queryFilter(BooleanFilter.create().must(IdFilter.create().fieldName(ContentIndexPath.REFERENCES.getPath()).value(contentId.toString()).build()).mustNot(IdFilter.create().fieldName(ContentIndexPath.ID.getPath()).value(contentId.toString()).build()).build()).aggregationQuery(TermsAggregationQuery.create("type").fieldName("type").orderDirection(TermsAggregationQuery.Direction.DESC).build()).build());
    final BucketAggregation bucketAggregation = (BucketAggregation) result.getAggregations().get("type");
    return bucketAggregation.getBuckets().getSet().stream().map(ContentDependenciesAggregation::new).collect(toList());
}
Also used : FindContentIdsByQueryResult(com.enonic.xp.content.FindContentIdsByQueryResult) BucketAggregation(com.enonic.xp.aggregation.BucketAggregation) HashSet(java.util.HashSet)

Aggregations

FindContentIdsByQueryResult (com.enonic.xp.content.FindContentIdsByQueryResult)17 ContentQuery (com.enonic.xp.content.ContentQuery)13 Test (org.junit.jupiter.api.Test)12 Content (com.enonic.xp.content.Content)8 PropertyTree (com.enonic.xp.data.PropertyTree)5 GetContentByIdsParams (com.enonic.xp.content.GetContentByIdsParams)4 ReprocessContentRequestJson (com.enonic.xp.impl.server.rest.model.ReprocessContentRequestJson)4 AbstractRunnableTaskTest (com.enonic.xp.task.AbstractRunnableTaskTest)4 RunnableTask (com.enonic.xp.task.RunnableTask)4 ContentDependencies (com.enonic.xp.content.ContentDependencies)3 ContentDependenciesAggregation (com.enonic.xp.content.ContentDependenciesAggregation)3 Contents (com.enonic.xp.content.Contents)3 GetContentTypeParams (com.enonic.xp.schema.content.GetContentTypeParams)3 BucketAggregation (com.enonic.xp.aggregation.BucketAggregation)2 Buckets (com.enonic.xp.aggregation.Buckets)2 Aggregations (com.enonic.xp.aggregation.Aggregations)1 StatsAggregation (com.enonic.xp.aggregation.StatsAggregation)1 ContentId (com.enonic.xp.content.ContentId)1 ContentPath (com.enonic.xp.content.ContentPath)1 ContentService (com.enonic.xp.content.ContentService)1