Merge "Prevent ConfigPusher from killing its thread"
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / test / java / org / opendaylight / controller / sal / restconf / impl / test / RestGetOperationTest.java
1 /*
2  * Copyright (c) 2014 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.Matchers.any;
12 import static org.mockito.Mockito.mock;
13 import static org.mockito.Mockito.when;
14 import static org.opendaylight.controller.sal.restconf.impl.test.RestOperationUtils.XML;
15 import static org.opendaylight.controller.sal.restconf.impl.test.RestOperationUtils.createUri;
16
17 import java.io.FileNotFoundException;
18 import java.io.UnsupportedEncodingException;
19 import java.net.URI;
20 import java.net.URISyntaxException;
21 import java.util.ArrayList;
22 import java.util.List;
23 import java.util.logging.Level;
24
25 import javax.ws.rs.core.Application;
26 import javax.ws.rs.core.MediaType;
27 import javax.ws.rs.core.Response;
28
29
30 import org.glassfish.jersey.server.ResourceConfig;
31 import org.glassfish.jersey.test.JerseyTest;
32 import org.glassfish.jersey.test.TestProperties;
33 import org.junit.BeforeClass;
34 import org.junit.Test;
35 import org.opendaylight.controller.sal.core.api.mount.MountInstance;
36 import org.opendaylight.controller.sal.core.api.mount.MountService;
37 import org.opendaylight.controller.sal.rest.api.Draft02;
38 import org.opendaylight.controller.sal.rest.impl.JsonToCompositeNodeProvider;
39 import org.opendaylight.controller.sal.rest.impl.StructuredDataToJsonProvider;
40 import org.opendaylight.controller.sal.rest.impl.StructuredDataToXmlProvider;
41 import org.opendaylight.controller.sal.rest.impl.XmlToCompositeNodeProvider;
42 import org.opendaylight.controller.sal.restconf.impl.BrokerFacade;
43 import org.opendaylight.controller.sal.restconf.impl.CompositeNodeWrapper;
44 import org.opendaylight.controller.sal.restconf.impl.ControllerContext;
45 import org.opendaylight.controller.sal.restconf.impl.RestconfImpl;
46 import org.opendaylight.controller.sal.restconf.impl.SimpleNodeWrapper;
47 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
48 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
49 import org.opendaylight.yangtools.yang.data.api.Node;
50 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
51
52 public class RestGetOperationTest extends JerseyTest {
53
54     private static BrokerFacade brokerFacade;
55     private static RestconfImpl restconfImpl;
56     private static SchemaContext schemaContextYangsIetf;
57     private static SchemaContext schemaContextTestModule;
58     private static CompositeNode answerFromGet;
59
60     @BeforeClass
61     public static void init() throws FileNotFoundException {
62         schemaContextYangsIetf = TestUtils.loadSchemaContext("/full-versions/yangs");
63         schemaContextTestModule = TestUtils.loadSchemaContext("/full-versions/test-module");
64         ControllerContext controllerContext = ControllerContext.getInstance();
65         controllerContext.setSchemas(schemaContextYangsIetf);
66         brokerFacade = mock(BrokerFacade.class);
67         restconfImpl = RestconfImpl.getInstance();
68         restconfImpl.setBroker(brokerFacade);
69         restconfImpl.setControllerContext(controllerContext);
70         answerFromGet = prepareCompositeNodeWithIetfInterfacesInterfacesData();
71     }
72
73     @Override
74     protected Application configure() {
75         /* enable/disable Jersey logs to console */
76 //         enable(TestProperties.LOG_TRAFFIC);
77 //         enable(TestProperties.DUMP_ENTITY);
78 //         enable(TestProperties.RECORD_LOG_LEVEL);
79 //         set(TestProperties.RECORD_LOG_LEVEL, Level.ALL.intValue());
80         ResourceConfig resourceConfig = new ResourceConfig();
81         resourceConfig = resourceConfig.registerInstances(restconfImpl, StructuredDataToXmlProvider.INSTANCE,
82                 StructuredDataToJsonProvider.INSTANCE, XmlToCompositeNodeProvider.INSTANCE,
83                 JsonToCompositeNodeProvider.INSTANCE);
84         return resourceConfig;
85     }
86
87     /**
88      * Tests of status codes for "/datastore/{identifier}".
89      */
90     @Test
91     public void getDatastoreStatusCodes() throws FileNotFoundException, UnsupportedEncodingException {
92         mockReadOperationalDataMethod();
93         String uri = createUri("/datastore/", "ietf-interfaces:interfaces/interface/eth0");
94         assertEquals(200, get(uri, MediaType.APPLICATION_XML));
95
96         uri = createUri("/datastore/", "wrong-module:interfaces/interface/eth0");
97         assertEquals(400, get(uri, MediaType.APPLICATION_XML));
98
99         // Test of request for not existing data. Returning status code 404
100         uri = createUri("/datastore/", "ietf-interfaces:interfaces/interface/eth0");
101         when(brokerFacade.readOperationalData(any(InstanceIdentifier.class))).thenReturn(null);
102         assertEquals(404, get(uri, MediaType.APPLICATION_XML));
103     }
104
105     /**
106      * Tests of status codes for "/operational/{identifier}".
107      */
108     @Test
109     public void getOperationalStatusCodes() throws UnsupportedEncodingException {
110         mockReadOperationalDataMethod();
111         String uri = createUri("/operational/", "ietf-interfaces:interfaces/interface/eth0");
112         assertEquals(200, get(uri, MediaType.APPLICATION_XML));
113
114         uri = createUri("/operational/", "wrong-module:interfaces/interface/eth0");
115         assertEquals(400, get(uri, MediaType.APPLICATION_XML));
116     }
117
118     /**
119      * Tests of status codes for "/config/{identifier}".
120      */
121     @Test
122     public void getConfigStatusCodes() throws UnsupportedEncodingException {
123         mockReadConfigurationDataMethod();
124         String uri = createUri("/config/", "ietf-interfaces:interfaces/interface/eth0");
125         assertEquals(200, get(uri, MediaType.APPLICATION_XML));
126
127         uri = createUri("/config/", "wrong-module:interfaces/interface/eth0");
128         assertEquals(400, get(uri, MediaType.APPLICATION_XML));
129     }
130
131     /**
132      * MountPoint test. URI represents mount point.
133      */
134     @Test
135     public void getDataWithUrlMountPoint() throws UnsupportedEncodingException, URISyntaxException {
136         when(
137                 brokerFacade.readConfigurationDataBehindMountPoint(any(MountInstance.class),
138                         any(InstanceIdentifier.class))).thenReturn(prepareCnDataForMountPointTest());
139         MountInstance mountInstance = mock(MountInstance.class);
140         when(mountInstance.getSchemaContext()).thenReturn(schemaContextTestModule);
141         MountService mockMountService = mock(MountService.class);
142         when(mockMountService.getMountPoint(any(InstanceIdentifier.class))).thenReturn(mountInstance);
143
144         ControllerContext.getInstance().setMountService(mockMountService);
145         
146         String uri = createUri("/config/",
147                 "ietf-interfaces:interfaces/interface/0/yang-ext:mount/test-module:cont/cont1");
148         assertEquals(200, get(uri, MediaType.APPLICATION_XML));
149
150         uri = createUri("/config/", "ietf-interfaces:interfaces/yang-ext:mount/test-module:cont/cont1");
151         assertEquals(200, get(uri, MediaType.APPLICATION_XML));
152     }
153
154     @Test
155     public void getDataMountPointIntoHighestElement() throws UnsupportedEncodingException, URISyntaxException {
156         when(brokerFacade.readConfigurationDataBehindMountPoint(any(MountInstance.class),
157                         any(InstanceIdentifier.class))).thenReturn(prepareCnDataForMountPointTest());
158         MountInstance mountInstance = mock(MountInstance.class);
159         when(mountInstance.getSchemaContext()).thenReturn(schemaContextTestModule);
160         MountService mockMountService = mock(MountService.class);
161         when(mockMountService.getMountPoint(any(InstanceIdentifier.class))).thenReturn(mountInstance);
162
163         ControllerContext.getInstance().setMountService(mockMountService);
164
165         String uri = createUri("/config/",
166                 "ietf-interfaces:interfaces/interface/0/yang-ext:mount/");
167         assertEquals(200, get(uri, MediaType.APPLICATION_XML));
168     }
169
170     private int get(String uri, String mediaType) {
171         return target(uri).request(mediaType).get().getStatus();
172     }
173
174     private CompositeNode prepareCnDataForMountPointTest() throws URISyntaxException {
175         CompositeNodeWrapper cont1 = new CompositeNodeWrapper(new URI("test:module"), "cont1");
176         SimpleNodeWrapper lf11 = new SimpleNodeWrapper(new URI("test:module"), "lf11", "lf11 value");
177         cont1.addValue(lf11);
178         return cont1.unwrap();
179     }
180
181     private void mockReadOperationalDataMethod() {
182         when(brokerFacade.readOperationalData(any(InstanceIdentifier.class))).thenReturn(answerFromGet);
183     }
184
185     private void mockReadConfigurationDataMethod() {
186         when(brokerFacade.readConfigurationData(any(InstanceIdentifier.class))).thenReturn(answerFromGet);
187     }
188
189     private static CompositeNode prepareCompositeNodeWithIetfInterfacesInterfacesData() {
190         CompositeNode intface;
191         try {
192             intface = new CompositeNodeWrapper(new URI("interface"), "interface");
193             List<Node<?>> childs = new ArrayList<>();
194
195             childs.add(new SimpleNodeWrapper(new URI("name"), "name", "eth0"));
196             childs.add(new SimpleNodeWrapper(new URI("type"), "type", "ethernetCsmacd"));
197             childs.add(new SimpleNodeWrapper(new URI("enabled"), "enabled", Boolean.FALSE));
198             childs.add(new SimpleNodeWrapper(new URI("description"), "description", "some interface"));
199             intface.setValue(childs);
200             return intface;
201         } catch (URISyntaxException e) {
202         }
203
204         return null;
205     }
206
207 }