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