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.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;
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 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())
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)));
114 final YangInstanceIdentifier path = YangInstanceIdentifier.create(NodeIdentifier.create(QName.create(
115 "urn:test", "2017-01-01", "cont")));
116 return codec.fromNormalizedNode(path, data);
119 private static final class TestingModuleInfoSnapshot implements ModuleInfoSnapshot {
120 static final TestingModuleInfoSnapshot INSTANCE = new TestingModuleInfoSnapshot();
122 private TestingModuleInfoSnapshot() {
127 public EffectiveModelContext getEffectiveModelContext() {
128 throw new UnsupportedOperationException();
132 public ListenableFuture<? extends YangTextSchemaSource> getSource(final SourceIdentifier sourceIdentifier) {
133 throw new UnsupportedOperationException();
137 @SuppressWarnings("unchecked")
138 public <T> Class<T> loadClass(final String fullyQualifiedName) throws ClassNotFoundException {
139 return (Class<T>) Class.forName(fullyQualifiedName);