Bug 6099 - ControllerContext#addKeyValue ignores key type when key is derived
[netconf.git] / restconf / sal-rest-connector / src / test / java / org / opendaylight / controller / sal / rest / impl / InstanceIdentifierTypeLeafTest.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 package org.opendaylight.controller.sal.rest.impl;
9
10 import org.junit.Assert;
11 import org.junit.Test;
12 import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
13 import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
14 import org.opendaylight.netconf.sal.restconf.impl.InstanceIdentifierContext;
15 import org.opendaylight.yangtools.yang.common.QName;
16 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
19 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
20
21 public class InstanceIdentifierTypeLeafTest {
22
23     @Test
24     public void stringToInstanceIdentifierTest() throws Exception {
25         final SchemaContext schemaContext = TestRestconfUtils.loadSchemaContext("/instanceidentifier");
26         ControllerContext.getInstance().setGlobalSchema(schemaContext);
27         final InstanceIdentifierContext<?> instanceIdentifier =
28                 ControllerContext.getInstance().toInstanceIdentifier(
29                         "/iid-value-module:cont-iid/iid-list/%2Fiid-value-module%3Acont-iid%2Fiid-value-module%3A"
30                                 + "values-iid%5Biid-value-module:value-iid='value'%5D");
31         final YangInstanceIdentifier yiD = instanceIdentifier.getInstanceIdentifier();
32         Assert.assertNotNull(yiD);
33         final PathArgument lastPathArgument = yiD.getLastPathArgument();
34         Assert.assertTrue(lastPathArgument.getNodeType().getNamespace().toString().equals("iid:value:module"));
35         Assert.assertTrue(lastPathArgument.getNodeType().getLocalName().equals("iid-list"));
36
37         final NodeIdentifierWithPredicates list = (NodeIdentifierWithPredicates) lastPathArgument;
38         final YangInstanceIdentifier value = (YangInstanceIdentifier) list.getKeyValues()
39                 .get(QName.create(lastPathArgument.getNodeType(), "iid-leaf"));
40         final PathArgument lastPathArgumentOfValue = value.getLastPathArgument();
41         Assert.assertTrue(lastPathArgumentOfValue.getNodeType().getNamespace().toString().equals("iid:value:module"));
42         Assert.assertTrue(lastPathArgumentOfValue.getNodeType().getLocalName().equals("values-iid"));
43
44         final NodeIdentifierWithPredicates valueList = (NodeIdentifierWithPredicates) lastPathArgumentOfValue;
45         final String valueIid = (String) valueList.getKeyValues()
46                 .get(QName.create(lastPathArgumentOfValue.getNodeType(), "value-iid"));
47         Assert.assertEquals("value", valueIid);
48     }
49
50 }