Merge "Changed NetconfDeviceDatastoreAdapter and NetconfDeviceTopologyAdapter to...
[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.doReturn;
13 import static org.mockito.Mockito.doThrow;
14 import static org.mockito.Mockito.mock;
15 import static org.mockito.Mockito.times;
16 import static org.mockito.Mockito.verify;
17 import static org.mockito.Mockito.when;
18 import static org.opendaylight.controller.sal.restconf.impl.test.RestOperationUtils.XML;
19 import com.google.common.base.Optional;
20 import com.google.common.collect.ImmutableList;
21 import com.google.common.util.concurrent.CheckedFuture;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.io.UnsupportedEncodingException;
25 import java.net.URI;
26 import java.net.URISyntaxException;
27 import java.text.ParseException;
28 import java.util.ArrayList;
29 import java.util.Collection;
30 import java.util.Collections;
31 import java.util.List;
32 import java.util.Set;
33 import javax.ws.rs.client.Entity;
34 import javax.ws.rs.core.Application;
35 import javax.ws.rs.core.MediaType;
36 import org.glassfish.jersey.server.ResourceConfig;
37 import org.glassfish.jersey.test.JerseyTest;
38 import org.junit.BeforeClass;
39 import org.junit.Ignore;
40 import org.junit.Test;
41 import org.mockito.ArgumentCaptor;
42 import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
43 import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
44 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
45 import org.opendaylight.controller.sal.rest.api.Draft02;
46 import org.opendaylight.controller.sal.rest.impl.JsonNormalizedNodeBodyReader;
47 import org.opendaylight.controller.sal.rest.impl.NormalizedNodeJsonBodyWriter;
48 import org.opendaylight.controller.sal.rest.impl.NormalizedNodeXmlBodyWriter;
49 import org.opendaylight.controller.sal.rest.impl.RestconfDocumentedExceptionMapper;
50 import org.opendaylight.controller.sal.rest.impl.XmlNormalizedNodeBodyReader;
51 import org.opendaylight.controller.sal.restconf.impl.BrokerFacade;
52 import org.opendaylight.controller.sal.restconf.impl.CompositeNodeWrapper;
53 import org.opendaylight.controller.sal.restconf.impl.ControllerContext;
54 import org.opendaylight.controller.sal.restconf.impl.RestconfDocumentedException;
55 import org.opendaylight.controller.sal.restconf.impl.RestconfImpl;
56 import org.opendaylight.yangtools.yang.common.RpcError;
57 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
58 import org.opendaylight.yangtools.yang.common.RpcResult;
59 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
60 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
61 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
62 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
63 import org.opendaylight.yangtools.yang.model.api.Module;
64 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
65
66 public class RestPostOperationTest extends JerseyTest {
67
68     private static String xmlDataAbsolutePath;
69     private static String xmlDataInterfaceAbsolutePath;
70     private static String xmlDataRpcInput;
71     private static String xmlBlockData;
72     private static String xmlTestInterface;
73     private static CompositeNodeWrapper cnSnDataOutput;
74     private static String xmlData3;
75     private static String xmlData4;
76
77     private static BrokerFacade brokerFacade;
78     private static RestconfImpl restconfImpl;
79     private static SchemaContext schemaContextYangsIetf;
80     private static SchemaContext schemaContextTestModule;
81     private static SchemaContext schemaContext;
82
83     private static DOMMountPointService mountService;
84
85     @BeforeClass
86     public static void init() throws URISyntaxException, IOException {
87         schemaContextYangsIetf = TestUtils.loadSchemaContext("/full-versions/yangs");
88         schemaContextTestModule = TestUtils.loadSchemaContext("/full-versions/test-module");
89         brokerFacade = mock(BrokerFacade.class);
90         restconfImpl = RestconfImpl.getInstance();
91         restconfImpl.setBroker(brokerFacade);
92
93         final Set<Module> modules = TestUtils.loadModulesFrom("/test-config-data/yang1");
94         schemaContext = TestUtils.loadSchemaContext(modules);
95
96         loadData();
97     }
98
99     @Override
100     protected Application configure() {
101         /* enable/disable Jersey logs to console */
102         // enable(TestProperties.LOG_TRAFFIC);
103         // enable(TestProperties.DUMP_ENTITY);
104         // enable(TestProperties.RECORD_LOG_LEVEL);
105         // set(TestProperties.RECORD_LOG_LEVEL, Level.ALL.intValue());
106         ResourceConfig resourceConfig = new ResourceConfig();
107         resourceConfig = resourceConfig.registerInstances(restconfImpl, new XmlNormalizedNodeBodyReader(),
108                 new NormalizedNodeXmlBodyWriter(), new JsonNormalizedNodeBodyReader(), new NormalizedNodeJsonBodyWriter());
109         resourceConfig.registerClasses(RestconfDocumentedExceptionMapper.class);
110         return resourceConfig;
111     }
112
113     private void setSchemaControllerContext(final SchemaContext schema) {
114         final ControllerContext context = ControllerContext.getInstance();
115         context.setSchemas(schema);
116         restconfImpl.setControllerContext(context);
117     }
118
119     @Test
120     @Ignore //FIXME we don't wish to mock CompositeNode as result
121     public void postOperationsStatusCodes() throws IOException {
122         setSchemaControllerContext(schemaContextTestModule);
123         mockInvokeRpc(cnSnDataOutput, true);
124         String uri = "/operations/test-module:rpc-test";
125         assertEquals(200, post(uri, MediaType.APPLICATION_XML, xmlDataRpcInput));
126
127         mockInvokeRpc(null, true);
128         assertEquals(204, post(uri, MediaType.APPLICATION_XML, xmlDataRpcInput));
129
130         mockInvokeRpc(null, false);
131         assertEquals(500, post(uri, MediaType.APPLICATION_XML, xmlDataRpcInput));
132
133         final List<RpcError> rpcErrors = new ArrayList<>();
134         rpcErrors.add(RpcResultBuilder.newError(ErrorType.RPC, "tag1", "message1", "applicationTag1", "info1", null));
135         rpcErrors.add(RpcResultBuilder.newWarning(ErrorType.PROTOCOL, "tag2", "message2", "applicationTag2", "info2",
136                 null));
137         mockInvokeRpc(null, false, rpcErrors);
138         assertEquals(500, post(uri, MediaType.APPLICATION_XML, xmlDataRpcInput));
139
140         uri = "/operations/test-module:rpc-wrongtest";
141         assertEquals(400, post(uri, MediaType.APPLICATION_XML, xmlDataRpcInput));
142     }
143
144     @Test
145     @Ignore // TODO RestconfDocumentedExceptionMapper needs be fixed before
146     public void postConfigOnlyStatusCodes() throws UnsupportedEncodingException {
147         setSchemaControllerContext(schemaContextYangsIetf);
148         final String uri = "/config";
149         mockCommitConfigurationDataPostMethod(true);
150         assertEquals(204, post(uri, MediaType.APPLICATION_XML, xmlDataAbsolutePath));
151
152         mockCommitConfigurationDataPostMethod(false);
153         assertEquals(500, post(uri, MediaType.APPLICATION_XML, xmlDataAbsolutePath));
154
155         assertEquals(400, post(uri, MediaType.APPLICATION_XML, ""));
156     }
157
158     @Test
159     public void postConfigStatusCodes() throws UnsupportedEncodingException {
160         setSchemaControllerContext(schemaContextYangsIetf);
161         final String uri = "/config/ietf-interfaces:interfaces";
162
163         mockCommitConfigurationDataPostMethod(true);
164         assertEquals(204, post(uri, MediaType.APPLICATION_XML, xmlDataInterfaceAbsolutePath));
165
166         mockCommitConfigurationDataPostMethod(false);
167         assertEquals(500, post(uri, MediaType.APPLICATION_XML, xmlDataInterfaceAbsolutePath));
168
169         // FIXME : empty json input post value return NullPointerException by parsing -> err. code 500
170 //        assertEquals(400, post(uri, MediaType.APPLICATION_JSON, ""));
171     }
172
173     @Test
174     @Ignore /// xmlData* need netconf-yang
175     public void postDataViaUrlMountPoint() throws UnsupportedEncodingException {
176         setSchemaControllerContext(schemaContextYangsIetf);
177         when(
178                 brokerFacade.commitConfigurationDataPost(any(DOMMountPoint.class), any(YangInstanceIdentifier.class),
179                         any(NormalizedNode.class))).thenReturn(mock(CheckedFuture.class));
180
181         final DOMMountPoint mountInstance = mock(DOMMountPoint.class);
182         when(mountInstance.getSchemaContext()).thenReturn(schemaContextTestModule);
183         final DOMMountPointService mockMountService = mock(DOMMountPointService.class);
184         when(mockMountService.getMountPoint(any(YangInstanceIdentifier.class))).thenReturn(Optional.of(mountInstance));
185
186         ControllerContext.getInstance().setMountService(mockMountService);
187
188         String uri = "/config/ietf-interfaces:interfaces/interface/0/";
189         assertEquals(204, post(uri, Draft02.MediaTypes.DATA + XML, xmlData4));
190         uri = "/config/ietf-interfaces:interfaces/interface/0/yang-ext:mount/test-module:cont";
191         assertEquals(204, post(uri, Draft02.MediaTypes.DATA + XML, xmlData3));
192
193         assertEquals(400, post(uri, MediaType.APPLICATION_JSON, ""));
194     }
195
196     private void mockInvokeRpc(final CompositeNode result, final boolean sucessful, final Collection<RpcError> errors) {
197
198         final DummyRpcResult.Builder<CompositeNode> builder = new DummyRpcResult.Builder<CompositeNode>().result(result)
199                 .isSuccessful(sucessful);
200         if (!errors.isEmpty()) {
201             builder.errors(errors);
202         }
203         final RpcResult<CompositeNode> rpcResult = builder.build();
204 //        when(brokerFacade.invokeRpc(any(QName.class), any(CompositeNode.class))).thenReturn(
205 //                Futures.<RpcResult<CompositeNode>> immediateFuture(rpcResult));
206     }
207
208     /**
209      * @deprecated has to be removed for lithium release
210      */
211     @Deprecated
212     private void mockInvokeRpc(final CompositeNode result, final boolean sucessful) {
213         mockInvokeRpc(result, sucessful, Collections.<RpcError> emptyList());
214     }
215
216     private void mockCommitConfigurationDataPostMethod(final boolean succesfulComit) {
217         if (succesfulComit) {
218             doReturn(mock(CheckedFuture.class)).when(brokerFacade).commitConfigurationDataPost(any(YangInstanceIdentifier.class), any(NormalizedNode.class));
219         } else {
220             doThrow(RestconfDocumentedException.class).when(brokerFacade).commitConfigurationDataPost(
221                     any(YangInstanceIdentifier.class), any(NormalizedNode.class));
222         }
223     }
224
225     @Test
226     public void createConfigurationDataTest() throws UnsupportedEncodingException, ParseException {
227         initMocking();
228         final RpcResult<TransactionStatus> rpcResult = new DummyRpcResult.Builder<TransactionStatus>().result(
229                 TransactionStatus.COMMITED).build();
230
231         when(brokerFacade.commitConfigurationDataPost(any(YangInstanceIdentifier.class), any(NormalizedNode.class)))
232                 .thenReturn(mock(CheckedFuture.class));
233
234         final ArgumentCaptor<YangInstanceIdentifier> instanceIdCaptor = ArgumentCaptor.forClass(YangInstanceIdentifier.class);
235         final ArgumentCaptor<NormalizedNode> compNodeCaptor = ArgumentCaptor.forClass(NormalizedNode.class);
236
237
238         // FIXME : identify who is set the schemaContext
239 //        final String URI_1 = "/config";
240 //        assertEquals(204, post(URI_1, Draft02.MediaTypes.DATA + XML, xmlTestInterface));
241 //        verify(brokerFacade).commitConfigurationDataPost(instanceIdCaptor.capture(), compNodeCaptor.capture());
242         final String identifier = "[(urn:ietf:params:xml:ns:yang:test-interface?revision=2014-07-01)interfaces]";
243 //        assertEquals(identifier, ImmutableList.copyOf(instanceIdCaptor.getValue().getPathArguments()).toString());
244
245         final String URI_2 = "/config/test-interface:interfaces";
246         assertEquals(204, post(URI_2, Draft02.MediaTypes.DATA + XML, xmlBlockData));
247         // FIXME : NEVER test a nr. of call some service in complex test suite
248 //        verify(brokerFacade, times(2))
249         verify(brokerFacade, times(1))
250                 .commitConfigurationDataPost(instanceIdCaptor.capture(), compNodeCaptor.capture());
251 //        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]";
252         assertEquals(identifier, ImmutableList.copyOf(instanceIdCaptor.getValue().getPathArguments()).toString());
253     }
254
255     @Test
256     public void createConfigurationDataNullTest() throws UnsupportedEncodingException {
257         initMocking();
258
259         when(brokerFacade.commitConfigurationDataPost(any(YangInstanceIdentifier.class), any(NormalizedNode.class)))
260                 .thenReturn(null);
261
262         //FIXME : find who is set schemaContext
263 //        final String URI_1 = "/config";
264 //        assertEquals(204, post(URI_1, Draft02.MediaTypes.DATA + XML, xmlTestInterface));
265
266         final String URI_2 = "/config/test-interface:interfaces";
267         assertEquals(204, post(URI_2, Draft02.MediaTypes.DATA + XML, xmlBlockData));
268     }
269
270     private static void initMocking() {
271         final ControllerContext controllerContext = ControllerContext.getInstance();
272         controllerContext.setSchemas(schemaContext);
273         mountService = mock(DOMMountPointService.class);
274         controllerContext.setMountService(mountService);
275         brokerFacade = mock(BrokerFacade.class);
276         restconfImpl = RestconfImpl.getInstance();
277         restconfImpl.setBroker(brokerFacade);
278         restconfImpl.setControllerContext(controllerContext);
279     }
280
281     private int post(final String uri, final String mediaType, final String data) {
282         return target(uri).request(mediaType).post(Entity.entity(data, mediaType)).getStatus();
283     }
284
285     private static void loadData() throws IOException, URISyntaxException {
286         InputStream xmlStream = RestconfImplTest.class
287                 .getResourceAsStream("/parts/ietf-interfaces_interfaces_absolute_path.xml");
288         xmlDataAbsolutePath = TestUtils.getDocumentInPrintableForm(TestUtils.loadDocumentFrom(xmlStream));
289         xmlStream = RestconfImplTest.class
290                 .getResourceAsStream("/parts/ietf-interfaces_interfaces_interface_absolute_path.xml");
291         xmlDataInterfaceAbsolutePath = TestUtils.getDocumentInPrintableForm(TestUtils.loadDocumentFrom(xmlStream));
292         final String xmlPathRpcInput = RestconfImplTest.class.getResource("/full-versions/test-data2/data-rpc-input.xml")
293                 .getPath();
294         xmlDataRpcInput = TestUtils.loadTextFile(xmlPathRpcInput);
295         final String xmlPathBlockData = RestconfImplTest.class.getResource("/test-config-data/xml/block-data.xml").getPath();
296         xmlBlockData = TestUtils.loadTextFile(xmlPathBlockData);
297         final String xmlPathTestInterface = RestconfImplTest.class.getResource("/test-config-data/xml/test-interface.xml")
298                 .getPath();
299         xmlTestInterface = TestUtils.loadTextFile(xmlPathTestInterface);
300         cnSnDataOutput = prepareCnSnRpcOutput();
301         final String data3Input = RestconfImplTest.class.getResource("/full-versions/test-data2/data3.xml").getPath();
302         xmlData3 = TestUtils.loadTextFile(data3Input);
303         final String data4Input = RestconfImplTest.class.getResource("/full-versions/test-data2/data7.xml").getPath();
304         xmlData4 = TestUtils.loadTextFile(data4Input);
305     }
306
307     private static CompositeNodeWrapper prepareCnSnRpcOutput() throws URISyntaxException {
308         final CompositeNodeWrapper cnSnDataOutput = new CompositeNodeWrapper(new URI("test:module"), "output");
309         final CompositeNodeWrapper cont = new CompositeNodeWrapper(new URI("test:module"), "cont-output");
310         cnSnDataOutput.addValue(cont);
311         cnSnDataOutput.unwrap();
312         return cnSnDataOutput;
313     }
314 }