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.test;
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.dom.codec.impl.BindingNormalizedNodeCodecRegistry;
20 import org.opendaylight.mdsal.binding.generator.impl.GeneratedClassLoadingStrategy;
21 import org.opendaylight.mdsal.binding.generator.impl.ModuleInfoBackedContext;
22 import org.opendaylight.mdsal.binding.generator.util.BindingRuntimeContext;
23 import org.opendaylight.mdsal.binding.spec.reflect.BindingReflections;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.augment.rev140709.TreeComplexUsesAugment;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.augment.rev140709.TreeLeafOnlyAugment;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.augment.rev140709.TreeLeafOnlyAugmentBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.Top;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.two.level.list.TopLevelList;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.two.level.list.TopLevelListBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.two.level.list.TopLevelListKey;
31 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
32 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
33 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
34 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
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 {
45 private SchemaContext schemaContext;
46 private BindingRuntimeContext runtimeContext;
47 private ClassExcludingClassLoadingStrategy mockedContext;
48 private BindingNormalizedNodeCodecRegistry registry;
52 final ModuleInfoBackedContext ctx = ModuleInfoBackedContext.create();
53 ctx.addModuleInfos(BindingReflections.loadModuleInfos());
54 mockedContext = new ClassExcludingClassLoadingStrategy(ctx);
55 schemaContext = ctx.tryToCreateSchemaContext().get();
56 runtimeContext = BindingRuntimeContext.create(mockedContext, schemaContext);
57 registry = new BindingNormalizedNodeCodecRegistry(runtimeContext);
60 private static final TopLevelListKey TOP_FOO_KEY = new TopLevelListKey("foo");
61 private static final InstanceIdentifier<TopLevelList> BA_TOP_LEVEL_LIST = InstanceIdentifier.builder(Top.class)
62 .child(TopLevelList.class, TOP_FOO_KEY).build();
63 private static final InstanceIdentifier<TreeLeafOnlyAugment> BA_TREE_LEAF_ONLY = BA_TOP_LEVEL_LIST
64 .augmentation(TreeLeafOnlyAugment.class);
68 @Test(expected = MissingClassInLoadingStrategyException.class)
69 public void testCorrectExceptionThrown() {
70 materializeWithExclusions(TreeLeafOnlyAugment.class, TreeComplexUsesAugment.class);
71 registry.toYangInstanceIdentifier(BA_TREE_LEAF_ONLY);
76 public void testUsingBindingInstanceIdentifier() {
77 materializeWithExclusions(TreeLeafOnlyAugment.class, TreeComplexUsesAugment.class);
78 mockedContext.includeClass(TreeLeafOnlyAugment.class);
79 final YangInstanceIdentifier domYY = registry.toYangInstanceIdentifier(BA_TREE_LEAF_ONLY);
84 public void testUsingBindingData() {
85 materializeWithExclusions(TreeLeafOnlyAugment.class, TreeComplexUsesAugment.class);
86 mockedContext.includeClass(TreeLeafOnlyAugment.class);
87 final TopLevelList data =
88 new TopLevelListBuilder()
90 .addAugmentation(TreeLeafOnlyAugment.class,
91 new TreeLeafOnlyAugmentBuilder().setSimpleValue("foo").build()).build();
92 final Entry<YangInstanceIdentifier, NormalizedNode<?, ?>> domData =
93 registry.toNormalizedNode(BA_TOP_LEVEL_LIST, data);
94 assertNotNull(domData);
98 private void materializeWithExclusions(final Class<?>... clzToExclude) {
99 for (final Class<?> clz : clzToExclude) {
100 mockedContext.excludeClass(clz);
102 registry.toYangInstanceIdentifier(BA_TOP_LEVEL_LIST);
105 private static class ClassExcludingClassLoadingStrategy extends GeneratedClassLoadingStrategy {
107 private final Set<String> exclusions = new HashSet<>();
108 private final GeneratedClassLoadingStrategy delegate;
110 void excludeClass(final Class<?> clz) {
111 exclusions.add(clz.getName());
114 void includeClass(final Class<?> clz) {
115 exclusions.remove(clz.getName());
118 protected ClassExcludingClassLoadingStrategy(final GeneratedClassLoadingStrategy delegate) {
119 this.delegate = requireNonNull(delegate);
123 public Class<?> loadClass(final String fullyQualifiedName) throws ClassNotFoundException {
124 if (exclusions.contains(fullyQualifiedName)) {
125 throw new ClassNotFoundException(String.format("Class %s is not available for test reasons.",
126 fullyQualifiedName));
128 return delegate.loadClass(fullyQualifiedName);