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