Search in sources :

Example 1 with ContentsResultMapper

use of com.enonic.xp.lib.content.mapper.ContentsResultMapper 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());
}
Also used : Contents(com.enonic.xp.content.Contents) GetContentByIdsParams(com.enonic.xp.content.GetContentByIdsParams) ContentsResultMapper(com.enonic.xp.lib.content.mapper.ContentsResultMapper) ContentIds(com.enonic.xp.content.ContentIds)

Example 2 with ContentsResultMapper

use of com.enonic.xp.lib.content.mapper.ContentsResultMapper 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)

Aggregations

Contents (com.enonic.xp.content.Contents)2 GetContentByIdsParams (com.enonic.xp.content.GetContentByIdsParams)2 ContentsResultMapper (com.enonic.xp.lib.content.mapper.ContentsResultMapper)2 ContentIds (com.enonic.xp.content.ContentIds)1 ContentQuery (com.enonic.xp.content.ContentQuery)1 FindContentIdsByQueryResult (com.enonic.xp.content.FindContentIdsByQueryResult)1 ScriptValue (com.enonic.xp.script.ScriptValue)1 JsonMapGenerator (com.enonic.xp.script.serializer.JsonMapGenerator)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 Test (org.junit.jupiter.api.Test)1