Bug 1073: Implemented Transaction chain on InMemoryDOMDataStore level.
[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
154     @Test
155     public void postDataViaUrlMountPoint() throws UnsupportedEncodingException {
156         controllerContext.setSchemas(schemaContextYangsIetf);
157         RpcResult<TransactionStatus> rpcResult = new DummyRpcResult.Builder<TransactionStatus>().result(
158                 TransactionStatus.COMMITED).build();
159         Future<RpcResult<TransactionStatus>> dummyFuture = DummyFuture.builder().rpcResult(rpcResult).build();
160         when(
161                 brokerFacade.commitConfigurationDataPostBehindMountPoint(any(MountInstance.class),
162                         any(InstanceIdentifier.class), any(CompositeNode.class))).thenReturn(dummyFuture);
163
164         MountInstance mountInstance = mock(MountInstance.class);
165         when(mountInstance.getSchemaContext()).thenReturn(schemaContextTestModule);
166         MountService mockMountService = mock(MountService.class);
167         when(mockMountService.getMountPoint(any(InstanceIdentifier.class))).thenReturn(mountInstance);
168
169         ControllerContext.getInstance().setMountService(mockMountService);
170
171         String uri = "/config/ietf-interfaces:interfaces/interface/0/";
172         assertEquals(204, post(uri, Draft02.MediaTypes.DATA + XML, xmlData4));
173         uri = "/config/ietf-interfaces:interfaces/interface/0/yang-ext:mount/test-module:cont";
174         assertEquals(204, post(uri, Draft02.MediaTypes.DATA + XML, xmlData3));
175     }
176
177     private void mockInvokeRpc(CompositeNode result, boolean sucessful) {
178         RpcResult<CompositeNode> rpcResult = new DummyRpcResult.Builder<CompositeNode>().result(result)
179                 .isSuccessful(sucessful).build();
180         when(brokerFacade.invokeRpc(any(QName.class), any(CompositeNode.class)))
181             .thenReturn(Futures.<RpcResult<CompositeNode>>immediateFuture( rpcResult ));
182     }
183
184     private void mockCommitConfigurationDataPostMethod(TransactionStatus statusName) {
185         RpcResult<TransactionStatus> rpcResult = new DummyRpcResult.Builder<TransactionStatus>().result(statusName)
186                 .build();
187         Future<RpcResult<TransactionStatus>> dummyFuture = null;
188         if (statusName != null) {
189             dummyFuture = DummyFuture.builder().rpcResult(rpcResult).build();
190         } else {
191             dummyFuture = DummyFuture.builder().build();
192         }
193
194         when(brokerFacade.commitConfigurationDataPost(any(InstanceIdentifier.class), any(CompositeNode.class)))
195                 .thenReturn(dummyFuture);
196     }
197
198     @Test
199     public void createConfigurationDataTest() throws UnsupportedEncodingException, ParseException {
200         initMocking();
201         RpcResult<TransactionStatus> rpcResult = new DummyRpcResult.Builder<TransactionStatus>().result(
202                 TransactionStatus.COMMITED).build();
203         Future<RpcResult<TransactionStatus>> dummyFuture = DummyFuture.builder().rpcResult(rpcResult).build();
204
205         when(brokerFacade.commitConfigurationDataPost(any(InstanceIdentifier.class), any(CompositeNode.class)))
206                 .thenReturn(dummyFuture);
207
208         ArgumentCaptor<InstanceIdentifier> instanceIdCaptor = ArgumentCaptor.forClass(InstanceIdentifier.class);
209         ArgumentCaptor<CompositeNode> compNodeCaptor = ArgumentCaptor.forClass(CompositeNode.class);
210
211         String URI_1 = "/config";
212         assertEquals(204, post(URI_1, Draft02.MediaTypes.DATA + XML, xmlTestInterface));
213         verify(brokerFacade).commitConfigurationDataPost(instanceIdCaptor.capture(), compNodeCaptor.capture());
214         String identifier = "[(urn:ietf:params:xml:ns:yang:test-interface?revision=2014-07-01)interfaces]";
215         assertEquals(identifier, instanceIdCaptor.getValue().getPath().toString());
216
217         String URI_2 = "/config/test-interface:interfaces";
218         assertEquals(204, post(URI_2, Draft02.MediaTypes.DATA + XML, xmlBlockData));
219         verify(brokerFacade, times(2))
220                 .commitConfigurationDataPost(instanceIdCaptor.capture(), compNodeCaptor.capture());
221         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]";
222         assertEquals(identifier, instanceIdCaptor.getValue().getPath().toString());
223     }
224
225     @Test
226     public void createConfigurationDataNullTest() throws UnsupportedEncodingException {
227         initMocking();
228
229         when(brokerFacade.commitConfigurationDataPost(any(InstanceIdentifier.class), any(CompositeNode.class)))
230                 .thenReturn(null);
231
232         String URI_1 = "/config";
233         assertEquals(202, post(URI_1, Draft02.MediaTypes.DATA + XML, xmlTestInterface));
234
235         String URI_2 = "/config/test-interface:interfaces";
236         assertEquals(202, post(URI_2, Draft02.MediaTypes.DATA + XML, xmlBlockData));
237     }
238
239     private static void initMocking() {
240         controllerContext = ControllerContext.getInstance();
241         controllerContext.setSchemas(schemaContext);
242         mountService = mock(MountService.class);
243         controllerContext.setMountService(mountService);
244         brokerFacade = mock(BrokerFacade.class);
245         restconfImpl = RestconfImpl.getInstance();
246         restconfImpl.setBroker(brokerFacade);
247         restconfImpl.setControllerContext(controllerContext);
248     }
249
250     private int post(String uri, String mediaType, String data) {
251         return target(uri).request(mediaType).post(Entity.entity(data, mediaType)).getStatus();
252     }
253
254     private static void loadData() throws IOException, URISyntaxException {
255         InputStream xmlStream = RestconfImplTest.class
256                 .getResourceAsStream("/parts/ietf-interfaces_interfaces_absolute_path.xml");
257         xmlDataAbsolutePath = TestUtils.getDocumentInPrintableForm(TestUtils.loadDocumentFrom(xmlStream));
258         xmlStream = RestconfImplTest.class
259                 .getResourceAsStream("/parts/ietf-interfaces_interfaces_interface_absolute_path.xml");
260         xmlDataInterfaceAbsolutePath = TestUtils.getDocumentInPrintableForm(TestUtils.loadDocumentFrom(xmlStream));
261         String xmlPathRpcInput = RestconfImplTest.class.getResource("/full-versions/test-data2/data-rpc-input.xml")
262                 .getPath();
263         xmlDataRpcInput = TestUtils.loadTextFile(xmlPathRpcInput);
264         String xmlPathBlockData = RestconfImplTest.class.getResource("/test-config-data/xml/block-data.xml").getPath();
265         xmlBlockData = TestUtils.loadTextFile(xmlPathBlockData);
266         String xmlPathTestInterface = RestconfImplTest.class.getResource("/test-config-data/xml/test-interface.xml")
267                 .getPath();
268         xmlTestInterface = TestUtils.loadTextFile(xmlPathTestInterface);
269         cnSnDataOutput = prepareCnSnRpcOutput();
270         String data3Input = RestconfImplTest.class.getResource("/full-versions/test-data2/data3.xml").getPath();
271         xmlData3 = TestUtils.loadTextFile(data3Input);
272         String data4Input = RestconfImplTest.class.getResource("/full-versions/test-data2/data7.xml").getPath();
273         xmlData4 = TestUtils.loadTextFile(data4Input);
274     }
275
276     private static CompositeNodeWrapper prepareCnSnRpcOutput() throws URISyntaxException {
277         CompositeNodeWrapper cnSnDataOutput = new CompositeNodeWrapper(new URI("test:module"), "output");
278         CompositeNodeWrapper cont = new CompositeNodeWrapper(new URI("test:module"), "cont-output");
279         cnSnDataOutput.addValue(cont);
280         cnSnDataOutput.unwrap();
281         return cnSnDataOutput;
282     }
283 }