Correct ActionProviderService method definition
[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 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.ContainerNode;
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.EffectiveModelContext;
36 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
37 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
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 ContainerNode prepareData(final EffectiveModelContext schemaCtx, final Object value) {
100         return Builders.containerBuilder()
101             .withNodeIdentifier(new NodeIdentifier(QName.create("urn:test", "2017-01-01", "cont")))
102             .withChild(Builders.leafBuilder()
103                 .withNodeIdentifier(new NodeIdentifier(QName.create("urn:test", "2017-01-01", "vlan-id")))
104                 .withValue(value).build())
105             .build();
106     }
107
108     private static Entry<InstanceIdentifier<?>, DataObject> fromNormalizedNode(final NormalizedNode data,
109             final EffectiveModelContext schemaCtx) {
110         final CurrentAdapterSerializer codec = new CurrentAdapterSerializer(new BindingCodecContext(
111             new DefaultBindingRuntimeContext(new DefaultBindingRuntimeGenerator().generateTypeMapping(schemaCtx),
112                     TestingModuleInfoSnapshot.INSTANCE)));
113
114         final YangInstanceIdentifier path = YangInstanceIdentifier.create(NodeIdentifier.create(QName.create(
115             "urn:test", "2017-01-01", "cont")));
116         return codec.fromNormalizedNode(path, data);
117     }
118
119     private static final class TestingModuleInfoSnapshot implements ModuleInfoSnapshot {
120         static final TestingModuleInfoSnapshot INSTANCE = new TestingModuleInfoSnapshot();
121
122         private TestingModuleInfoSnapshot() {
123             // Hidden on purpose
124         }
125
126         @Override
127         public EffectiveModelContext getEffectiveModelContext() {
128             throw new UnsupportedOperationException();
129         }
130
131         @Override
132         public ListenableFuture<? extends YangTextSchemaSource> getSource(final SourceIdentifier sourceIdentifier) {
133             throw new UnsupportedOperationException();
134         }
135
136         @Override
137         @SuppressWarnings("unchecked")
138         public <T> Class<T> loadClass(final String fullyQualifiedName) throws ClassNotFoundException {
139             return (Class<T>) Class.forName(fullyQualifiedName);
140         }
141     }
142 }