Gneeral cleanup and enable checkstyle.
Change-Id: Ib63901ef099b1b457504e2b8e11341ab357a394a
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
</dependency>
</dependencies>
- <!--
- Maven Site Configuration
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-checkstyle-plugin</artifactId>
+ <configuration>
+ <propertyExpansion>checkstyle.violationSeverity=error</propertyExpansion>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
- The following configuration is necessary for maven-site-plugin to
- correctly identify the correct deployment path for OpenDaylight Maven
- sites.
- -->
- <url>${odl.site.url}/${project.groupId}/${stream}/${project.artifactId}/</url>
+ <!--
+ Maven Site Configuration
- <distributionManagement>
- <site>
- <id>opendaylight-site</id>
- <url>${nexus.site.url}/${project.artifactId}/</url>
- </site>
- </distributionManagement>
+ The following configuration is necessary for maven-site-plugin to
+ correctly identify the correct deployment path for OpenDaylight Maven
+ sites.
+ -->
+ <url>${odl.site.url}/${project.groupId}/${stream}/${project.artifactId}/</url>
+
+ <distributionManagement>
+ <site>
+ <id>opendaylight-site</id>
+ <url>${nexus.site.url}/${project.artifactId}/</url>
+ </site>
+ </distributionManagement>
</project>
interface JSONCodec<T> extends TypeAwareCodec<T, Object, JsonWriter> {
/**
- * {@inheritDoc}
+ * {@inheritDoc}.
*
* @throws IOException if the write fails
*/
* Factory for creating JSON equivalents of codecs. Each instance of this object is bound to
* a particular {@link SchemaContext}, but can be reused by multiple {@link JSONNormalizedNodeStreamWriter}s.
*
+ * <p>
* There are multiple implementations available, each with distinct thread-safety, CPU/memory trade-offs and reuse
* characteristics. See {@link #getShared(SchemaContext)}, {@link #getPrecomputed(SchemaContext)},
* {@link #createLazy(SchemaContext)} and {@link #createSimple(SchemaContext)} for details.
* used by multiple threads concurrently. If the SchemaContext instance does not have a cached instance
* of {@link JSONCodecFactory}, it will be completely precomputed before this method will return.
*
+ * <p>
* Choosing this implementation is appropriate when the memory overhead of keeping a full codec tree is not as
* great a concern as predictable performance. When compared to the implementation returned by
* {@link #getShared(SchemaContext)}, this implementation is expected to offer higher performance and have lower
* peak memory footprint when most of the SchemaContext is actually in use.
*
+ * <p>
* For call sites which do not want to pay the CPU cost of pre-computing this implementation, but still would like
* to use it if is available (by being populated by some other caller), you can use
* {@link #getPrecomputedIfAvailable(SchemaContext)}.
* return the same instance as long as the associated SchemaContext is present or the factory is not invalidated
* by memory pressure. Returned object can be safely used by multiple threads concurrently.
*
+ * <p>
* Choosing this implementation is a safe default, as it will not incur prohibitive blocking, nor will it tie up
* memory in face of pressure.
*
* return distinct objects every time it is invoked. Returned object may not be used from multiple threads
* concurrently.
*
+ * <p>
* This implementation is appropriate for one-off serialization from a single thread. It will aggressively cache
* codecs for reuse and will tie them up in memory until the factory is freed.
*
* Create a simplistic, thread-safe {@link JSONCodecFactory} for a {@link SchemaContext}. This method will return
* distinct objects every time it is invoked. Returned object may be use from multiple threads concurrently.
*
+ * <p>
* This implementation exists mostly for completeness only, as it does not perform any caching at all and each codec
* is computed every time it is requested. This may be useful in extremely constrained environments, where memory
* footprint is more critical than performance.
/**
* This implementation will create JSON output as output stream.
*
+ * <p>
* Values of leaf and leaf-list are NOT translated according to codecs.
- *
*/
public final class JSONNormalizedNodeStreamWriter implements NormalizedNodeStreamWriter {
/**
private final JsonWriter writer;
private JSONStreamWriterContext context;
- private JSONNormalizedNodeStreamWriter(final JSONCodecFactory codecFactory, final SchemaPath path, final JsonWriter JsonWriter, final JSONStreamWriterRootContext rootContext) {
- this.writer = Preconditions.checkNotNull(JsonWriter);
+ private JSONNormalizedNodeStreamWriter(final JSONCodecFactory codecFactory, final SchemaPath path,
+ final JsonWriter writer, final JSONStreamWriterRootContext rootContext) {
+ this.writer = Preconditions.checkNotNull(writer);
this.codecs = Preconditions.checkNotNull(codecFactory);
this.tracker = SchemaTracker.create(codecFactory.getSchemaContext(), path);
this.context = Preconditions.checkNotNull(rootContext);
/**
* Create a new stream writer, which writes to the specified output stream.
*
+ * <p>
* The codec factory can be reused between multiple writers.
*
+ * <p>
* Returned writer is exclusive user of JsonWriter, which means it will start
* top-level JSON element and ends it.
*
+ * <p>
* This instance of writer can be used only to emit one top level element,
* otherwise it will produce incorrect JSON.
*
* @param jsonWriter JsonWriter
* @return A stream writer instance
*/
- public static NormalizedNodeStreamWriter createExclusiveWriter(final JSONCodecFactory codecFactory, final SchemaPath path, final URI initialNs, final JsonWriter jsonWriter) {
- return new JSONNormalizedNodeStreamWriter(codecFactory, path, jsonWriter, new JSONStreamWriterExclusiveRootContext(initialNs));
+ public static NormalizedNodeStreamWriter createExclusiveWriter(final JSONCodecFactory codecFactory,
+ final SchemaPath path, final URI initialNs, final JsonWriter jsonWriter) {
+ return new JSONNormalizedNodeStreamWriter(codecFactory, path, jsonWriter,
+ new JSONStreamWriterExclusiveRootContext(initialNs));
}
/**
* Create a new stream writer, which writes to the specified output stream.
*
+ * <p>
* The codec factory can be reused between multiple writers.
*
+ * <p>
* Returned writer can be used emit multiple top level element,
* but does not start / close parent JSON object, which must be done
* by user providing {@code jsonWriter} instance in order for
* @param jsonWriter JsonWriter
* @return A stream writer instance
*/
- public static NormalizedNodeStreamWriter createNestedWriter(final JSONCodecFactory codecFactory, final SchemaPath path, final URI initialNs, final JsonWriter jsonWriter) {
- return new JSONNormalizedNodeStreamWriter(codecFactory, path, jsonWriter, new JSONStreamWriterSharedRootContext(initialNs));
+ public static NormalizedNodeStreamWriter createNestedWriter(final JSONCodecFactory codecFactory,
+ final SchemaPath path, final URI initialNs, final JsonWriter jsonWriter) {
+ return new JSONNormalizedNodeStreamWriter(codecFactory, path, jsonWriter,
+ new JSONStreamWriterSharedRootContext(initialNs));
}
@Override
}
@SuppressWarnings("unchecked")
- private void writeValue(final Object value, final JSONCodec<?> codec)
- throws IOException {
- try {
- ((JSONCodec<Object>) codec).writeValue(writer, value);
- } catch (IOException | RuntimeException e) {
- throw e;
- } catch (Exception e) {
- throw new RuntimeException(e);
- }
+ private void writeValue(final Object value, final JSONCodec<?> codec) throws IOException {
+ ((JSONCodec<Object>) codec).writeValue(writer, value);
}
private void writeAnyXmlValue(final DOMSource anyXmlValue) throws IOException {
writer.beginObject();
inObject = true;
// name
- } else if (!inArray){
+ } else if (!inArray) {
writer.name(childNode.getNodeName());
}
}
import org.opendaylight.yangtools.yang.model.api.SchemaContext;
/**
- * Abstract base class for a single level of {@link JSONNormalizedNodeStreamWriter}
- * recursion. Provides the base API towards the writer, which is then specialized
- * by subclasses.
+ * Abstract base class for a single level of {@link JSONNormalizedNodeStreamWriter} recursion. Provides the base API
+ * towards the writer, which is then specialized by subclasses.
*/
abstract class JSONStreamWriterContext {
private final JSONStreamWriterContext parent;
}
/**
- * Write a child JSON node identifier, optionally prefixing it with the module name
- * corresponding to its namespace.
+ * Write a child JSON node identifier, optionally prefixing it with the module name corresponding to its namespace.
*
* @param schema Schema context
* @param writer Output writer
* @param qname Namespace/name tuple
* @throws IOException when the writer reports it
*/
- final void writeChildJsonIdentifier(final SchemaContext schema, final JsonWriter writer, final QName qname) throws IOException {
+ final void writeChildJsonIdentifier(final SchemaContext schema, final JsonWriter writer, final QName qname)
+ throws IOException {
final StringBuilder sb = new StringBuilder();
// Prepend module name if namespaces do not match
}
/**
- * Write our JSON node identifier, optionally prefixing it with the module name
- * corresponding to its namespace.
+ * Write our JSON node identifier, optionally prefixing it with the module name corresponding to its namespace.
*
* @param schema Schema context
* @param writer Output writer
* @param qname Namespace/name tuple
* @throws IOException when the writer reports it
*/
- protected final void writeMyJsonIdentifier(final SchemaContext schema, final JsonWriter writer, final QName qname) throws IOException {
+ protected final void writeMyJsonIdentifier(final SchemaContext schema, final JsonWriter writer, final QName qname)
+ throws IOException {
parent.writeChildJsonIdentifier(schema, writer, qname);
}
*
* @param schema Schema context
* @param writer Output writer
- * @throws IOException
+ * @throws IOException when the writer reports it
*/
- protected abstract void emitStart(final SchemaContext schema, final JsonWriter writer) throws IOException;
+ protected abstract void emitStart(SchemaContext schema, JsonWriter writer) throws IOException;
/**
* Emit the end of an element.
*
* @param schema Schema context
* @param writer Output writer
- * @throws IOException
+ * @throws IOException when writer reports it
*/
- protected abstract void emitEnd(final JsonWriter writer) throws IOException;
+ protected abstract void emitEnd(JsonWriter writer) throws IOException;
private void emitMyself(final SchemaContext schema, final JsonWriter writer) throws IOException {
if (!emittedMyself) {
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
import org.opendaylight.yangtools.yang.model.api.SchemaContext;
-
/**
* A recursion level of {@link JSONNormalizedNodeStreamWriter}, which represents
* a JSON object which has to be prefixed with its identifier -- such as a
* container.
*/
final class JSONStreamWriterNamedObjectContext extends JSONStreamWriterObjectContext {
- protected JSONStreamWriterNamedObjectContext(final JSONStreamWriterContext parent, final PathArgument arg, final boolean mandatory) {
+ protected JSONStreamWriterNamedObjectContext(final JSONStreamWriterContext parent, final PathArgument arg,
+ final boolean mandatory) {
super(parent, arg, mandatory);
}
* as when it is in a containing list.
*/
class JSONStreamWriterObjectContext extends JSONStreamWriterQNameContext {
- protected JSONStreamWriterObjectContext(final JSONStreamWriterContext parent, final PathArgument arg, final boolean mandatory) {
+ protected JSONStreamWriterObjectContext(final JSONStreamWriterContext parent, final PathArgument arg,
+ final boolean mandatory) {
super(Preconditions.checkNotNull(parent), arg.getNodeType(), mandatory);
}
abstract class JSONStreamWriterQNameContext extends JSONStreamWriterContext {
private final QName qname;
- protected JSONStreamWriterQNameContext(final JSONStreamWriterContext parent, final QName qname, final boolean mandatory) {
+ protected JSONStreamWriterQNameContext(final JSONStreamWriterContext parent, final QName qname,
+ final boolean mandatory) {
super(parent, mandatory);
this.qname = Preconditions.checkNotNull(qname);
}
import java.io.Flushable;
import java.io.IOException;
import java.net.URI;
+import java.util.AbstractMap.SimpleImmutableEntry;
import java.util.ArrayDeque;
import java.util.Collections;
import java.util.Deque;
import java.util.HashSet;
+import java.util.Map.Entry;
import java.util.Set;
import javax.xml.transform.dom.DOMSource;
import org.opendaylight.yangtools.util.xml.UntrustedXML;
traverseAnyXmlValue(in, doc, childElement);
}
in.endObject();
- case END_DOCUMENT:
- case NAME:
- case END_OBJECT:
- case END_ARRAY:
+ break;
+ default:
break;
}
}
public void read(final JsonReader in, AbstractNodeDataWithSchema parent) throws IOException {
switch (in.peek()) {
- case STRING:
- case NUMBER:
- setValue(parent, in.nextString());
- break;
- case BOOLEAN:
- setValue(parent, Boolean.toString(in.nextBoolean()));
- break;
- case NULL:
- in.nextNull();
- setValue(parent, null);
- break;
- case BEGIN_ARRAY:
- in.beginArray();
- while (in.hasNext()) {
- if (parent instanceof LeafNodeDataWithSchema) {
- read(in, parent);
- } else {
- final AbstractNodeDataWithSchema newChild = newArrayEntry(parent);
- read(in, newChild);
- }
- }
- in.endArray();
- return;
- case BEGIN_OBJECT:
- final Set<String> namesakes = new HashSet<>();
- in.beginObject();
- /*
- * This allows parsing of incorrectly /as showcased/
- * in testconf nesting of list items - eg.
- * lists with one value are sometimes serialized
- * without wrapping array.
- *
- */
- if (isArray(parent)) {
- parent = newArrayEntry(parent);
- }
- while (in.hasNext()) {
- final String jsonElementName = in.nextName();
- DataSchemaNode parentSchema = parent.getSchema();
- if (parentSchema instanceof YangModeledAnyXmlSchemaNode) {
- parentSchema = ((YangModeledAnyXmlSchemaNode) parentSchema).getSchemaOfAnyXmlData();
+ case STRING:
+ case NUMBER:
+ setValue(parent, in.nextString());
+ break;
+ case BOOLEAN:
+ setValue(parent, Boolean.toString(in.nextBoolean()));
+ break;
+ case NULL:
+ in.nextNull();
+ setValue(parent, null);
+ break;
+ case BEGIN_ARRAY:
+ in.beginArray();
+ while (in.hasNext()) {
+ if (parent instanceof LeafNodeDataWithSchema) {
+ read(in, parent);
+ } else {
+ final AbstractNodeDataWithSchema newChild = newArrayEntry(parent);
+ read(in, newChild);
+ }
}
- final NamespaceAndName namespaceAndName = resolveNamespace(jsonElementName, parentSchema);
- final String localName = namespaceAndName.getName();
- addNamespace(namespaceAndName.getUri());
- if (!namesakes.add(jsonElementName)) {
- throw new JsonSyntaxException("Duplicate name " + jsonElementName + " in JSON input.");
+ in.endArray();
+ return;
+ case BEGIN_OBJECT:
+ final Set<String> namesakes = new HashSet<>();
+ in.beginObject();
+ /*
+ * This allows parsing of incorrectly /as showcased/
+ * in testconf nesting of list items - eg.
+ * lists with one value are sometimes serialized
+ * without wrapping array.
+ *
+ */
+ if (isArray(parent)) {
+ parent = newArrayEntry(parent);
}
-
- final Deque<DataSchemaNode> childDataSchemaNodes = ParserStreamUtils.findSchemaNodeByNameAndNamespace(
- parentSchema, localName, getCurrentNamespace());
- Preconditions.checkState(!childDataSchemaNodes.isEmpty(),
- "Schema for node with name %s and namespace %s does not exist.", localName, getCurrentNamespace());
-
- final AbstractNodeDataWithSchema newChild = ((CompositeNodeDataWithSchema) parent)
- .addChild(childDataSchemaNodes);
- if (newChild instanceof AnyXmlNodeDataWithSchema) {
- readAnyXmlValue(in, (AnyXmlNodeDataWithSchema) newChild, jsonElementName);
- } else {
- read(in, newChild);
+ while (in.hasNext()) {
+ final String jsonElementName = in.nextName();
+ DataSchemaNode parentSchema = parent.getSchema();
+ if (parentSchema instanceof YangModeledAnyXmlSchemaNode) {
+ parentSchema = ((YangModeledAnyXmlSchemaNode) parentSchema).getSchemaOfAnyXmlData();
+ }
+ final Entry<String, URI> namespaceAndName = resolveNamespace(jsonElementName, parentSchema);
+ final String localName = namespaceAndName.getKey();
+ addNamespace(namespaceAndName.getValue());
+ if (!namesakes.add(jsonElementName)) {
+ throw new JsonSyntaxException("Duplicate name " + jsonElementName + " in JSON input.");
+ }
+
+ final Deque<DataSchemaNode> childDataSchemaNodes =
+ ParserStreamUtils.findSchemaNodeByNameAndNamespace(parentSchema, localName,
+ getCurrentNamespace());
+ Preconditions.checkState(!childDataSchemaNodes.isEmpty(),
+ "Schema for node with name %s and namespace %s does not exist.", localName,
+ getCurrentNamespace());
+
+ final AbstractNodeDataWithSchema newChild = ((CompositeNodeDataWithSchema) parent)
+ .addChild(childDataSchemaNodes);
+ if (newChild instanceof AnyXmlNodeDataWithSchema) {
+ readAnyXmlValue(in, (AnyXmlNodeDataWithSchema) newChild, jsonElementName);
+ } else {
+ read(in, newChild);
+ }
+ removeNamespace();
}
- removeNamespace();
- }
- in.endObject();
- return;
- case END_DOCUMENT:
- case NAME:
- case END_OBJECT:
- case END_ARRAY:
- break;
+ in.endObject();
+ return;
+ default:
+ break;
}
}
} else if (parent instanceof LeafListNodeDataWithSchema) {
newChild = new LeafListEntryNodeDataWithSchema(parent.getSchema());
} else {
- throw new IllegalStateException("Found an unexpected array nested under "+ parent.getSchema().getQName());
+ throw new IllegalStateException("Found an unexpected array nested under " + parent.getSchema().getQName());
}
((CompositeNodeDataWithSchema) parent).addChild(newChild);
return newChild;
namespaces.push(namespace);
}
- private NamespaceAndName resolveNamespace(final String childName, final DataSchemaNode dataSchemaNode) {
+ private Entry<String, URI> resolveNamespace(final String childName, final DataSchemaNode dataSchemaNode) {
final int lastIndexOfColon = childName.lastIndexOf(':');
String moduleNamePart = null;
String nodeNamePart = null;
} else if (potentialUris.size() == 1) {
namespace = potentialUris.iterator().next();
} else if (potentialUris.size() > 1) {
- throw new IllegalStateException("Choose suitable module name for element "+nodeNamePart+":"+toModuleNames(potentialUris));
+ throw new IllegalStateException("Choose suitable module name for element " + nodeNamePart + ":"
+ + toModuleNames(potentialUris));
} else if (potentialUris.isEmpty()) {
- throw new IllegalStateException("Schema node with name "+nodeNamePart+" wasn't found under "+dataSchemaNode.getQName()+".");
+ throw new IllegalStateException("Schema node with name " + nodeNamePart + " was not found under "
+ + dataSchemaNode.getQName() + ".");
}
}
- return new NamespaceAndName(nodeNamePart, namespace);
+ return new SimpleImmutableEntry<>(nodeNamePart, namespace);
}
private String toModuleNames(final Set<URI> potentialUris) {
final StringBuilder builder = new StringBuilder();
for (final URI potentialUri : potentialUris) {
- builder.append("\n");
+ builder.append('\n');
//FIXME how to get information about revision from JSON input? currently first available is used.
builder.append(schema.findModuleByNamespace(potentialUri).iterator().next().getName());
}
return namespaces.peek();
}
- private static class NamespaceAndName {
- private final URI uri;
- private final String name;
-
- public NamespaceAndName(final String name, final URI uri) {
- this.name = name;
- this.uri = uri;
- }
-
- public String getName() {
- return name;
- }
-
- public URI getUri() {
- return uri;
- }
- }
-
@Override
public void flush() throws IOException {
writer.flush();
import java.io.Writer;
/**
- * Factory Method class for JsonWriter creation
+ * Factory Method class for JsonWriter creation.
*/
@Beta
public final class JsonWriterFactory {
*
* @param <T> Deserialized value type
*/
-final class NumberJSONCodec<T extends Number> extends AbstractJSONCodec<T > {
+final class NumberJSONCodec<T extends Number> extends AbstractJSONCodec<T> {
NumberJSONCodec(final DataStringCodec<T> codec) {
super(codec);
}
}
@Override
+ @SuppressWarnings("checkstyle:illegalCatch")
public final T parseValue(final Object ctx, final String str) {
for (JSONCodec<?> codec : codecs) {
final Object ret;
}
@Override
+ @SuppressWarnings("checkstyle:illegalCatch")
public final void writeValue(final JsonWriter ctx, final T value) throws IOException {
for (JSONCodec<?> codec : codecs) {
if (!codec.getDataType().isInstance(value)) {
// serialization
final Writer writer = new StringWriter();
- final NormalizedNodeStreamWriter jsonStream = JSONNormalizedNodeStreamWriter.
- createExclusiveWriter(JSONCodecFactory.getShared(schemaContext), SchemaPath.ROOT, null,
- JsonWriterFactory.createJsonWriter(writer, 2));
+ final NormalizedNodeStreamWriter jsonStream = JSONNormalizedNodeStreamWriter.createExclusiveWriter(
+ JSONCodecFactory.getShared(schemaContext), SchemaPath.ROOT, null,
+ JsonWriterFactory.createJsonWriter(writer, 2));
final NormalizedNodeWriter nodeWriter = NormalizedNodeWriter.forStreamWriter(jsonStream);
nodeWriter.write(transformedInput);
nodeWriter.close();
// serialization
final Writer writer = new StringWriter();
- final NormalizedNodeStreamWriter jsonStream = JSONNormalizedNodeStreamWriter.
- createExclusiveWriter(JSONCodecFactory.getShared(schemaContext), SchemaPath.ROOT, null,
- JsonWriterFactory.createJsonWriter(writer, 2));
+ final NormalizedNodeStreamWriter jsonStream = JSONNormalizedNodeStreamWriter.createExclusiveWriter(
+ JSONCodecFactory.getShared(schemaContext), SchemaPath.ROOT, null,
+ JsonWriterFactory.createJsonWriter(writer, 2));
final NormalizedNodeWriter nodeWriter = NormalizedNodeWriter.forStreamWriter(jsonStream);
nodeWriter.write(transformedInput);
nodeWriter.close();
private static DOMSource getParsedAnyXmlValue(final NormalizedNode<?, ?> transformedInput, final QName anyxmlName) {
assertTrue(transformedInput instanceof ContainerNode);
final ContainerNode cont1 = (ContainerNode) transformedInput;
- final DataContainerChild<? extends PathArgument, ?> child = cont1.getChild(new NodeIdentifier(anyxmlName)).get();
- assertNotNull(child);
+ final DataContainerChild<? extends PathArgument, ?> child = cont1.getChild(new NodeIdentifier(anyxmlName))
+ .get();
assertTrue(child instanceof AnyXmlNode);
final AnyXmlNode anyXmlNode = (AnyXmlNode) child;
return anyXmlNode.getValue();
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
+
import com.google.common.base.Optional;
import com.google.gson.stream.JsonReader;
import java.io.File;
assertTrue(transformedInput instanceof ContainerNode);
ContainerNode root = (ContainerNode) transformedInput;
- Optional<DataContainerChild<? extends PathArgument, ?>> ref1 = root.getChild(NodeIdentifier.create((QName
- .create("foo", "2016-01-22", "ref1"))));
- Optional<DataContainerChild<? extends PathArgument, ?>> ref2 = root.getChild(NodeIdentifier.create((QName
- .create("foo", "2016-01-22", "ref2"))));
- Optional<DataContainerChild<? extends PathArgument, ?>> ref3 = root.getChild(NodeIdentifier.create((QName
- .create("foo", "2016-01-22", "ref3"))));
- Optional<DataContainerChild<? extends PathArgument, ?>> ref4 = root.getChild(NodeIdentifier.create((QName
- .create("foo", "2016-01-22", "ref4"))));
+ final Optional<DataContainerChild<? extends PathArgument, ?>> ref1 = root.getChild(NodeIdentifier.create(
+ QName.create("foo", "2016-01-22", "ref1")));
+ final Optional<DataContainerChild<? extends PathArgument, ?>> ref2 = root.getChild(NodeIdentifier.create(
+ QName.create("foo", "2016-01-22", "ref2")));
+ final Optional<DataContainerChild<? extends PathArgument, ?>> ref3 = root.getChild(NodeIdentifier.create(
+ QName.create("foo", "2016-01-22", "ref3")));
+ final Optional<DataContainerChild<? extends PathArgument, ?>> ref4 = root.getChild(NodeIdentifier.create(
+ QName.create("foo", "2016-01-22", "ref4")));
assertTrue(ref1.isPresent());
assertTrue(ref2.isPresent());
assertTrue(ref3.isPresent());
assertTrue(ref4.isPresent());
- Object value1 = ref1.get().getValue();
- Object value2 = ref2.get().getValue();
- Object value3 = ref3.get().getValue();
- Object value4 = ref4.get().getValue();
+ final Object value1 = ref1.get().getValue();
+ final Object value2 = ref2.get().getValue();
+ final Object value3 = ref3.get().getValue();
+ final Object value4 = ref4.get().getValue();
assertTrue(value1 instanceof Set);
assertTrue(value2 instanceof Set);
assertTrue(value3 instanceof Set);
assertTrue(value4 instanceof Set);
- Set<?> set1 = (Set<?>) value1;
- Set<?> set2 = (Set<?>) value2;
- Set<?> set3 = (Set<?>) value3;
- Set<?> set4 = (Set<?>) value4;
+ final Set<?> set1 = (Set<?>) value1;
+ final Set<?> set2 = (Set<?>) value2;
+ final Set<?> set3 = (Set<?>) value3;
+ final Set<?> set4 = (Set<?>) value4;
assertEquals(1, set1.size());
assertEquals(2, set2.size());
import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
public class Bug7246Test {
- private static String NS = "my-namespace";
- private static String REV = "1970-01-01";
+ private static final String NS = "my-namespace";
+ private static final String REV = "1970-01-01";
@Test
public void test() throws Exception {
assertEquals(expextedJson, serializedJson);
}
- private QName qN(final String localName) {
+ private static QName qN(final String localName) {
return QName.create(NS, REV, localName);
}
import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
/**
- *
- * Each test tests whether json input is correctly transformed to normalized node structure
+ * Each test tests whether json input is correctly transformed to normalized node structure.
*/
public class JsonStreamToNormalizedNodeTest {
}
/**
- * Test of translating internal augmentations to normalized nodes structure
+ * Test of translating internal augmentations to normalized nodes structure.
*
+ * <p>
* 2 nodes are added via internal augmentation A, 1 node via internal augmentation B and one node is originally
* member of case.
- *
*/
@Test
public void caseNodeAugmentationInChoiceInContainer() throws IOException, URISyntaxException {
}
/**
- * also test using of namesakes (equal local names with different
- *
- * @throws IOException
- * @throws URISyntaxException
+ * also test using of namesakes (equal local names with different.
*/
@Test
public void caseNodeExternalAugmentationInChoiceInContainer() throws IOException, URISyntaxException {
- final 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());
}
/**
- * augmentation of choice - adding new case
+ * augmentation of choice - adding new case.
*/
@Test
public void choiceNodeAugmentationInContainer() throws IOException, URISyntaxException {
@Test
public void unkeyedNodeInContainer() throws IOException, URISyntaxException {
final String inputJson = loadTextFile("/complexjson/unkeyed-node-in-container.json");
- verifyTransformationToNormalizedNode(inputJson, TestingNormalizedNodeStructuresCreator.unkeyedNodeInContainer());
+ verifyTransformationToNormalizedNode(inputJson,
+ TestingNormalizedNodeStructuresCreator.unkeyedNodeInContainer());
}
/**
* Top level JSON element contains no information about module name.
*
+ * <p>
* It should be possible to find out potential module name from available schema context.
- *
*/
@Test
public void missingModuleInfoInTopLevelElement() throws IOException, URISyntaxException {
}
/**
- *
* Exception expected.
*
+ * <p>
* It tests case when several elements with the same name and various namespaces exists and are in JSON specified
* without module name prefix.
*/
}
/**
- *
* Exception expected.
*
+ * <p>
* Json input contains element which doesn't exist in YANG schema
*/
@Test
//second parameter isn't necessary because error will be raised before it is used.
verifyTransformationToNormalizedNode(inputJson, null);
} catch (final IllegalStateException e) {
- assertTrue(e.getMessage().contains("Schema node with name dummy-element wasn't found"));
+ assertTrue(e.getMessage().contains("Schema node with name dummy-element was not found"));
}
}
-
@Test
public void listItemWithoutArray() throws IOException, URISyntaxException {
final String inputJson = loadTextFile("/complexjson/keyed-list-restconf-behaviour.json");
assertNotNull(transformedInput);
}
- @Test
+ @Test
public void multipleChoiceAugmentation() throws IOException, URISyntaxException {
final String inputJson = loadTextFile("/complexjson/multiple-choice-augmentation-in-container.json");
assertEquals("Transformation of json input to normalized node wasn't successful.", awaitedStructure,
transformedInput);
}
-
}
* Each test tests whether json output obtained after transformation contains is corect. The transformation takes
* normalized node data structure and transform it to json output. To make it easier validate json output it is loaded
* via gson as structure of json elements which are walked and compared with awaited values.
- *
*/
public class NormalizedNodeToJsonStreamTest {
private static final QName EMPTY_LEAF = QName.create(CONT_1, "empty");
private static SchemaContext schemaContext;
- public interface JsonValidator {
- void validate(final String jsonOutput);
- }
-
@BeforeClass
public static void initialization() throws IOException, URISyntaxException, ReactorException {
schemaContext = YangParserTestUtils.parseYangSources("/complexjson/yang");
final Writer writer = new StringWriter();
final NormalizedNode<?, ?> leafNodeInContainer = TestingNormalizedNodeStructuresCreator.leafNodeInContainer();
final String jsonOutput = normalizedNodeToJsonStreamTransformation(writer, leafNodeInContainer);
- ((JsonValidator) jsonOutput1 -> {
- final JsonObject cont1 = resolveCont1(jsonOutput1);
- assertNotNull(cont1);
-
- final JsonPrimitive lf11 = childPrimitive(cont1, "complexjson:lf11", "lf11");
- assertNotNull(lf11);
- final int asInt = lf11.getAsInt();
- assertEquals(453, asInt);
- }).validate(jsonOutput);
+ final JsonObject cont1 = resolveCont1(jsonOutput);
+ assertNotNull(cont1);
+ final JsonPrimitive lf11 = childPrimitive(cont1, "complexjson:lf11", "lf11");
+ assertNotNull(lf11);
+ final int asInt = lf11.getAsInt();
+ assertEquals(453, asInt);
}
@Test
final NormalizedNode<?, ?> leafListNodeInContainer = TestingNormalizedNodeStructuresCreator
.leafListNodeInContainerMultiline();
final String jsonOutput = normalizedNodeToJsonStreamTransformation(writer, leafListNodeInContainer);
- ((JsonValidator) jsonOutput1 -> {
- final JsonObject cont1 = resolveCont1(jsonOutput1);
- assertNotNull(cont1);
- final JsonArray lflst11 = childArray(cont1, "complexjson:lflst11", "lflst11");
- assertNotNull(lflst11);
-
- final HashSet<Object> lflst11Values = Sets.newHashSet();
- for (final JsonElement jsonElement : lflst11) {
- assertTrue(jsonElement instanceof JsonPrimitive);
- lflst11Values.add(jsonElement.getAsString());
- }
-
- assertEquals(Sets.newHashSet("lflst11 value2\r\nanother line 2", "lflst11 value1\nanother line 1"),
- lflst11Values);
- }).validate(jsonOutput);
-
+ final JsonObject cont1 = resolveCont1(jsonOutput);
+ assertNotNull(cont1);
+ final JsonArray lflst11 = childArray(cont1, "complexjson:lflst11", "lflst11");
+ assertNotNull(lflst11);
+
+ final HashSet<Object> lflst11Values = Sets.newHashSet();
+ for (final JsonElement jsonElement : lflst11) {
+ assertTrue(jsonElement instanceof JsonPrimitive);
+ lflst11Values.add(jsonElement.getAsString());
+ }
+
+ assertEquals(Sets.newHashSet("lflst11 value2\r\nanother line 2", "lflst11 value1\nanother line 1"),
+ lflst11Values);
}
@Test
final NormalizedNode<?, ?> leafNodeViaAugmentationInContainer = TestingNormalizedNodeStructuresCreator
.leafNodeViaAugmentationInContainer();
final String jsonOutput = normalizedNodeToJsonStreamTransformation(writer, leafNodeViaAugmentationInContainer);
- ((JsonValidator) jsonOutput1 -> {
- final JsonObject cont1 = resolveCont1(jsonOutput1);
- assertNotNull(cont1);
-
- final JsonPrimitive lf12_1 = childPrimitive(cont1, "complexjson:lf12_1", "lf12_1");
- assertNotNull(lf12_1);
- final String asString = lf12_1.getAsString();
- assertEquals("lf12 value", asString);
- }).validate(jsonOutput);
+ final JsonObject cont1 = resolveCont1(jsonOutput);
+ assertNotNull(cont1);
+ final JsonPrimitive lf12_1 = childPrimitive(cont1, "complexjson:lf12_1", "lf12_1");
+ assertNotNull(lf12_1);
+ final String asString = lf12_1.getAsString();
+ assertEquals("lf12 value", asString);
}
@Test
final NormalizedNode<?, ?> leafListNodeInContainer = TestingNormalizedNodeStructuresCreator
.leafListNodeInContainer();
final String jsonOutput = normalizedNodeToJsonStreamTransformation(writer, leafListNodeInContainer);
- ((JsonValidator) jsonOutput1 -> {
- final JsonObject cont1 = resolveCont1(jsonOutput1);
- assertNotNull(cont1);
- final JsonArray lflst11 = childArray(cont1, "complexjson:lflst11", "lflst11");
- assertNotNull(lflst11);
-
- final HashSet<Object> lflst11Values = Sets.newHashSet();
- for (final JsonElement jsonElement : lflst11) {
- assertTrue(jsonElement instanceof JsonPrimitive);
- lflst11Values.add(jsonElement.getAsString());
- }
-
- assertEquals(Sets.newHashSet("lflst11 value2", "lflst11 value1"), lflst11Values);
- }).validate(jsonOutput);
+ final JsonObject cont1 = resolveCont1(jsonOutput);
+ assertNotNull(cont1);
+ final JsonArray lflst11 = childArray(cont1, "complexjson:lflst11", "lflst11");
+ assertNotNull(lflst11);
+
+ final HashSet<Object> lflst11Values = Sets.newHashSet();
+ for (final JsonElement jsonElement : lflst11) {
+ assertTrue(jsonElement instanceof JsonPrimitive);
+ lflst11Values.add(jsonElement.getAsString());
+ }
+
+ assertEquals(Sets.newHashSet("lflst11 value2", "lflst11 value1"), lflst11Values);
}
@Test
final NormalizedNode<?, ?> keyedListNodeInContainer = TestingNormalizedNodeStructuresCreator
.keyedListNodeInContainer();
final String jsonOutput = normalizedNodeToJsonStreamTransformation(writer, keyedListNodeInContainer);
- ((JsonValidator) jsonOutput1 -> {
- final JsonObject cont1 = resolveCont1(jsonOutput1);
- assertNotNull(cont1);
- final JsonArray lst11 = childArray(cont1, "complexjson:lst11", "lst11");
- assertNotNull(lst11);
-
- final Iterator<JsonElement> iterator = lst11.iterator();
- assertTrue(iterator.hasNext());
- final JsonElement lst11Entry1Raw = iterator.next();
- assertFalse(iterator.hasNext());
- assertTrue(lst11Entry1Raw instanceof JsonObject);
- final JsonObject lst11Entry1 = (JsonObject) lst11Entry1Raw;
-
- final JsonPrimitive key111 = childPrimitive(lst11Entry1, "complexjson:key111", "key111");
- assertNotNull(key111);
- final JsonPrimitive lf112 = childPrimitive(lst11Entry1, "complexjson:lf112", "lf112");
- assertNotNull(lf112);
- final JsonPrimitive lf113 = childPrimitive(lst11Entry1, "complexjson:lf113", "lf113");
- assertNotNull(lf113);
- final JsonPrimitive lf111 = childPrimitive(lst11Entry1, "complexjson:lf111", "lf111");
- assertNotNull(lf111);
-
- assertEquals("key111 value", key111.getAsString());
- assertEquals("/complexjson:cont1/complexjson:lflst11[.='foo']", lf112.getAsString());
- assertEquals("lf113 value", lf113.getAsString());
- assertEquals("lf111 value", lf111.getAsString());
- }).validate(jsonOutput);
+ final JsonObject cont1 = resolveCont1(jsonOutput);
+ assertNotNull(cont1);
+ final JsonArray lst11 = childArray(cont1, "complexjson:lst11", "lst11");
+ assertNotNull(lst11);
+
+ final Iterator<JsonElement> iterator = lst11.iterator();
+ assertTrue(iterator.hasNext());
+ final JsonElement lst11Entry1Raw = iterator.next();
+ assertFalse(iterator.hasNext());
+ assertTrue(lst11Entry1Raw instanceof JsonObject);
+ final JsonObject lst11Entry1 = (JsonObject) lst11Entry1Raw;
+
+ final JsonPrimitive key111 = childPrimitive(lst11Entry1, "complexjson:key111", "key111");
+ assertNotNull(key111);
+ final JsonPrimitive lf112 = childPrimitive(lst11Entry1, "complexjson:lf112", "lf112");
+ assertNotNull(lf112);
+ final JsonPrimitive lf113 = childPrimitive(lst11Entry1, "complexjson:lf113", "lf113");
+ assertNotNull(lf113);
+ final JsonPrimitive lf111 = childPrimitive(lst11Entry1, "complexjson:lf111", "lf111");
+ assertNotNull(lf111);
+
+ assertEquals("key111 value", key111.getAsString());
+ assertEquals("/complexjson:cont1/complexjson:lflst11[.='foo']", lf112.getAsString());
+ assertEquals("lf113 value", lf113.getAsString());
+ assertEquals("lf111 value", lf111.getAsString());
}
@Test
final NormalizedNode<?, ?> choiceNodeInContainer = TestingNormalizedNodeStructuresCreator
.choiceNodeInContainer();
final String jsonOutput = normalizedNodeToJsonStreamTransformation(writer, choiceNodeInContainer);
- ((JsonValidator) jsonOutput1 -> {
- final JsonObject cont1 = resolveCont1(jsonOutput1);
- assertNotNull(cont1);
- final JsonPrimitive lf13 = childPrimitive(cont1, "complexjson:lf13", "lf13");
- assertNotNull(lf13);
-
- assertEquals("lf13 value", lf13.getAsString());
- }).validate(jsonOutput);
+ final JsonObject cont1 = resolveCont1(jsonOutput);
+ assertNotNull(cont1);
+ final JsonPrimitive lf13 = childPrimitive(cont1, "complexjson:lf13", "lf13");
+ assertNotNull(lf13);
+
+ assertEquals("lf13 value", lf13.getAsString());
}
/**
* tested case when case c11A in choice choc11 is augmented (two leaves (augment A) and one leaf (augment B) are
- * added)
- *
- * after running this test following exception is raised
+ * added).
*
+ * <p>
+ * after running this test following exception is raised:
* java.lang.IllegalArgumentException: Augmentation allowed only in DataNodeContainer
* [ChoiceNodeImpl[qname=(ns:complex:json?revision=2014-08-11)choc11]]
- *
*/
- // @Ignore
@Test
public void caseNodeAugmentationInChoiceInContainer() throws IOException, URISyntaxException {
final Writer writer = new StringWriter();
.caseNodeAugmentationInChoiceInContainer();
final String jsonOutput = normalizedNodeToJsonStreamTransformation(writer,
caseNodeAugmentationInChoiceInContainer);
- ((JsonValidator) jsonOutput1 -> {
- final JsonObject cont1 = resolveCont1(jsonOutput1);
- assertNotNull(cont1);
-
- final JsonPrimitive lf15_21 = childPrimitive(cont1, "complexjson:lf15_21", "lf15_21");
- assertNotNull(lf15_21);
- final JsonPrimitive lf13 = childPrimitive(cont1, "complexjson:lf13", "lf13");
- assertNotNull(lf13);
- final JsonPrimitive lf15_11 = childPrimitive(cont1, "complexjson:lf15_11", "lf15_11");
- assertNotNull(lf15_11);
- final JsonPrimitive lf15_12 = childPrimitive(cont1, "complexjson:lf15_12", "lf15_12");
- assertNotNull(lf15_12);
-
- assertEquals("lf15_21 value", lf15_21.getAsString());
- assertEquals("lf13 value", lf13.getAsString());
- assertTrue("one two".equals(lf15_11.getAsString()) || "two one".equals(lf15_11.getAsString()));
- assertEquals("complexjson:lf11", lf15_12.getAsString());
-
- }).validate(jsonOutput);
+ final JsonObject cont1 = resolveCont1(jsonOutput);
+ assertNotNull(cont1);
+
+ final JsonPrimitive lf15_21 = childPrimitive(cont1, "complexjson:lf15_21", "lf15_21");
+ assertNotNull(lf15_21);
+ final JsonPrimitive lf13 = childPrimitive(cont1, "complexjson:lf13", "lf13");
+ assertNotNull(lf13);
+ final JsonPrimitive lf15_11 = childPrimitive(cont1, "complexjson:lf15_11", "lf15_11");
+ assertNotNull(lf15_11);
+ final JsonPrimitive lf15_12 = childPrimitive(cont1, "complexjson:lf15_12", "lf15_12");
+ assertNotNull(lf15_12);
+
+ assertEquals("lf15_21 value", lf15_21.getAsString());
+ assertEquals("lf13 value", lf13.getAsString());
+ assertTrue("one two".equals(lf15_11.getAsString()) || "two one".equals(lf15_11.getAsString()));
+ assertEquals("complexjson:lf11", lf15_12.getAsString());
}
/**
* tested case when case c11A in choice choc11 is augmented (two leaves (augment A) internally and one two leaves
- * with the same names externally (augment B) are added)
- *
- * after running this test following exception is raised
+ * with the same names externally (augment B) are added).
*
+ * <p>
+ * after running this test following exception is raised:
* java.lang.IllegalArgumentException: Augmentation allowed only in DataNodeContainer
* [ChoiceNodeImpl[qname=(ns:complex:json?revision=2014-08-11)choc11]]
- *
*/
- // @Ignore
@Test
public void caseNodeExternalAugmentationInChoiceInContainer() throws IOException, URISyntaxException {
final Writer writer = new StringWriter();
- final NormalizedNode<?, ?> caseNodeExternalAugmentationInChoiceInContainer = TestingNormalizedNodeStructuresCreator
- .caseNodeExternalAugmentationInChoiceInContainer();
+ final NormalizedNode<?, ?> caseNodeExternalAugmentationInChoiceInContainer =
+ TestingNormalizedNodeStructuresCreator.caseNodeExternalAugmentationInChoiceInContainer();
final String jsonOutput = normalizedNodeToJsonStreamTransformation(writer,
caseNodeExternalAugmentationInChoiceInContainer);
- ((JsonValidator) jsonOutput1 -> {
- final JsonObject cont1 = resolveCont1(jsonOutput1);
- assertNotNull(cont1);
-
- final JsonPrimitive lf15_11Augment = childPrimitive(cont1, "complexjson-augmentation:lf15_11");
- assertNotNull(lf15_11Augment);
- final JsonPrimitive lf15_12Augment = childPrimitive(cont1, "complexjson-augmentation:lf15_12");
- assertNotNull(lf15_12Augment);
- final JsonPrimitive lf13 = childPrimitive(cont1, "complexjson:lf13", "lf13");
- assertNotNull(lf13);
- final JsonPrimitive lf15_11 = childPrimitive(cont1, "complexjson:lf15_11", "lf15_11");
- assertNotNull(lf15_11);
- final JsonPrimitive lf15_12 = childPrimitive(cont1, "complexjson:lf15_12", "lf15_12");
- assertNotNull(lf15_12);
-
- assertEquals("lf15_11 value from augmentation", lf15_11Augment.getAsString());
- assertEquals("lf15_12 value from augmentation", lf15_12Augment.getAsString());
- assertEquals("lf13 value", lf13.getAsString());
- assertTrue("one two".equals(lf15_11.getAsString()) || "two one".equals(lf15_11.getAsString()));
- assertEquals("complexjson:lf11", lf15_12.getAsString());
-
- }).validate(jsonOutput);
+ final JsonObject cont1 = resolveCont1(jsonOutput);
+ assertNotNull(cont1);
+
+ final JsonPrimitive lf15_11Augment = childPrimitive(cont1, "complexjson-augmentation:lf15_11");
+ assertNotNull(lf15_11Augment);
+ final JsonPrimitive lf15_12Augment = childPrimitive(cont1, "complexjson-augmentation:lf15_12");
+ assertNotNull(lf15_12Augment);
+ final JsonPrimitive lf13 = childPrimitive(cont1, "complexjson:lf13", "lf13");
+ assertNotNull(lf13);
+ final JsonPrimitive lf15_11 = childPrimitive(cont1, "complexjson:lf15_11", "lf15_11");
+ assertNotNull(lf15_11);
+ final JsonPrimitive lf15_12 = childPrimitive(cont1, "complexjson:lf15_12", "lf15_12");
+ assertNotNull(lf15_12);
+
+ assertEquals("lf15_11 value from augmentation", lf15_11Augment.getAsString());
+ assertEquals("lf15_12 value from augmentation", lf15_12Augment.getAsString());
+ assertEquals("lf13 value", lf13.getAsString());
+ assertTrue("one two".equals(lf15_11.getAsString()) || "two one".equals(lf15_11.getAsString()));
+ assertEquals("complexjson:lf11", lf15_12.getAsString());
}
/**
- * augmentation of choice - adding new case
- *
- * after running this test following exception is raised
+ * augmentation of choice - adding new case.
*
+ * <p>
+ * after running this test following exception is raised:
* java.lang.IllegalArgumentException: Augmentation allowed only in DataNodeContainer
* [ChoiceNodeImpl[qname=(ns:complex:json?revision=2014-08-11)choc11]]
- *
*/
- // @Ignore
@Test
public void choiceNodeAugmentationInContainer() throws IOException, URISyntaxException {
final Writer writer = new StringWriter();
final NormalizedNode<?, ?> choiceNodeAugmentationInContainer = TestingNormalizedNodeStructuresCreator
.choiceNodeAugmentationInContainer();
final String jsonOutput = normalizedNodeToJsonStreamTransformation(writer, choiceNodeAugmentationInContainer);
- ((JsonValidator) jsonOutput1 -> {
- final JsonObject cont1 = resolveCont1(jsonOutput1);
- assertNotNull(cont1);
-
- final JsonPrimitive lf17 = childPrimitive(cont1, "complexjson:lf17", "lf17");
- assertNotNull(lf17);
- assertEquals("lf17 value", lf17.getAsString());
- }).validate(jsonOutput);
+ final JsonObject cont1 = resolveCont1(jsonOutput);
+ assertNotNull(cont1);
+
+ final JsonPrimitive lf17 = childPrimitive(cont1, "complexjson:lf17", "lf17");
+ assertNotNull(lf17);
+ assertEquals("lf17 value", lf17.getAsString());
}
@Test
final NormalizedNode<?, ?> unkeyedNodeInContainer = TestingNormalizedNodeStructuresCreator
.unkeyedNodeInContainer();
final String jsonOutput = normalizedNodeToJsonStreamTransformation(writer, unkeyedNodeInContainer);
- ((JsonValidator) jsonOutput1 -> {
- final JsonObject cont1 = resolveCont1(jsonOutput1);
- assertNotNull(cont1);
-
- final JsonArray lst12 = childArray(cont1, "complexjson:lst12", "lst12");
- assertNotNull(lst12);
-
- final Iterator<JsonElement> iterator = lst12.iterator();
- assertTrue(iterator.hasNext());
- final JsonElement lst12Entry1Raw = iterator.next();
- assertFalse(iterator.hasNext());
+ final JsonObject cont1 = resolveCont1(jsonOutput);
+ assertNotNull(cont1);
- assertTrue(lst12Entry1Raw instanceof JsonObject);
- final JsonObject lst12Entry1 = (JsonObject) lst12Entry1Raw;
- final JsonPrimitive lf121 = childPrimitive(lst12Entry1, "complexjson:lf121", "lf121");
- assertNotNull(lf121);
+ final JsonArray lst12 = childArray(cont1, "complexjson:lst12", "lst12");
+ assertNotNull(lst12);
- assertEquals("lf121 value", lf121.getAsString());
+ final Iterator<JsonElement> iterator = lst12.iterator();
+ assertTrue(iterator.hasNext());
+ final JsonElement lst12Entry1Raw = iterator.next();
+ assertFalse(iterator.hasNext());
- }).validate(jsonOutput);
+ assertTrue(lst12Entry1Raw instanceof JsonObject);
+ final JsonObject lst12Entry1 = (JsonObject) lst12Entry1Raw;
+ final JsonPrimitive lf121 = childPrimitive(lst12Entry1, "complexjson:lf121", "lf121");
+ assertNotNull(lf121);
+ assertEquals("lf121 value", lf121.getAsString());
}
@Test
private static String normalizedNodeToJsonStreamTransformation(final Writer writer,
final NormalizedNode<?, ?> inputStructure) throws IOException {
- final NormalizedNodeStreamWriter jsonStream = JSONNormalizedNodeStreamWriter.
- createExclusiveWriter(JSONCodecFactory.getShared(schemaContext), SchemaPath.ROOT, null,
- JsonWriterFactory.createJsonWriter(writer, 2));
+ final NormalizedNodeStreamWriter jsonStream = JSONNormalizedNodeStreamWriter.createExclusiveWriter(
+ JSONCodecFactory.getShared(schemaContext), SchemaPath.ROOT, null,
+ JsonWriterFactory.createJsonWriter(writer, 2));
final NormalizedNodeWriter nodeWriter = NormalizedNodeWriter.forStreamWriter(jsonStream);
nodeWriter.write(inputStructure);
nodeWriter.close();
return writer.toString();
}
-
}
/**
* Demonstrates how to log events produced by a {@link JsonReader}.
- *
- * @throws IOException
*/
@Test
public void ownStreamWriterImplementationDemonstration() throws IOException {
final LoggingNormalizedNodeStreamWriter logWriter = new LoggingNormalizedNodeStreamWriter();
// JSON -> StreamWriter parser
- try (final JsonParserStream jsonHandler = JsonParserStream.create(logWriter, schemaContext)) {
+ try (JsonParserStream jsonHandler = JsonParserStream.create(logWriter, schemaContext)) {
// Process multiple readers, flush()/close() as needed
jsonHandler.parse(reader);
}
/**
* Demonstrates how to create an immutable NormalizedNode tree from a {@link JsonReader} and
* then writes the data back into string representation.
- *
- * @throws IOException
*/
@Ignore
@Test
* This is the serialization part.
*/
// We want to write the first child out
- final DataContainerChild<? extends PathArgument, ?> firstChild = (DataContainerChild<? extends PathArgument, ?>) parsedData;
+ final DataContainerChild<? extends PathArgument, ?> firstChild =
+ (DataContainerChild<? extends PathArgument, ?>) parsedData;
LOG.debug("Serializing first child: {}", firstChild);
// String holder
// StreamWriter which outputs JSON strings
// StreamWriter which outputs JSON strings
- final NormalizedNodeStreamWriter jsonStream = JSONNormalizedNodeStreamWriter.
- createExclusiveWriter(JSONCodecFactory.getShared(schemaContext), SchemaPath.ROOT, null,
- JsonWriterFactory.createJsonWriter(writer, 2));
+ final NormalizedNodeStreamWriter jsonStream = JSONNormalizedNodeStreamWriter.createExclusiveWriter(
+ JSONCodecFactory.getShared(schemaContext), SchemaPath.ROOT, null,
+ JsonWriterFactory.createJsonWriter(writer, 2));
// NormalizedNode -> StreamWriter
final NormalizedNodeWriter nodeWriter = NormalizedNodeWriter.forStreamWriter(jsonStream);
// Just to put it somewhere
LOG.debug("Serialized JSON: {}", writer.toString());
}
-
}
import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
-import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListNode;
}
private static DataContainerChild<? extends PathArgument, ?> augmentC11AWithLf15_21Node() {
- DataContainerNodeBuilder<AugmentationIdentifier, AugmentationNode> choc11_c11AugmentBuilder = Builders
- .augmentationBuilder();
- choc11_c11AugmentBuilder.withNodeIdentifier(new AugmentationIdentifier(Sets.newHashSet(QName.create(
- "ns:complex:json", "2014-08-11", "lf15_21"))));
-
- choc11_c11AugmentBuilder.withChild(lf15_21Node());
- return choc11_c11AugmentBuilder.build();
+ return Builders.augmentationBuilder().withNodeIdentifier(
+ new AugmentationIdentifier(Sets.newHashSet(QName.create("ns:complex:json", "2014-08-11", "lf15_21"))))
+ .withChild(lf15_21Node()).build();
}
private static LeafNode<Object> lf15_21Node() {
}
private static DataContainerChild<? extends PathArgument, ?> augmentC11AWithLf15_11AndLf15_12Node() {
- DataContainerNodeBuilder<AugmentationIdentifier, AugmentationNode> choc11_c11AugmentBuilder = Builders
- .augmentationBuilder();
- choc11_c11AugmentBuilder.withNodeIdentifier(new AugmentationIdentifier(Sets.newHashSet(
- QName.create("ns:complex:json", "2014-08-11", "lf15_11"),
- QName.create("ns:complex:json", "2014-08-11", "lf15_12"))));
- choc11_c11AugmentBuilder.withChild(lf15_11Node());
- choc11_c11AugmentBuilder.withChild(lf15_12Node());
- return choc11_c11AugmentBuilder.build();
+ return Builders.augmentationBuilder()
+ .withNodeIdentifier(new AugmentationIdentifier(Sets.newHashSet(
+ QName.create("ns:complex:json", "2014-08-11", "lf15_11"),
+ QName.create("ns:complex:json", "2014-08-11", "lf15_12"))))
+ .withChild(lf15_11Node())
+ .withChild(lf15_12Node())
+ .build();
}
private static LeafNode<Object> lf15_12Node() {
}
private static DataContainerChild<? extends PathArgument, ?> childLst11() {
- CollectionNodeBuilder<MapEntryNode, MapNode> lst11 = Builders.mapBuilder().withNodeIdentifier(
- new NodeIdentifier(QName.create("ns:complex:json", "2014-08-11", "lst11")));
DataContainerNodeAttrBuilder<NodeIdentifierWithPredicates, MapEntryNode> lst11Entry1Builder = Builders
.mapEntryBuilder();
lst11Entry1Builder.withChild(Builders.leafBuilder()
.withNodeIdentifier(new NodeIdentifier(QName.create("ns:complex:json", "2014-08-11", "lf111")))
.withValue("lf111 value").build());
- lst11.withChild(lst11Entry1Builder.build());
- return lst11.build();
+ return Builders.mapBuilder().withNodeIdentifier(new NodeIdentifier(
+ QName.create("ns:complex:json", "2014-08-11", "lst11"))).withChild(lst11Entry1Builder.build()).build();
}
private static Object lf112Value() {
new NodeIdentifier(QName.create("ns:complex:json", "2014-08-11", "lflst11")));
lflst11.withChild(Builders
.leafSetEntryBuilder()
- .withNodeIdentifier(
- new NodeWithValue<>(QName.create("ns:complex:json", "2014-08-11", "lflst11"), "lflst11 value1"))
+ .withNodeIdentifier(new NodeWithValue<>(QName.create("ns:complex:json", "2014-08-11", "lflst11"),
+ "lflst11 value1"))
.withValue("lflst11 value1").build());
lflst11.withChild(Builders
.leafSetEntryBuilder()
- .withNodeIdentifier(
- new NodeWithValue<>(QName.create("ns:complex:json", "2014-08-11", "lflst11"), "lflst11 value2"))
+ .withNodeIdentifier(new NodeWithValue<>(QName.create("ns:complex:json", "2014-08-11", "lflst11"),
+ "lflst11 value2"))
.withValue("lflst11 value2").build());
return lflst11.build();
}
new NodeIdentifier(QName.create("ns:complex:json", "2014-08-11", "lflst11")));
lflst11.withChild(Builders
.leafSetEntryBuilder()
- .withNodeIdentifier(
- new NodeWithValue<>(QName.create("ns:complex:json", "2014-08-11", "lflst11"), "lflst11 value1\nanother line 1"))
+ .withNodeIdentifier(new NodeWithValue<>(QName.create("ns:complex:json", "2014-08-11", "lflst11"),
+ "lflst11 value1\nanother line 1"))
.withValue("lflst11 value1\nanother line 1").build());
lflst11.withChild(Builders
.leafSetEntryBuilder()
- .withNodeIdentifier(
- new NodeWithValue<>(QName.create("ns:complex:json", "2014-08-11", "lflst11"), "lflst11 value2\r\nanother line 2"))
+ .withNodeIdentifier(new NodeWithValue<>(QName.create("ns:complex:json", "2014-08-11", "lflst11"),
+ "lflst11 value2\r\nanother line 2"))
.withValue("lflst11 value2\r\nanother line 2").build());
return lflst11.build();
}
public static NormalizedNode<?, ?> leafListNodeInContainer() {
return cont1Node(childLflst11());
}
+
public static NormalizedNode<?, ?> leafListNodeInContainerMultiline() {
return cont1Node(childLflst11Multiline());
}
}
/**
- * choc11 contains lf13, lf15_11 and lf15_12 are added via external augmentation
- *
- * @return
+ * choc11 contains lf13, lf15_11 and lf15_12 are added via external augmentation.
*/
public static NormalizedNode<?, ?> caseNodeAugmentationInChoiceInContainer() {
return cont1Node(choc11Node(augmentC11AWithLf15_11AndLf15_12Node(), lf13Node(), augmentC11AWithLf15_21Node()));
}
public static NormalizedNode<?, ?> caseNodeExternalAugmentationInChoiceInContainer() {
- return cont1Node(choc11Node(lf13Node(), augmentC11AWithLf15_11AndLf15_12Node(), externalAugmentC11AWithLf15_11AndLf15_12Node()));
+ return cont1Node(choc11Node(lf13Node(), augmentC11AWithLf15_11AndLf15_12Node(),
+ externalAugmentC11AWithLf15_11AndLf15_12Node()));
}
public static NormalizedNode<?, ?> choiceNodeAugmentationInContainer() {