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 org.junit.Assert.assertNotNull;
12 import com.google.common.base.Preconditions;
13 import java.util.HashSet;
14 import java.util.Map.Entry;
16 import javassist.ClassPool;
17 import org.junit.Before;
18 import org.junit.Test;
19 import org.opendaylight.mdsal.binding.dom.codec.gen.impl.StreamWriterGenerator;
20 import org.opendaylight.mdsal.binding.dom.codec.impl.BindingNormalizedNodeCodecRegistry;
21 import org.opendaylight.mdsal.binding.dom.codec.impl.MissingClassInLoadingStrategyException;
22 import org.opendaylight.mdsal.binding.generator.impl.GeneratedClassLoadingStrategy;
23 import org.opendaylight.mdsal.binding.generator.impl.ModuleInfoBackedContext;
24 import org.opendaylight.mdsal.binding.generator.util.BindingRuntimeContext;
25 import org.opendaylight.mdsal.binding.generator.util.JavassistUtils;
26 import org.opendaylight.mdsal.binding.spec.reflect.BindingReflections;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.augment.rev140709.TreeComplexUsesAugment;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.augment.rev140709.TreeLeafOnlyAugment;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.augment.rev140709.TreeLeafOnlyAugmentBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.Top;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.two.level.list.TopLevelList;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.two.level.list.TopLevelListBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.two.level.list.TopLevelListKey;
34 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
35 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
36 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
37 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
40 * This sets of tests are designed in way, that schema context contains models for all augmentations, but backing class
41 * loading strategy is not aware of some of the classes, and becames aware of them after codec was used.
44 * The idea of this suite is to test that codecs will work even if situation like this happens.
46 public class AugmentationClassDiscoveredAfterCodecTest {
48 private SchemaContext schemaContext;
49 private BindingRuntimeContext runtimeContext;
50 private ClassExcludingClassLoadingStrategy mockedContext;
51 private BindingNormalizedNodeCodecRegistry registry;
55 final ModuleInfoBackedContext ctx = ModuleInfoBackedContext.create();
56 ctx.addModuleInfos(BindingReflections.loadModuleInfos());
57 mockedContext = new ClassExcludingClassLoadingStrategy(ctx);
58 schemaContext = ctx.tryToCreateSchemaContext().get();
59 runtimeContext = BindingRuntimeContext.create(mockedContext, schemaContext);
60 final JavassistUtils utils = JavassistUtils.forClassPool(ClassPool.getDefault());
61 registry = new BindingNormalizedNodeCodecRegistry(StreamWriterGenerator.create(utils));
62 registry.onBindingRuntimeContextUpdated(runtimeContext);
65 private static final TopLevelListKey TOP_FOO_KEY = new TopLevelListKey("foo");
66 private static final InstanceIdentifier<TopLevelList> BA_TOP_LEVEL_LIST = InstanceIdentifier.builder(Top.class)
67 .child(TopLevelList.class, TOP_FOO_KEY).build();
68 private static final InstanceIdentifier<TreeLeafOnlyAugment> BA_TREE_LEAF_ONLY = BA_TOP_LEVEL_LIST
69 .augmentation(TreeLeafOnlyAugment.class);
73 @Test(expected = MissingClassInLoadingStrategyException.class)
74 public void testCorrectExceptionThrown() {
75 materializeWithExclusions(TreeLeafOnlyAugment.class, TreeComplexUsesAugment.class);
76 registry.toYangInstanceIdentifier(BA_TREE_LEAF_ONLY);
81 public void testUsingBindingInstanceIdentifier() {
82 materializeWithExclusions(TreeLeafOnlyAugment.class, TreeComplexUsesAugment.class);
83 mockedContext.includeClass(TreeLeafOnlyAugment.class);
84 final YangInstanceIdentifier domYY = registry.toYangInstanceIdentifier(BA_TREE_LEAF_ONLY);
89 public void testUsingBindingData() {
90 materializeWithExclusions(TreeLeafOnlyAugment.class, TreeComplexUsesAugment.class);
91 mockedContext.includeClass(TreeLeafOnlyAugment.class);
92 final TopLevelList data =
93 new TopLevelListBuilder()
95 .addAugmentation(TreeLeafOnlyAugment.class,
96 new TreeLeafOnlyAugmentBuilder().setSimpleValue("foo").build()).build();
97 final Entry<YangInstanceIdentifier, NormalizedNode<?, ?>> domData =
98 registry.toNormalizedNode(BA_TOP_LEVEL_LIST, data);
99 assertNotNull(domData);
103 private void materializeWithExclusions(final Class<?>... clzToExclude) {
104 for (final Class<?> clz : clzToExclude) {
105 mockedContext.excludeClass(clz);
107 registry.toYangInstanceIdentifier(BA_TOP_LEVEL_LIST);
110 private static class ClassExcludingClassLoadingStrategy extends GeneratedClassLoadingStrategy {
112 private final Set<String> exclusions = new HashSet<>();
113 private final GeneratedClassLoadingStrategy delegate;
115 void excludeClass(final Class<?> clz) {
116 exclusions.add(clz.getName());
119 void includeClass(final Class<?> clz) {
120 exclusions.remove(clz.getName());
123 protected ClassExcludingClassLoadingStrategy(final GeneratedClassLoadingStrategy delegate) {
124 this.delegate = Preconditions.checkNotNull(delegate);
128 public Class<?> loadClass(final String fullyQualifiedName) throws ClassNotFoundException {
129 if (exclusions.contains(fullyQualifiedName)) {
130 throw new ClassNotFoundException(String.format("Class %s is not available for test reasons.",
131 fullyQualifiedName));
133 return delegate.loadClass(fullyQualifiedName);