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