2 * Copyright (c) 2015 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.codec.impl;
10 import static java.util.Objects.requireNonNull;
11 import static org.junit.Assert.assertNotNull;
13 import java.util.HashSet;
14 import java.util.Map.Entry;
16 import org.junit.Before;
17 import org.junit.Test;
18 import org.opendaylight.binding.runtime.api.BindingRuntimeContext;
19 import org.opendaylight.binding.runtime.api.ClassLoadingStrategy;
20 import org.opendaylight.binding.runtime.api.DefaultBindingRuntimeContext;
21 import org.opendaylight.binding.runtime.spi.BindingRuntimeHelpers;
22 import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer;
23 import org.opendaylight.mdsal.binding.dom.codec.api.MissingClassInLoadingStrategyException;
24 import org.opendaylight.mdsal.binding.generator.impl.DefaultBindingRuntimeGenerator;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.augment.rev140709.TreeComplexUsesAugment;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.augment.rev140709.TreeLeafOnlyAugment;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.augment.rev140709.TreeLeafOnlyAugmentBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.Top;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.two.level.list.TopLevelList;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.two.level.list.TopLevelListBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.two.level.list.TopLevelListKey;
32 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
33 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
34 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
37 * This sets of tests are designed in way, that schema context contains models for all augmentations, but backing class
38 * loading strategy is not aware of some of the classes, and becames aware of them after codec was used.
41 * The idea of this suite is to test that codecs will work even if situation like this happens.
43 public class AugmentationClassDiscoveredAfterCodecTest {
44 private BindingNormalizedNodeSerializer serializer;
45 private FilteringClassLoadingStrategy filter;
49 // Baseline state: strategy is cognizant of the classes
50 final BindingRuntimeContext delegate = BindingRuntimeHelpers.createRuntimeContext(
51 new DefaultBindingRuntimeGenerator());
53 // Class loading filter, manipulated by tests
54 filter = new FilteringClassLoadingStrategy(delegate.getStrategy());
55 serializer = new BindingCodecContext(DefaultBindingRuntimeContext.create(delegate.getTypes(), filter));
58 private static final TopLevelListKey TOP_FOO_KEY = new TopLevelListKey("foo");
59 private static final InstanceIdentifier<TopLevelList> BA_TOP_LEVEL_LIST = InstanceIdentifier.builder(Top.class)
60 .child(TopLevelList.class, TOP_FOO_KEY).build();
61 private static final InstanceIdentifier<TreeLeafOnlyAugment> BA_TREE_LEAF_ONLY = BA_TOP_LEVEL_LIST
62 .augmentation(TreeLeafOnlyAugment.class);
64 @Test(expected = MissingClassInLoadingStrategyException.class)
65 public void testCorrectExceptionThrown() {
66 materializeWithExclusions(TreeLeafOnlyAugment.class, TreeComplexUsesAugment.class);
67 serializer.toYangInstanceIdentifier(BA_TREE_LEAF_ONLY);
71 public void testUsingBindingInstanceIdentifier() {
72 materializeWithExclusions(TreeLeafOnlyAugment.class, TreeComplexUsesAugment.class);
73 filter.includeClass(TreeLeafOnlyAugment.class);
74 final YangInstanceIdentifier domYY = serializer.toYangInstanceIdentifier(BA_TREE_LEAF_ONLY);
79 public void testUsingBindingData() {
80 materializeWithExclusions(TreeLeafOnlyAugment.class, TreeComplexUsesAugment.class);
81 filter.includeClass(TreeLeafOnlyAugment.class);
82 final TopLevelList data =
83 new TopLevelListBuilder()
85 .addAugmentation(TreeLeafOnlyAugment.class,
86 new TreeLeafOnlyAugmentBuilder().setSimpleValue("foo").build()).build();
87 final Entry<YangInstanceIdentifier, NormalizedNode<?, ?>> domData =
88 serializer.toNormalizedNode(BA_TOP_LEVEL_LIST, data);
89 assertNotNull(domData);
92 private void materializeWithExclusions(final Class<?>... clzToExclude) {
93 for (final Class<?> clz : clzToExclude) {
94 filter.excludeClass(clz);
96 serializer.toYangInstanceIdentifier(BA_TOP_LEVEL_LIST);
99 private static final class FilteringClassLoadingStrategy implements ClassLoadingStrategy {
100 private final Set<String> exclusions = new HashSet<>();
101 private final ClassLoadingStrategy delegate;
103 FilteringClassLoadingStrategy(final ClassLoadingStrategy delegate) {
104 this.delegate = requireNonNull(delegate);
107 void excludeClass(final Class<?> clz) {
108 exclusions.add(clz.getName());
111 void includeClass(final Class<?> clz) {
112 exclusions.remove(clz.getName());
116 public Class<?> loadClass(final String fullyQualifiedName) throws ClassNotFoundException {
117 if (exclusions.contains(fullyQualifiedName)) {
118 throw new ClassNotFoundException(String.format("Class %s is not available for test reasons.",
119 fullyQualifiedName));
121 return delegate.loadClass(fullyQualifiedName);