Search in sources :

Example 16 with Contents

use of com.enonic.xp.content.Contents 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 17 with Contents

use of com.enonic.xp.content.Contents 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 18 with Contents

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

the class ContentDependenciesResolver method resolveOutboundDependenciesAggregation.

private Collection<ContentDependenciesAggregation> resolveOutboundDependenciesAggregation(final ContentId contentId) {
    final Map<ContentTypeName, Long> aggregationJsonMap = new HashMap<>();
    final Contents contents = this.contentService.getByIds(new GetContentByIdsParams(this.contentService.getOutboundDependencies(contentId)));
    contents.forEach(existingContent -> {
        final ContentTypeName contentTypeName = existingContent.getType();
        final Long count = aggregationJsonMap.containsKey(contentTypeName) ? aggregationJsonMap.get(contentTypeName) + 1 : 1;
        aggregationJsonMap.put(contentTypeName, count);
    });
    return aggregationJsonMap.entrySet().stream().map(entry -> new ContentDependenciesAggregation(entry.getKey(), entry.getValue())).collect(toList());
}
Also used : IdFilter(com.enonic.xp.query.filter.IdFilter) BucketAggregation(com.enonic.xp.aggregation.BucketAggregation) ContentService(com.enonic.xp.content.ContentService) ContentIndexPath(com.enonic.xp.content.ContentIndexPath) FindContentIdsByQueryResult(com.enonic.xp.content.FindContentIdsByQueryResult) Collection(java.util.Collection) HashMap(java.util.HashMap) ContentTypeName(com.enonic.xp.schema.content.ContentTypeName) ContentDependenciesAggregation(com.enonic.xp.content.ContentDependenciesAggregation) HashSet(java.util.HashSet) ContentQuery(com.enonic.xp.content.ContentQuery) ContentId(com.enonic.xp.content.ContentId) Collectors.toList(java.util.stream.Collectors.toList) GetContentByIdsParams(com.enonic.xp.content.GetContentByIdsParams) Contents(com.enonic.xp.content.Contents) BooleanFilter(com.enonic.xp.query.filter.BooleanFilter) TermsAggregationQuery(com.enonic.xp.query.aggregation.TermsAggregationQuery) Map(java.util.Map) ContentDependencies(com.enonic.xp.content.ContentDependencies) ContentDependenciesAggregation(com.enonic.xp.content.ContentDependenciesAggregation) Contents(com.enonic.xp.content.Contents) GetContentByIdsParams(com.enonic.xp.content.GetContentByIdsParams) HashMap(java.util.HashMap) ContentTypeName(com.enonic.xp.schema.content.ContentTypeName)

Example 19 with Contents

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

the class FindContentByParentCommand method execute.

FindContentByParentResult execute() {
    final FindNodesByParentResult result = nodeService.findByParent(createFindNodesByParentParams());
    final Nodes nodes = this.nodeService.getByIds(result.getNodeIds());
    final Contents contents = this.translator.fromNodes(nodes, true);
    return FindContentByParentResult.create().contents(contents).totalHits(result.getTotalHits()).hits(result.getHits()).build();
}
Also used : Contents(com.enonic.xp.content.Contents) FindNodesByParentResult(com.enonic.xp.node.FindNodesByParentResult) Nodes(com.enonic.xp.node.Nodes)

Example 20 with Contents

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

the class ContentServiceImplTest_delete method create_delete_content.

@Test
public void create_delete_content() throws Exception {
    // Creates a content
    final CreateContentParams createContentParams = CreateContentParams.create().contentData(new PropertyTree()).displayName("This is my content").name("myContent").parent(ContentPath.ROOT).type(ContentTypeName.folder()).build();
    final Content content = this.contentService.create(createContentParams);
    // Deletes the content
    final DeleteContentParams deleteContentParams = DeleteContentParams.create().contentPath(content.getPath()).build();
    final DeleteContentsResult deletedContents = this.contentService.deleteWithoutFetch(deleteContentParams);
    assertNotNull(deletedContents);
    assertEquals(1, deletedContents.getDeletedContents().getSize());
    assertEquals(content.getId(), deletedContents.getDeletedContents().first());
    // Checks that the content is deleted
    final ContentIds contentIds = ContentIds.from(content.getId());
    final GetContentByIdsParams getContentByIdsParams = new GetContentByIdsParams(contentIds);
    final Contents foundContents = this.contentService.getByIds(getContentByIdsParams);
    assertEquals(0, foundContents.getSize());
}
Also used : DeleteContentParams(com.enonic.xp.content.DeleteContentParams) GetContentByIdsParams(com.enonic.xp.content.GetContentByIdsParams) Contents(com.enonic.xp.content.Contents) CreateContentParams(com.enonic.xp.content.CreateContentParams) Content(com.enonic.xp.content.Content) PropertyTree(com.enonic.xp.data.PropertyTree) ContentIds(com.enonic.xp.content.ContentIds) DeleteContentsResult(com.enonic.xp.content.DeleteContentsResult) Test(org.junit.jupiter.api.Test)

Aggregations

Contents (com.enonic.xp.content.Contents)41 Test (org.junit.jupiter.api.Test)30 Content (com.enonic.xp.content.Content)25 GetContentByIdsParams (com.enonic.xp.content.GetContentByIdsParams)16 ContentIds (com.enonic.xp.content.ContentIds)12 ContentPaths (com.enonic.xp.content.ContentPaths)7 PropertyTree (com.enonic.xp.data.PropertyTree)7 ContentId (com.enonic.xp.content.ContentId)5 ContentQuery (com.enonic.xp.content.ContentQuery)5 DeleteContentsResult (com.enonic.xp.content.DeleteContentsResult)5 FindContentByParentParams (com.enonic.xp.content.FindContentByParentParams)5 FindContentByParentResult (com.enonic.xp.content.FindContentByParentResult)5 FindContentIdsByQueryResult (com.enonic.xp.content.FindContentIdsByQueryResult)5 CreateContentParams (com.enonic.xp.content.CreateContentParams)4 Nodes (com.enonic.xp.node.Nodes)3 BucketAggregation (com.enonic.xp.aggregation.BucketAggregation)2 Buckets (com.enonic.xp.aggregation.Buckets)2 ApplicationKey (com.enonic.xp.app.ApplicationKey)2 ContentDependencies (com.enonic.xp.content.ContentDependencies)2 ContentIndexPath (com.enonic.xp.content.ContentIndexPath)2