super(schema);
}
- public AbstractNodeDataWithSchema addChild(final Deque<DataSchemaNode> schemas) {
+ public AbstractNodeDataWithSchema addChild(final Deque<DataSchemaNode> schemas, final boolean rootListItem) {
Preconditions.checkArgument(!schemas.isEmpty(), "Expecting at least one schema");
// Pop the first node...
final DataSchemaNode schema = schemas.pop();
if (schemas.isEmpty()) {
// Simple, direct node
- return addChild(schema);
+ return addChild(schema,rootListItem);
}
// The choice/case mess, reuse what we already popped
CompositeNodeDataWithSchema caseNodeDataWithSchema = findChoice(childNodes, choiceCandidate, caseCandidate);
if (caseNodeDataWithSchema == null) {
- ChoiceNodeDataWithSchema choiceNodeDataWithSchema = new ChoiceNodeDataWithSchema(choiceNode);
+ final ChoiceNodeDataWithSchema choiceNodeDataWithSchema = new ChoiceNodeDataWithSchema(choiceNode);
addChild(choiceNodeDataWithSchema);
- caseNodeDataWithSchema = choiceNodeDataWithSchema.addCompositeChild(caseNode);
+ caseNodeDataWithSchema = choiceNodeDataWithSchema.addCompositeChild(caseNode,rootListItem);
}
- return caseNodeDataWithSchema.addChild(schemas);
+ return caseNodeDataWithSchema.addChild(schemas, rootListItem);
}
private AbstractNodeDataWithSchema addSimpleChild(final DataSchemaNode schema) {
private CaseNodeDataWithSchema findChoice(final Collection<AbstractNodeDataWithSchema> childNodes, final DataSchemaNode choiceCandidate,
final DataSchemaNode caseCandidate) {
if (childNodes != null) {
- for (AbstractNodeDataWithSchema nodeDataWithSchema : childNodes) {
+ for (final AbstractNodeDataWithSchema nodeDataWithSchema : childNodes) {
if (nodeDataWithSchema instanceof ChoiceNodeDataWithSchema
&& nodeDataWithSchema.getSchema().getQName().equals(choiceCandidate.getQName())) {
- CaseNodeDataWithSchema casePrevious = ((ChoiceNodeDataWithSchema) nodeDataWithSchema).getCase();
+ final CaseNodeDataWithSchema casePrevious = ((ChoiceNodeDataWithSchema) nodeDataWithSchema).getCase();
Preconditions.checkArgument(casePrevious.getSchema().getQName().equals(caseCandidate.getQName()),
"Data from case %s are specified but other data from case %s were specified erlier. Data aren't from the same case.",
return null;
}
- AbstractNodeDataWithSchema addCompositeChild(final DataSchemaNode schema) {
+ AbstractNodeDataWithSchema addCompositeChild(final DataSchemaNode schema, final boolean rootListItem) {
CompositeNodeDataWithSchema newChild;
if (schema instanceof ListSchemaNode) {
newChild = new ListNodeDataWithSchema(schema);
+ /*
+ * If we are reading root we may want to emit map also for object which represent one list
+ * item.
+ * */
+ if(rootListItem) {
+ addCompositeChild(newChild);
+ final ListEntryNodeDataWithSchema entry = new ListEntryNodeDataWithSchema(schema);
+ newChild.addChild(entry);
+ return entry;
+ }
} else if (schema instanceof LeafListSchemaNode) {
newChild = new LeafListNodeDataWithSchema(schema);
} else if (schema instanceof ContainerSchemaNode) {
}
void addCompositeChild(final CompositeNodeDataWithSchema newChild) {
- AugmentationSchema augSchema = findCorrespondingAugment(getSchema(), newChild.getSchema());
+ final AugmentationSchema augSchema = findCorrespondingAugment(getSchema(), newChild.getSchema());
if (augSchema != null) {
augmentationsToChild.put(augSchema, newChild);
} else {
}
}
- private AbstractNodeDataWithSchema addChild(final DataSchemaNode schema) {
- AbstractNodeDataWithSchema newChild = addSimpleChild(schema);
- return newChild == null ? addCompositeChild(schema) : newChild;
+ private AbstractNodeDataWithSchema addChild(final DataSchemaNode schema, final boolean rootListItem) {
+ final AbstractNodeDataWithSchema newChild = addSimpleChild(schema);
+ return newChild == null ? addCompositeChild(schema,rootListItem) : newChild;
}
public void addChild(final AbstractNodeDataWithSchema newChild) {
*/
AugmentationSchema findCorrespondingAugment(final DataSchemaNode parent, final DataSchemaNode child) {
if (parent instanceof AugmentationTarget && !((parent instanceof ChoiceCaseNode) || (parent instanceof ChoiceSchemaNode))) {
- for (AugmentationSchema augmentation : ((AugmentationTarget) parent).getAvailableAugmentations()) {
- DataSchemaNode childInAugmentation = augmentation.getDataChildByName(child.getQName());
+ for (final AugmentationSchema augmentation : ((AugmentationTarget) parent).getAvailableAugmentations()) {
+ final DataSchemaNode childInAugmentation = augmentation.getDataChildByName(child.getQName());
if (childInAugmentation != null) {
return augmentation;
}
@Override
public void write(final NormalizedNodeStreamWriter writer) throws IOException {
- for (AbstractNodeDataWithSchema child : children) {
+ for (final AbstractNodeDataWithSchema child : children) {
child.write(writer);
}
- for (Entry<AugmentationSchema, Collection<AbstractNodeDataWithSchema>> augmentationToChild : augmentationsToChild.asMap().entrySet()) {
+ for (final Entry<AugmentationSchema, Collection<AbstractNodeDataWithSchema>> augmentationToChild : augmentationsToChild.asMap().entrySet()) {
final Collection<AbstractNodeDataWithSchema> childsFromAgumentation = augmentationToChild.getValue();
if (!childsFromAgumentation.isEmpty()) {
writer.startAugmentationNode(toAugmentationIdentifier(augmentationToChild.getKey()));
- for (AbstractNodeDataWithSchema nodeDataWithSchema : childsFromAgumentation) {
+ for (final AbstractNodeDataWithSchema nodeDataWithSchema : childsFromAgumentation) {
nodeDataWithSchema.write(writer);
}
package org.opendaylight.yangtools.yang.data.codec.gson;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.opendaylight.yangtools.yang.data.codec.gson.TestUtils.loadModules;
import static org.opendaylight.yangtools.yang.data.codec.gson.TestUtils.loadTextFile;
import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNormalizedNodeStreamWriter;
import org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeResult;
import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+import org.opendaylight.yangtools.yang.model.api.SchemaNode;
/**
*
@Test
public void leafNodeInContainer() throws IOException, URISyntaxException {
- String inputJson = loadTextFile("/complexjson/leaf-node-in-container.json");
+ final String inputJson = loadTextFile("/complexjson/leaf-node-in-container.json");
verifyTransformationToNormalizedNode(inputJson, TestingNormalizedNodeStructuresCreator.leafNodeInContainer());
}
@Test
public void leafNodeViaAugmentationInContainer() throws IOException, URISyntaxException {
- String inputJson = loadTextFile("/complexjson/leaf-node-via-augmentation-in-container.json");
+ final String inputJson = loadTextFile("/complexjson/leaf-node-via-augmentation-in-container.json");
verifyTransformationToNormalizedNode(inputJson,
TestingNormalizedNodeStructuresCreator.leafNodeViaAugmentationInContainer());
}
@Test
public void leafListNodeInContainer() throws IOException, URISyntaxException {
- String inputJson = loadTextFile("/complexjson/leaflist-node-in-container.json");
+ final String inputJson = loadTextFile("/complexjson/leaflist-node-in-container.json");
verifyTransformationToNormalizedNode(inputJson,
TestingNormalizedNodeStructuresCreator.leafListNodeInContainer());
}
@Test
public void keyedListNodeInContainer() throws IOException, URISyntaxException {
- String inputJson = loadTextFile("/complexjson/keyed-list-node-in-container.json");
+ final String inputJson = loadTextFile("/complexjson/keyed-list-node-in-container.json");
verifyTransformationToNormalizedNode(inputJson,
TestingNormalizedNodeStructuresCreator.keyedListNodeInContainer());
}
@Test
public void choiceNodeInContainer() throws IOException, URISyntaxException {
- String inputJson = loadTextFile("/complexjson/choice-node-in-container.json");
+ final String inputJson = loadTextFile("/complexjson/choice-node-in-container.json");
verifyTransformationToNormalizedNode(inputJson, TestingNormalizedNodeStructuresCreator.choiceNodeInContainer());
}
*/
@Test
public void caseNodeAugmentationInChoiceInContainer() throws IOException, URISyntaxException {
- String inputJson = loadTextFile("/complexjson/case-node-augmentation-in-choice-in-container.json");
+ final String inputJson = loadTextFile("/complexjson/case-node-augmentation-in-choice-in-container.json");
verifyTransformationToNormalizedNode(inputJson,
TestingNormalizedNodeStructuresCreator.caseNodeAugmentationInChoiceInContainer());
}
*/
@Test
public void caseNodeExternalAugmentationInChoiceInContainer() throws IOException, URISyntaxException {
- String inputJson = loadTextFile("/complexjson/case-node-external-augmentation-in-choice-in-container.json");
+ final String inputJson = loadTextFile("/complexjson/case-node-external-augmentation-in-choice-in-container.json");
verifyTransformationToNormalizedNode(inputJson,
TestingNormalizedNodeStructuresCreator.caseNodeExternalAugmentationInChoiceInContainer());
}
*/
@Test
public void choiceNodeAugmentationInContainer() throws IOException, URISyntaxException {
- String inputJson = loadTextFile("/complexjson/choice-node-augmentation-in-container.json");
+ final String inputJson = loadTextFile("/complexjson/choice-node-augmentation-in-container.json");
verifyTransformationToNormalizedNode(inputJson,
TestingNormalizedNodeStructuresCreator.choiceNodeAugmentationInContainer());
}
@Test
public void unkeyedNodeInContainer() throws IOException, URISyntaxException {
- String inputJson = loadTextFile("/complexjson/unkeyed-node-in-container.json");
+ final String inputJson = loadTextFile("/complexjson/unkeyed-node-in-container.json");
verifyTransformationToNormalizedNode(inputJson, TestingNormalizedNodeStructuresCreator.unkeyedNodeInContainer());
}
*/
@Test
public void missingModuleInfoInTopLevelElement() throws IOException, URISyntaxException {
- String inputJson = loadTextFile("/complexjson/missing-module-in-top-level.json");
+ final String inputJson = loadTextFile("/complexjson/missing-module-in-top-level.json");
verifyTransformationToNormalizedNode(inputJson, TestingNormalizedNodeStructuresCreator.topLevelContainer());
}
*/
@Test
public void leafNamesakes() throws IOException, URISyntaxException {
- String inputJson = loadTextFile("/complexjson/namesakes.json");
+ final String inputJson = loadTextFile("/complexjson/namesakes.json");
try {
//second parameter isn't necessary because error will be raised before it is used.
verifyTransformationToNormalizedNode(inputJson, null);
- } catch (IllegalStateException e) {
+ } catch (final IllegalStateException e) {
final String errorMessage = e.getMessage();
assertTrue(errorMessage.contains("Choose suitable module name for element lf11-namesake:"));
assertTrue(errorMessage.contains("complexjson-augmentation"));
*/
@Test
public void parsingNotExistingElement() throws IOException, URISyntaxException {
- String inputJson = loadTextFile("/complexjson/not-existing-element.json");
+ final String inputJson = loadTextFile("/complexjson/not-existing-element.json");
try {
//second parameter isn't necessary because error will be raised before it is used.
verifyTransformationToNormalizedNode(inputJson, null);
- } catch (IllegalStateException e) {
+ } catch (final IllegalStateException e) {
assertTrue(e.getMessage().contains("Schema node with name dummy-element wasn't found."));
}
}
+ @Test
+ public void listItemWithoutArray() throws IOException, URISyntaxException {
+ final String inputJson = loadTextFile("/complexjson/keyed-list-restconf-behaviour.json");
+
+ final NormalizedNodeResult result = new NormalizedNodeResult();
+ final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
+ final SchemaNode parentNode = schemaContext.getDataChildByName("cont1");
+ final JsonParserStream jsonParser = JsonParserStream.create(streamWriter, schemaContext, parentNode);
+ jsonParser.parse(new JsonReader(new StringReader(inputJson)));
+ final NormalizedNode<?, ?> transformedInput = result.getResult();
+ assertNotNull(transformedInput);
+ }
+
private void verifyTransformationToNormalizedNode(final String inputJson,
final NormalizedNode<?, ?> awaitedStructure) {
- NormalizedNodeResult result = new NormalizedNodeResult();
+ final NormalizedNodeResult result = new NormalizedNodeResult();
final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
- JsonParserStream jsonParser = JsonParserStream.create(streamWriter, schemaContext);
+ final JsonParserStream jsonParser = JsonParserStream.create(streamWriter, schemaContext);
jsonParser.parse(new JsonReader(new StringReader(inputJson)));
- NormalizedNode<?, ?> transformedInput = result.getResult();
+ final NormalizedNode<?, ?> transformedInput = result.getResult();
assertEquals("Transformation of json input to normalized node wasn't successful.", awaitedStructure,
transformedInput);
}