2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.restconf.nb.rfc8040.utils.parser;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertTrue;
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;
23 * Unit tests for {@link IdentifierCodec} mostly according to examples from draft-ietf-netconf-restconf-13.
25 public class IdentifierCodecTest {
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";
31 private EffectiveModelContext schemaContext;
34 public void init() throws FileNotFoundException {
35 this.schemaContext = YangParserTestUtils.parseYangFiles(TestRestconfUtils.loadFiles("/restconf/parser"));
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.
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);
49 assertEquals("Failed codec deserialization and serialization test",
50 IdentifierCodecTest.URI_WITH_LIST_AND_LEAF, serializedDataYangII);
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.
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);
64 assertEquals("Failed codec deserialization and serialization test",
65 IdentifierCodecTest.URI_WITH_INT_VAL_LEAF_LIST, serializedDataYangII);
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.
74 public void codecDeserializeNullTest() {
75 final YangInstanceIdentifier dataYangII = IdentifierCodec.deserialize(null, this.schemaContext);
76 assertEquals("Failed codec deserialization test", YangInstanceIdentifier.empty(), dataYangII);
80 * Positive test of serialization <code>YangInstanceIdentifier.EMPTY</code>. Empty <code>String</code> is
81 * expected to be returned.
84 public void codecSerializeEmptyTest() {
85 final String serialized = IdentifierCodec.serialize(YangInstanceIdentifier.empty(), this.schemaContext);
86 assertTrue("Failed codec serialization test", serialized.isEmpty());
90 * Positive test of serialization <code>YangInstanceIdentifier.EMPTY</code> and deserialization of result back to
91 * <code>YangInstanceIdentifier.EMPTY</code>.
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));