summaryrefslogtreecommitdiffstats
path: root/netconf/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/CnSnToXmlAndJsonInstanceIdentifierTest.java
blob: 96100cbb5d2157cdb50a96dc10e65ba3a887a311 (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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
 * Copyright (c) 2014 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.controller.sal.restconf.impl.test;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.io.ByteArrayInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent;
import org.junit.BeforeClass;
import org.opendaylight.yangtools.yang.common.QName;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;

public class CnSnToXmlAndJsonInstanceIdentifierTest extends YangAndXmlAndDataSchemaLoader {

    @BeforeClass
    public static void initialize() throws FileNotFoundException, ReactorException {
        dataLoad("/instanceidentifier/yang", 4, "instance-identifier-module", "cont");
    }


    private static void validateXmlOutput(final String xml) throws XMLStreamException {
        final XMLInputFactory xmlInFactory = XMLInputFactory.newInstance();
        XMLEventReader eventReader;

        eventReader = xmlInFactory.createXMLEventReader(new ByteArrayInputStream(xml.getBytes()));
        String augmentAugmentModulePrefix = null;
        String augmentModulePrefix = null;
        String instanceIdentifierModulePrefix = null;
        while (eventReader.hasNext()) {
            final XMLEvent nextEvent = eventReader.nextEvent();
            if (nextEvent.isStartElement()) {
                final StartElement startElement = (StartElement) nextEvent;
                if (startElement.getName().getLocalPart().equals("lf111")) {
                    final Iterator<?> prefixes =
                            startElement.getNamespaceContext().getPrefixes("augment:augment:module");

                    while (prefixes.hasNext() && augmentAugmentModulePrefix == null) {
                        final String prefix = (String) prefixes.next();
                        if (!prefix.isEmpty()) {
                            augmentAugmentModulePrefix = prefix;
                        }
                    }

                    augmentModulePrefix = startElement.getNamespaceContext().getPrefix("augment:module");
                    instanceIdentifierModulePrefix =
                            startElement.getNamespaceContext().getPrefix("instance:identifier:module");
                    break;
                }
            }
        }

        assertNotNull(augmentAugmentModulePrefix);
        assertNotNull(augmentModulePrefix);
        assertNotNull(instanceIdentifierModulePrefix);

        final String instanceIdentifierValue = "/" + instanceIdentifierModulePrefix + ":cont/"
                + instanceIdentifierModulePrefix + ":cont1/" + augmentModulePrefix + ":lst11[" + augmentModulePrefix
                + ":keyvalue111='value1'][" + augmentModulePrefix + ":keyvalue112='value2']/"
                + augmentAugmentModulePrefix + ":lf112";

        assertTrue(xml.contains(instanceIdentifierValue));

    }

    private static void validateXmlOutputWithLeafList(final String xml) throws XMLStreamException {
        final XMLInputFactory xmlInFactory = XMLInputFactory.newInstance();
        XMLEventReader eventReader;

        eventReader = xmlInFactory.createXMLEventReader(new ByteArrayInputStream(xml.getBytes()));
        String augmentModuleLfLstPrefix = null;
        String iiModulePrefix = null;
        while (eventReader.hasNext()) {
            final XMLEvent nextEvent = eventReader.nextEvent();
            if (nextEvent.isStartElement()) {
                final StartElement startElement = (StartElement) nextEvent;
                if (startElement.getName().getLocalPart().equals("lf111")) {
                    final Iterator<?> prefixes =
                            startElement.getNamespaceContext().getPrefixes("augment:module:leaf:list");

                    while (prefixes.hasNext() && augmentModuleLfLstPrefix == null) {
                        final String prefix = (String) prefixes.next();
                        if (!prefix.isEmpty()) {
                            augmentModuleLfLstPrefix = prefix;
                        }
                    }
                    iiModulePrefix = startElement.getNamespaceContext().getPrefix("instance:identifier:module");
                    break;
                }
            }
        }

        assertNotNull(augmentModuleLfLstPrefix);
        assertNotNull(iiModulePrefix);

        final String instanceIdentifierValue = "/" + iiModulePrefix + ":cont/" + iiModulePrefix + ":cont1/"
                + augmentModuleLfLstPrefix + ":lflst11[.='lflst11_1']";

        assertTrue(xml.contains(instanceIdentifierValue));

    }

    private static YangInstanceIdentifier createInstanceIdentifier() {
        final List<PathArgument> pathArguments = new ArrayList<>();
        pathArguments.add(new NodeIdentifier(QName.create("instance:identifier:module", "cont")));
        pathArguments.add(new NodeIdentifier(QName.create("instance:identifier:module", "cont1")));

        final QName qName = QName.create("augment:module", "lst11");
        final Map<QName, Object> keyValues = new HashMap<>();
        keyValues.put(QName.create("augment:module", "keyvalue111"), "value1");
        keyValues.put(QName.create("augment:module", "keyvalue112"), "value2");
        final NodeIdentifierWithPredicates nodeIdentifierWithPredicates =
                NodeIdentifierWithPredicates.of(qName, keyValues);
        pathArguments.add(nodeIdentifierWithPredicates);

        pathArguments.add(new NodeIdentifier(QName.create("augment:augment:module", "lf112")));

        return YangInstanceIdentifier.create(pathArguments);
    }

    private static YangInstanceIdentifier createInstanceIdentifierWithLeafList() {
        final List<PathArgument> pathArguments = new ArrayList<>();
        pathArguments.add(new NodeIdentifier(QName.create("instance:identifier:module", "cont")));
        pathArguments.add(new NodeIdentifier(QName.create("instance:identifier:module", "cont1")));
        pathArguments.add(new NodeWithValue<>(QName.create("augment:module:leaf:list", "lflst11"), "lflst11_1"));

        return YangInstanceIdentifier.create(pathArguments);
    }

}