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 com.google.common.util.concurrent.Futures;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.io.UnsupportedEncodingException;
27 import java.net.URISyntaxException;
28 import java.text.ParseException;
29 import java.util.ArrayList;
30 import java.util.Collection;
31 import java.util.Collections;
32 import java.util.List;
34 import javax.ws.rs.client.Entity;
35 import javax.ws.rs.core.Application;
36 import javax.ws.rs.core.MediaType;
37 import org.glassfish.jersey.server.ResourceConfig;
38 import org.glassfish.jersey.test.JerseyTest;
39 import org.junit.BeforeClass;
40 import org.junit.Ignore;
41 import org.junit.Test;
42 import org.mockito.ArgumentCaptor;
43 import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
44 import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
45 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
46 import org.opendaylight.controller.sal.rest.api.Draft02;
47 import org.opendaylight.controller.sal.rest.impl.JsonToCompositeNodeProvider;
48 import org.opendaylight.controller.sal.rest.impl.RestconfDocumentedExceptionMapper;
49 import org.opendaylight.controller.sal.rest.impl.StructuredDataToJsonProvider;
50 import org.opendaylight.controller.sal.rest.impl.StructuredDataToXmlProvider;
51 import org.opendaylight.controller.sal.rest.impl.XmlToCompositeNodeProvider;
52 import org.opendaylight.controller.sal.restconf.impl.BrokerFacade;
53 import org.opendaylight.controller.sal.restconf.impl.CompositeNodeWrapper;
54 import org.opendaylight.controller.sal.restconf.impl.ControllerContext;
55 import org.opendaylight.controller.sal.restconf.impl.RestconfDocumentedException;
56 import org.opendaylight.controller.sal.restconf.impl.RestconfImpl;
57 import org.opendaylight.yangtools.yang.common.QName;
58 import org.opendaylight.yangtools.yang.common.RpcError;
59 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
60 import org.opendaylight.yangtools.yang.common.RpcResult;
61 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
62 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
63 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
64 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
65 import org.opendaylight.yangtools.yang.model.api.Module;
66 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
68 public class RestPostOperationTest extends JerseyTest {
70 private static String xmlDataAbsolutePath;
71 private static String xmlDataInterfaceAbsolutePath;
72 private static String xmlDataRpcInput;
73 private static String xmlBlockData;
74 private static String xmlTestInterface;
75 private static CompositeNodeWrapper cnSnDataOutput;
76 private static String xmlData3;
77 private static String xmlData4;
79 private static ControllerContext controllerContext;
80 private static BrokerFacade brokerFacade;
81 private static RestconfImpl restconfImpl;
82 private static SchemaContext schemaContextYangsIetf;
83 private static SchemaContext schemaContextTestModule;
84 private static SchemaContext schemaContext;
86 private static DOMMountPointService mountService;
89 public static void init() throws URISyntaxException, IOException {
90 schemaContextYangsIetf = TestUtils.loadSchemaContext("/full-versions/yangs");
91 schemaContextTestModule = TestUtils.loadSchemaContext("/full-versions/test-module");
92 controllerContext = ControllerContext.getInstance();
93 brokerFacade = mock(BrokerFacade.class);
94 restconfImpl = RestconfImpl.getInstance();
95 restconfImpl.setBroker(brokerFacade);
96 restconfImpl.setControllerContext(controllerContext);
98 final Set<Module> modules = TestUtils.loadModulesFrom("/test-config-data/yang1");
99 schemaContext = TestUtils.loadSchemaContext(modules);
105 protected Application configure() {
106 /* enable/disable Jersey logs to console */
107 // enable(TestProperties.LOG_TRAFFIC);
108 // enable(TestProperties.DUMP_ENTITY);
109 // enable(TestProperties.RECORD_LOG_LEVEL);
110 // set(TestProperties.RECORD_LOG_LEVEL, Level.ALL.intValue());
111 ResourceConfig resourceConfig = new ResourceConfig();
112 resourceConfig = resourceConfig.registerInstances(restconfImpl, StructuredDataToXmlProvider.INSTANCE,
113 StructuredDataToJsonProvider.INSTANCE, XmlToCompositeNodeProvider.INSTANCE,
114 JsonToCompositeNodeProvider.INSTANCE);
115 resourceConfig.registerClasses(RestconfDocumentedExceptionMapper.class);
116 return resourceConfig;
120 public void postOperationsStatusCodes() throws IOException {
121 controllerContext.setSchemas(schemaContextTestModule);
122 mockInvokeRpc(cnSnDataOutput, true);
123 String uri = "/operations/test-module:rpc-test";
124 assertEquals(200, post(uri, MediaType.APPLICATION_XML, xmlDataRpcInput));
126 mockInvokeRpc(null, true);
127 assertEquals(204, post(uri, MediaType.APPLICATION_XML, xmlDataRpcInput));
129 mockInvokeRpc(null, false);
130 assertEquals(500, post(uri, MediaType.APPLICATION_XML, xmlDataRpcInput));
132 final List<RpcError> rpcErrors = new ArrayList<>();
133 rpcErrors.add(RpcResultBuilder.newError(ErrorType.RPC, "tag1", "message1", "applicationTag1", "info1", null));
134 rpcErrors.add(RpcResultBuilder.newWarning(ErrorType.PROTOCOL, "tag2", "message2", "applicationTag2", "info2",
136 mockInvokeRpc(null, false, rpcErrors);
137 assertEquals(500, post(uri, MediaType.APPLICATION_XML, xmlDataRpcInput));
139 uri = "/operations/test-module:rpc-wrongtest";
140 assertEquals(400, post(uri, MediaType.APPLICATION_XML, xmlDataRpcInput));
144 public void postConfigOnlyStatusCodes() throws UnsupportedEncodingException {
145 controllerContext.setSchemas(schemaContextYangsIetf);
146 final String uri = "/config";
147 mockCommitConfigurationDataPostMethod(true);
148 assertEquals(204, post(uri, MediaType.APPLICATION_XML, xmlDataAbsolutePath));
150 mockCommitConfigurationDataPostMethod(false);
151 assertEquals(500, post(uri, MediaType.APPLICATION_XML, xmlDataAbsolutePath));
153 assertEquals(400, post(uri, MediaType.APPLICATION_XML, ""));
157 @Ignore // FIXME : find problem with codec
158 public void postConfigStatusCodes() throws UnsupportedEncodingException {
159 controllerContext.setSchemas(schemaContextYangsIetf);
160 final String uri = "/config/ietf-interfaces:interfaces";
162 mockCommitConfigurationDataPostMethod(true);
163 assertEquals(204, post(uri, MediaType.APPLICATION_XML, xmlDataInterfaceAbsolutePath));
165 mockCommitConfigurationDataPostMethod(false);
166 assertEquals(500, post(uri, MediaType.APPLICATION_XML, xmlDataInterfaceAbsolutePath));
168 assertEquals(400, post(uri, MediaType.APPLICATION_JSON, ""));
172 public void postDataViaUrlMountPoint() throws UnsupportedEncodingException {
173 controllerContext.setSchemas(schemaContextYangsIetf);
175 brokerFacade.commitConfigurationDataPost(any(DOMMountPoint.class), any(YangInstanceIdentifier.class),
176 any(NormalizedNode.class))).thenReturn(mock(CheckedFuture.class));
178 final DOMMountPoint mountInstance = mock(DOMMountPoint.class);
179 when(mountInstance.getSchemaContext()).thenReturn(schemaContextTestModule);
180 final DOMMountPointService mockMountService = mock(DOMMountPointService.class);
181 when(mockMountService.getMountPoint(any(YangInstanceIdentifier.class))).thenReturn(Optional.of(mountInstance));
183 ControllerContext.getInstance().setMountService(mockMountService);
185 String uri = "/config/ietf-interfaces:interfaces/interface/0/";
186 assertEquals(204, post(uri, Draft02.MediaTypes.DATA + XML, xmlData4));
187 uri = "/config/ietf-interfaces:interfaces/interface/0/yang-ext:mount/test-module:cont";
188 assertEquals(204, post(uri, Draft02.MediaTypes.DATA + XML, xmlData3));
190 assertEquals(400, post(uri, MediaType.APPLICATION_JSON, ""));
193 private void mockInvokeRpc(final CompositeNode result, final boolean sucessful, final Collection<RpcError> errors) {
195 final DummyRpcResult.Builder<CompositeNode> builder = new DummyRpcResult.Builder<CompositeNode>().result(result)
196 .isSuccessful(sucessful);
197 if (!errors.isEmpty()) {
198 builder.errors(errors);
200 final RpcResult<CompositeNode> rpcResult = builder.build();
201 when(brokerFacade.invokeRpc(any(QName.class), any(CompositeNode.class))).thenReturn(
202 Futures.<RpcResult<CompositeNode>> immediateFuture(rpcResult));
205 private void mockInvokeRpc(final CompositeNode result, final boolean sucessful) {
206 mockInvokeRpc(result, sucessful, Collections.<RpcError> emptyList());
209 private void mockCommitConfigurationDataPostMethod(final boolean succesfulComit) {
210 if (succesfulComit) {
211 doReturn(mock(CheckedFuture.class)).when(brokerFacade).commitConfigurationDataPost(any(YangInstanceIdentifier.class), any(NormalizedNode.class));
213 doThrow(RestconfDocumentedException.class).when(brokerFacade).commitConfigurationDataPost(
214 any(YangInstanceIdentifier.class), any(NormalizedNode.class));
219 public void createConfigurationDataTest() throws UnsupportedEncodingException, ParseException {
221 final RpcResult<TransactionStatus> rpcResult = new DummyRpcResult.Builder<TransactionStatus>().result(
222 TransactionStatus.COMMITED).build();
224 when(brokerFacade.commitConfigurationDataPost(any(YangInstanceIdentifier.class), any(NormalizedNode.class)))
225 .thenReturn(mock(CheckedFuture.class));
227 final ArgumentCaptor<YangInstanceIdentifier> instanceIdCaptor = ArgumentCaptor.forClass(YangInstanceIdentifier.class);
228 final ArgumentCaptor<NormalizedNode> compNodeCaptor = ArgumentCaptor.forClass(NormalizedNode.class);
230 final String URI_1 = "/config";
231 assertEquals(204, post(URI_1, Draft02.MediaTypes.DATA + XML, xmlTestInterface));
232 verify(brokerFacade).commitConfigurationDataPost(instanceIdCaptor.capture(), compNodeCaptor.capture());
233 String identifier = "[(urn:ietf:params:xml:ns:yang:test-interface?revision=2014-07-01)interfaces]";
234 assertEquals(identifier, ImmutableList.copyOf(instanceIdCaptor.getValue().getPathArguments()).toString());
236 final String URI_2 = "/config/test-interface:interfaces";
237 assertEquals(204, post(URI_2, Draft02.MediaTypes.DATA + XML, xmlBlockData));
238 verify(brokerFacade, times(2))
239 .commitConfigurationDataPost(instanceIdCaptor.capture(), compNodeCaptor.capture());
240 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]";
241 assertEquals(identifier, ImmutableList.copyOf(instanceIdCaptor.getValue().getPathArguments()).toString());
245 public void createConfigurationDataNullTest() throws UnsupportedEncodingException {
248 when(brokerFacade.commitConfigurationDataPost(any(YangInstanceIdentifier.class), any(NormalizedNode.class)))
251 final String URI_1 = "/config";
252 assertEquals(204, post(URI_1, Draft02.MediaTypes.DATA + XML, xmlTestInterface));
254 final String URI_2 = "/config/test-interface:interfaces";
255 assertEquals(204, post(URI_2, Draft02.MediaTypes.DATA + XML, xmlBlockData));
258 private static void initMocking() {
259 controllerContext = ControllerContext.getInstance();
260 controllerContext.setSchemas(schemaContext);
261 mountService = mock(DOMMountPointService.class);
262 controllerContext.setMountService(mountService);
263 brokerFacade = mock(BrokerFacade.class);
264 restconfImpl = RestconfImpl.getInstance();
265 restconfImpl.setBroker(brokerFacade);
266 restconfImpl.setControllerContext(controllerContext);
269 private int post(final String uri, final String mediaType, final String data) {
270 return target(uri).request(mediaType).post(Entity.entity(data, mediaType)).getStatus();
273 private static void loadData() throws IOException, URISyntaxException {
274 InputStream xmlStream = RestconfImplTest.class
275 .getResourceAsStream("/parts/ietf-interfaces_interfaces_absolute_path.xml");
276 xmlDataAbsolutePath = TestUtils.getDocumentInPrintableForm(TestUtils.loadDocumentFrom(xmlStream));
277 xmlStream = RestconfImplTest.class
278 .getResourceAsStream("/parts/ietf-interfaces_interfaces_interface_absolute_path.xml");
279 xmlDataInterfaceAbsolutePath = TestUtils.getDocumentInPrintableForm(TestUtils.loadDocumentFrom(xmlStream));
280 final String xmlPathRpcInput = RestconfImplTest.class.getResource("/full-versions/test-data2/data-rpc-input.xml")
282 xmlDataRpcInput = TestUtils.loadTextFile(xmlPathRpcInput);
283 final String xmlPathBlockData = RestconfImplTest.class.getResource("/test-config-data/xml/block-data.xml").getPath();
284 xmlBlockData = TestUtils.loadTextFile(xmlPathBlockData);
285 final String xmlPathTestInterface = RestconfImplTest.class.getResource("/test-config-data/xml/test-interface.xml")
287 xmlTestInterface = TestUtils.loadTextFile(xmlPathTestInterface);
288 cnSnDataOutput = prepareCnSnRpcOutput();
289 final String data3Input = RestconfImplTest.class.getResource("/full-versions/test-data2/data3.xml").getPath();
290 xmlData3 = TestUtils.loadTextFile(data3Input);
291 final String data4Input = RestconfImplTest.class.getResource("/full-versions/test-data2/data7.xml").getPath();
292 xmlData4 = TestUtils.loadTextFile(data4Input);
295 private static CompositeNodeWrapper prepareCnSnRpcOutput() throws URISyntaxException {
296 final CompositeNodeWrapper cnSnDataOutput = new CompositeNodeWrapper(new URI("test:module"), "output");
297 final CompositeNodeWrapper cont = new CompositeNodeWrapper(new URI("test:module"), "cont-output");
298 cnSnDataOutput.addValue(cont);
299 cnSnDataOutput.unwrap();
300 return cnSnDataOutput;