2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.sal.restconf.impl.test;
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;
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;
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;
66 public class RestPostOperationTest extends JerseyTest {
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;
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;
83 private static DOMMountPointService mountService;
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);
93 final Set<Module> modules = TestUtils.loadModulesFrom("/test-config-data/yang1");
94 schemaContext = TestUtils.loadSchemaContext(modules);
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;
113 private void setSchemaControllerContext(final SchemaContext schema) {
114 final ControllerContext context = ControllerContext.getInstance();
115 context.setSchemas(schema);
116 restconfImpl.setControllerContext(context);
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));
127 mockInvokeRpc(null, true);
128 assertEquals(204, post(uri, MediaType.APPLICATION_XML, xmlDataRpcInput));
130 mockInvokeRpc(null, false);
131 assertEquals(500, post(uri, MediaType.APPLICATION_XML, xmlDataRpcInput));
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",
137 mockInvokeRpc(null, false, rpcErrors);
138 assertEquals(500, post(uri, MediaType.APPLICATION_XML, xmlDataRpcInput));
140 uri = "/operations/test-module:rpc-wrongtest";
141 assertEquals(400, post(uri, MediaType.APPLICATION_XML, xmlDataRpcInput));
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));
152 mockCommitConfigurationDataPostMethod(false);
153 assertEquals(500, post(uri, MediaType.APPLICATION_XML, xmlDataAbsolutePath));
155 assertEquals(400, post(uri, MediaType.APPLICATION_XML, ""));
159 public void postConfigStatusCodes() throws UnsupportedEncodingException {
160 setSchemaControllerContext(schemaContextYangsIetf);
161 final String uri = "/config/ietf-interfaces:interfaces";
163 mockCommitConfigurationDataPostMethod(true);
164 assertEquals(204, post(uri, MediaType.APPLICATION_XML, xmlDataInterfaceAbsolutePath));
166 mockCommitConfigurationDataPostMethod(false);
167 assertEquals(500, post(uri, MediaType.APPLICATION_XML, xmlDataInterfaceAbsolutePath));
169 // FIXME : empty json input post value return NullPointerException by parsing -> err. code 500
170 // assertEquals(400, post(uri, MediaType.APPLICATION_JSON, ""));
174 @Ignore /// xmlData* need netconf-yang
175 public void postDataViaUrlMountPoint() throws UnsupportedEncodingException {
176 setSchemaControllerContext(schemaContextYangsIetf);
178 brokerFacade.commitConfigurationDataPost(any(DOMMountPoint.class), any(YangInstanceIdentifier.class),
179 any(NormalizedNode.class))).thenReturn(mock(CheckedFuture.class));
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));
186 ControllerContext.getInstance().setMountService(mockMountService);
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));
193 assertEquals(400, post(uri, MediaType.APPLICATION_JSON, ""));
196 private void mockInvokeRpc(final CompositeNode result, final boolean sucessful, final Collection<RpcError> errors) {
198 final DummyRpcResult.Builder<CompositeNode> builder = new DummyRpcResult.Builder<CompositeNode>().result(result)
199 .isSuccessful(sucessful);
200 if (!errors.isEmpty()) {
201 builder.errors(errors);
203 final RpcResult<CompositeNode> rpcResult = builder.build();
204 // when(brokerFacade.invokeRpc(any(QName.class), any(CompositeNode.class))).thenReturn(
205 // Futures.<RpcResult<CompositeNode>> immediateFuture(rpcResult));
209 * @deprecated has to be removed for lithium release
212 private void mockInvokeRpc(final CompositeNode result, final boolean sucessful) {
213 mockInvokeRpc(result, sucessful, Collections.<RpcError> emptyList());
216 private void mockCommitConfigurationDataPostMethod(final boolean succesfulComit) {
217 if (succesfulComit) {
218 doReturn(mock(CheckedFuture.class)).when(brokerFacade).commitConfigurationDataPost(any(YangInstanceIdentifier.class), any(NormalizedNode.class));
220 doThrow(RestconfDocumentedException.class).when(brokerFacade).commitConfigurationDataPost(
221 any(YangInstanceIdentifier.class), any(NormalizedNode.class));
226 public void createConfigurationDataTest() throws UnsupportedEncodingException, ParseException {
228 final RpcResult<TransactionStatus> rpcResult = new DummyRpcResult.Builder<TransactionStatus>().result(
229 TransactionStatus.COMMITED).build();
231 when(brokerFacade.commitConfigurationDataPost(any(YangInstanceIdentifier.class), any(NormalizedNode.class)))
232 .thenReturn(mock(CheckedFuture.class));
234 final ArgumentCaptor<YangInstanceIdentifier> instanceIdCaptor = ArgumentCaptor.forClass(YangInstanceIdentifier.class);
235 final ArgumentCaptor<NormalizedNode> compNodeCaptor = ArgumentCaptor.forClass(NormalizedNode.class);
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());
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 // FIXME : identifier flow to interface only, why we want to see block too ?
252 // 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]";
253 assertEquals(identifier, ImmutableList.copyOf(instanceIdCaptor.getValue().getPathArguments()).toString());
257 public void createConfigurationDataNullTest() throws UnsupportedEncodingException {
260 when(brokerFacade.commitConfigurationDataPost(any(YangInstanceIdentifier.class), any(NormalizedNode.class)))
263 //FIXME : find who is set schemaContext
264 // final String URI_1 = "/config";
265 // assertEquals(204, post(URI_1, Draft02.MediaTypes.DATA + XML, xmlTestInterface));
267 final String URI_2 = "/config/test-interface:interfaces";
268 assertEquals(204, post(URI_2, Draft02.MediaTypes.DATA + XML, xmlBlockData));
271 private static void initMocking() {
272 final ControllerContext controllerContext = ControllerContext.getInstance();
273 controllerContext.setSchemas(schemaContext);
274 mountService = mock(DOMMountPointService.class);
275 controllerContext.setMountService(mountService);
276 brokerFacade = mock(BrokerFacade.class);
277 restconfImpl = RestconfImpl.getInstance();
278 restconfImpl.setBroker(brokerFacade);
279 restconfImpl.setControllerContext(controllerContext);
282 private int post(final String uri, final String mediaType, final String data) {
283 return target(uri).request(mediaType).post(Entity.entity(data, mediaType)).getStatus();
286 private static void loadData() throws IOException, URISyntaxException {
287 InputStream xmlStream = RestconfImplTest.class
288 .getResourceAsStream("/parts/ietf-interfaces_interfaces_absolute_path.xml");
289 xmlDataAbsolutePath = TestUtils.getDocumentInPrintableForm(TestUtils.loadDocumentFrom(xmlStream));
290 xmlStream = RestconfImplTest.class
291 .getResourceAsStream("/parts/ietf-interfaces_interfaces_interface_absolute_path.xml");
292 xmlDataInterfaceAbsolutePath = TestUtils.getDocumentInPrintableForm(TestUtils.loadDocumentFrom(xmlStream));
293 final String xmlPathRpcInput = RestconfImplTest.class.getResource("/full-versions/test-data2/data-rpc-input.xml")
295 xmlDataRpcInput = TestUtils.loadTextFile(xmlPathRpcInput);
296 final String xmlPathBlockData = RestconfImplTest.class.getResource("/test-config-data/xml/block-data.xml").getPath();
297 xmlBlockData = TestUtils.loadTextFile(xmlPathBlockData);
298 final String xmlPathTestInterface = RestconfImplTest.class.getResource("/test-config-data/xml/test-interface.xml")
300 xmlTestInterface = TestUtils.loadTextFile(xmlPathTestInterface);
301 cnSnDataOutput = prepareCnSnRpcOutput();
302 final String data3Input = RestconfImplTest.class.getResource("/full-versions/test-data2/data3.xml").getPath();
303 xmlData3 = TestUtils.loadTextFile(data3Input);
304 final String data4Input = RestconfImplTest.class.getResource("/full-versions/test-data2/data7.xml").getPath();
305 xmlData4 = TestUtils.loadTextFile(data4Input);
308 private static CompositeNodeWrapper prepareCnSnRpcOutput() throws URISyntaxException {
309 final CompositeNodeWrapper cnSnDataOutput = new CompositeNodeWrapper(new URI("test:module"), "output");
310 final CompositeNodeWrapper cont = new CompositeNodeWrapper(new URI("test:module"), "cont-output");
311 cnSnDataOutput.addValue(cont);
312 cnSnDataOutput.unwrap();
313 return cnSnDataOutput;