use of com.enonic.xp.content.Contents in project xp by enonic.
the class QueryContentHandler method convert.
private ContentsResultMapper convert(final FindContentIdsByQueryResult findQueryResult) {
final ContentIds contentIds = findQueryResult.getContentIds();
final Contents contents;
if (contentIds.isEmpty()) {
contents = Contents.empty();
} else {
contents = this.contentService.getByIds(new GetContentByIdsParams(contentIds));
}
return new ContentsResultMapper(contents, findQueryResult.getTotalHits(), findQueryResult.getAggregations(), findQueryResult.getHighlight(), findQueryResult.getSort(), findQueryResult.getScore());
}
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);
}
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());
}
use of com.enonic.xp.content.Contents in project xp by enonic.
the class PublishContentHandlerTest method publishWithoutChildrenOrDependencies.
@Test
public void publishWithoutChildrenOrDependencies() {
Contents published = Contents.from(exampleContent(PUB_ID_3, "mycontent", "My Content", "/mysite/somepage", "myfield", "Hello World"));
PublishContentResult exampleResult = PublishContentResult.create().setPushed(published.getIds()).build();
ContentIds ids = ContentIds.from(PUB_ID_3);
PushContentParams pushParams = PushContentParams.create().contentIds(ids).target(Branch.from("master")).excludeChildrenIds(ids).includeDependencies(false).build();
Mockito.when(this.contentService.publish(pushParams)).thenReturn(exampleResult);
runFunction("/test/PublishContentHandlerTest.js", "publishWithoutChildrenOrDependencies");
}
use of com.enonic.xp.content.Contents in project xp by enonic.
the class GetChildContentHandlerTest method getChildrenByPath.
@Test
public void getChildrenByPath() throws Exception {
final Contents contents = TestDataFixtures.newContents(3);
final FindContentByParentResult findResult = FindContentByParentResult.create().hits(contents.getSize()).totalHits(20).contents(contents).build();
Mockito.when(this.contentService.findByParent(Mockito.isA(FindContentByParentParams.class))).thenReturn(findResult);
runFunction("/test/GetChildContentHandlerTest.js", "getChildrenByPath");
}
Aggregations