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 java.lang.reflect.Field;
17 import java.lang.reflect.InvocationTargetException;
18 import java.lang.reflect.Method;
19 import java.util.Map.Entry;
20 import org.junit.Test;
21 import org.opendaylight.mdsal.binding.dom.codec.impl.BindingCodecContext;
22 import org.opendaylight.mdsal.binding.generator.impl.DefaultBindingRuntimeGenerator;
23 import org.opendaylight.mdsal.binding.runtime.api.DefaultBindingRuntimeContext;
24 import org.opendaylight.yangtools.util.ClassLoaderUtils;
25 import org.opendaylight.yangtools.yang.binding.DataObject;
26 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
27 import org.opendaylight.yangtools.yang.common.QName;
28 import org.opendaylight.yangtools.yang.common.Uint16;
29 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
30 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
31 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
32 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
33 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
34 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
35 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
36 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
37 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
38 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
40 public class CurrentAdapterSerializerTest {
45 * Test for yang with leaf of type int in container where data are created
46 * with int value (acceptable data).
52 public void fromNormalizedNodeTest() throws Exception {
53 final EffectiveModelContext schemaCtx = YangParserTestUtils.parseYangResource("/test.yang");
54 final NormalizedNode<?, ?> data = prepareData(schemaCtx, Uint16.valueOf(42));
55 final Entry<InstanceIdentifier<?>, DataObject> fromNormalizedNode = fromNormalizedNode(data, schemaCtx);
57 final DataObject value = fromNormalizedNode.getValue();
59 final Class<? extends DataObject> iface = value.implementedInterface();
60 assertEquals("Cont", iface.getSimpleName());
61 final Object[] objs = {};
62 final Object invoked = iface.getDeclaredMethod("getVlanId").invoke(value, objs);
63 final Field declaredField = invoked.getClass().getDeclaredField("_id");
64 declaredField.setAccessible(true);
65 final Object id = declaredField.get(invoked);
66 final Field val = id.getClass().getDeclaredField("_value");
67 val.setAccessible(true);
68 assertEquals(Uint16.valueOf(42), val.get(id));
75 * Test for yang with leaf of type int in container where data are created
76 * with String value (non acceptable data - should failed with
77 * {@link IllegalArgumentException})
83 public void fromNormalizedNodeWithAnotherInputDataTest() throws Exception {
84 final EffectiveModelContext schemaCtx = YangParserTestUtils.parseYangResource("/test.yang");
85 final NormalizedNode<?, ?> data = prepareData(schemaCtx, "42");
87 final Entry<InstanceIdentifier<?>, DataObject> fromNormalizedNode = fromNormalizedNode(data, schemaCtx);
88 final DataObject value = fromNormalizedNode.getValue();
90 final Class<? extends DataObject> iface = value.implementedInterface();
91 assertEquals("Cont", iface.getSimpleName());
93 final Method getVlanId = iface.getDeclaredMethod("getVlanId");
94 final InvocationTargetException ex = assertThrows(InvocationTargetException.class,
95 () -> getVlanId.invoke(value));
96 assertThat(ex.getCause(), instanceOf(IllegalArgumentException.class));
99 private static NormalizedNode<?, ?> prepareData(final EffectiveModelContext schemaCtx, final Object value) {
100 final DataSchemaNode dataChildByName =
101 schemaCtx.getDataChildByName(QName.create("urn:test", "2017-01-01", "cont"));
102 final DataSchemaNode leaf = ((ContainerSchemaNode) dataChildByName)
103 .getDataChildByName(QName.create("urn:test", "2017-01-01", "vlan-id"));
105 final DataContainerChild<?, ?> child = Builders.leafBuilder((LeafSchemaNode) leaf).withValue(value).build();
106 final NormalizedNode<?, ?> data =
107 Builders.containerBuilder((ContainerSchemaNode) dataChildByName).withChild(child).build();
111 private static Entry<InstanceIdentifier<?>, DataObject> fromNormalizedNode(final NormalizedNode<?, ?> data,
112 final EffectiveModelContext schemaCtx) {
113 final CurrentAdapterSerializer codec = new CurrentAdapterSerializer(new BindingCodecContext(
114 DefaultBindingRuntimeContext.create(new DefaultBindingRuntimeGenerator().generateTypeMapping(schemaCtx),
115 ClassLoaderUtils::loadClassWithTCCL)));
117 final YangInstanceIdentifier path = YangInstanceIdentifier.create(NodeIdentifier.create(QName.create(
118 "urn:test", "2017-01-01", "cont")));
119 return codec.fromNormalizedNode(path, data);