Fix license header violations in sal-binding-broker
[controller.git] / opendaylight / md-sal / sal-binding-broker / src / test / java / org / opendaylight / controller / md / sal / binding / impl / test / 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.controller.md.sal.binding.impl.test;
10
11 import static org.junit.Assert.assertTrue;
12
13 import javassist.ClassPool;
14 import org.junit.Test;
15 import org.opendaylight.controller.md.sal.binding.impl.BindingToNormalizedNodeCodec;
16 import org.opendaylight.controller.md.sal.binding.test.AbstractSchemaAwareTest;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.augment.rev140709.TreeComplexUsesAugment;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.augment.rev140709.TreeLeafOnlyAugment;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.Top;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.two.level.list.TopLevelList;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.two.level.list.TopLevelListKey;
22 import org.opendaylight.yangtools.binding.data.codec.gen.impl.DataObjectSerializerGenerator;
23 import org.opendaylight.yangtools.binding.data.codec.gen.impl.StreamWriterGenerator;
24 import org.opendaylight.yangtools.binding.data.codec.impl.BindingNormalizedNodeCodecRegistry;
25 import org.opendaylight.yangtools.sal.binding.generator.impl.GeneratedClassLoadingStrategy;
26 import org.opendaylight.yangtools.sal.binding.generator.util.JavassistUtils;
27 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
28 import org.opendaylight.yangtools.yang.common.QName;
29 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
30 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
31 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
32
33 public class BindingNormalizedCodecTest extends AbstractSchemaAwareTest {
34
35     private static final TopLevelListKey TOP_FOO_KEY = new TopLevelListKey("foo");
36     private static final InstanceIdentifier<TopLevelList> BA_TOP_LEVEL_LIST = InstanceIdentifier
37             .builder(Top.class).child(TopLevelList.class, TOP_FOO_KEY).toInstance();
38     private static final InstanceIdentifier<TreeLeafOnlyAugment> BA_TREE_LEAF_ONLY = BA_TOP_LEVEL_LIST.augmentation(TreeLeafOnlyAugment.class);
39     private static final InstanceIdentifier<TreeComplexUsesAugment> BA_TREE_COMPLEX_USES = BA_TOP_LEVEL_LIST.augmentation(TreeComplexUsesAugment.class);
40     private static final QName SIMPLE_VALUE_QNAME = QName.create(TreeComplexUsesAugment.QNAME, "simple-value");
41
42
43    private BindingToNormalizedNodeCodec codec;
44
45     @Override
46     protected void setupWithSchema(final SchemaContext context) {
47         final DataObjectSerializerGenerator streamWriter = StreamWriterGenerator.create(JavassistUtils.forClassPool(ClassPool.getDefault()));
48         final BindingNormalizedNodeCodecRegistry registry = new BindingNormalizedNodeCodecRegistry(streamWriter);
49         codec = new BindingToNormalizedNodeCodec(GeneratedClassLoadingStrategy.getTCCLClassLoadingStrategy(), registry);
50         codec.onGlobalContextUpdated(context);
51     };
52
53     @Test
54     public void testComplexAugmentationSerialization() {
55
56         final PathArgument lastArg = codec.toYangInstanceIdentifier(BA_TREE_COMPLEX_USES).getLastPathArgument();
57         assertTrue(lastArg instanceof AugmentationIdentifier);
58     }
59
60
61     @Test
62     public void testLeafOnlyAugmentationSerialization() {
63
64         final PathArgument leafOnlyLastArg = codec.toYangInstanceIdentifier(BA_TREE_LEAF_ONLY).getLastPathArgument();
65         assertTrue(leafOnlyLastArg instanceof AugmentationIdentifier);
66         assertTrue(((AugmentationIdentifier) leafOnlyLastArg).getPossibleChildNames().contains(SIMPLE_VALUE_QNAME));
67     }
68
69 }