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.hamcrest.CoreMatchers.instanceOf;
11 import static org.hamcrest.MatcherAssert.assertThat;
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertNotNull;
14 import static org.junit.Assert.assertThrows;
16 import com.google.common.util.concurrent.ListenableFuture;
17 import java.lang.reflect.Field;
18 import java.lang.reflect.InvocationTargetException;
19 import java.lang.reflect.Method;
20 import java.util.Map.Entry;
21 import org.junit.Test;
22 import org.opendaylight.mdsal.binding.dom.codec.impl.BindingCodecContext;
23 import org.opendaylight.mdsal.binding.generator.impl.DefaultBindingRuntimeGenerator;
24 import org.opendaylight.mdsal.binding.runtime.api.DefaultBindingRuntimeContext;
25 import org.opendaylight.mdsal.binding.runtime.api.ModuleInfoSnapshot;
26 import org.opendaylight.yangtools.yang.binding.DataObject;
27 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
28 import org.opendaylight.yangtools.yang.common.QName;
29 import org.opendaylight.yangtools.yang.common.Uint16;
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.schema.DataContainerChild;
33 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
34 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
35 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
36 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
37 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
38 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
39 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
40 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
41 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
43 public class CurrentAdapterSerializerTest {
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 EffectiveModelContext schemaCtx = YangParserTestUtils.parseYangResource("/test.yang");
57 final NormalizedNode<?, ?> data = prepareData(schemaCtx, Uint16.valueOf(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(Uint16.valueOf(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 EffectiveModelContext 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());
96 final Method getVlanId = iface.getDeclaredMethod("getVlanId");
97 final InvocationTargetException ex = assertThrows(InvocationTargetException.class,
98 () -> getVlanId.invoke(value));
99 assertThat(ex.getCause(), instanceOf(IllegalArgumentException.class));
102 private static NormalizedNode<?, ?> prepareData(final EffectiveModelContext schemaCtx, final Object value) {
103 final DataSchemaNode dataChildByName =
104 schemaCtx.getDataChildByName(QName.create("urn:test", "2017-01-01", "cont"));
105 final DataSchemaNode leaf = ((ContainerSchemaNode) dataChildByName)
106 .getDataChildByName(QName.create("urn:test", "2017-01-01", "vlan-id"));
108 final DataContainerChild<?, ?> child = Builders.leafBuilder((LeafSchemaNode) leaf).withValue(value).build();
109 final NormalizedNode<?, ?> data =
110 Builders.containerBuilder((ContainerSchemaNode) dataChildByName).withChild(child).build();
114 private static Entry<InstanceIdentifier<?>, DataObject> fromNormalizedNode(final NormalizedNode<?, ?> data,
115 final EffectiveModelContext schemaCtx) {
116 final CurrentAdapterSerializer codec = new CurrentAdapterSerializer(new BindingCodecContext(
117 new DefaultBindingRuntimeContext(new DefaultBindingRuntimeGenerator().generateTypeMapping(schemaCtx),
118 TestingModuleInfoSnapshot.INSTANCE)));
120 final YangInstanceIdentifier path = YangInstanceIdentifier.create(NodeIdentifier.create(QName.create(
121 "urn:test", "2017-01-01", "cont")));
122 return codec.fromNormalizedNode(path, data);
125 private static final class TestingModuleInfoSnapshot implements ModuleInfoSnapshot {
126 static final TestingModuleInfoSnapshot INSTANCE = new TestingModuleInfoSnapshot();
128 private TestingModuleInfoSnapshot() {
133 public EffectiveModelContext getEffectiveModelContext() {
134 throw new UnsupportedOperationException();
138 public ListenableFuture<? extends YangTextSchemaSource> getSource(SourceIdentifier sourceIdentifier) {
139 throw new UnsupportedOperationException();
143 @SuppressWarnings("unchecked")
144 public <T> Class<T> loadClass(String fullyQualifiedName) throws ClassNotFoundException {
145 return (Class<T>) Class.forName(fullyQualifiedName);