Bug 6931 - Fix unsupported specific type of leaf
[netconf.git] / restconf / sal-rest-connector / src / test / java / org / opendaylight / restconf / parser / IdentifierCodecTest.java
1 /*
2  * Copyright (c) 2016 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.restconf.parser;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertTrue;
13 import org.junit.Before;
14 import org.junit.Test;
15 import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
16 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
17 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
18
19 /**
20  * Unit tests for {@link IdentifierCodec} mostly according to examples from draft-ietf-netconf-restconf-13
21  */
22 public class IdentifierCodecTest {
23
24     private static final String URI_WITH_LIST_AND_LEAF =
25             "list-test:top/list1=%2C%27" + '"' + "%3A" + '"' + "%20%2F,,foo/list2=a,b/result";
26     private static final String URI_WITH_LEAF_LIST = "list-test:top/Y=x%3Ay";
27     private static final String URI_WITH_INT_VAL_LEAF_LIST = "list-test:top/Y=4";
28
29     private SchemaContext schemaContext;
30
31     @Before
32     public void init() throws Exception {
33         this.schemaContext = TestRestconfUtils.loadSchemaContext("/restconf/parser");
34     }
35
36     /**
37      * Positive test of deserialization URI <code>String</code> to <code>YangInstanceIdentifier</code> and
38      * serialization of <code>YangInstanceIdentifier</code> to <code>String/code> when original <code>String</code>
39      * URI contains list identifier and leaf identifier.
40      */
41     @Test
42     public void codecListAndLeafTest() {
43         final YangInstanceIdentifier dataYangII = IdentifierCodec.deserialize(
44                 this.URI_WITH_LIST_AND_LEAF, this.schemaContext);
45         final String serializedDataYangII = IdentifierCodec.serialize(dataYangII, this.schemaContext);
46
47         assertEquals("Failed codec deserialization and serialization test",
48                 this.URI_WITH_LIST_AND_LEAF, serializedDataYangII);
49     }
50
51     /**
52      * Positive test of deserialization URI <code>String</code> to <code>YangInstanceIdentifier</code> and
53      * serialization of <code>YangInstanceIdentifier</code> to <code>String/code> when original <code>String</code>
54      * URI contains leaf list identifier.
55      */
56     @Test
57     public void codecLeafListTest() {
58         final YangInstanceIdentifier dataYangII = IdentifierCodec.deserialize(
59                 this.URI_WITH_INT_VAL_LEAF_LIST, this.schemaContext);
60         final String serializedDataYangII = IdentifierCodec.serialize(dataYangII, this.schemaContext);
61
62         assertEquals("Failed codec deserialization and serialization test",
63                 this.URI_WITH_INT_VAL_LEAF_LIST, serializedDataYangII);
64     }
65
66     /**
67      * Positive test of deserialization URI <code>String</code> to <code>YangInstanceIdentifier</code> when
68      * <code>String</code> URI is <code>null</code>. <code>YangInstanceIdentifier.EMPTY</code> is
69      * expected to be returned.
70      */
71     @Test
72     public void codecDeserializeNullTest () {
73         final YangInstanceIdentifier dataYangII = IdentifierCodec.deserialize(null, this.schemaContext);
74         assertEquals("Failed codec deserialization test", YangInstanceIdentifier.EMPTY, dataYangII);
75     }
76
77     /**
78      * Positive test of serialization <code>YangInstanceIdentifier.EMPTY</code>. Empty <code>String</code> is
79      * expected to be returned.
80      */
81     @Test
82     public void codecSerializeEmptyTest () {
83         final String serialized = IdentifierCodec.serialize(YangInstanceIdentifier.EMPTY, this.schemaContext);
84         assertTrue("Failed codec serialization test", serialized.isEmpty());
85     }
86
87     /**
88      * Positive test of serialization <code>YangInstanceIdentifier.EMPTY</code> and deserialization of result back to
89      * <code>YangInstanceIdentifier.EMPTY</code>.
90      */
91     @Test
92     public void codecDeserializeAndSerializeEmptyTest() {
93         final String serialized = IdentifierCodec.serialize(YangInstanceIdentifier.EMPTY, this.schemaContext);
94         assertEquals("Failed codec serialization and deserialization test",
95                 YangInstanceIdentifier.EMPTY, IdentifierCodec.deserialize(serialized, this.schemaContext));
96     }
97 }