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.spi.BindingRuntimeHelpers;
21 import org.opendaylight.mdsal.binding.dom.codec.api.MissingClassInLoadingStrategyException;
22 import org.opendaylight.mdsal.binding.generator.impl.DefaultBindingRuntimeGenerator;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.augment.rev140709.TreeComplexUsesAugment;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.augment.rev140709.TreeLeafOnlyAugment;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.augment.rev140709.TreeLeafOnlyAugmentBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.Top;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.two.level.list.TopLevelList;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.two.level.list.TopLevelListBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.two.level.list.TopLevelListKey;
30 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
31 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
32 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
35 * This sets of tests are designed in way, that schema context contains models for all augmentations, but backing class
36 * loading strategy is not aware of some of the classes, and becames aware of them after codec was used.
39 * The idea of this suite is to test that codecs will work even if situation like this happens.
41 public class AugmentationClassDiscoveredAfterCodecTest {
42 private BindingNormalizedNodeCodecRegistry registry;
43 private FilteringClassLoadingStrategy filter;
47 // Baseline state: strategy is cognizant of the classes
48 final BindingRuntimeContext delegate = BindingRuntimeHelpers.createRuntimeContext(
49 new DefaultBindingRuntimeGenerator());
51 // Class loading filter, manipulated by tests
52 filter = new FilteringClassLoadingStrategy(delegate.getStrategy());
53 registry = new BindingNormalizedNodeCodecRegistry(BindingRuntimeContext.create(delegate.getTypes(), filter));
56 private static final TopLevelListKey TOP_FOO_KEY = new TopLevelListKey("foo");
57 private static final InstanceIdentifier<TopLevelList> BA_TOP_LEVEL_LIST = InstanceIdentifier.builder(Top.class)
58 .child(TopLevelList.class, TOP_FOO_KEY).build();
59 private static final InstanceIdentifier<TreeLeafOnlyAugment> BA_TREE_LEAF_ONLY = BA_TOP_LEVEL_LIST
60 .augmentation(TreeLeafOnlyAugment.class);
62 @Test(expected = MissingClassInLoadingStrategyException.class)
63 public void testCorrectExceptionThrown() {
64 materializeWithExclusions(TreeLeafOnlyAugment.class, TreeComplexUsesAugment.class);
65 registry.toYangInstanceIdentifier(BA_TREE_LEAF_ONLY);
69 public void testUsingBindingInstanceIdentifier() {
70 materializeWithExclusions(TreeLeafOnlyAugment.class, TreeComplexUsesAugment.class);
71 filter.includeClass(TreeLeafOnlyAugment.class);
72 final YangInstanceIdentifier domYY = registry.toYangInstanceIdentifier(BA_TREE_LEAF_ONLY);
77 public void testUsingBindingData() {
78 materializeWithExclusions(TreeLeafOnlyAugment.class, TreeComplexUsesAugment.class);
79 filter.includeClass(TreeLeafOnlyAugment.class);
80 final TopLevelList data =
81 new TopLevelListBuilder()
83 .addAugmentation(TreeLeafOnlyAugment.class,
84 new TreeLeafOnlyAugmentBuilder().setSimpleValue("foo").build()).build();
85 final Entry<YangInstanceIdentifier, NormalizedNode<?, ?>> domData =
86 registry.toNormalizedNode(BA_TOP_LEVEL_LIST, data);
87 assertNotNull(domData);
90 private void materializeWithExclusions(final Class<?>... clzToExclude) {
91 for (final Class<?> clz : clzToExclude) {
92 filter.excludeClass(clz);
94 registry.toYangInstanceIdentifier(BA_TOP_LEVEL_LIST);
97 private static final class FilteringClassLoadingStrategy implements ClassLoadingStrategy {
98 private final Set<String> exclusions = new HashSet<>();
99 private final ClassLoadingStrategy delegate;
101 FilteringClassLoadingStrategy(final ClassLoadingStrategy delegate) {
102 this.delegate = requireNonNull(delegate);
105 void excludeClass(final Class<?> clz) {
106 exclusions.add(clz.getName());
109 void includeClass(final Class<?> clz) {
110 exclusions.remove(clz.getName());
114 public Class<?> loadClass(final String fullyQualifiedName) throws ClassNotFoundException {
115 if (exclusions.contains(fullyQualifiedName)) {
116 throw new ClassNotFoundException(String.format("Class %s is not available for test reasons.",
117 fullyQualifiedName));
119 return delegate.loadClass(fullyQualifiedName);