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.parseYangResource("/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 final Class<? extends DataObject> iface = value.implementedInterface();
63 assertEquals("Cont", iface.getSimpleName());
64 final Object[] objs = {};
65 final Object invoked = iface.getDeclaredMethod("getVlanId").invoke(value, objs);
66 final Field declaredField = invoked.getClass().getDeclaredField("_id");
67 declaredField.setAccessible(true);
68 final Object id = declaredField.get(invoked);
69 final Field val = id.getClass().getDeclaredField("_value");
70 val.setAccessible(true);
71 assertEquals(42, val.get(id));
78 * Test for yang with leaf of type int in container where data are created
79 * with String value (non acceptable data - should failed with
80 * {@link IllegalArgumentException})
86 public void fromNormalizedNodeWithAnotherInputDataTest() throws Exception {
87 final SchemaContext schemaCtx = YangParserTestUtils.parseYangResource("/test.yang");
88 final NormalizedNode<?, ?> data = prepareData(schemaCtx, "42");
90 final Entry<InstanceIdentifier<?>, DataObject> fromNormalizedNode = fromNormalizedNode(data, schemaCtx);
91 final DataObject value = fromNormalizedNode.getValue();
93 final Class<? extends DataObject> iface = value.implementedInterface();
94 assertEquals("Cont", iface.getSimpleName());
95 final Object[] objs = {};
97 iface.getDeclaredMethod("getVlanId").invoke(value, objs);
99 } catch (final InvocationTargetException e) {
100 final Throwable cause = e.getCause();
101 assertNotNull(cause);
102 assertEquals(cause.getClass(), IllegalArgumentException.class);
106 private static NormalizedNode<?, ?> prepareData(final SchemaContext schemaCtx, final Object value) {
107 final DataSchemaNode dataChildByName =
108 schemaCtx.getDataChildByName(QName.create("urn:test", "2017-01-01", "cont"));
109 final DataSchemaNode leaf = ((ContainerSchemaNode) dataChildByName)
110 .getDataChildByName(QName.create("urn:test", "2017-01-01", "vlan-id"));
112 final DataContainerChild<?, ?> child = Builders.leafBuilder((LeafSchemaNode) leaf).withValue(value).build();
113 final NormalizedNode<?, ?> data =
114 Builders.containerBuilder((ContainerSchemaNode) dataChildByName).withChild(child).build();
118 private static Entry<InstanceIdentifier<?>, DataObject> fromNormalizedNode(final NormalizedNode<?, ?> data,
119 final SchemaContext schemaCtx) {
120 final DataObjectSerializerGenerator serializerGenerator =
121 StreamWriterGenerator.create(JavassistUtils.forClassPool(ClassPool.getDefault()));
122 final BindingNormalizedNodeCodecRegistry codecRegistry =
123 new BindingNormalizedNodeCodecRegistry(serializerGenerator);
124 final GeneratedClassLoadingStrategy classLoadingStrategy =
125 GeneratedClassLoadingStrategy.getTCCLClassLoadingStrategy();
126 final BindingRuntimeContext ctx = BindingRuntimeContext.create(classLoadingStrategy, schemaCtx);
127 codecRegistry.onBindingRuntimeContextUpdated(ctx);
128 final BindingToNormalizedNodeCodec codec =
129 new BindingToNormalizedNodeCodec(classLoadingStrategy, codecRegistry);
130 final List<PathArgument> pathArgs = new ArrayList<>();
131 pathArgs.add(NodeIdentifier.create(QName.create("urn:test", "2017-01-01", "cont")));
133 final YangInstanceIdentifier path = YangInstanceIdentifier.create(pathArgs);
134 final Entry<InstanceIdentifier<?>, DataObject> fromNormalizedNode = codec.fromNormalizedNode(path, data);
136 return fromNormalizedNode;