checkNotNull(parent);
checkNotNull(parentSchema);
- List<String> longestPathToElementViaChoiceCase = new ArrayList<>();
for (Node<?> child : parent.getChildren()) {
- Deque<String> choiceCasePathStack = new ArrayDeque<>(longestPathToElementViaChoiceCase);
- SchemaLocation schemaLocation = findFirstSchemaForNode(child, parentSchema.getChildNodes(),
- choiceCasePathStack);
-
- if (schemaLocation == null) {
- if (!choiceCasePathStack.isEmpty()) {
- throw new UnsupportedDataTypeException("On choice-case path " + choiceCasePathStack
- + " wasn't found data schema for " + child.getNodeType().getLocalName());
- } else {
- throw new UnsupportedDataTypeException("Probably the data node \""
- + child.getNodeType().getLocalName() + "\" is not conform to schema");
- }
- }
-
- longestPathToElementViaChoiceCase = resolveLongerPath(longestPathToElementViaChoiceCase,
- schemaLocation.getLocation());
+ DataSchemaNode childSchema = findFirstSchemaForNode(child, parentSchema.getChildNodes());
- DataSchemaNode childSchema = schemaLocation.getSchema();
+ if (childSchema == null) {
+ throw new UnsupportedDataTypeException("Probably the data node \"" + child.getNodeType().getLocalName()
+ + "\" is not conform to schema");
+ }
if (childSchema instanceof ContainerSchemaNode) {
Preconditions.checkState(child instanceof CompositeNode,
}
for (Node<?> child : parent.getChildren()) {
- SchemaLocation schemaLocation = findFirstSchemaForNode(child, parentSchema.getChildNodes(),
- new ArrayDeque<>(longestPathToElementViaChoiceCase));
-
- DataSchemaNode childSchema = schemaLocation.getSchema();
+ DataSchemaNode childSchema = findFirstSchemaForNode(child, parentSchema.getChildNodes());
if (childSchema instanceof LeafListSchemaNode) {
foundLeafLists.remove((LeafListSchemaNode) childSchema);
} else if (childSchema instanceof ListSchemaNode) {
}
}
- private List<String> resolveLongerPath(List<String> l1, List<String> l2) {
- return l1.size() > l2.size() ? l1 : l2;
- }
-
- private SchemaLocation findFirstSchemaForNode(Node<?> node, Set<DataSchemaNode> dataSchemaNode,
- Deque<String> pathIterator) {
- Map<String, ChoiceNode> choiceSubnodes = new HashMap<>();
+ private DataSchemaNode findFirstSchemaForNode(Node<?> node, Set<DataSchemaNode> dataSchemaNode) {
for (DataSchemaNode dsn : dataSchemaNode) {
- if (dsn instanceof ChoiceNode) {
- choiceSubnodes.put(dsn.getQName().getLocalName(), (ChoiceNode) dsn);
- } else if (node.getNodeType().getLocalName().equals(dsn.getQName().getLocalName())) {
- return new SchemaLocation(dsn);
- }
- }
-
- for (ChoiceNode choiceSubnode : choiceSubnodes.values()) {
- if ((!pathIterator.isEmpty() && pathIterator.peekLast().equals(choiceSubnode.getQName().getLocalName()))
- || pathIterator.isEmpty()) {
- String pathPartChoice = pathIterator.pollLast();
- for (ChoiceCaseNode concreteCase : choiceSubnode.getCases()) {
- if ((!pathIterator.isEmpty() && pathIterator.peekLast().equals(
- concreteCase.getQName().getLocalName()))
- || pathIterator.isEmpty()) {
- String pathPartCase = pathIterator.pollLast();
- SchemaLocation schemaLocation = findFirstSchemaForNode(node, concreteCase.getChildNodes(),
- pathIterator);
- if (schemaLocation != null) {
- schemaLocation.addPathPart(concreteCase.getQName().getLocalName());
- schemaLocation.addPathPart(choiceSubnode.getQName().getLocalName());
- return schemaLocation;
- }
- if (pathPartCase != null) {
- pathIterator.addLast(pathPartCase);
- }
+ if (node.getNodeType().getLocalName().equals(dsn.getQName().getLocalName())) {
+ return dsn;
+ } else if (dsn instanceof ChoiceNode) {
+ for (ChoiceCaseNode choiceCase : ((ChoiceNode) dsn).getCases()) {
+ DataSchemaNode foundDsn = findFirstSchemaForNode(node, choiceCase.getChildNodes());
+ if (foundDsn != null) {
+ return foundDsn;
}
}
- if (pathPartChoice != null) {
- pathIterator.addLast(pathPartChoice);
- }
}
}
return null;
}
/**
- * Test when some data are in one case node and other in another. Exception
- * expected!!
+ * Test when some data are in one case node and other in another. This isn't
+ * correct. Next Json validator should return error because nodes has to be
+ * from one case below concrete choice.
+ *
*/
@Test
- public void compNodeDataOnVariousChoiceCasePathTest() {
- boolean exceptionCatched = false;
+ public void nodeSchemasOnVariousChoiceCasePathTest() {
try {
TestUtils.writeCompNodeWithSchemaContextToJson(
- TestUtils.loadCompositeNode("/yang-to-json-conversion/choice/xml/data_various_path.xml"),
+ TestUtils.loadCompositeNode("/yang-to-json-conversion/choice/xml/data_various_path_err.xml"),
"/yang-to-json-conversion/choice/xml", modules, dataSchemaNode);
- } catch (UnsupportedDataTypeException e) {
- exceptionCatched = true;
-
} catch (WebApplicationException | IOException e) {
// shouldn't end here
assertTrue(false);
}
+ }
- assertTrue(exceptionCatched);
-
+ /**
+ * Test when some data are in one case node and other in another.
+ * Additionally data are loadef from various choices. This isn't
+ * correct. Next Json validator should return error because nodes has to be
+ * from one case below concrete choice.
+ *
+ */
+ @Test
+ public void nodeSchemasOnVariousChoiceCasePathAndMultipleChoicesTest() {
+ try {
+ TestUtils
+ .writeCompNodeWithSchemaContextToJson(
+ TestUtils
+ .loadCompositeNode("/yang-to-json-conversion/choice/xml/data_more_choices_same_level_various_paths_err.xml"),
+ "/yang-to-json-conversion/choice/xml", modules, dataSchemaNode);
+ } catch (WebApplicationException | IOException e) {
+ // shouldn't end here
+ assertTrue(false);
+ }
}
/**
* Test when second level data are red first, then first and at the end
* third level. Level represents pass through couple choice-case
*/
- @Ignore
+
@Test
- public void compNodeDataWithRandomOrderAccordingLevel() {
+ public void nodeSchemasWithRandomOrderAccordingLevel() {
try {
- String jsonOutput = TestUtils.writeCompNodeWithSchemaContextToJson(
+ TestUtils.writeCompNodeWithSchemaContextToJson(
TestUtils.loadCompositeNode("/yang-to-json-conversion/choice/xml/data_random_level.xml"),
"/yang-to-json-conversion/choice/xml", modules, dataSchemaNode);
+
} catch (WebApplicationException | IOException e) {
// shouldn't end here
assertTrue(false);
/**
* Test when element from no first case is used
*/
- @Ignore
@Test
- public void compNodeDataNoFirstCase() {
+ public void nodeSchemasNotInFirstCase() {
try {
- String jsonOutput = TestUtils.writeCompNodeWithSchemaContextToJson(
+ TestUtils.writeCompNodeWithSchemaContextToJson(
TestUtils.loadCompositeNode("/yang-to-json-conversion/choice/xml/data_no_first_case.xml"),
"/yang-to-json-conversion/choice/xml", modules, dataSchemaNode);
} catch (WebApplicationException | IOException e) {
/**
* Test when element in case is list
*/
- @Ignore
@Test
- public void compNodeDataAsList() {
+ public void nodeSchemaAsList() {
try {
- String jsonOutput = TestUtils.writeCompNodeWithSchemaContextToJson(
+ TestUtils.writeCompNodeWithSchemaContextToJson(
TestUtils.loadCompositeNode("/yang-to-json-conversion/choice/xml/data_list.xml"),
"/yang-to-json-conversion/choice/xml", modules, dataSchemaNode);
} catch (WebApplicationException | IOException e) {
/**
* Test when element in case is container
*/
- @Ignore
@Test
- public void compNodeDataAsContainer() {
+ public void nodeSchemaAsContainer() {
try {
- String jsonOutput = TestUtils.writeCompNodeWithSchemaContextToJson(
+ TestUtils.writeCompNodeWithSchemaContextToJson(
TestUtils.loadCompositeNode("/yang-to-json-conversion/choice/xml/data_container.xml"),
"/yang-to-json-conversion/choice/xml", modules, dataSchemaNode);
} catch (WebApplicationException | IOException e) {
}
/**
- * Test when element in case is container
+ * Test when element in case is leaflist
*/
- @Ignore
@Test
- public void compNodeDataAsLeafList() {
+ public void nodeSchemaAsLeafList() {
try {
- String jsonOutput = TestUtils.writeCompNodeWithSchemaContextToJson(
+ TestUtils.writeCompNodeWithSchemaContextToJson(
TestUtils.loadCompositeNode("/yang-to-json-conversion/choice/xml/data_leaflist.xml"),
"/yang-to-json-conversion/choice/xml", modules, dataSchemaNode);
} catch (WebApplicationException | IOException e) {
}
}
+ /**
+ *
+ */
+ @Test
+ public void nodeSchemasInMultipleChoicesTest() {
+ try {
+ TestUtils
+ .writeCompNodeWithSchemaContextToJson(TestUtils
+ .loadCompositeNode("/yang-to-json-conversion/choice/xml/data_more_choices_same_level.xml"),
+ "/yang-to-json-conversion/choice/xml", modules, dataSchemaNode);
+ } catch (WebApplicationException | IOException e) {
+ // shouldn't end here
+ assertTrue(false);
+ }
+ }
+
+ /**
+ * Test whether is possible to find data schema for node which is specified
+ * as dirrect subnode of choice (case without CASE key word)
+ */
+ @Test
+ public void nodeSchemasInCaseNotDefinedWithCaseKeyword() {
+ try {
+ TestUtils.writeCompNodeWithSchemaContextToJson(TestUtils
+ .loadCompositeNode("/yang-to-json-conversion/choice/xml/data_case_defined_without_case.xml"),
+ "/yang-to-json-conversion/choice/xml", modules, dataSchemaNode);
+ } catch (WebApplicationException | IOException e) {
+ // shouldn't end here
+ assertTrue(false);
+ }
+ }
+
+ /**
+ * Test of multiple use of choices
+ */
+ @Test
+ public void nodeSchemasInThreeChoicesAtSameLevel() {
+ try {
+ TestUtils.writeCompNodeWithSchemaContextToJson(TestUtils
+ .loadCompositeNode("/yang-to-json-conversion/choice/xml/data_three_choices_same_level.xml"),
+ "/yang-to-json-conversion/choice/xml", modules, dataSchemaNode);
+ } catch (WebApplicationException | IOException e) {
+ // shouldn't end here
+ assertTrue(false);
+ }
+ }
}