Skip to content
This repository has been archived by the owner on Nov 23, 2021. It is now read-only.

JcrSupport

Mariusz Kubiś edited this page Jul 15, 2016 · 3 revisions

JcrSupport

Bobcat provides few utility classes that support operations directly on JCR - this allows a fast setup of the initial state for a test run.

JcrUtils

JcrUtils class is used to obtain instances of JcrSession and JcrHelper working with a chosen AEM instance. Bobcat provides two extensions: JcrAuthorUtils and JcrPublishUtils that are built based on the properties specified in instances.properies file.

JcrHelper

JcrHelper gives an easy way to create, delete nodes in the repository and obtain their properties. JcrHelper's API provides methods for:

  • creating a node with the given name and type
  • creating a node with the given name and type and set of properties
  • getting node under specified path
  • deleting a node
  • adding a property to a node
  • obtaining a property from a node
  • removing a property from a node
  • checking if a node has a specified property
  • checking if a node has a specified valued in a given property
  • copying node

How to start?

Inject jcr util for AEM instance:

  @Inject
  private JcrAuthorUtils jcrAuthorUtils;

Initialize jcr session and jcrHelper

  private Session session;

  private JcrHelper jcrHelper;

  @Before
  public void before() throws RepositoryException {
    this.session = jcrAuthorUtils.getSession();
    this.jcrHelper = jcrAuthorUtils.getJcrHelper();

Write a test

  @Test
  public void createNode() throws RepositoryException {
    jcrHelper.createNode("/content", "test", "nt:unstructured");
    assertTrue("Node does not exist", session.nodeExists("/content/test"));
  }
  @Test
  public void copyNodeTest() throws RepositoryException {
    String NODE_PATH = "/content/test";
    String COPIED_NODE_PATH = "/content/sites/test";
  
    jcrHelper.createNode("/content", "test", "nt:unstructured", getPropertiesMap());
    assertTrue("Node does not exist", session.nodeExists(NODE_PATH));
    assertTrue("Wrong node property", jcrHelper.hasNodePropertyValue(NODE_PATH, "property1", "value1"));
    
    jcrHelper.createNode(NODE_PATH, "test1", "nt:unstructured", getPropertiesMap());
    String NODE_CHILD_PATH = "/content/test/test1";
    assertTrue("Node does not exist", session.nodeExists(NODE_CHILD_PATH));
    assertTrue("Wrong node property", jcrHelper.hasNodePropertyValue(NODE_CHILD_PATH, "property1", "value1"));
    
    jcrHelper.copyNode(NODE_PATH, "/content/sites");
    assertTrue("Node does not exist", session.nodeExists(COPIED_NODE_PATH));
    assertTrue("Wrong node property", jcrHelper.hasNodePropertyValue(COPIED_NODE_PATH, "property1", "value1"));
    
    String COPIED_NODE_CHILD_PATH = "/content/sites/test/test1";
    assertTrue("Node does not exist", session.nodeExists(COPIED_NODE_CHILD_PATH));
    assertTrue("Wrong node property", jcrHelper.hasNodePropertyValue(COPIED_NODE_CHILD_PATH, "property1", "value1"));
  }
  
  public Map<String, Pair<String, Integer>> getPropertiesMap() {
    Map<String, Pair<String, Integer>> property = new HashMap<>();
    Pair<String, Integer> entry = new MutablePair<>("value1", 1);
    property.put("property1", entry);
    return property;
  }

Getting started with Bobcat

  1. Getting started

AEM Related Features

  1. Authoring tutorial - Classic
  1. AEM Classic Authoring Advanced usage
  1. Authoring tutorial - Touch UI
Clone this wiki locally