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 / RestPostOperationTest.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.times;
14 import static org.mockito.Mockito.verify;
15 import static org.mockito.Mockito.when;
16 import static org.opendaylight.controller.sal.restconf.impl.test.RestOperationUtils.XML;
17 import static org.opendaylight.controller.sal.restconf.impl.test.RestOperationUtils.createUri;
18
19 import java.io.IOException;
20 import java.io.InputStream;
21 import java.io.UnsupportedEncodingException;
22 import java.net.URI;
23 import java.net.URISyntaxException;
24 import java.net.URLEncoder;
25 import java.text.ParseException;
26 import java.util.Set;
27 import java.util.concurrent.Future;
28
29 import javax.ws.rs.client.Entity;
30 import javax.ws.rs.core.Application;
31 import javax.ws.rs.core.MediaType;
32 import javax.ws.rs.core.Response;
33
34 import org.glassfish.jersey.server.ResourceConfig;
35 import org.glassfish.jersey.test.JerseyTest;
36 import org.junit.BeforeClass;
37 import org.junit.Test;
38 import org.mockito.ArgumentCaptor;
39 import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
40 import org.opendaylight.controller.sal.core.api.mount.MountInstance;
41 import org.opendaylight.controller.sal.core.api.mount.MountService;
42 import org.opendaylight.controller.sal.rest.api.Draft02;
43 import org.opendaylight.controller.sal.rest.impl.JsonToCompositeNodeProvider;
44 import org.opendaylight.controller.sal.rest.impl.StructuredDataToJsonProvider;
45 import org.opendaylight.controller.sal.rest.impl.StructuredDataToXmlProvider;
46 import org.opendaylight.controller.sal.rest.impl.XmlMapper;
47 import org.opendaylight.controller.sal.rest.impl.XmlToCompositeNodeProvider;
48 import org.opendaylight.controller.sal.restconf.impl.BrokerFacade;
49 import org.opendaylight.controller.sal.restconf.impl.CompositeNodeWrapper;
50 import org.opendaylight.controller.sal.restconf.impl.ControllerContext;
51 import org.opendaylight.controller.sal.restconf.impl.RestconfImpl;
52 import org.opendaylight.yangtools.yang.common.QName;
53 import org.opendaylight.yangtools.yang.common.RpcResult;
54 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
55 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
56 import org.opendaylight.yangtools.yang.model.api.Module;
57 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
58
59 import com.google.common.base.Charsets;
60
61 public class RestPostOperationTest extends JerseyTest {
62
63     private static String xmlDataAbsolutePath;
64     private static String xmlDataInterfaceAbsolutePath;
65     private static String xmlDataRpcInput;
66     private static String xmlBlockData;
67     private static String xmlTestInterface;
68     private static CompositeNodeWrapper cnSnDataOutput;
69     private static String xmlData3;
70     private static String xmlData4;
71
72     private static ControllerContext controllerContext;
73     private static BrokerFacade brokerFacade;
74     private static RestconfImpl restconfImpl;
75     private static SchemaContext schemaContextYangsIetf;
76     private static SchemaContext schemaContextTestModule;
77     private static SchemaContext schemaContext;
78
79     private static MountService mountService;
80
81     @BeforeClass
82     public static void init() throws URISyntaxException, IOException {
83         schemaContextYangsIetf = TestUtils.loadSchemaContext("/full-versions/yangs");
84         schemaContextTestModule = TestUtils.loadSchemaContext("/full-versions/test-module");
85         controllerContext = ControllerContext.getInstance();
86         brokerFacade = mock(BrokerFacade.class);
87         restconfImpl = RestconfImpl.getInstance();
88         restconfImpl.setBroker(brokerFacade);
89         restconfImpl.setControllerContext(controllerContext);
90
91         Set<Module> modules = TestUtils.loadModulesFrom("/test-config-data/yang1");
92         schemaContext = TestUtils.loadSchemaContext(modules);
93
94         loadData();
95     }
96
97     @Override
98     protected Application configure() {
99         /* enable/disable Jersey logs to console */
100         // enable(TestProperties.LOG_TRAFFIC);
101         // enable(TestProperties.DUMP_ENTITY);
102         // enable(TestProperties.RECORD_LOG_LEVEL);
103         // set(TestProperties.RECORD_LOG_LEVEL, Level.ALL.intValue());
104         ResourceConfig resourceConfig = new ResourceConfig();
105         resourceConfig = resourceConfig.registerInstances(restconfImpl, StructuredDataToXmlProvider.INSTANCE,
106                 StructuredDataToJsonProvider.INSTANCE, XmlToCompositeNodeProvider.INSTANCE,
107                 JsonToCompositeNodeProvider.INSTANCE);
108         return resourceConfig;
109     }
110
111     @Test
112     public void postOperationsStatusCodes() throws UnsupportedEncodingException {
113         controllerContext.setSchemas(schemaContextTestModule);
114         mockInvokeRpc(cnSnDataOutput, true);
115         String uri = createUri("/operations/", "test-module:rpc-test");
116         assertEquals(200, post(uri, MediaType.APPLICATION_XML, xmlDataRpcInput));
117
118         mockInvokeRpc(null, true);
119         assertEquals(204, post(uri, MediaType.APPLICATION_XML, xmlDataRpcInput));
120
121         mockInvokeRpc(null, false);
122         assertEquals(500, post(uri, MediaType.APPLICATION_XML, xmlDataRpcInput));
123
124         uri = createUri("/operations/", "test-module:rpc-wrongtest");
125         assertEquals(404, post(uri, MediaType.APPLICATION_XML, xmlDataRpcInput));
126     }
127
128     @Test
129     public void postConfigOnlyStatusCodes() throws UnsupportedEncodingException {
130         controllerContext.setSchemas(schemaContextYangsIetf);
131         mockCommitConfigurationDataPostMethod(TransactionStatus.COMMITED);
132         String uri = createUri("/config", "");
133         assertEquals(204, post(uri, MediaType.APPLICATION_XML, xmlDataAbsolutePath));
134
135         mockCommitConfigurationDataPostMethod(null);
136         assertEquals(202, post(uri, MediaType.APPLICATION_XML, xmlDataAbsolutePath));
137
138         mockCommitConfigurationDataPostMethod(TransactionStatus.FAILED);
139         assertEquals(500, post(uri, MediaType.APPLICATION_XML, xmlDataAbsolutePath));
140     }
141
142     @Test
143     public void postConfigStatusCodes() throws UnsupportedEncodingException {
144         controllerContext.setSchemas(schemaContextYangsIetf);
145         mockCommitConfigurationDataPostMethod(TransactionStatus.COMMITED);
146         String uri = createUri("/config/", "ietf-interfaces:interfaces");
147         assertEquals(204, post(uri, MediaType.APPLICATION_XML, xmlDataInterfaceAbsolutePath));
148
149         mockCommitConfigurationDataPostMethod(null);
150         assertEquals(202, post(uri, MediaType.APPLICATION_XML, xmlDataInterfaceAbsolutePath));
151
152         mockCommitConfigurationDataPostMethod(TransactionStatus.FAILED);
153         assertEquals(500, post(uri, MediaType.APPLICATION_XML, xmlDataInterfaceAbsolutePath));
154     }
155
156     @Test
157     public void postDatastoreStatusCodes() throws UnsupportedEncodingException {
158         controllerContext.setSchemas(schemaContextYangsIetf);
159         mockCommitConfigurationDataPostMethod(TransactionStatus.COMMITED);
160         String uri = createUri("/datastore/", "ietf-interfaces:interfaces");
161         assertEquals(204, post(uri, MediaType.APPLICATION_XML, xmlDataInterfaceAbsolutePath));
162
163         mockCommitConfigurationDataPostMethod(null);
164         assertEquals(202, post(uri, MediaType.APPLICATION_XML, xmlDataInterfaceAbsolutePath));
165
166         mockCommitConfigurationDataPostMethod(TransactionStatus.FAILED);
167         assertEquals(500, post(uri, MediaType.APPLICATION_XML, xmlDataInterfaceAbsolutePath));
168     }
169
170     @Test
171     public void postDataViaUrlMountPoint() throws UnsupportedEncodingException {
172         controllerContext.setSchemas(schemaContextYangsIetf);
173         RpcResult<TransactionStatus> rpcResult = new DummyRpcResult.Builder<TransactionStatus>().result(
174                 TransactionStatus.COMMITED).build();
175         Future<RpcResult<TransactionStatus>> dummyFuture = DummyFuture.builder().rpcResult(rpcResult).build();
176         when(
177                 brokerFacade.commitConfigurationDataPostBehindMountPoint(any(MountInstance.class),
178                         any(InstanceIdentifier.class), any(CompositeNode.class))).thenReturn(dummyFuture);
179
180         MountInstance mountInstance = mock(MountInstance.class);
181         when(mountInstance.getSchemaContext()).thenReturn(schemaContextTestModule);
182         MountService mockMountService = mock(MountService.class);
183         when(mockMountService.getMountPoint(any(InstanceIdentifier.class))).thenReturn(mountInstance);
184
185         ControllerContext.getInstance().setMountService(mockMountService);
186
187         String uri = createUri("/config/", "ietf-interfaces:interfaces/interface/0/");
188         assertEquals(204, post(uri, Draft02.MediaTypes.DATA + XML, xmlData4));
189         uri = createUri("/config/", "ietf-interfaces:interfaces/interface/0/yang-ext:mount/test-module:cont");
190         assertEquals(204, post(uri, Draft02.MediaTypes.DATA + XML, xmlData3));
191     }
192
193     private void mockInvokeRpc(CompositeNode result, boolean sucessful) {
194         RpcResult<CompositeNode> rpcResult = new DummyRpcResult.Builder<CompositeNode>().result(result)
195                 .isSuccessful(sucessful).build();
196         when(brokerFacade.invokeRpc(any(QName.class), any(CompositeNode.class))).thenReturn(rpcResult);
197     }
198
199     private void mockCommitConfigurationDataPostMethod(TransactionStatus statusName) {
200         RpcResult<TransactionStatus> rpcResult = new DummyRpcResult.Builder<TransactionStatus>().result(statusName)
201                 .build();
202         Future<RpcResult<TransactionStatus>> dummyFuture = null;
203         if (statusName != null) {
204             dummyFuture = DummyFuture.builder().rpcResult(rpcResult).build();
205         } else {
206             dummyFuture = DummyFuture.builder().build();
207         }
208
209         when(brokerFacade.commitConfigurationDataPost(any(InstanceIdentifier.class), any(CompositeNode.class)))
210                 .thenReturn(dummyFuture);
211     }
212
213     @Test
214     public void createConfigurationDataTest() throws UnsupportedEncodingException, ParseException {
215         initMocking();
216         RpcResult<TransactionStatus> rpcResult = new DummyRpcResult.Builder<TransactionStatus>().result(
217                 TransactionStatus.COMMITED).build();
218         Future<RpcResult<TransactionStatus>> dummyFuture = DummyFuture.builder().rpcResult(rpcResult).build();
219
220         when(brokerFacade.commitConfigurationDataPost(any(InstanceIdentifier.class), any(CompositeNode.class)))
221                 .thenReturn(dummyFuture);
222
223         ArgumentCaptor<InstanceIdentifier> instanceIdCaptor = ArgumentCaptor.forClass(InstanceIdentifier.class);
224         ArgumentCaptor<CompositeNode> compNodeCaptor = ArgumentCaptor.forClass(CompositeNode.class);
225
226         String URI_1 = createUri("/config", "");
227         assertEquals(204, post(URI_1, Draft02.MediaTypes.DATA + XML, xmlTestInterface));
228         verify(brokerFacade).commitConfigurationDataPost(instanceIdCaptor.capture(), compNodeCaptor.capture());
229         String identifier = "[(urn:ietf:params:xml:ns:yang:test-interface?revision=2014-07-01)interfaces]";
230         assertEquals(identifier, instanceIdCaptor.getValue().getPath().toString());
231
232         String URI_2 = createUri("/config/", "test-interface:interfaces");
233         assertEquals(204, post(URI_2, Draft02.MediaTypes.DATA + XML, xmlBlockData));
234         verify(brokerFacade, times(2))
235                 .commitConfigurationDataPost(instanceIdCaptor.capture(), compNodeCaptor.capture());
236         identifier = "[(urn:ietf:params:xml:ns:yang:test-interface?revision=2014-07-01)interfaces, (urn:ietf:params:xml:ns:yang:test-interface?revision=2014-07-01)block]";
237         assertEquals(identifier, instanceIdCaptor.getValue().getPath().toString());
238     }
239
240     @Test
241     public void createConfigurationDataNullTest() throws UnsupportedEncodingException {
242         initMocking();
243
244         when(brokerFacade.commitConfigurationDataPost(any(InstanceIdentifier.class), any(CompositeNode.class)))
245                 .thenReturn(null);
246
247         String URI_1 = createUri("/config", "");
248         assertEquals(202, post(URI_1, Draft02.MediaTypes.DATA + XML, xmlTestInterface));
249
250         String URI_2 = createUri("/config/", "test-interface:interfaces");
251         assertEquals(202, post(URI_2, Draft02.MediaTypes.DATA + XML, xmlBlockData));
252     }
253
254     private String createUri(String prefix, String encodedPart) throws UnsupportedEncodingException {
255         return URI.create(prefix + URLEncoder.encode(encodedPart, Charsets.US_ASCII.name()).toString()).toASCIIString();
256     }
257
258     private static void initMocking() {
259         controllerContext = ControllerContext.getInstance();
260         controllerContext.setSchemas(schemaContext);
261         mountService = mock(MountService.class);
262         controllerContext.setMountService(mountService);
263         brokerFacade = mock(BrokerFacade.class);
264         restconfImpl = RestconfImpl.getInstance();
265         restconfImpl.setBroker(brokerFacade);
266         restconfImpl.setControllerContext(controllerContext);
267     }
268
269     private int post(String uri, String mediaType, String data) {
270         return target(uri).request(mediaType).post(Entity.entity(data, mediaType)).getStatus();
271     }
272
273     private static void loadData() throws IOException, URISyntaxException {
274         InputStream xmlStream = RestconfImplTest.class
275                 .getResourceAsStream("/parts/ietf-interfaces_interfaces_absolute_path.xml");
276         xmlDataAbsolutePath = TestUtils.getDocumentInPrintableForm(TestUtils.loadDocumentFrom(xmlStream));
277         xmlStream = RestconfImplTest.class
278                 .getResourceAsStream("/parts/ietf-interfaces_interfaces_interface_absolute_path.xml");
279         xmlDataInterfaceAbsolutePath = TestUtils.getDocumentInPrintableForm(TestUtils.loadDocumentFrom(xmlStream));
280         String xmlPathRpcInput = RestconfImplTest.class.getResource("/full-versions/test-data2/data-rpc-input.xml")
281                 .getPath();
282         xmlDataRpcInput = TestUtils.loadTextFile(xmlPathRpcInput);
283         String xmlPathBlockData = RestconfImplTest.class.getResource("/test-config-data/xml/block-data.xml").getPath();
284         xmlBlockData = TestUtils.loadTextFile(xmlPathBlockData);
285         String xmlPathTestInterface = RestconfImplTest.class.getResource("/test-config-data/xml/test-interface.xml")
286                 .getPath();
287         xmlTestInterface = TestUtils.loadTextFile(xmlPathTestInterface);
288         cnSnDataOutput = prepareCnSnRpcOutput();
289         String data3Input = RestconfImplTest.class.getResource("/full-versions/test-data2/data3.xml").getPath();
290         xmlData3 = TestUtils.loadTextFile(data3Input);
291         String data4Input = RestconfImplTest.class.getResource("/full-versions/test-data2/data7.xml").getPath();
292         xmlData4 = TestUtils.loadTextFile(data4Input);
293     }
294
295     private static CompositeNodeWrapper prepareCnSnRpcOutput() throws URISyntaxException {
296         CompositeNodeWrapper cnSnDataOutput = new CompositeNodeWrapper(new URI("test:module"), "output");
297         CompositeNodeWrapper cont = new CompositeNodeWrapper(new URI("test:module"), "cont-output");
298         cnSnDataOutput.addValue(cont);
299         cnSnDataOutput.unwrap();
300         return cnSnDataOutput;
301     }
302 }