Search in sources :

Example 6 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 7 with JsonMapGenerator

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

the class MacroContextMapperTest method assertJson.

private void assertJson(final String name, final MapSerializable value) throws Exception {
    final String resource = "/" + getClass().getName().replace('.', '/') + "-" + name + ".json";
    final URL url = getClass().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 8 with JsonMapGenerator

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

the class PropertyTreeMapperTest method serializeAndAssert.

private void serializeAndAssert(final String name, final PropertyTree value) throws Exception {
    final String resource = name + ".json";
    final URL url = getClass().getResource(resource);
    assertNotNull(url, "File [" + resource + "] not found");
    final JsonNode expectedJson = MAPPER.readTree(url);
    final JsonMapGenerator generator = new JsonMapGenerator();
    new PropertyTreeMapper(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 9 with JsonMapGenerator

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

the class ContextMapperTest method test.

@Test
public void test() {
    User user = User.create().login(PrincipalKey.ofSuperUser().getId()).displayName("Super User").key(PrincipalKey.ofSuperUser()).build();
    AuthenticationInfo authInfo = AuthenticationInfo.create().user(user).principals(RoleKeys.ADMIN, RoleKeys.EVERYONE).build();
    Context context = ContextBuilder.create().repositoryId(RepositoryId.from("repository.id")).branch(Branch.create().value("master").build()).authInfo(authInfo).attribute("attrAsString", "value").attribute("attrAsInteger", Integer.MAX_VALUE).attribute("attrAsLong", Long.MIN_VALUE).attribute("attrAsBoolean", true).attribute("authInfoDetails", authInfo).attribute("testMapper", new TestMapper()).build();
    context.getLocalScope().setAttribute("attrAsString", "localValue");
    context.getLocalScope().setAttribute("attr1", "localValue");
    context.getLocalScope().setSession(new SessionMock());
    context.getLocalScope().getSession().setAttribute("attrAsString", "sessionValue");
    context.getLocalScope().getSession().setAttribute("attr2", "sessionValue");
    JsonMapGenerator generator = new JsonMapGenerator();
    new ContextMapper(context).serialize(generator);
    JsonNode actualJson = (JsonNode) generator.getRoot();
    JsonNode attributes = actualJson.get("attributes");
    assertNull(attributes.get("authInfoDetails"));
    assertNull(attributes.get(Branch.class.getName()));
    assertNull(attributes.get(RepositoryId.class.getName()));
    assertNull(attributes.get(AuthenticationInfo.class.getName()));
    assertEquals("value", attributes.get("attrAsString").asText());
    assertEquals(Integer.MAX_VALUE, attributes.get("attrAsInteger").asInt());
    assertTrue(attributes.get("attrAsBoolean").asBoolean());
    assertEquals(Long.MIN_VALUE, attributes.get("attrAsLong").asLong());
    assertNotNull(attributes.get("testMapper"));
    assertEquals("localValue", attributes.get("attr1").asText());
    assertEquals("sessionValue", attributes.get("attr2").asText());
}
Also used : Context(com.enonic.xp.context.Context) User(com.enonic.xp.security.User) JsonMapGenerator(com.enonic.xp.script.serializer.JsonMapGenerator) JsonNode(com.fasterxml.jackson.databind.JsonNode) AuthenticationInfo(com.enonic.xp.security.auth.AuthenticationInfo) SessionMock(com.enonic.xp.session.SessionMock) Test(org.junit.jupiter.api.Test)

Example 10 with JsonMapGenerator

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

the class PropertyTreeMapperTest method serializeAndAssert.

private void serializeAndAssert(final String name, final PropertyTree value) throws Exception {
    final String resource = name + ".json";
    final URL url = getClass().getResource(resource);
    assertNotNull(url, "File [" + resource + "] not found");
    final JsonNode expectedJson = MAPPER.readTree(url);
    final JsonMapGenerator generator = new JsonMapGenerator();
    new PropertyTreeMapper(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)

Aggregations

JsonMapGenerator (com.enonic.xp.script.serializer.JsonMapGenerator)16 Test (org.junit.jupiter.api.Test)11 JsonNode (com.fasterxml.jackson.databind.JsonNode)8 URL (java.net.URL)5 PushNodesResult (com.enonic.xp.node.PushNodesResult)4 Aggregations (com.enonic.xp.aggregation.Aggregations)1 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