Fix checkstyle in mdsal-binding-dom-codec
[mdsal.git] / binding / mdsal-binding-dom-codec / src / test / java / org / opendaylight / mdsal / binding / dom / codec / test / 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.test;
9
10 import static org.junit.Assert.assertNotNull;
11
12 import com.google.common.base.Preconditions;
13 import java.util.HashSet;
14 import java.util.Map.Entry;
15 import java.util.Set;
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.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.augment.rev140709.TreeComplexUsesAugment;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.augment.rev140709.TreeLeafOnlyAugment;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.augment.rev140709.TreeLeafOnlyAugmentBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.Top;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.two.level.list.TopLevelList;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.two.level.list.TopLevelListBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.two.level.list.TopLevelListKey;
33 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
34 import org.opendaylight.yangtools.yang.binding.util.BindingReflections;
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;
38
39 /**
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.
42  *
43  * <p>
44  * The idea of this suite is to test that codecs will work even if situation like this happens.
45  */
46 public class AugmentationClassDiscoveredAfterCodecTest {
47
48     private SchemaContext schemaContext;
49     private BindingRuntimeContext runtimeContext;
50     private ClassExcludingClassLoadingStrategy mockedContext;
51     private BindingNormalizedNodeCodecRegistry registry;
52
53     @Before
54     public void setup() {
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);
63     }
64
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);
70
71
72
73     @Test(expected = MissingClassInLoadingStrategyException.class)
74     public void testCorrectExceptionThrown() {
75         materializeWithExclusions(TreeLeafOnlyAugment.class, TreeComplexUsesAugment.class);
76         registry.toYangInstanceIdentifier(BA_TREE_LEAF_ONLY);
77     }
78
79
80     @Test
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);
85         assertNotNull(domYY);
86     }
87
88     @Test
89     public void testUsingBindingData() {
90         materializeWithExclusions(TreeLeafOnlyAugment.class, TreeComplexUsesAugment.class);
91         mockedContext.includeClass(TreeLeafOnlyAugment.class);
92         final TopLevelList data =
93                 new TopLevelListBuilder()
94                         .setKey(TOP_FOO_KEY)
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);
100     }
101
102
103     private void materializeWithExclusions(final Class<?>... clzToExclude) {
104         for (final Class<?> clz : clzToExclude) {
105             mockedContext.excludeClass(clz);
106         }
107         registry.toYangInstanceIdentifier(BA_TOP_LEVEL_LIST);
108     }
109
110     private static class ClassExcludingClassLoadingStrategy extends GeneratedClassLoadingStrategy {
111
112         private final Set<String> exclusions = new HashSet<>();
113         private final GeneratedClassLoadingStrategy delegate;
114
115         void excludeClass(final Class<?> clz) {
116             exclusions.add(clz.getName());
117         }
118
119         void includeClass(final Class<?> clz) {
120             exclusions.remove(clz.getName());
121         }
122
123         protected ClassExcludingClassLoadingStrategy(final GeneratedClassLoadingStrategy delegate) {
124             this.delegate = Preconditions.checkNotNull(delegate);
125         }
126
127         @Override
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));
132             }
133             return delegate.loadClass(fullyQualifiedName);
134         }
135     }
136 }