summaryrefslogtreecommitdiffstats
path: root/netconf/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/netconf/sal/restconf/impl/InstanceIdentifierCodecImplTest.java
blob: e0d48bb6daf733a2051d393750473ad2063ee558 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
 * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 */
package org.opendaylight.netconf.sal.restconf.impl;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

import java.io.FileNotFoundException;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
import org.opendaylight.netconf.sal.restconf.impl.RestCodec.InstanceIdentifierCodecImpl;
import org.opendaylight.restconf.common.util.IdentityValuesDTO;
import org.opendaylight.yangtools.yang.common.QName;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;

public class InstanceIdentifierCodecImplTest {
    private static EffectiveModelContext schemaContext;

    private InstanceIdentifierCodecImpl instanceIdentifierDTO;
    private YangInstanceIdentifier instanceIdentifierBadNamespace;
    private YangInstanceIdentifier instanceIdentifierOKList;
    private YangInstanceIdentifier instanceIdentifierOKLeafList;

    @BeforeClass
    public static void init() throws FileNotFoundException {
        schemaContext = YangParserTestUtils.parseYangFiles(
                TestRestconfUtils.loadFiles("/restconf/parser/deserializer"));
    }

    @Before
    public void setUp() throws Exception {
        ControllerContext controllerContext = TestRestconfUtils.newControllerContext(schemaContext);

        this.instanceIdentifierDTO = new InstanceIdentifierCodecImpl(null, controllerContext);

        final QName baseQName = QName.create("deserializer:test", "2016-06-06", "deserializer-test");
        final QName contA = QName.create(baseQName, "contA");
        final QName leafList = QName.create(baseQName, "leaf-list-A");

        this.instanceIdentifierOKLeafList = YangInstanceIdentifier.builder()
                .node(contA)
                .node(new YangInstanceIdentifier.NodeWithValue<>(leafList, "instance"))
                .build();

        this.instanceIdentifierOKList = YangInstanceIdentifier.builder()
                .node(NodeIdentifierWithPredicates.of(
                        QName.create(baseQName, "list-one-key"),
                        QName.create(QName.create(baseQName, "list-one-key"), "name"), "value"))
                .build();

        this.instanceIdentifierBadNamespace = YangInstanceIdentifier.builder()
                .nodeWithKey(QName.create("nonexistent:module", "2016-10-17", "nonexistent-1"),
                        QName.create("nonexistent:module", "2016-10-17", "nonexistent"),
                        "value")
                .build();
    }

    @Test
    public void testSerializeDeserializeList() throws Exception {
        final IdentityValuesDTO valuesDTO =
                this.instanceIdentifierDTO.serialize(this.instanceIdentifierOKList);

        final YangInstanceIdentifier deserializedIdentifier =
                this.instanceIdentifierDTO.deserialize(valuesDTO);
        assertEquals(this.instanceIdentifierOKList, deserializedIdentifier);
    }

    @Test
    public void testSerializeDeserializeLeafList() throws Exception {
        final IdentityValuesDTO valuesDTO =
                this.instanceIdentifierDTO.serialize(this.instanceIdentifierOKLeafList);

        final YangInstanceIdentifier deserializedIdentifier =
                this.instanceIdentifierDTO.deserialize(valuesDTO);
        assertEquals(this.instanceIdentifierOKLeafList, deserializedIdentifier);
    }

    @Test
    public void testSerializeDeserializeBadModuleNamespace() throws Exception {
        final IdentityValuesDTO valuesDTO =
                this.instanceIdentifierDTO.serialize(this.instanceIdentifierBadNamespace);
        assertEquals("nonexistent-1", valuesDTO.getValuesWithNamespaces().get(0).getValue());
        assertEquals("nonexistent:module", valuesDTO.getValuesWithNamespaces().get(0).getNamespace());

        final YangInstanceIdentifier deserializedIdentifier =
                this.instanceIdentifierDTO.deserialize(valuesDTO);
        assertNull(deserializedIdentifier);
    }
}