Move mdsal-binding-dom-codec tests
[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.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;
34
35 /**
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.
38  *
39  * <p>
40  * The idea of this suite is to test that codecs will work even if situation like this happens.
41  */
42 public class AugmentationClassDiscoveredAfterCodecTest {
43
44     private SchemaContext schemaContext;
45     private BindingRuntimeContext runtimeContext;
46     private ClassExcludingClassLoadingStrategy mockedContext;
47     private BindingNormalizedNodeCodecRegistry registry;
48
49     @Before
50     public void setup() {
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);
57     }
58
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);
64
65
66
67     @Test(expected = MissingClassInLoadingStrategyException.class)
68     public void testCorrectExceptionThrown() {
69         materializeWithExclusions(TreeLeafOnlyAugment.class, TreeComplexUsesAugment.class);
70         registry.toYangInstanceIdentifier(BA_TREE_LEAF_ONLY);
71     }
72
73
74     @Test
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);
79         assertNotNull(domYY);
80     }
81
82     @Test
83     public void testUsingBindingData() {
84         materializeWithExclusions(TreeLeafOnlyAugment.class, TreeComplexUsesAugment.class);
85         mockedContext.includeClass(TreeLeafOnlyAugment.class);
86         final TopLevelList data =
87                 new TopLevelListBuilder()
88                         .withKey(TOP_FOO_KEY)
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);
94     }
95
96
97     private void materializeWithExclusions(final Class<?>... clzToExclude) {
98         for (final Class<?> clz : clzToExclude) {
99             mockedContext.excludeClass(clz);
100         }
101         registry.toYangInstanceIdentifier(BA_TOP_LEVEL_LIST);
102     }
103
104     private static class ClassExcludingClassLoadingStrategy extends GeneratedClassLoadingStrategy {
105
106         private final Set<String> exclusions = new HashSet<>();
107         private final GeneratedClassLoadingStrategy delegate;
108
109         void excludeClass(final Class<?> clz) {
110             exclusions.add(clz.getName());
111         }
112
113         void includeClass(final Class<?> clz) {
114             exclusions.remove(clz.getName());
115         }
116
117         protected ClassExcludingClassLoadingStrategy(final GeneratedClassLoadingStrategy delegate) {
118             this.delegate = requireNonNull(delegate);
119         }
120
121         @Override
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));
126             }
127             return delegate.loadClass(fullyQualifiedName);
128         }
129     }
130 }