e677a8625edfd1ce2e85690b624a90b7bfe91fa3
[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 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 java.util.ServiceLoader;
22 import org.junit.Test;
23 import org.opendaylight.mdsal.binding.dom.codec.spi.BindingDOMCodecFactory;
24 import org.opendaylight.mdsal.binding.generator.impl.DefaultBindingRuntimeGenerator;
25 import org.opendaylight.mdsal.binding.runtime.api.DefaultBindingRuntimeContext;
26 import org.opendaylight.mdsal.binding.runtime.api.ModuleInfoSnapshot;
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.common.Uint16;
31 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
32 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
33 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
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.EffectiveModelContext;
37 import org.opendaylight.yangtools.yang.model.api.source.SourceIdentifier;
38 import org.opendaylight.yangtools.yang.model.api.source.YangTextSource;
39 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
40
41 public class CurrentAdapterSerializerTest {
42     /**
43      * Positive test.
44      *
45      * <p>
46      * Test for yang with leaf of type int in container where data are created
47      * with int value (acceptable data).
48      *
49      * @throws Exception
50      *             - throw exception
51      */
52     @Test
53     public void fromNormalizedNodeTest() throws Exception {
54         final EffectiveModelContext schemaCtx = YangParserTestUtils.parseYangResource("/test.yang");
55         final NormalizedNode data = prepareData(schemaCtx, Uint16.valueOf(42));
56         final Entry<InstanceIdentifier<?>, DataObject> fromNormalizedNode = fromNormalizedNode(data, schemaCtx);
57
58         final DataObject value = fromNormalizedNode.getValue();
59         assertNotNull(value);
60         final Class<? extends DataObject> iface = value.implementedInterface();
61         assertEquals("Cont", iface.getSimpleName());
62         final Object[] objs = {};
63         final Object invoked = iface.getDeclaredMethod("getVlanId").invoke(value, objs);
64         final Field declaredField = invoked.getClass().getDeclaredField("_id");
65         declaredField.setAccessible(true);
66         final Object id = declaredField.get(invoked);
67         final Field val = id.getClass().getDeclaredField("_value");
68         val.setAccessible(true);
69         assertEquals(Uint16.valueOf(42), val.get(id));
70     }
71
72     /**
73      * Negative test.
74      *
75      * <p>
76      * Test for yang with leaf of type int in container where data are created
77      * with String value (non acceptable data - should failed with
78      * {@link IllegalArgumentException})
79      *
80      * @throws Exception
81      *             - throw exception
82      */
83     @Test
84     public void fromNormalizedNodeWithAnotherInputDataTest() throws Exception {
85         final EffectiveModelContext schemaCtx = YangParserTestUtils.parseYangResource("/test.yang");
86         final NormalizedNode data = prepareData(schemaCtx, "42");
87
88         final Entry<InstanceIdentifier<?>, DataObject> fromNormalizedNode = fromNormalizedNode(data, schemaCtx);
89         final DataObject value = fromNormalizedNode.getValue();
90         assertNotNull(value);
91         final Class<? extends DataObject> iface = value.implementedInterface();
92         assertEquals("Cont", iface.getSimpleName());
93
94         final Method getVlanId = iface.getDeclaredMethod("getVlanId");
95         final InvocationTargetException ex = assertThrows(InvocationTargetException.class,
96             () -> getVlanId.invoke(value));
97         assertThat(ex.getCause(), instanceOf(IllegalArgumentException.class));
98     }
99
100     private static ContainerNode prepareData(final EffectiveModelContext schemaCtx, final Object value) {
101         return Builders.containerBuilder()
102             .withNodeIdentifier(new NodeIdentifier(QName.create("urn:test", "2017-01-01", "cont")))
103             .withChild(Builders.leafBuilder()
104                 .withNodeIdentifier(new NodeIdentifier(QName.create("urn:test", "2017-01-01", "vlan-id")))
105                 .withValue(value).build())
106             .build();
107     }
108
109     private static Entry<InstanceIdentifier<?>, DataObject> fromNormalizedNode(final NormalizedNode data,
110             final EffectiveModelContext schemaCtx) {
111         final CurrentAdapterSerializer codec = new CurrentAdapterSerializer(
112                 ServiceLoader.load(BindingDOMCodecFactory.class).findFirst().orElseThrow().createBindingDOMCodec(
113                         new DefaultBindingRuntimeContext(new DefaultBindingRuntimeGenerator()
114                                 .generateTypeMapping(schemaCtx), TestingModuleInfoSnapshot.INSTANCE)));
115
116         final YangInstanceIdentifier path = YangInstanceIdentifier.of(NodeIdentifier.create(QName.create(
117             "urn:test", "2017-01-01", "cont")));
118         return codec.fromNormalizedNode(path, data);
119     }
120
121     private static final class TestingModuleInfoSnapshot implements ModuleInfoSnapshot {
122         static final TestingModuleInfoSnapshot INSTANCE = new TestingModuleInfoSnapshot();
123
124         private TestingModuleInfoSnapshot() {
125             // Hidden on purpose
126         }
127
128         @Override
129         public EffectiveModelContext modelContext() {
130             throw new UnsupportedOperationException();
131         }
132
133         @Override
134         public ListenableFuture<? extends YangTextSource> getSource(final SourceIdentifier sourceIdentifier) {
135             throw new UnsupportedOperationException();
136         }
137
138         @Override
139         @SuppressWarnings("unchecked")
140         public <T> Class<T> loadClass(final String fullyQualifiedName) throws ClassNotFoundException {
141             return (Class<T>) Class.forName(fullyQualifiedName);
142         }
143     }
144 }