import org.slf4j.LoggerFactory;
class JsonToCompositeNodeReader {
- private static final Logger LOG = LoggerFactory.getLogger(JsonReader.class);
+ private static final Logger LOG = LoggerFactory.getLogger(JsonToCompositeNodeReader.class);
private static final Splitter COLON_SPLITTER = Splitter.on(':');
private JsonToCompositeNodeReader() {
}
}
+ /**
+ * Transform input value to URI instance.
+ *
+ * Input string has to be in format moduleName:localName. moduleName part is then transformed to URI instance.
+ * If moduleName part contains character like "<" or ">" then null value is returned because they
+ * aren't valid URI characters.
+ *
+ * @param jsonElementName
+ * value in format moduleName:localName
+ * @return
+ */
private static URI getNamespaceFor(final String jsonElementName) {
final Iterator<String> it = COLON_SPLITTER.split(jsonElementName).iterator();
- // The string needs to me in form "moduleName:localName"
+ // The string needs to be in form "moduleName:localName"
if (it.hasNext()) {
final String maybeURI = it.next();
if (Iterators.size(it) == 1) {
- return URI.create(maybeURI);
+ try {
+ return URI.create(maybeURI);
+ } catch (IllegalArgumentException e) {
+ LOG.debug("Value {} couldn't be interpreted as URI.", maybeURI);
+ }
}
}
}
}
- // it could be identityref Built-In Type
+ // it could be identityref Built-In Type therefore it is necessary to look at value as module_name:local_name
URI namespace = getNamespaceFor(value);
if (namespace != null) {
return new IdentityValuesDTO(namespace.toString(), getLocalNameFor(value), null, value);
assertTrue(exceptionMessage.contains("Root element of Json has to be Object"));
}
+ /**
+ * Tests case when JSON input data value is in format string1:string2 and first string contain characters "<" or ">" (invalid URI characters).
+ *
+ * During loading data it is also interpreting as data value in moduleName:localName (potential leafref value).
+ * ModuleName part is transformed to URI which causes exception which is caught and URI value is null which cause that potential value in simple node is
+ * simple string (value from JSON input) and not IdentityValueDTO instance which is used for leaf-ref candidates.
+ */
+ @Test
+ public void invalidUriCharacterInValue() {
+ final Node<?> rootNode = TestUtils.readInputToCnSn("/json-to-cnsn/invalid-uri-character-in-value.json", true,
+ JsonToCompositeNodeProvider.INSTANCE);
+
+ assertTrue(rootNode instanceof CompositeNode);
+ Node<?> lf1 = null;
+ Node<?> lf2 = null;
+ for(Node<?> child : ((CompositeNode)rootNode).getChildren()) {
+ if (child.getNodeType().getLocalName().equals("lf1")) {
+ lf1 = child;
+ } else if (child.getNodeType().getLocalName().equals("lf2")) {
+ lf2 = child;
+ }
+ }
+
+ assertNotNull(lf1);
+ assertNotNull(lf2);
+ assertTrue(lf1 instanceof SimpleNode<?>);
+ assertTrue(lf2 instanceof SimpleNode<?>);
+
+ assertEquals("module<Name:value lf1", ((SimpleNode<?>) lf1).getValue());
+ assertEquals("module>Name:value lf2", ((SimpleNode<?>) lf2).getValue());
+ }
+
}