Promote BindingRuntimeContext to binding-generator-api
[mdsal.git] / binding / mdsal-binding-dom-codec / src / test / java / org / opendaylight / mdsal / binding / dom / codec / impl / AugmentationClassDiscoveredAfterCodecTest.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.mdsal.binding.dom.codec.impl;
9
10 import static java.util.Objects.requireNonNull;
11 import static org.junit.Assert.assertNotNull;
12
13 import java.util.HashSet;
14 import java.util.Map.Entry;
15 import java.util.Set;
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.api.BindingRuntimeContext;
20 import org.opendaylight.mdsal.binding.generator.impl.DefaultBindingRuntimeGenerator;
21 import org.opendaylight.mdsal.binding.generator.impl.GeneratedClassLoadingStrategy;
22 import org.opendaylight.mdsal.binding.generator.impl.ModuleInfoBackedContext;
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.EffectiveModelContext;
35
36 /**
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.
39  *
40  * <p>
41  * The idea of this suite is to test that codecs will work even if situation like this happens.
42  */
43 public class AugmentationClassDiscoveredAfterCodecTest {
44
45     private EffectiveModelContext schemaContext;
46     private BindingRuntimeContext runtimeContext;
47     private ClassExcludingClassLoadingStrategy mockedContext;
48     private BindingNormalizedNodeCodecRegistry registry;
49
50     @Before
51     public void setup() {
52         final ModuleInfoBackedContext ctx = ModuleInfoBackedContext.create();
53         ctx.addModuleInfos(BindingReflections.loadModuleInfos());
54         mockedContext = new ClassExcludingClassLoadingStrategy(ctx);
55         schemaContext = ctx.tryToCreateModelContext().get();
56         runtimeContext = BindingRuntimeContext.create(
57             new DefaultBindingRuntimeGenerator().generateTypeMapping(schemaContext), mockedContext);
58         registry = new BindingNormalizedNodeCodecRegistry(runtimeContext);
59     }
60
61     private static final TopLevelListKey TOP_FOO_KEY = new TopLevelListKey("foo");
62     private static final InstanceIdentifier<TopLevelList> BA_TOP_LEVEL_LIST = InstanceIdentifier.builder(Top.class)
63             .child(TopLevelList.class, TOP_FOO_KEY).build();
64     private static final InstanceIdentifier<TreeLeafOnlyAugment> BA_TREE_LEAF_ONLY = BA_TOP_LEVEL_LIST
65             .augmentation(TreeLeafOnlyAugment.class);
66
67
68
69     @Test(expected = MissingClassInLoadingStrategyException.class)
70     public void testCorrectExceptionThrown() {
71         materializeWithExclusions(TreeLeafOnlyAugment.class, TreeComplexUsesAugment.class);
72         registry.toYangInstanceIdentifier(BA_TREE_LEAF_ONLY);
73     }
74
75
76     @Test
77     public void testUsingBindingInstanceIdentifier() {
78         materializeWithExclusions(TreeLeafOnlyAugment.class, TreeComplexUsesAugment.class);
79         mockedContext.includeClass(TreeLeafOnlyAugment.class);
80         final YangInstanceIdentifier domYY = registry.toYangInstanceIdentifier(BA_TREE_LEAF_ONLY);
81         assertNotNull(domYY);
82     }
83
84     @Test
85     public void testUsingBindingData() {
86         materializeWithExclusions(TreeLeafOnlyAugment.class, TreeComplexUsesAugment.class);
87         mockedContext.includeClass(TreeLeafOnlyAugment.class);
88         final TopLevelList data =
89                 new TopLevelListBuilder()
90                         .withKey(TOP_FOO_KEY)
91                         .addAugmentation(TreeLeafOnlyAugment.class,
92                                 new TreeLeafOnlyAugmentBuilder().setSimpleValue("foo").build()).build();
93         final Entry<YangInstanceIdentifier, NormalizedNode<?, ?>> domData =
94                 registry.toNormalizedNode(BA_TOP_LEVEL_LIST, data);
95         assertNotNull(domData);
96     }
97
98
99     private void materializeWithExclusions(final Class<?>... clzToExclude) {
100         for (final Class<?> clz : clzToExclude) {
101             mockedContext.excludeClass(clz);
102         }
103         registry.toYangInstanceIdentifier(BA_TOP_LEVEL_LIST);
104     }
105
106     private static class ClassExcludingClassLoadingStrategy extends GeneratedClassLoadingStrategy {
107
108         private final Set<String> exclusions = new HashSet<>();
109         private final GeneratedClassLoadingStrategy delegate;
110
111         void excludeClass(final Class<?> clz) {
112             exclusions.add(clz.getName());
113         }
114
115         void includeClass(final Class<?> clz) {
116             exclusions.remove(clz.getName());
117         }
118
119         protected ClassExcludingClassLoadingStrategy(final GeneratedClassLoadingStrategy delegate) {
120             this.delegate = requireNonNull(delegate);
121         }
122
123         @Override
124         public Class<?> loadClass(final String fullyQualifiedName) throws ClassNotFoundException {
125             if (exclusions.contains(fullyQualifiedName)) {
126                 throw new ClassNotFoundException(String.format("Class %s is not available for test reasons.",
127                         fullyQualifiedName));
128             }
129             return delegate.loadClass(fullyQualifiedName);
130         }
131     }
132 }