a65babdd645367506de55b1712dbe994f61acee9
[netconf.git] / restconf / restconf-nb-bierman02 / src / test / java / org / opendaylight / controller / sal / restconf / impl / test / Bug8072Test.java
1 /*
2  * Copyright (c) 2017 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.restconf.impl.test;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.mockito.Mockito.doReturn;
12 import static org.mockito.Mockito.mock;
13
14 import java.io.FileNotFoundException;
15 import java.util.HashMap;
16 import java.util.Map;
17 import java.util.Optional;
18 import org.junit.BeforeClass;
19 import org.junit.Test;
20 import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
21 import org.opendaylight.mdsal.dom.api.DOMMountPoint;
22 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
23 import org.opendaylight.mdsal.dom.spi.FixedDOMSchemaService;
24 import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
25 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
26 import org.opendaylight.yangtools.yang.common.QName;
27 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
28 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
29 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
30 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
31
32 public class Bug8072Test {
33     private static final String EXTERNAL_MODULE_NAME = "test-module";
34     private static final QName MODULES_QNAME = QName.create("test:module", "2014-01-09", "modules");
35     private static final QName MODULE_QNAME = QName.create("test:module", "2014-01-09", "module");
36     private static final QName NAME_QNAME = QName.create("test:module", "2014-01-09", "name");
37     private static final QName TYPE_QNAME = QName.create("test:module", "2014-01-09", "type");
38     private static final QName MODULE_TYPE_QNAME = QName.create("test:module", "2014-01-09", "module-type");
39
40     private static EffectiveModelContext schemaContext;
41
42     private final ControllerContext controllerContext;
43
44     public Bug8072Test() throws FileNotFoundException {
45         final EffectiveModelContext mountPointContext = TestUtils.loadSchemaContext("/full-versions/test-module");
46         final DOMMountPoint mountInstance = mock(DOMMountPoint.class);
47         controllerContext = TestRestconfUtils.newControllerContext(schemaContext, mountInstance);
48         doReturn(Optional.of(FixedDOMSchemaService.of(() -> mountPointContext))).when(mountInstance)
49             .getService(DOMSchemaService.class);
50     }
51
52     @BeforeClass
53     public static void init() throws FileNotFoundException, ReactorException {
54         schemaContext = TestUtils.loadSchemaContext("/full-versions/yangs");
55         assertEquals(0, schemaContext.findModules(EXTERNAL_MODULE_NAME).size());
56     }
57
58     @Test
59     public void testIdentityRefFromExternalModule() throws FileNotFoundException, ReactorException {
60         final InstanceIdentifierContext ctx = controllerContext.toInstanceIdentifier(
61                 "simple-nodes:users/yang-ext:mount/test-module:modules/module/test-module:module-type/name");
62
63         final Map<QName, Object> keyValues = new HashMap<>();
64         keyValues.put(NAME_QNAME, "name");
65         keyValues.put(TYPE_QNAME, MODULE_TYPE_QNAME);
66         final YangInstanceIdentifier expectedYII = YangInstanceIdentifier.of(MODULES_QNAME).node(MODULE_QNAME)
67             .node(NodeIdentifierWithPredicates.of(MODULE_QNAME, keyValues));
68
69         assertEquals(expectedYII, ctx.getInstanceIdentifier());
70     }
71 }