b3586c124f837c591389e56d09b7a8734bf4c1b1
[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.binding.runtime.api.BindingRuntimeContext;
19 import org.opendaylight.binding.runtime.api.ClassLoadingStrategy;
20 import org.opendaylight.binding.runtime.api.DefaultBindingRuntimeContext;
21 import org.opendaylight.binding.runtime.spi.BindingRuntimeHelpers;
22 import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer;
23 import org.opendaylight.mdsal.binding.dom.codec.api.MissingClassInLoadingStrategyException;
24 import org.opendaylight.mdsal.binding.generator.impl.DefaultBindingRuntimeGenerator;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.augment.rev140709.TreeComplexUsesAugment;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.augment.rev140709.TreeLeafOnlyAugment;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.augment.rev140709.TreeLeafOnlyAugmentBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.Top;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.two.level.list.TopLevelList;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.two.level.list.TopLevelListBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.two.level.list.TopLevelListKey;
32 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
33 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
34 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
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     private BindingNormalizedNodeSerializer serializer;
45     private FilteringClassLoadingStrategy filter;
46
47     @Before
48     public void setup() {
49         // Baseline state: strategy is cognizant of the classes
50         final BindingRuntimeContext delegate = BindingRuntimeHelpers.createRuntimeContext(
51             new DefaultBindingRuntimeGenerator());
52
53         // Class loading filter, manipulated by tests
54         filter = new FilteringClassLoadingStrategy(delegate.getStrategy());
55         serializer = new BindingCodecContext(DefaultBindingRuntimeContext.create(delegate.getTypes(), filter));
56     }
57
58     private static final TopLevelListKey TOP_FOO_KEY = new TopLevelListKey("foo");
59     private static final InstanceIdentifier<TopLevelList> BA_TOP_LEVEL_LIST = InstanceIdentifier.builder(Top.class)
60             .child(TopLevelList.class, TOP_FOO_KEY).build();
61     private static final InstanceIdentifier<TreeLeafOnlyAugment> BA_TREE_LEAF_ONLY = BA_TOP_LEVEL_LIST
62             .augmentation(TreeLeafOnlyAugment.class);
63
64     @Test(expected = MissingClassInLoadingStrategyException.class)
65     public void testCorrectExceptionThrown() {
66         materializeWithExclusions(TreeLeafOnlyAugment.class, TreeComplexUsesAugment.class);
67         serializer.toYangInstanceIdentifier(BA_TREE_LEAF_ONLY);
68     }
69
70     @Test
71     public void testUsingBindingInstanceIdentifier() {
72         materializeWithExclusions(TreeLeafOnlyAugment.class, TreeComplexUsesAugment.class);
73         filter.includeClass(TreeLeafOnlyAugment.class);
74         final YangInstanceIdentifier domYY = serializer.toYangInstanceIdentifier(BA_TREE_LEAF_ONLY);
75         assertNotNull(domYY);
76     }
77
78     @Test
79     public void testUsingBindingData() {
80         materializeWithExclusions(TreeLeafOnlyAugment.class, TreeComplexUsesAugment.class);
81         filter.includeClass(TreeLeafOnlyAugment.class);
82         final TopLevelList data =
83                 new TopLevelListBuilder()
84                         .withKey(TOP_FOO_KEY)
85                         .addAugmentation(TreeLeafOnlyAugment.class,
86                                 new TreeLeafOnlyAugmentBuilder().setSimpleValue("foo").build()).build();
87         final Entry<YangInstanceIdentifier, NormalizedNode<?, ?>> domData =
88                 serializer.toNormalizedNode(BA_TOP_LEVEL_LIST, data);
89         assertNotNull(domData);
90     }
91
92     private void materializeWithExclusions(final Class<?>... clzToExclude) {
93         for (final Class<?> clz : clzToExclude) {
94             filter.excludeClass(clz);
95         }
96         serializer.toYangInstanceIdentifier(BA_TOP_LEVEL_LIST);
97     }
98
99     private static final class FilteringClassLoadingStrategy implements ClassLoadingStrategy {
100         private final Set<String> exclusions = new HashSet<>();
101         private final ClassLoadingStrategy delegate;
102
103         FilteringClassLoadingStrategy(final ClassLoadingStrategy delegate) {
104             this.delegate = requireNonNull(delegate);
105         }
106
107         void excludeClass(final Class<?> clz) {
108             exclusions.add(clz.getName());
109         }
110
111         void includeClass(final Class<?> clz) {
112             exclusions.remove(clz.getName());
113         }
114
115         @Override
116         public Class<?> loadClass(final String fullyQualifiedName) throws ClassNotFoundException {
117             if (exclusions.contains(fullyQualifiedName)) {
118                 throw new ClassNotFoundException(String.format("Class %s is not available for test reasons.",
119                         fullyQualifiedName));
120             }
121             return delegate.loadClass(fullyQualifiedName);
122         }
123     }
124 }