Search in sources :

Example 1 with JsonMapGenerator

use of com.enonic.xp.script.serializer.JsonMapGenerator in project xp by enonic.

the class IndexConfigDocMapperTest method all.

@Test
public void all() throws Exception {
    final IndexConfigDocument doc = PatternIndexConfigDocument.create().defaultConfig(IndexConfig.BY_TYPE).add("path", IndexConfig.PATH).add("none", IndexConfig.NONE).add("minimal", IndexConfig.MINIMAL).add("full", IndexConfig.FULLTEXT).add("byType", IndexConfig.BY_TYPE).add("property1", IndexConfig.BY_TYPE).add("property1.*", IndexConfig.BY_TYPE).add("property1.x", IndexConfig.BY_TYPE).add("property1.property2", IndexConfig.BY_TYPE).add("property1.*.property3", IndexConfig.create(IndexConfig.BY_TYPE).addLanguage("en").build()).build();
    final JsonMapGenerator jsonGenerator = new JsonMapGenerator();
    new IndexConfigDocMapper(doc).serialize(jsonGenerator);
    assertJson("index_config_full.json", jsonGenerator);
}
Also used : JsonMapGenerator(com.enonic.xp.script.serializer.JsonMapGenerator) PatternIndexConfigDocument(com.enonic.xp.index.PatternIndexConfigDocument) IndexConfigDocument(com.enonic.xp.index.IndexConfigDocument) Test(org.junit.jupiter.api.Test)

Example 2 with JsonMapGenerator

use of com.enonic.xp.script.serializer.JsonMapGenerator in project xp by enonic.

the class ScriptEventListenerImplTest method testEvent.

@Test
public void testEvent() {
    final Event event = Event.create("application").localOrigin(true).value("a", 1).build();
    this.listener.onEvent(event);
    assertNotNull(this.event);
    assertTrue(this.event instanceof MapSerializable);
    final MapSerializable serializable = (MapSerializable) this.event;
    final JsonMapGenerator generator = new JsonMapGenerator();
    serializable.serialize(generator);
}
Also used : MapSerializable(com.enonic.xp.script.serializer.MapSerializable) Event(com.enonic.xp.event.Event) JsonMapGenerator(com.enonic.xp.script.serializer.JsonMapGenerator) Test(org.junit.jupiter.api.Test)

Example 3 with JsonMapGenerator

use of com.enonic.xp.script.serializer.JsonMapGenerator in project xp by enonic.

the class JsonAssert method assertJson.

public static void assertJson(final Class context, final String name, final MapSerializable value) throws Exception {
    final String resource = "/" + context.getName().replace('.', '/') + "-" + name + ".json";
    final URL url = context.getResource(resource);
    assertNotNull(url, "File [" + resource + "] not found");
    final JsonNode expectedJson = MAPPER.readTree(url);
    final JsonMapGenerator generator = new JsonMapGenerator();
    value.serialize(generator);
    final JsonNode actualJson = (JsonNode) generator.getRoot();
    final String expectedStr = MAPPER.writeValueAsString(expectedJson);
    final String actualStr = MAPPER.writeValueAsString(actualJson);
    assertEquals(expectedStr, actualStr);
}
Also used : JsonMapGenerator(com.enonic.xp.script.serializer.JsonMapGenerator) JsonNode(com.fasterxml.jackson.databind.JsonNode) URL(java.net.URL)

Example 4 with JsonMapGenerator

use of com.enonic.xp.script.serializer.JsonMapGenerator 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 5 with JsonMapGenerator

use of com.enonic.xp.script.serializer.JsonMapGenerator in project xp by enonic.

the class PushNodesResultMapperTest method single_failed.

@Test
public void single_failed() throws Exception {
    final PushNodesResult result = PushNodesResult.create().addFailed(createEntry("a"), PushNodesResult.Reason.ACCESS_DENIED).build();
    final JsonMapGenerator jsonGenerator = new JsonMapGenerator();
    new PushNodesResultMapper(result, NodeIds.empty()).serialize(jsonGenerator);
    assertJson("nodeResult/single_failed.json", jsonGenerator);
}
Also used : PushNodesResult(com.enonic.xp.node.PushNodesResult) JsonMapGenerator(com.enonic.xp.script.serializer.JsonMapGenerator) Test(org.junit.jupiter.api.Test)

Aggregations

JsonMapGenerator (com.enonic.xp.script.serializer.JsonMapGenerator)15 Test (org.junit.jupiter.api.Test)10 JsonNode (com.fasterxml.jackson.databind.JsonNode)7 URL (java.net.URL)5 PushNodesResult (com.enonic.xp.node.PushNodesResult)4 ContentQuery (com.enonic.xp.content.ContentQuery)1 Contents (com.enonic.xp.content.Contents)1 FindContentIdsByQueryResult (com.enonic.xp.content.FindContentIdsByQueryResult)1 GetContentByIdsParams (com.enonic.xp.content.GetContentByIdsParams)1 Context (com.enonic.xp.context.Context)1 Event (com.enonic.xp.event.Event)1 IndexConfigDocument (com.enonic.xp.index.IndexConfigDocument)1 PatternIndexConfigDocument (com.enonic.xp.index.PatternIndexConfigDocument)1 ContentsResultMapper (com.enonic.xp.lib.content.mapper.ContentsResultMapper)1 FindNodesByMultiRepoQueryResult (com.enonic.xp.node.FindNodesByMultiRepoQueryResult)1 NodeComparison (com.enonic.xp.node.NodeComparison)1 ResolveSyncWorkResult (com.enonic.xp.node.ResolveSyncWorkResult)1 ScriptValue (com.enonic.xp.script.ScriptValue)1 MapSerializable (com.enonic.xp.script.serializer.MapSerializable)1 User (com.enonic.xp.security.User)1