forked from Netcentric/vault-upgrade-hook
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Netcentric#24: remove redundant interface, remove sling dependency fr…
…om user preferences upgrader.
- Loading branch information
Showing
14 changed files
with
297 additions
and
190 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
vault-upgrade-hook/src/main/java/biz/netcentric/vlt/upgrade/util/JsonNodeSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
vault-upgrade-hook/src/main/java/biz/netcentric/vlt/upgrade/util/JsonResourceSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
16 changes: 0 additions & 16 deletions
16
vault-upgrade-hook/src/main/java/biz/netcentric/vlt/upgrade/util/ResourceSerializer.java
This file was deleted.
Oops, something went wrong.
61 changes: 0 additions & 61 deletions
61
...grade-hook/src/main/java/biz/netcentric/vlt/upgrade/util/impl/JsonResourceSerializer.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
vault-upgrade-hook/src/test/java/biz/netcentric/vlt/upgrade/util/JsonNodeSerializerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |
Oops, something went wrong.