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 java.util.ServiceLoader;
21 import org.junit.Test;
22 import org.opendaylight.mdsal.binding.dom.codec.spi.BindingDOMCodecFactory;
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.api.source.SourceIdentifier;
37 import org.opendaylight.yangtools.yang.model.api.source.YangTextSource;
38 import org.opendaylight.yangtools.yang.model.repo.api.MissingSchemaSourceException;
39 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
41 public class CurrentAdapterSerializerTest {
46 * Test for yang with leaf of type int in container where data are created
47 * with int value (acceptable data).
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);
58 final DataObject value = fromNormalizedNode.getValue();
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));
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})
84 public void fromNormalizedNodeWithAnotherInputDataTest() throws Exception {
85 final EffectiveModelContext schemaCtx = YangParserTestUtils.parseYangResource("/test.yang");
86 final NormalizedNode data = prepareData(schemaCtx, "42");
88 final Entry<InstanceIdentifier<?>, DataObject> fromNormalizedNode = fromNormalizedNode(data, schemaCtx);
89 final DataObject value = fromNormalizedNode.getValue();
91 final Class<? extends DataObject> iface = value.implementedInterface();
92 assertEquals("Cont", iface.getSimpleName());
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));
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())
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)));
116 final YangInstanceIdentifier path = YangInstanceIdentifier.of(NodeIdentifier.create(QName.create(
117 "urn:test", "2017-01-01", "cont")));
118 return codec.fromNormalizedNode(path, data);
121 private static final class TestingModuleInfoSnapshot implements ModuleInfoSnapshot {
122 static final TestingModuleInfoSnapshot INSTANCE = new TestingModuleInfoSnapshot();
124 private TestingModuleInfoSnapshot() {
129 @SuppressWarnings("unchecked")
130 public <T> Class<T> loadClass(final String fullyQualifiedName) throws ClassNotFoundException {
131 return (Class<T>) Class.forName(fullyQualifiedName);
135 public YangTextSource yangTextSource(final SourceIdentifier arg0) {
140 public YangTextSource getYangTextSource(final SourceIdentifier sourceId) throws MissingSchemaSourceException {
141 throw new MissingSchemaSourceException(sourceId, "no sources");
145 public EffectiveModelContext modelContext() {
146 throw new UnsupportedOperationException();