86e2982f6929052dc702b29b365135995db27a03
[mdsal.git] / binding / mdsal-binding-dom-adapter / src / test / java / org / opendaylight / mdsal / binding / dom / adapter / CurrentAdapterSerializerTest.java
1 /*
2  * Copyright (c) 2017 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.mdsal.binding.dom.adapter;
9
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;
15
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;
39
40 public class CurrentAdapterSerializerTest {
41     /**
42      * Positive test.
43      *
44      * <p>
45      * Test for yang with leaf of type int in container where data are created
46      * with int value (acceptable data).
47      *
48      * @throws Exception
49      *             - throw exception
50      */
51     @Test
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);
56
57         final DataObject value = fromNormalizedNode.getValue();
58         assertNotNull(value);
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));
69     }
70
71     /**
72      * Negative test.
73      *
74      * <p>
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})
78      *
79      * @throws Exception
80      *             - throw exception
81      */
82     @Test
83     public void fromNormalizedNodeWithAnotherInputDataTest() throws Exception {
84         final EffectiveModelContext schemaCtx = YangParserTestUtils.parseYangResource("/test.yang");
85         final NormalizedNode<?, ?> data = prepareData(schemaCtx, "42");
86
87         final Entry<InstanceIdentifier<?>, DataObject> fromNormalizedNode = fromNormalizedNode(data, schemaCtx);
88         final DataObject value = fromNormalizedNode.getValue();
89         assertNotNull(value);
90         final Class<? extends DataObject> iface = value.implementedInterface();
91         assertEquals("Cont", iface.getSimpleName());
92
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));
97     }
98
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"));
104
105         final DataContainerChild<?, ?> child = Builders.leafBuilder((LeafSchemaNode) leaf).withValue(value).build();
106         final NormalizedNode<?, ?> data =
107                 Builders.containerBuilder((ContainerSchemaNode) dataChildByName).withChild(child).build();
108         return data;
109     }
110
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)));
116
117         final YangInstanceIdentifier path = YangInstanceIdentifier.create(NodeIdentifier.create(QName.create(
118             "urn:test", "2017-01-01", "cont")));
119         return codec.fromNormalizedNode(path, data);
120     }
121 }