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