Fix checkstyle in mdsal-binding-dom-codec
[mdsal.git] / binding / mdsal-binding-dom-codec / src / test / java / org / opendaylight / mdsal / binding / dom / codec / test / ExceptionReportingTest.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 javassist.ClassPool;
11 import org.junit.Test;
12 import org.opendaylight.mdsal.binding.dom.codec.gen.impl.StreamWriterGenerator;
13 import org.opendaylight.mdsal.binding.dom.codec.impl.BindingNormalizedNodeCodecRegistry;
14 import org.opendaylight.mdsal.binding.dom.codec.impl.IncorrectNestingException;
15 import org.opendaylight.mdsal.binding.dom.codec.impl.MissingSchemaException;
16 import org.opendaylight.mdsal.binding.dom.codec.impl.MissingSchemaForClassException;
17 import org.opendaylight.mdsal.binding.generator.impl.ModuleInfoBackedContext;
18 import org.opendaylight.mdsal.binding.generator.util.BindingRuntimeContext;
19 import org.opendaylight.mdsal.binding.generator.util.JavassistUtils;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.augment.rev140709.TreeComplexUsesAugment;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.augment.rev140709.TreeLeafOnlyAugment;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.Top;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.two.level.list.TopLevelList;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.two.level.list.TopLevelListKey;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.yangtools.test.union.rev150121.LowestLevel1;
26 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
27 import org.opendaylight.yangtools.yang.binding.YangModuleInfo;
28 import org.opendaylight.yangtools.yang.binding.util.BindingReflections;
29 import org.opendaylight.yangtools.yang.common.QName;
30 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
31 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
32
33 public class ExceptionReportingTest {
34
35
36     private static final BindingNormalizedNodeCodecRegistry CODEC_WITHOUT_TOP = codec(LowestLevel1.class);
37     private static final BindingNormalizedNodeCodecRegistry ONLY_TOP_CODEC = codec(Top.class);
38     private static final BindingNormalizedNodeCodecRegistry FULL_CODEC = codec(TreeComplexUsesAugment.class);
39
40     private static final TopLevelListKey TOP_FOO_KEY = new TopLevelListKey("foo");
41     private static final InstanceIdentifier<TopLevelList> BA_TOP_LEVEL_LIST = InstanceIdentifier
42             .builder(Top.class).child(TopLevelList.class, TOP_FOO_KEY).build();
43     private static final InstanceIdentifier<TreeLeafOnlyAugment> BA_TREE_LEAF_ONLY =
44             BA_TOP_LEVEL_LIST.augmentation(TreeLeafOnlyAugment.class);
45
46     private static final QName TOP_QNAME = Top.QNAME;
47     private static final YangInstanceIdentifier BI_TOP_PATH = YangInstanceIdentifier.builder().node(TOP_QNAME).build();
48     private static final YangInstanceIdentifier BI_TREE_LEAF_ONLY = FULL_CODEC.toYangInstanceIdentifier(
49         BA_TREE_LEAF_ONLY);
50
51     @Test(expected = MissingSchemaException.class)
52     public void testDOMTop() {
53         CODEC_WITHOUT_TOP.fromYangInstanceIdentifier(BI_TOP_PATH);
54     }
55
56     @Test(expected = MissingSchemaException.class)
57     public void testDOMAugment() {
58         CODEC_WITHOUT_TOP.fromYangInstanceIdentifier(BI_TREE_LEAF_ONLY);
59     }
60
61     @Test(expected = MissingSchemaForClassException.class)
62     public void testBindingTop() {
63         CODEC_WITHOUT_TOP.toYangInstanceIdentifier(BA_TOP_LEVEL_LIST);
64     }
65
66     @Test(expected = MissingSchemaForClassException.class)
67     public void testBindingAugment() {
68         ONLY_TOP_CODEC.toYangInstanceIdentifier(BA_TREE_LEAF_ONLY);
69     }
70
71     @Test(expected = IncorrectNestingException.class)
72     public void testBindingSkippedRoot() {
73         FULL_CODEC.toYangInstanceIdentifier(InstanceIdentifier.create(TopLevelList.class));
74     }
75
76     @SuppressWarnings({"unchecked", "rawtypes"})
77     @Test(expected = IncorrectNestingException.class)
78     public void testBindingIncorrectAugment() {
79         FULL_CODEC.toYangInstanceIdentifier(InstanceIdentifier.create(Top.class).augmentation(
80             (Class) TreeComplexUsesAugment.class));
81     }
82
83     @SuppressWarnings("checkstyle:illegalCatch")
84     private static BindingNormalizedNodeCodecRegistry codec(final Class<?>... classes) {
85         final ModuleInfoBackedContext ctx = ModuleInfoBackedContext.create();
86         for (final Class<?> clazz : classes) {
87             YangModuleInfo modInfo;
88             try {
89                 modInfo = BindingReflections.getModuleInfo(clazz);
90                 ctx.registerModuleInfo(modInfo);
91             } catch (final Exception e) {
92                 throw new IllegalStateException(e);
93             }
94         }
95         final SchemaContext schema = ctx.tryToCreateSchemaContext().get();
96         final BindingRuntimeContext runtimeCtx = BindingRuntimeContext.create(ctx, schema);
97         final JavassistUtils utils = JavassistUtils.forClassPool(ClassPool.getDefault());
98         final BindingNormalizedNodeCodecRegistry registry = new BindingNormalizedNodeCodecRegistry(
99             StreamWriterGenerator.create(utils));
100         registry.onBindingRuntimeContextUpdated(runtimeCtx);
101         return registry;
102     }
103 }