730178ea32b84c22f613d82a7bb38eb3e5520d99
[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 //    public void postRpcNoPayload() throws Exception {
121 //        setSchemaControllerContext(schemaContextTestModule);
122 //        final String uri = "/operations/test-module:no-payload-rpc-test";
123 //        final String mediaType = MediaType.APPLICATION_XML;
124 //        final Response response = target(uri).request(mediaType).post(Entity.entity("", mediaType));
125 //        assertNotNull(response);
126 //
127 //    }
128
129     @Test
130     @Ignore //FIXME we don't wish to mock CompositeNode as result
131     public void postOperationsStatusCodes() throws IOException {
132         setSchemaControllerContext(schemaContextTestModule);
133         mockInvokeRpc(cnSnDataOutput, true);
134         String uri = "/operations/test-module:rpc-test";
135         assertEquals(200, post(uri, MediaType.APPLICATION_XML, xmlDataRpcInput));
136
137         mockInvokeRpc(null, true);
138         assertEquals(204, post(uri, MediaType.APPLICATION_XML, xmlDataRpcInput));
139
140         mockInvokeRpc(null, false);
141         assertEquals(500, post(uri, MediaType.APPLICATION_XML, xmlDataRpcInput));
142
143         final List<RpcError> rpcErrors = new ArrayList<>();
144         rpcErrors.add(RpcResultBuilder.newError(ErrorType.RPC, "tag1", "message1", "applicationTag1", "info1", null));
145         rpcErrors.add(RpcResultBuilder.newWarning(ErrorType.PROTOCOL, "tag2", "message2", "applicationTag2", "info2",
146                 null));
147         mockInvokeRpc(null, false, rpcErrors);
148         assertEquals(500, post(uri, MediaType.APPLICATION_XML, xmlDataRpcInput));
149
150         uri = "/operations/test-module:rpc-wrongtest";
151         assertEquals(400, post(uri, MediaType.APPLICATION_XML, xmlDataRpcInput));
152     }
153
154     @Test
155     @Ignore // TODO RestconfDocumentedExceptionMapper needs be fixed before
156     public void postConfigOnlyStatusCodes() throws UnsupportedEncodingException {
157         setSchemaControllerContext(schemaContextYangsIetf);
158         final String uri = "/config";
159         mockCommitConfigurationDataPostMethod(true);
160         assertEquals(204, post(uri, MediaType.APPLICATION_XML, xmlDataAbsolutePath));
161
162         mockCommitConfigurationDataPostMethod(false);
163         assertEquals(500, post(uri, MediaType.APPLICATION_XML, xmlDataAbsolutePath));
164
165         assertEquals(400, post(uri, MediaType.APPLICATION_XML, ""));
166     }
167
168     @Test
169     @Ignore //jenkins has problem with JerseyTest - we expecting problems with singletons ControllerContext as schemaContext holder
170     public void postConfigStatusCodes() throws UnsupportedEncodingException {
171         setSchemaControllerContext(schemaContextYangsIetf);
172         final String uri = "/config/ietf-interfaces:interfaces";
173
174         mockCommitConfigurationDataPostMethod(true);
175         assertEquals(204, post(uri, MediaType.APPLICATION_XML, xmlDataInterfaceAbsolutePath));
176
177         mockCommitConfigurationDataPostMethod(false);
178         assertEquals(500, post(uri, MediaType.APPLICATION_XML, xmlDataInterfaceAbsolutePath));
179
180         // FIXME : empty json input post value return NullPointerException by parsing -> err. code 500
181 //        assertEquals(400, post(uri, MediaType.APPLICATION_JSON, ""));
182     }
183
184     @Test
185     @Ignore /// xmlData* need netconf-yang
186     public void postDataViaUrlMountPoint() throws UnsupportedEncodingException {
187         setSchemaControllerContext(schemaContextYangsIetf);
188         when(
189                 brokerFacade.commitConfigurationDataPost(any(DOMMountPoint.class), any(YangInstanceIdentifier.class),
190                         any(NormalizedNode.class))).thenReturn(mock(CheckedFuture.class));
191
192         final DOMMountPoint mountInstance = mock(DOMMountPoint.class);
193         when(mountInstance.getSchemaContext()).thenReturn(schemaContextTestModule);
194         final DOMMountPointService mockMountService = mock(DOMMountPointService.class);
195         when(mockMountService.getMountPoint(any(YangInstanceIdentifier.class))).thenReturn(Optional.of(mountInstance));
196
197         ControllerContext.getInstance().setMountService(mockMountService);
198
199         String uri = "/config/ietf-interfaces:interfaces/interface/0/";
200         assertEquals(204, post(uri, Draft02.MediaTypes.DATA + XML, xmlData4));
201         uri = "/config/ietf-interfaces:interfaces/interface/0/yang-ext:mount/test-module:cont";
202         assertEquals(204, post(uri, Draft02.MediaTypes.DATA + XML, xmlData3));
203
204         assertEquals(400, post(uri, MediaType.APPLICATION_JSON, ""));
205     }
206
207     private void mockInvokeRpc(final CompositeNode result, final boolean sucessful, final Collection<RpcError> errors) {
208
209         final DummyRpcResult.Builder<CompositeNode> builder = new DummyRpcResult.Builder<CompositeNode>().result(result)
210                 .isSuccessful(sucessful);
211         if (!errors.isEmpty()) {
212             builder.errors(errors);
213         }
214         final RpcResult<CompositeNode> rpcResult = builder.build();
215 //        when(brokerFacade.invokeRpc(any(QName.class), any(CompositeNode.class))).thenReturn(
216 //                Futures.<RpcResult<CompositeNode>> immediateFuture(rpcResult));
217     }
218
219     /**
220      * @deprecated has to be removed for lithium release
221      */
222     @Deprecated
223     private void mockInvokeRpc(final CompositeNode result, final boolean sucessful) {
224         mockInvokeRpc(result, sucessful, Collections.<RpcError> emptyList());
225     }
226
227     private void mockCommitConfigurationDataPostMethod(final boolean succesfulComit) {
228         if (succesfulComit) {
229             doReturn(mock(CheckedFuture.class)).when(brokerFacade).commitConfigurationDataPost(any(YangInstanceIdentifier.class), any(NormalizedNode.class));
230         } else {
231             doThrow(RestconfDocumentedException.class).when(brokerFacade).commitConfigurationDataPost(
232                     any(YangInstanceIdentifier.class), any(NormalizedNode.class));
233         }
234     }
235
236     @Test
237     @Ignore //jenkins has problem with JerseyTest - we expecting problems with singletons ControllerContext as schemaContext holder
238     public void createConfigurationDataTest() throws UnsupportedEncodingException, ParseException {
239         initMocking();
240         final RpcResult<TransactionStatus> rpcResult = new DummyRpcResult.Builder<TransactionStatus>().result(
241                 TransactionStatus.COMMITED).build();
242
243         when(brokerFacade.commitConfigurationDataPost(any(YangInstanceIdentifier.class), any(NormalizedNode.class)))
244                 .thenReturn(mock(CheckedFuture.class));
245
246         final ArgumentCaptor<YangInstanceIdentifier> instanceIdCaptor = ArgumentCaptor.forClass(YangInstanceIdentifier.class);
247         final ArgumentCaptor<NormalizedNode> compNodeCaptor = ArgumentCaptor.forClass(NormalizedNode.class);
248
249
250         // FIXME : identify who is set the schemaContext
251 //        final String URI_1 = "/config";
252 //        assertEquals(204, post(URI_1, Draft02.MediaTypes.DATA + XML, xmlTestInterface));
253 //        verify(brokerFacade).commitConfigurationDataPost(instanceIdCaptor.capture(), compNodeCaptor.capture());
254         final String identifier = "[(urn:ietf:params:xml:ns:yang:test-interface?revision=2014-07-01)interfaces]";
255 //        assertEquals(identifier, ImmutableList.copyOf(instanceIdCaptor.getValue().getPathArguments()).toString());
256
257         final String URI_2 = "/config/test-interface:interfaces";
258         assertEquals(204, post(URI_2, Draft02.MediaTypes.DATA + XML, xmlBlockData));
259         // FIXME : NEVER test a nr. of call some service in complex test suite
260 //        verify(brokerFacade, times(2))
261         verify(brokerFacade, times(1))
262                 .commitConfigurationDataPost(instanceIdCaptor.capture(), compNodeCaptor.capture());
263 //        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]";
264         assertEquals(identifier, ImmutableList.copyOf(instanceIdCaptor.getValue().getPathArguments()).toString());
265     }
266
267     @Test
268     public void createConfigurationDataNullTest() throws UnsupportedEncodingException {
269         initMocking();
270
271         when(brokerFacade.commitConfigurationDataPost(any(YangInstanceIdentifier.class), any(NormalizedNode.class)))
272                 .thenReturn(null);
273
274         //FIXME : find who is set schemaContext
275 //        final String URI_1 = "/config";
276 //        assertEquals(204, post(URI_1, Draft02.MediaTypes.DATA + XML, xmlTestInterface));
277
278         final String URI_2 = "/config/test-interface:interfaces";
279         assertEquals(204, post(URI_2, Draft02.MediaTypes.DATA + XML, xmlBlockData));
280     }
281
282     private static void initMocking() {
283         final ControllerContext controllerContext = ControllerContext.getInstance();
284         controllerContext.setSchemas(schemaContext);
285         mountService = mock(DOMMountPointService.class);
286         controllerContext.setMountService(mountService);
287         brokerFacade = mock(BrokerFacade.class);
288         restconfImpl = RestconfImpl.getInstance();
289         restconfImpl.setBroker(brokerFacade);
290         restconfImpl.setControllerContext(controllerContext);
291     }
292
293     private int post(final String uri, final String mediaType, final String data) {
294         return target(uri).request(mediaType).post(Entity.entity(data, mediaType)).getStatus();
295     }
296
297     private static void loadData() throws IOException, URISyntaxException {
298         InputStream xmlStream = RestconfImplTest.class
299                 .getResourceAsStream("/parts/ietf-interfaces_interfaces_absolute_path.xml");
300         xmlDataAbsolutePath = TestUtils.getDocumentInPrintableForm(TestUtils.loadDocumentFrom(xmlStream));
301         xmlStream = RestconfImplTest.class
302                 .getResourceAsStream("/parts/ietf-interfaces_interfaces_interface_absolute_path.xml");
303         xmlDataInterfaceAbsolutePath = TestUtils.getDocumentInPrintableForm(TestUtils.loadDocumentFrom(xmlStream));
304         final String xmlPathRpcInput = RestconfImplTest.class.getResource("/full-versions/test-data2/data-rpc-input.xml")
305                 .getPath();
306         xmlDataRpcInput = TestUtils.loadTextFile(xmlPathRpcInput);
307         final String xmlPathBlockData = RestconfImplTest.class.getResource("/test-config-data/xml/block-data.xml").getPath();
308         xmlBlockData = TestUtils.loadTextFile(xmlPathBlockData);
309         final String xmlPathTestInterface = RestconfImplTest.class.getResource("/test-config-data/xml/test-interface.xml")
310                 .getPath();
311         xmlTestInterface = TestUtils.loadTextFile(xmlPathTestInterface);
312         cnSnDataOutput = prepareCnSnRpcOutput();
313         final String data3Input = RestconfImplTest.class.getResource("/full-versions/test-data2/data3.xml").getPath();
314         xmlData3 = TestUtils.loadTextFile(data3Input);
315         final String data4Input = RestconfImplTest.class.getResource("/full-versions/test-data2/data7.xml").getPath();
316         xmlData4 = TestUtils.loadTextFile(data4Input);
317     }
318
319     private static CompositeNodeWrapper prepareCnSnRpcOutput() throws URISyntaxException {
320         final CompositeNodeWrapper cnSnDataOutput = new CompositeNodeWrapper(new URI("test:module"), "output");
321         final CompositeNodeWrapper cont = new CompositeNodeWrapper(new URI("test:module"), "cont-output");
322         cnSnDataOutput.addValue(cont);
323         cnSnDataOutput.unwrap();
324         return cnSnDataOutput;
325     }
326 }