afa6eb2151ca8c157aee1f0e9fb197b64d8f2abc
[mdsal.git] / binding / mdsal-binding-dom-adapter / src / test / java / org / opendaylight / mdsal / binding / dom / adapter / BindingNormalizedCodecTest.java
1 /*
2  * Copyright (c) 2014, 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
9 package org.opendaylight.mdsal.binding.dom.adapter;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertTrue;
13 import static org.junit.Assert.fail;
14
15 import com.google.common.collect.ImmutableBiMap;
16 import com.google.common.collect.ImmutableMap;
17 import com.google.common.collect.ImmutableSet;
18 import com.google.common.collect.ImmutableSetMultimap;
19 import com.google.common.collect.SetMultimap;
20 import com.google.common.util.concurrent.Uninterruptibles;
21 import java.lang.reflect.Method;
22 import java.net.URI;
23 import java.util.Map;
24 import java.util.Set;
25 import java.util.concurrent.CountDownLatch;
26 import java.util.concurrent.TimeUnit;
27 import java.util.concurrent.atomic.AtomicReference;
28 import org.junit.Test;
29 import org.opendaylight.mdsal.binding.dom.adapter.test.AbstractSchemaAwareTest;
30 import org.opendaylight.mdsal.binding.dom.codec.impl.BindingNormalizedNodeCodecRegistry;
31 import org.opendaylight.mdsal.binding.generator.impl.GeneratedClassLoadingStrategy;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.bi.ba.rpcservice.rev140701.OpendaylightTestRpcServiceService;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.augment.rev140709.TreeComplexUsesAugment;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.augment.rev140709.TreeLeafOnlyAugment;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.Top;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.two.level.list.TopLevelList;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.two.level.list.TopLevelListKey;
38 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
39 import org.opendaylight.yangtools.yang.common.QName;
40 import org.opendaylight.yangtools.yang.common.QNameModule;
41 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
42 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
43 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
44 import org.opendaylight.yangtools.yang.model.api.Module;
45 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
46 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
47 import org.opendaylight.yangtools.yang.model.util.AbstractSchemaContext;
48
49 public class BindingNormalizedCodecTest extends AbstractSchemaAwareTest {
50
51     private static final TopLevelListKey TOP_FOO_KEY = new TopLevelListKey("foo");
52     private static final InstanceIdentifier<TopLevelList> BA_TOP_LEVEL_LIST = InstanceIdentifier
53             .builder(Top.class).child(TopLevelList.class, TOP_FOO_KEY).build();
54     private static final InstanceIdentifier<TreeLeafOnlyAugment> BA_TREE_LEAF_ONLY = BA_TOP_LEVEL_LIST
55         .augmentation(TreeLeafOnlyAugment.class);
56     private static final InstanceIdentifier<TreeComplexUsesAugment> BA_TREE_COMPLEX_USES = BA_TOP_LEVEL_LIST
57         .augmentation(TreeComplexUsesAugment.class);
58     private static final QName SIMPLE_VALUE_QNAME = QName.create(TreeComplexUsesAugment.QNAME, "simple-value");
59     private static final QName NAME_QNAME = QName.create(Top.QNAME, "name");
60     private static final YangInstanceIdentifier BI_TOP_LEVEL_LIST = YangInstanceIdentifier.builder().node(Top.QNAME)
61         .node(TopLevelList.QNAME).nodeWithKey(TopLevelList.QNAME, NAME_QNAME, TOP_FOO_KEY.getName()).build();
62
63     private BindingToNormalizedNodeCodec codec;
64     private SchemaContext context;
65
66     @Override
67     protected void setupWithSchema(final SchemaContext schemaContext) {
68         this.context = schemaContext;
69         final BindingNormalizedNodeCodecRegistry registry = new BindingNormalizedNodeCodecRegistry();
70         this.codec = new BindingToNormalizedNodeCodec(GeneratedClassLoadingStrategy.getTCCLClassLoadingStrategy(),
71                 registry, true);
72     }
73
74     @Test
75     public void testComplexAugmentationSerialization() {
76         this.codec.onGlobalContextUpdated(this.context);
77         final PathArgument lastArg = this.codec.toYangInstanceIdentifier(BA_TREE_COMPLEX_USES).getLastPathArgument();
78         assertTrue(lastArg instanceof AugmentationIdentifier);
79     }
80
81
82     @Test
83     public void testLeafOnlyAugmentationSerialization() {
84         this.codec.onGlobalContextUpdated(this.context);
85         final PathArgument leafOnlyLastArg = this.codec.toYangInstanceIdentifier(BA_TREE_LEAF_ONLY)
86             .getLastPathArgument();
87         assertTrue(leafOnlyLastArg instanceof AugmentationIdentifier);
88         assertTrue(((AugmentationIdentifier) leafOnlyLastArg).getPossibleChildNames().contains(SIMPLE_VALUE_QNAME));
89     }
90
91     @Test
92     @SuppressWarnings("checkstyle:illegalCatch")
93     public void testToYangInstanceIdentifierBlocking() {
94         this.codec.onGlobalContextUpdated(new EmptySchemaContext());
95
96         final CountDownLatch done = new CountDownLatch(1);
97         final AtomicReference<YangInstanceIdentifier> yangId = new AtomicReference<>();
98         final AtomicReference<RuntimeException> error = new AtomicReference<>();
99         new Thread(() -> {
100             try {
101                 yangId.set(BindingNormalizedCodecTest.this.codec.toYangInstanceIdentifierBlocking(BA_TOP_LEVEL_LIST));
102             } catch (final RuntimeException e) {
103                 error.set(e);
104             } finally {
105                 done.countDown();
106             }
107         }).start();
108
109         Uninterruptibles.sleepUninterruptibly(500, TimeUnit.MILLISECONDS);
110         this.codec.onGlobalContextUpdated(this.context);
111
112         assertTrue("toYangInstanceIdentifierBlocking completed",
113                 Uninterruptibles.awaitUninterruptibly(done, 3, TimeUnit.SECONDS));
114         if (error.get() != null) {
115             throw error.get();
116         }
117
118         assertEquals("toYangInstanceIdentifierBlocking", BI_TOP_LEVEL_LIST, yangId.get());
119     }
120
121     @Test
122     public void testGetRpcMethodToSchemaPathWithNoInitialSchemaContext() {
123         testGetRpcMethodToSchemaPath();
124     }
125
126     @Test
127     public void testGetRpcMethodToSchemaPathBlocking() {
128         this.codec.onGlobalContextUpdated(new EmptySchemaContext());
129         testGetRpcMethodToSchemaPath();
130     }
131
132     @SuppressWarnings("checkstyle:illegalCatch")
133     private void testGetRpcMethodToSchemaPath() {
134         final CountDownLatch done = new CountDownLatch(1);
135         final AtomicReference<ImmutableBiMap<Method, SchemaPath>> retMap = new AtomicReference<>();
136         final AtomicReference<RuntimeException> error = new AtomicReference<>();
137         new Thread(() -> {
138             try {
139                 retMap.set(BindingNormalizedCodecTest.this.codec.getRpcMethodToSchemaPath(
140                             OpendaylightTestRpcServiceService.class));
141             } catch (final RuntimeException e) {
142                 error.set(e);
143             } finally {
144                 done.countDown();
145             }
146         }).start();
147
148         Uninterruptibles.sleepUninterruptibly(500, TimeUnit.MILLISECONDS);
149         this.codec.onGlobalContextUpdated(this.context);
150
151         assertTrue("getRpcMethodToSchemaPath completed",
152                 Uninterruptibles.awaitUninterruptibly(done, 3, TimeUnit.SECONDS));
153         if (error.get() != null) {
154             throw error.get();
155         }
156
157         for (final Method method : retMap.get().keySet()) {
158             if (method.getName().equals("rockTheHouse")) {
159                 return;
160             }
161         }
162
163         fail("rockTheHouse RPC method not found");
164     }
165
166     static class EmptySchemaContext extends AbstractSchemaContext {
167         @Override
168         public Set<Module> getModules() {
169             return ImmutableSet.of();
170         }
171
172         @Override
173         protected Map<QNameModule, Module> getModuleMap() {
174             return ImmutableMap.of();
175         }
176
177         @Override
178         protected SetMultimap<URI, Module> getNamespaceToModules() {
179             return ImmutableSetMultimap.of();
180         }
181
182         @Override
183         protected SetMultimap<String, Module> getNameToModules() {
184             return ImmutableSetMultimap.of();
185         }
186     }
187 }