94663085bd81a1b747800565af34559412c17e16
[netconf.git] / restconf / sal-rest-connector / 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.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertNull;
13 import static org.mockito.Matchers.any;
14 import static org.mockito.Mockito.mock;
15 import static org.mockito.Mockito.when;
16
17 import com.google.common.base.Optional;
18 import java.io.FileNotFoundException;
19 import java.util.HashMap;
20 import java.util.Map;
21 import java.util.Set;
22 import org.junit.BeforeClass;
23 import org.junit.Test;
24 import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
25 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
26 import org.opendaylight.netconf.sal.restconf.impl.BrokerFacade;
27 import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
28 import org.opendaylight.netconf.sal.restconf.impl.InstanceIdentifierContext;
29 import org.opendaylight.netconf.sal.restconf.impl.RestconfImpl;
30 import org.opendaylight.yangtools.yang.common.QName;
31 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
32 import org.opendaylight.yangtools.yang.model.api.Module;
33 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
34 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
35
36 public class Bug8072Test {
37     private static final String EXTERNAL_MODULE_NAME = "test-module";
38     private static final QName MODULES_QNAME = QName.create("test:module", "2014-01-09", "modules");
39     private static final QName MODULE_QNAME = QName.create("test:module", "2014-01-09", "module");
40     private static final QName NAME_QNAME = QName.create("test:module", "2014-01-09", "name");
41     private static final QName TYPE_QNAME = QName.create("test:module", "2014-01-09", "type");
42     private static final QName MODULE_TYPE_QNAME = QName.create("test:module", "2014-01-09", "module-type");
43
44     private static final ControllerContext CONTROLLER_CONTEXT = ControllerContext.getInstance();
45
46     @BeforeClass
47     public static void init() throws FileNotFoundException, ReactorException {
48         final SchemaContext globalContext = TestUtils.loadSchemaContext("/full-versions/yangs");
49         assertNull(globalContext.findModuleByName(EXTERNAL_MODULE_NAME, null));
50         final Set<Module> allModules = globalContext.getModules();
51         assertNotNull(allModules);
52         CONTROLLER_CONTEXT.setSchemas(globalContext);
53     }
54
55     @Test
56     public void testIdentityRefFromExternalModule() throws FileNotFoundException, ReactorException {
57         initMountService();
58         final InstanceIdentifierContext<?> ctx = CONTROLLER_CONTEXT.toInstanceIdentifier(
59                 "simple-nodes:users/yang-ext:mount/test-module:modules/module/test-module:module-type/name");
60
61         final Map<QName, Object> keyValues = new HashMap<>();
62         keyValues.put(NAME_QNAME, "name");
63         keyValues.put(TYPE_QNAME, MODULE_TYPE_QNAME);
64         final YangInstanceIdentifier expectedYII = YangInstanceIdentifier.of(MODULES_QNAME).node(MODULE_QNAME)
65             .node(new YangInstanceIdentifier.NodeIdentifierWithPredicates(MODULE_QNAME, keyValues));
66
67         assertEquals(expectedYII, ctx.getInstanceIdentifier());
68     }
69
70     private void initMountService() throws FileNotFoundException, ReactorException {
71         final DOMMountPointService mountService = mock(DOMMountPointService.class);
72         CONTROLLER_CONTEXT.setMountService(mountService);
73         final BrokerFacade brokerFacade = mock(BrokerFacade.class);
74         final RestconfImpl restconfImpl = RestconfImpl.getInstance();
75         restconfImpl.setBroker(brokerFacade);
76         restconfImpl.setControllerContext(CONTROLLER_CONTEXT);
77         final SchemaContext mountPointContext = TestUtils.loadSchemaContext("/full-versions/test-module");
78         final DOMMountPoint mountInstance = mock(DOMMountPoint.class);
79         when(mountInstance.getSchemaContext()).thenReturn(mountPointContext);
80         when(mountService.getMountPoint(any(YangInstanceIdentifier.class))).thenReturn(Optional.of(mountInstance));
81     }
82 }