8e512528a9ad7fd7f63285b3dbf3e8fe0584b374
[netconf.git] / restconf / sal-rest-connector / src / test / java / org / opendaylight / controller / sal / restconf / impl / test / Netconf505Test.java
1 /*
2  * Copyright (c) 2018 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
9 package org.opendaylight.controller.sal.restconf.impl.test;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertNull;
14 import static org.mockito.Matchers.any;
15 import static org.mockito.Mockito.mock;
16 import static org.mockito.Mockito.when;
17
18 import com.google.common.base.Optional;
19 import java.io.FileNotFoundException;
20 import java.util.HashMap;
21 import java.util.Map;
22 import java.util.Set;
23 import org.junit.BeforeClass;
24 import org.junit.Test;
25 import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
26 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
27 import org.opendaylight.netconf.sal.restconf.impl.BrokerFacade;
28 import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
29 import org.opendaylight.netconf.sal.restconf.impl.InstanceIdentifierContext;
30 import org.opendaylight.netconf.sal.restconf.impl.RestconfImpl;
31 import org.opendaylight.yangtools.yang.common.QName;
32 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
33 import org.opendaylight.yangtools.yang.model.api.Module;
34 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
35 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
36
37 /**
38  * Checks if restconf is able to deserialize external leafref.
39  *
40  * @see <a href="https://jira.opendaylight.org/browse/NETCONF-505">NETCONF-505</a>
41  */
42 public class Netconf505Test {
43     private static final String EXTERNAL_MODULE_NAME = "leafref-module";
44     private static final QName CONT_QNAME = QName.create("leafref:module", "2014-04-17", "cont");
45     private static final QName LIST_QNAME = QName.create("leafref:module", "2014-04-17", "lst-with-lfref-key");
46     private static final QName KEY_QNAME = QName.create("leafref:module", "2014-04-17", "lfref-key");
47
48     private static final ControllerContext CONTROLLER_CONTEXT = ControllerContext.getInstance();
49
50     @BeforeClass
51     public static void init() throws FileNotFoundException, ReactorException {
52         final SchemaContext globalContext = TestUtils.loadSchemaContext("/full-versions/yangs");
53         assertNull(globalContext.findModuleByName(EXTERNAL_MODULE_NAME, null));
54         final Set<Module> allModules = globalContext.getModules();
55         assertNotNull(allModules);
56         CONTROLLER_CONTEXT.setSchemas(globalContext);
57     }
58
59     @Test
60     public void testLeafRefFromExternalModule() throws FileNotFoundException, ReactorException {
61         initMountService();
62         final InstanceIdentifierContext<?> ctx = CONTROLLER_CONTEXT.toInstanceIdentifier(
63             "simple-nodes:users/yang-ext:mount/leafref-module:cont/lst-with-lfref-key/id-string");
64
65         final Map<QName, Object> keyValues = new HashMap<>();
66         keyValues.put(KEY_QNAME, "id-string");
67         final YangInstanceIdentifier expectedYII = YangInstanceIdentifier.of(CONT_QNAME).node(LIST_QNAME)
68             .node(new YangInstanceIdentifier.NodeIdentifierWithPredicates(LIST_QNAME, keyValues));
69
70         assertEquals(expectedYII, ctx.getInstanceIdentifier());
71     }
72
73     private void initMountService() throws FileNotFoundException, ReactorException {
74         final DOMMountPointService mountService = mock(DOMMountPointService.class);
75         CONTROLLER_CONTEXT.setMountService(mountService);
76         final BrokerFacade brokerFacade = mock(BrokerFacade.class);
77         final RestconfImpl restconfImpl = RestconfImpl.getInstance();
78         restconfImpl.setBroker(brokerFacade);
79         restconfImpl.setControllerContext(CONTROLLER_CONTEXT);
80         final SchemaContext mountPointContext = TestUtils.loadSchemaContext("/leafref/yang");
81         final DOMMountPoint mountInstance = mock(DOMMountPoint.class);
82         when(mountInstance.getSchemaContext()).thenReturn(mountPointContext);
83         when(mountService.getMountPoint(any(YangInstanceIdentifier.class))).thenReturn(Optional.of(mountInstance));
84     }
85 }