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