Search in sources :

Example 96 with Context

use of com.enonic.xp.context.Context 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 97 with Context

use of com.enonic.xp.context.Context in project xp by enonic.

the class MultiRepoConnectTest method testExample.

@Test
public void testExample() {
    final Context context = ContextBuilder.create().authInfo(AuthenticationInfo.create().user(User.create().key(PrincipalKey.ofUser(IdProviderKey.system(), "test-user")).login("test-user").build()).principals(RoleKeys.ADMIN).build()).build();
    context.runWith(() -> runScript("/lib/xp/examples/node/multiRepoConnect.js"));
}
Also used : Context(com.enonic.xp.context.Context) Test(org.junit.jupiter.api.Test)

Example 98 with Context

use of com.enonic.xp.context.Context in project xp by enonic.

the class ContextFilter method doHandle.

@Override
protected void doHandle(final HttpServletRequest req, final HttpServletResponse res, final FilterChain chain) throws Exception {
    final Context context = ContextBuilder.create().build();
    context.getLocalScope().setAttribute(ContentConstants.BRANCH_DRAFT);
    context.getLocalScope().setAttribute(ContentConstants.CONTENT_REPO_ID);
    context.getLocalScope().setAttribute("__currentTimeMillis", System.currentTimeMillis());
    context.getLocalScope().setSession(new SessionWrapper(req));
    context.callWith(() -> {
        chain.doFilter(req, res);
        return null;
    });
}
Also used : Context(com.enonic.xp.context.Context)

Aggregations

Context (com.enonic.xp.context.Context)98 Test (org.junit.jupiter.api.Test)35 AuthenticationInfo (com.enonic.xp.security.auth.AuthenticationInfo)20 Node (com.enonic.xp.node.Node)16 InternalContext (com.enonic.xp.repo.impl.InternalContext)16 User (com.enonic.xp.security.User)13 Content (com.enonic.xp.content.Content)11 PropertyTree (com.enonic.xp.data.PropertyTree)11 Branch (com.enonic.xp.branch.Branch)9 AbstractNodeTest (com.enonic.xp.repo.impl.node.AbstractNodeTest)9 ContextAccessor (com.enonic.xp.context.ContextAccessor)8 NodePath (com.enonic.xp.node.NodePath)8 PrincipalKey (com.enonic.xp.security.PrincipalKey)8 ContextBuilder (com.enonic.xp.context.ContextBuilder)7 ContentId (com.enonic.xp.content.ContentId)6 CreateNodeParams (com.enonic.xp.node.CreateNodeParams)6 Repository (com.enonic.xp.repository.Repository)6 LogAuditLogParams (com.enonic.xp.audit.LogAuditLogParams)5 ProcessUpdateParams (com.enonic.xp.content.processor.ProcessUpdateParams)5 NodeComparison (com.enonic.xp.node.NodeComparison)5