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