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