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