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.mdsal.binding.dom.codec.api.MissingClassInLoadingStrategyException;
19 import org.opendaylight.mdsal.binding.generator.impl.GeneratedClassLoadingStrategy;
20 import org.opendaylight.mdsal.binding.generator.impl.ModuleInfoBackedContext;
21 import org.opendaylight.mdsal.binding.generator.util.BindingRuntimeContext;
22 import org.opendaylight.mdsal.binding.spec.reflect.BindingReflections;
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;
33 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
36 * This sets of tests are designed in way, that schema context contains models for all augmentations, but backing class
37 * loading strategy is not aware of some of the classes, and becames aware of them after codec was used.
40 * The idea of this suite is to test that codecs will work even if situation like this happens.
42 public class AugmentationClassDiscoveredAfterCodecTest {
44 private SchemaContext schemaContext;
45 private BindingRuntimeContext runtimeContext;
46 private ClassExcludingClassLoadingStrategy mockedContext;
47 private BindingNormalizedNodeCodecRegistry registry;
51 final ModuleInfoBackedContext ctx = ModuleInfoBackedContext.create();
52 ctx.addModuleInfos(BindingReflections.loadModuleInfos());
53 mockedContext = new ClassExcludingClassLoadingStrategy(ctx);
54 schemaContext = ctx.tryToCreateSchemaContext().get();
55 runtimeContext = BindingRuntimeContext.create(mockedContext, schemaContext);
56 registry = new BindingNormalizedNodeCodecRegistry(runtimeContext);
59 private static final TopLevelListKey TOP_FOO_KEY = new TopLevelListKey("foo");
60 private static final InstanceIdentifier<TopLevelList> BA_TOP_LEVEL_LIST = InstanceIdentifier.builder(Top.class)
61 .child(TopLevelList.class, TOP_FOO_KEY).build();
62 private static final InstanceIdentifier<TreeLeafOnlyAugment> BA_TREE_LEAF_ONLY = BA_TOP_LEVEL_LIST
63 .augmentation(TreeLeafOnlyAugment.class);
67 @Test(expected = MissingClassInLoadingStrategyException.class)
68 public void testCorrectExceptionThrown() {
69 materializeWithExclusions(TreeLeafOnlyAugment.class, TreeComplexUsesAugment.class);
70 registry.toYangInstanceIdentifier(BA_TREE_LEAF_ONLY);
75 public void testUsingBindingInstanceIdentifier() {
76 materializeWithExclusions(TreeLeafOnlyAugment.class, TreeComplexUsesAugment.class);
77 mockedContext.includeClass(TreeLeafOnlyAugment.class);
78 final YangInstanceIdentifier domYY = registry.toYangInstanceIdentifier(BA_TREE_LEAF_ONLY);
83 public void testUsingBindingData() {
84 materializeWithExclusions(TreeLeafOnlyAugment.class, TreeComplexUsesAugment.class);
85 mockedContext.includeClass(TreeLeafOnlyAugment.class);
86 final TopLevelList data =
87 new TopLevelListBuilder()
89 .addAugmentation(TreeLeafOnlyAugment.class,
90 new TreeLeafOnlyAugmentBuilder().setSimpleValue("foo").build()).build();
91 final Entry<YangInstanceIdentifier, NormalizedNode<?, ?>> domData =
92 registry.toNormalizedNode(BA_TOP_LEVEL_LIST, data);
93 assertNotNull(domData);
97 private void materializeWithExclusions(final Class<?>... clzToExclude) {
98 for (final Class<?> clz : clzToExclude) {
99 mockedContext.excludeClass(clz);
101 registry.toYangInstanceIdentifier(BA_TOP_LEVEL_LIST);
104 private static class ClassExcludingClassLoadingStrategy extends GeneratedClassLoadingStrategy {
106 private final Set<String> exclusions = new HashSet<>();
107 private final GeneratedClassLoadingStrategy delegate;
109 void excludeClass(final Class<?> clz) {
110 exclusions.add(clz.getName());
113 void includeClass(final Class<?> clz) {
114 exclusions.remove(clz.getName());
117 protected ClassExcludingClassLoadingStrategy(final GeneratedClassLoadingStrategy delegate) {
118 this.delegate = requireNonNull(delegate);
122 public Class<?> loadClass(final String fullyQualifiedName) throws ClassNotFoundException {
123 if (exclusions.contains(fullyQualifiedName)) {
124 throw new ClassNotFoundException(String.format("Class %s is not available for test reasons.",
125 fullyQualifiedName));
127 return delegate.loadClass(fullyQualifiedName);