2 * Copyright (c) 2017 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.mdsal.binding.dom.adapter;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.fail;
14 import java.lang.reflect.Field;
15 import java.lang.reflect.InvocationTargetException;
16 import java.util.ArrayList;
17 import java.util.List;
18 import java.util.Map.Entry;
19 import javassist.ClassPool;
20 import org.junit.Test;
21 import org.opendaylight.mdsal.binding.dom.codec.gen.impl.DataObjectSerializerGenerator;
22 import org.opendaylight.mdsal.binding.dom.codec.gen.impl.StreamWriterGenerator;
23 import org.opendaylight.mdsal.binding.dom.codec.impl.BindingNormalizedNodeCodecRegistry;
24 import org.opendaylight.mdsal.binding.generator.impl.GeneratedClassLoadingStrategy;
25 import org.opendaylight.mdsal.binding.generator.util.BindingRuntimeContext;
26 import org.opendaylight.mdsal.binding.generator.util.JavassistUtils;
27 import org.opendaylight.yangtools.yang.binding.DataObject;
28 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
29 import org.opendaylight.yangtools.yang.common.QName;
30 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
31 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
32 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
33 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
34 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
35 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
36 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
37 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
38 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
39 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
40 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
42 public class BindingToNormalizedNodeCodecTest {
48 * Test for yang with leaf of type int in container where data are created
49 * with int value (acceptable data).
55 public void fromNormalizedNodeTest() throws Exception {
56 final SchemaContext schemaCtx = YangParserTestUtils.parseYangSource("/test.yang");
57 final NormalizedNode<?, ?> data = prepareData(schemaCtx, 42);
58 final Entry<InstanceIdentifier<?>, DataObject> fromNormalizedNode = fromNormalizedNode(data, schemaCtx);
60 final DataObject value = fromNormalizedNode.getValue();
62 assertEquals("Cont", value.getImplementedInterface().getSimpleName());
63 final Object[] objs = {};
64 final Object invoked = value.getImplementedInterface().getDeclaredMethods()[0].invoke(value, objs);
65 final Field declaredField = invoked.getClass().getDeclaredField("_id");
66 declaredField.setAccessible(true);
67 final Object id = declaredField.get(invoked);
68 final Field val = id.getClass().getDeclaredField("_value");
69 val.setAccessible(true);
70 assertEquals(42, val.get(id));
77 * Test for yang with leaf of type int in container where data are created
78 * with String value (non acceptable data - should failed with
79 * {@link IllegalArgumentException})
85 public void fromNormalizedNodeWithAnotherInputDataTest() throws Exception {
86 final SchemaContext schemaCtx = YangParserTestUtils.parseYangSource("/test.yang");
87 final NormalizedNode<?, ?> data = prepareData(schemaCtx, "42");
89 final Entry<InstanceIdentifier<?>, DataObject> fromNormalizedNode = fromNormalizedNode(data, schemaCtx);
90 final DataObject value = fromNormalizedNode.getValue();
92 assertEquals("Cont", value.getImplementedInterface().getSimpleName());
93 final Object[] objs = {};
95 value.getImplementedInterface().getDeclaredMethods()[0].invoke(value, objs);
97 } catch (final InvocationTargetException e) {
98 final Throwable cause = e.getCause();
100 assertEquals(cause.getClass(), IllegalArgumentException.class);
104 private static NormalizedNode<?, ?> prepareData(final SchemaContext schemaCtx, final Object value) {
105 final DataSchemaNode dataChildByName =
106 schemaCtx.getDataChildByName(QName.create("urn:test", "2017-01-01", "cont"));
107 final DataSchemaNode leaf = ((ContainerSchemaNode) dataChildByName)
108 .getDataChildByName(QName.create("urn:test", "2017-01-01", "vlan-id"));
110 final DataContainerChild<?, ?> child = Builders.leafBuilder((LeafSchemaNode) leaf).withValue(value).build();
111 final NormalizedNode<?, ?> data =
112 Builders.containerBuilder((ContainerSchemaNode) dataChildByName).withChild(child).build();
116 private static Entry<InstanceIdentifier<?>, DataObject> fromNormalizedNode(final NormalizedNode<?, ?> data,
117 final SchemaContext schemaCtx) throws Exception {
118 final DataObjectSerializerGenerator serializerGenerator =
119 StreamWriterGenerator.create(JavassistUtils.forClassPool(ClassPool.getDefault()));
120 final BindingNormalizedNodeCodecRegistry codecRegistry =
121 new BindingNormalizedNodeCodecRegistry(serializerGenerator);
122 final GeneratedClassLoadingStrategy classLoadingStrategy =
123 GeneratedClassLoadingStrategy.getTCCLClassLoadingStrategy();
124 final BindingRuntimeContext ctx = BindingRuntimeContext.create(classLoadingStrategy, schemaCtx);
125 codecRegistry.onBindingRuntimeContextUpdated(ctx);
126 final BindingToNormalizedNodeCodec codec =
127 new BindingToNormalizedNodeCodec(classLoadingStrategy, codecRegistry);
128 final List<PathArgument> pathArgs = new ArrayList<>();
129 pathArgs.add(NodeIdentifier.create(QName.create("urn:test", "2017-01-01", "cont")));
131 final YangInstanceIdentifier path = YangInstanceIdentifier.create(pathArgs);
132 final Entry<InstanceIdentifier<?>, DataObject> fromNormalizedNode = codec.fromNormalizedNode(path, data);
134 return fromNormalizedNode;