Skip to content

Commit

Permalink
Netcentric#24: remove redundant interface, remove sling dependency fr…
Browse files Browse the repository at this point in the history
…om user preferences upgrader.
  • Loading branch information
otarsko committed Oct 20, 2017
1 parent 4c1597a commit 982a1e6
Show file tree
Hide file tree
Showing 14 changed files with 297 additions and 190 deletions.
2 changes: 1 addition & 1 deletion vault-upgrade-hook/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.2</version>
<version>2.3</version>
</dependency>

<!-- optional dependencies for groovy scripts and sling pipes -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import biz.netcentric.vlt.upgrade.handler.OsgiUtil;
import biz.netcentric.vlt.upgrade.handler.OsgiUtil.ServiceWrapper;
import biz.netcentric.vlt.upgrade.util.PackageInstallLogger;
import biz.netcentric.vlt.upgrade.util.impl.JsonResourceSerializer;
import biz.netcentric.vlt.upgrade.util.JsonResourceSerializer;

public class SlingPipe extends UpgradeAction {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,9 @@
import javax.jcr.Value;

import org.apache.jackrabbit.vault.packaging.InstallContext;
import org.apache.sling.api.resource.ResourceResolver;

import biz.netcentric.vlt.upgrade.UpgradeAction;
import biz.netcentric.vlt.upgrade.UpgradeInfo;
import biz.netcentric.vlt.upgrade.handler.SlingUtils;
import biz.netcentric.vlt.upgrade.handler.UpgradeHandler;
import biz.netcentric.vlt.upgrade.util.PackageInstallLogger;

Expand Down Expand Up @@ -51,13 +49,12 @@ public Iterable<UpgradeAction> create(InstallContext ctx, UpgradeInfo info) thro
userIds.add(property.getString());
}
NodeIterator children = info.getNode().getNodes();
ResourceResolver resourceResolver = new SlingUtils().getResourceResolver(ctx.getSession());
while (children.hasNext()) {
Node child = children.nextNode();
if (NAME_SUFFIX_USER_PREFERENCES.equals(child.getName())) {
LOG.debug(ctx, "Found user preferences node '{}' below upgrade info {}", child.getPath(), info);
for (String userId : userIds) {
actions.add(new UserPreferencesUpgradeAction(userId, resourceResolver.getResource(child.getPath())));
actions.add(new UserPreferencesUpgradeAction(userId, child));
LOG.debug(ctx, "Added UserPreferencesUpgradeAction for userId '{}'.", userId);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.nio.charset.StandardCharsets;

import javax.jcr.Node;
import javax.jcr.RepositoryException;

import org.apache.jackrabbit.api.JackrabbitSession;
Expand All @@ -12,19 +13,20 @@
import org.apache.sling.api.resource.Resource;

import biz.netcentric.vlt.upgrade.UpgradeAction;
import biz.netcentric.vlt.upgrade.util.JsonNodeSerializer;
import biz.netcentric.vlt.upgrade.util.PackageInstallLogger;
import biz.netcentric.vlt.upgrade.util.impl.JsonResourceSerializer;
import biz.netcentric.vlt.upgrade.util.JsonResourceSerializer;

public class UserPreferencesUpgradeAction extends UpgradeAction {

private static final PackageInstallLogger LOG = PackageInstallLogger.create(UserPreferencesUpgradeAction.class);

private final Resource xmlFileResource;
private final Node xmlFileNode;
private final String userId;

public UserPreferencesUpgradeAction(String userId, Resource xmlFileResource) {
super(xmlFileResource.getName() + "/" + userId, Phase.PREPARE, getMd5(userId, xmlFileResource));
this.xmlFileResource = xmlFileResource;
public UserPreferencesUpgradeAction(String userId, Node xmlFileNode) throws RepositoryException {
super(xmlFileNode.getName() + "/" + userId, Phase.PREPARE, getMd5(userId, xmlFileNode));
this.xmlFileNode = xmlFileNode;
this.userId = userId;
}

Expand All @@ -50,12 +52,12 @@ public void execute(InstallContext ctx) throws RepositoryException {
ctx.getSession().save();
}
// copy the given node to the user preference
ctx.getSession().getWorkspace().copy(xmlFileResource.getPath(), userPreferencesPath);
ctx.getSession().getWorkspace().copy(xmlFileNode.getPath(), userPreferencesPath);
LOG.info(ctx, "updated user preferences of user '{}' in '{}'", userId, userPreferencesPath);
}

private static String getMd5(final String userId, final Resource xmlFileResource) {
String serializedAction = new JsonResourceSerializer().serialize(xmlFileResource);
private static String getMd5(final String userId, final Node xmlFileNode) throws RepositoryException {
String serializedAction = new JsonNodeSerializer().serialize(xmlFileNode);
return getMd5(String.format("%s:%s", userId, serializedAction), StandardCharsets.UTF_8.name());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* (C) Copyright 2016 Netcentric AG.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*/
package biz.netcentric.vlt.upgrade.util;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.Property;
import javax.jcr.PropertyIterator;
import javax.jcr.RepositoryException;

import com.google.gson.Gson;

public class JsonNodeSerializer {

private static final List<String> PROPERTY_PATTERN_TO_OMIT = Arrays.asList("jcr:.*");

private static final String NODE_NAME = "name";

public String serialize(final Node node) throws RepositoryException {

Map<String, Object> tree = getNodeMap(node);
traversToMap(node, tree);

return new Gson().toJson(tree);
}

private void traversToMap(final Node node, final Map<String, Object> parentMap) throws RepositoryException {
NodeIterator children = node.getNodes();
while (children.hasNext()) {
final Node child = children.nextNode();
Map<String, Object> childMap = getNodeMap(child);
parentMap.put(child.getName(), childMap);
traversToMap(child, childMap);
}
}

private Map<String, Object> getNodeMap(final Node node) throws RepositoryException {
HashMap<String, Object> map = new HashMap<>();
PropertyIterator properties = node.getProperties();
while(properties.hasNext()) {
Property property = properties.nextProperty();
for (final String propPattern : PROPERTY_PATTERN_TO_OMIT) {
if (!property.getName().matches(propPattern)) {
map.put(property.getName(), property.getValue());
}
}
}

map.put(NODE_NAME, node.getName());

return map;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* (C) Copyright 2016 Netcentric AG.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*/
package biz.netcentric.vlt.upgrade.util;

import javax.jcr.Node;
import javax.jcr.RepositoryException;

import org.apache.sling.api.resource.Resource;

public class JsonResourceSerializer {

public String serialize(final Resource resource) {

try {
return new JsonNodeSerializer().serialize(resource.adaptTo(Node.class));
} catch (RepositoryException e) {
throw new RuntimeException("Can not serialize resource: " + resource.getPath(), e);
}
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class SlingPipeTest {
public static final class Constructor {

@Rule
public final SlingContext sling = new SlingContext(ResourceResolverType.RESOURCERESOLVER_MOCK);
public final SlingContext sling = new SlingContext(ResourceResolverType.JCR_MOCK);

@Test
public void shouldHaveMd5() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@

import static org.assertj.core.api.Assertions.assertThat;

import java.util.Hashtable;

import javax.jcr.Node;
import javax.jcr.RepositoryException;

import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.apache.sling.testing.mock.sling.ResourceResolverType;
import org.apache.sling.testing.mock.sling.junit.SlingContext;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.experimental.runners.Enclosed;
Expand All @@ -22,13 +19,13 @@ public class UserPreferencesUpgradeActionTest {
public static final class Constructor {

@Rule
public final SlingContext sling = new SlingContext(ResourceResolverType.RESOURCERESOLVER_MOCK);
public final SlingContext sling = new SlingContext(ResourceResolverType.JCR_MOCK);

@Test
public void shouldHaveMd5() throws RepositoryException {
Resource resource = sling.create().resource("/content/resource/path/resourcename");

UserPreferencesUpgradeAction action = new UserPreferencesUpgradeAction("someUserId",resource);
UserPreferencesUpgradeAction action = new UserPreferencesUpgradeAction("someUserId",resource.adaptTo(Node.class));

assertThat(action.getContentHash()).as("User preferences action should have non-empty md5 hash").isNotEmpty();
}
Expand All @@ -38,8 +35,8 @@ public void shouldHaveDifferentMd5() throws RepositoryException {
Resource resource1 = sling.create().resource("/content/resource/path/resourcename1");
Resource resource2 = sling.create().resource("/content/resource/path/resourcename2");

UserPreferencesUpgradeAction action1 = new UserPreferencesUpgradeAction("someUserId", resource1);
UserPreferencesUpgradeAction action2 = new UserPreferencesUpgradeAction("someUserId", resource2);
UserPreferencesUpgradeAction action1 = new UserPreferencesUpgradeAction("someUserId", resource1.adaptTo(Node.class));
UserPreferencesUpgradeAction action2 = new UserPreferencesUpgradeAction("someUserId", resource2.adaptTo(Node.class));

assertThat(action1.getContentHash())
.as("Different upgrade actions should have different md5 hashes")
Expand All @@ -50,8 +47,8 @@ public void shouldHaveDifferentMd5() throws RepositoryException {
public void shouldUseUseridInMd5() throws RepositoryException {
Resource resource = sling.create().resource("/content/resource/path/resourcename");

UserPreferencesUpgradeAction action1 = new UserPreferencesUpgradeAction("someUserId",resource);
UserPreferencesUpgradeAction action2 = new UserPreferencesUpgradeAction("anotherUserId",resource);
UserPreferencesUpgradeAction action1 = new UserPreferencesUpgradeAction("someUserId",resource.adaptTo(Node.class));
UserPreferencesUpgradeAction action2 = new UserPreferencesUpgradeAction("anotherUserId",resource.adaptTo(Node.class));

assertThat(action1.getContentHash())
.as("The same upgrade configurations but for different users should have different md5 hash")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* (C) Copyright 2016 Netcentric AG.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*/
package biz.netcentric.vlt.upgrade.util;

import static org.assertj.core.api.Assertions.assertThat;

import javax.jcr.RepositoryException;

import org.junit.Test;

public class JsonNodeSerializerTest extends SerializerAbstractTest {

@Test
public void shouldReturnNonEmptyString() throws RepositoryException {
String result = new JsonNodeSerializer().serialize(node);
assertThat(result)
.as("Result of node's serialization should not be empty")
.isNotEmpty();
}

@Test
public void shouldSerializeNodeName() throws RepositoryException {
String result = new JsonNodeSerializer().serialize(node);
assertThat(result)
.as("Node's name should be serialized")
.contains(RESOURCE_NAME);
}

@Test
public void shouldSerializeNodeProperties() throws RepositoryException {
String result = new JsonNodeSerializer().serialize(node);
assertThat(result)
.as("Node's properties should be serialized")
.contains(PARENT_PROP)
.contains(PARENT_VALUE);
}

@Test
public void shouldSerializeChildNodeName() throws RepositoryException {
String result = new JsonNodeSerializer().serialize(node);
assertThat(result)
.as("Child node's name should be serialized")
.contains(CHILD_RESOURCE_NAME);
}

@Test
public void shouldSerializeChildNodeProperties() throws RepositoryException {
String result = new JsonNodeSerializer().serialize(node);
assertThat(result)
.as("Child node's properties should be serialized")
.contains(CHILD_PROP)
.contains(CHILD_VALUE);
}

@Test
public void shouldOmitProperties() throws RepositoryException {
String result = new JsonNodeSerializer().serialize(node);
assertThat(result)
.as("Auto created properties should not be used for serialization")
.doesNotContain("jcr:created");
}
}
Loading

0 comments on commit 982a1e6

Please sign in to comment.