origami_swan

The JCR Mock Library makes creating complex content data structures efficient and concise.

A Java Content Repository consists of a tree of content nodes. Each node is related to one or more nodes often in parent/child hierarchical structures. An example would be content about a particular shirt and an associated parent category or a pointer to a matching (and therefore related) pair of pants.

To author unit tests that prove the correctness of business rules that interact with this content is a tedious exercise. It requires some forty to fifty lines of Java code to set up even the simplest of structures – let alone to test anything meaningful.

What we found in our experience implementing content management solutions like Adobe’s Experience Manager is that these tests simply weren’t being authored.

Enter JCR Mock: this library makes creating complex content data structures efficient and concise. Nodes and their relationships are defined in a simple JSON file. A sample excerpt is shown here:

InputStream assetsJsonFile = getClass().getResourceAsStream(“/assets.json”);

Node assetsNode = JcrMockingUtils.createNodesFromJsonFile(assetsJsonFile);

With the assets file in this format:

products: {
awesome_shoe: {
name: ‘Awesome Shoe’,
description: ‘Description for an awesome shoe’,
price: ‘50.00’
},
awesome_shirt: {
name: ‘Awesome Shirt’,
description: ‘Description for an awesome shirt’,
price: ‘75.00’
},
awesome_jeans: {
name: ‘Awesome Jeans’,
description: ‘Description for awesome jeans’,
price: ‘175.00’
}
}

That’s it!

Currently hosted on github here.