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.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 import static org.opendaylight.controller.sal.restconf.impl.test.RestOperationUtils.createUri;
19 import java.io.IOException;
20 import java.io.InputStream;
21 import java.io.UnsupportedEncodingException;
23 import java.net.URISyntaxException;
24 import java.net.URLEncoder;
25 import java.text.ParseException;
27 import java.util.concurrent.Future;
29 import javax.ws.rs.client.Entity;
30 import javax.ws.rs.core.Application;
31 import javax.ws.rs.core.MediaType;
32 import javax.ws.rs.core.Response;
34 import org.glassfish.jersey.server.ResourceConfig;
35 import org.glassfish.jersey.test.JerseyTest;
36 import org.junit.BeforeClass;
37 import org.junit.Test;
38 import org.mockito.ArgumentCaptor;
39 import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
40 import org.opendaylight.controller.sal.core.api.mount.MountInstance;
41 import org.opendaylight.controller.sal.core.api.mount.MountService;
42 import org.opendaylight.controller.sal.rest.api.Draft02;
43 import org.opendaylight.controller.sal.rest.impl.JsonToCompositeNodeProvider;
44 import org.opendaylight.controller.sal.rest.impl.StructuredDataToJsonProvider;
45 import org.opendaylight.controller.sal.rest.impl.StructuredDataToXmlProvider;
46 import org.opendaylight.controller.sal.rest.impl.XmlMapper;
47 import org.opendaylight.controller.sal.rest.impl.XmlToCompositeNodeProvider;
48 import org.opendaylight.controller.sal.restconf.impl.BrokerFacade;
49 import org.opendaylight.controller.sal.restconf.impl.CompositeNodeWrapper;
50 import org.opendaylight.controller.sal.restconf.impl.ControllerContext;
51 import org.opendaylight.controller.sal.restconf.impl.RestconfImpl;
52 import org.opendaylight.yangtools.yang.common.QName;
53 import org.opendaylight.yangtools.yang.common.RpcResult;
54 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
55 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
56 import org.opendaylight.yangtools.yang.model.api.Module;
57 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
59 import com.google.common.base.Charsets;
61 public class RestPostOperationTest extends JerseyTest {
63 private static String xmlDataAbsolutePath;
64 private static String xmlDataInterfaceAbsolutePath;
65 private static String xmlDataRpcInput;
66 private static String xmlBlockData;
67 private static String xmlTestInterface;
68 private static CompositeNodeWrapper cnSnDataOutput;
69 private static String xmlData3;
70 private static String xmlData4;
72 private static ControllerContext controllerContext;
73 private static BrokerFacade brokerFacade;
74 private static RestconfImpl restconfImpl;
75 private static SchemaContext schemaContextYangsIetf;
76 private static SchemaContext schemaContextTestModule;
77 private static SchemaContext schemaContext;
79 private static MountService mountService;
82 public static void init() throws URISyntaxException, IOException {
83 schemaContextYangsIetf = TestUtils.loadSchemaContext("/full-versions/yangs");
84 schemaContextTestModule = TestUtils.loadSchemaContext("/full-versions/test-module");
85 controllerContext = ControllerContext.getInstance();
86 brokerFacade = mock(BrokerFacade.class);
87 restconfImpl = RestconfImpl.getInstance();
88 restconfImpl.setBroker(brokerFacade);
89 restconfImpl.setControllerContext(controllerContext);
91 Set<Module> modules = TestUtils.loadModulesFrom("/test-config-data/yang1");
92 schemaContext = TestUtils.loadSchemaContext(modules);
98 protected Application configure() {
99 /* enable/disable Jersey logs to console */
100 // enable(TestProperties.LOG_TRAFFIC);
101 // enable(TestProperties.DUMP_ENTITY);
102 // enable(TestProperties.RECORD_LOG_LEVEL);
103 // set(TestProperties.RECORD_LOG_LEVEL, Level.ALL.intValue());
104 ResourceConfig resourceConfig = new ResourceConfig();
105 resourceConfig = resourceConfig.registerInstances(restconfImpl, StructuredDataToXmlProvider.INSTANCE,
106 StructuredDataToJsonProvider.INSTANCE, XmlToCompositeNodeProvider.INSTANCE,
107 JsonToCompositeNodeProvider.INSTANCE);
108 return resourceConfig;
112 public void postOperationsStatusCodes() throws UnsupportedEncodingException {
113 controllerContext.setSchemas(schemaContextTestModule);
114 mockInvokeRpc(cnSnDataOutput, true);
115 String uri = createUri("/operations/", "test-module:rpc-test");
116 assertEquals(200, post(uri, MediaType.APPLICATION_XML, xmlDataRpcInput));
118 mockInvokeRpc(null, true);
119 assertEquals(204, post(uri, MediaType.APPLICATION_XML, xmlDataRpcInput));
121 mockInvokeRpc(null, false);
122 assertEquals(500, post(uri, MediaType.APPLICATION_XML, xmlDataRpcInput));
124 uri = createUri("/operations/", "test-module:rpc-wrongtest");
125 assertEquals(404, post(uri, MediaType.APPLICATION_XML, xmlDataRpcInput));
129 public void postConfigOnlyStatusCodes() throws UnsupportedEncodingException {
130 controllerContext.setSchemas(schemaContextYangsIetf);
131 mockCommitConfigurationDataPostMethod(TransactionStatus.COMMITED);
132 String uri = createUri("/config", "");
133 assertEquals(204, post(uri, MediaType.APPLICATION_XML, xmlDataAbsolutePath));
135 mockCommitConfigurationDataPostMethod(null);
136 assertEquals(202, post(uri, MediaType.APPLICATION_XML, xmlDataAbsolutePath));
138 mockCommitConfigurationDataPostMethod(TransactionStatus.FAILED);
139 assertEquals(500, post(uri, MediaType.APPLICATION_XML, xmlDataAbsolutePath));
143 public void postConfigStatusCodes() throws UnsupportedEncodingException {
144 controllerContext.setSchemas(schemaContextYangsIetf);
145 mockCommitConfigurationDataPostMethod(TransactionStatus.COMMITED);
146 String uri = createUri("/config/", "ietf-interfaces:interfaces");
147 assertEquals(204, post(uri, MediaType.APPLICATION_XML, xmlDataInterfaceAbsolutePath));
149 mockCommitConfigurationDataPostMethod(null);
150 assertEquals(202, post(uri, MediaType.APPLICATION_XML, xmlDataInterfaceAbsolutePath));
152 mockCommitConfigurationDataPostMethod(TransactionStatus.FAILED);
153 assertEquals(500, post(uri, MediaType.APPLICATION_XML, xmlDataInterfaceAbsolutePath));
157 public void postDatastoreStatusCodes() throws UnsupportedEncodingException {
158 controllerContext.setSchemas(schemaContextYangsIetf);
159 mockCommitConfigurationDataPostMethod(TransactionStatus.COMMITED);
160 String uri = createUri("/datastore/", "ietf-interfaces:interfaces");
161 assertEquals(204, post(uri, MediaType.APPLICATION_XML, xmlDataInterfaceAbsolutePath));
163 mockCommitConfigurationDataPostMethod(null);
164 assertEquals(202, post(uri, MediaType.APPLICATION_XML, xmlDataInterfaceAbsolutePath));
166 mockCommitConfigurationDataPostMethod(TransactionStatus.FAILED);
167 assertEquals(500, post(uri, MediaType.APPLICATION_XML, xmlDataInterfaceAbsolutePath));
171 public void postDataViaUrlMountPoint() throws UnsupportedEncodingException {
172 controllerContext.setSchemas(schemaContextYangsIetf);
173 RpcResult<TransactionStatus> rpcResult = new DummyRpcResult.Builder<TransactionStatus>().result(
174 TransactionStatus.COMMITED).build();
175 Future<RpcResult<TransactionStatus>> dummyFuture = DummyFuture.builder().rpcResult(rpcResult).build();
177 brokerFacade.commitConfigurationDataPostBehindMountPoint(any(MountInstance.class),
178 any(InstanceIdentifier.class), any(CompositeNode.class))).thenReturn(dummyFuture);
180 MountInstance mountInstance = mock(MountInstance.class);
181 when(mountInstance.getSchemaContext()).thenReturn(schemaContextTestModule);
182 MountService mockMountService = mock(MountService.class);
183 when(mockMountService.getMountPoint(any(InstanceIdentifier.class))).thenReturn(mountInstance);
185 ControllerContext.getInstance().setMountService(mockMountService);
187 String uri = createUri("/config/", "ietf-interfaces:interfaces/interface/0/");
188 assertEquals(204, post(uri, Draft02.MediaTypes.DATA + XML, xmlData4));
189 uri = createUri("/config/", "ietf-interfaces:interfaces/interface/0/yang-ext:mount/test-module:cont");
190 assertEquals(204, post(uri, Draft02.MediaTypes.DATA + XML, xmlData3));
193 private void mockInvokeRpc(CompositeNode result, boolean sucessful) {
194 RpcResult<CompositeNode> rpcResult = new DummyRpcResult.Builder<CompositeNode>().result(result)
195 .isSuccessful(sucessful).build();
196 when(brokerFacade.invokeRpc(any(QName.class), any(CompositeNode.class))).thenReturn(rpcResult);
199 private void mockCommitConfigurationDataPostMethod(TransactionStatus statusName) {
200 RpcResult<TransactionStatus> rpcResult = new DummyRpcResult.Builder<TransactionStatus>().result(statusName)
202 Future<RpcResult<TransactionStatus>> dummyFuture = null;
203 if (statusName != null) {
204 dummyFuture = DummyFuture.builder().rpcResult(rpcResult).build();
206 dummyFuture = DummyFuture.builder().build();
209 when(brokerFacade.commitConfigurationDataPost(any(InstanceIdentifier.class), any(CompositeNode.class)))
210 .thenReturn(dummyFuture);
214 public void createConfigurationDataTest() throws UnsupportedEncodingException, ParseException {
216 RpcResult<TransactionStatus> rpcResult = new DummyRpcResult.Builder<TransactionStatus>().result(
217 TransactionStatus.COMMITED).build();
218 Future<RpcResult<TransactionStatus>> dummyFuture = DummyFuture.builder().rpcResult(rpcResult).build();
220 when(brokerFacade.commitConfigurationDataPost(any(InstanceIdentifier.class), any(CompositeNode.class)))
221 .thenReturn(dummyFuture);
223 ArgumentCaptor<InstanceIdentifier> instanceIdCaptor = ArgumentCaptor.forClass(InstanceIdentifier.class);
224 ArgumentCaptor<CompositeNode> compNodeCaptor = ArgumentCaptor.forClass(CompositeNode.class);
226 String URI_1 = createUri("/config", "");
227 assertEquals(204, post(URI_1, Draft02.MediaTypes.DATA + XML, xmlTestInterface));
228 verify(brokerFacade).commitConfigurationDataPost(instanceIdCaptor.capture(), compNodeCaptor.capture());
229 String identifier = "[(urn:ietf:params:xml:ns:yang:test-interface?revision=2014-07-01)interfaces]";
230 assertEquals(identifier, instanceIdCaptor.getValue().getPath().toString());
232 String URI_2 = createUri("/config/", "test-interface:interfaces");
233 assertEquals(204, post(URI_2, Draft02.MediaTypes.DATA + XML, xmlBlockData));
234 verify(brokerFacade, times(2))
235 .commitConfigurationDataPost(instanceIdCaptor.capture(), compNodeCaptor.capture());
236 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]";
237 assertEquals(identifier, instanceIdCaptor.getValue().getPath().toString());
241 public void createConfigurationDataNullTest() throws UnsupportedEncodingException {
244 when(brokerFacade.commitConfigurationDataPost(any(InstanceIdentifier.class), any(CompositeNode.class)))
247 String URI_1 = createUri("/config", "");
248 assertEquals(202, post(URI_1, Draft02.MediaTypes.DATA + XML, xmlTestInterface));
250 String URI_2 = createUri("/config/", "test-interface:interfaces");
251 assertEquals(202, post(URI_2, Draft02.MediaTypes.DATA + XML, xmlBlockData));
254 private String createUri(String prefix, String encodedPart) throws UnsupportedEncodingException {
255 return URI.create(prefix + URLEncoder.encode(encodedPart, Charsets.US_ASCII.name()).toString()).toASCIIString();
258 private static void initMocking() {
259 controllerContext = ControllerContext.getInstance();
260 controllerContext.setSchemas(schemaContext);
261 mountService = mock(MountService.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(String uri, String mediaType, 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 String xmlPathRpcInput = RestconfImplTest.class.getResource("/full-versions/test-data2/data-rpc-input.xml")
282 xmlDataRpcInput = TestUtils.loadTextFile(xmlPathRpcInput);
283 String xmlPathBlockData = RestconfImplTest.class.getResource("/test-config-data/xml/block-data.xml").getPath();
284 xmlBlockData = TestUtils.loadTextFile(xmlPathBlockData);
285 String xmlPathTestInterface = RestconfImplTest.class.getResource("/test-config-data/xml/test-interface.xml")
287 xmlTestInterface = TestUtils.loadTextFile(xmlPathTestInterface);
288 cnSnDataOutput = prepareCnSnRpcOutput();
289 String data3Input = RestconfImplTest.class.getResource("/full-versions/test-data2/data3.xml").getPath();
290 xmlData3 = TestUtils.loadTextFile(data3Input);
291 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 CompositeNodeWrapper cnSnDataOutput = new CompositeNodeWrapper(new URI("test:module"), "output");
297 CompositeNodeWrapper cont = new CompositeNodeWrapper(new URI("test:module"), "cont-output");
298 cnSnDataOutput.addValue(cont);
299 cnSnDataOutput.unwrap();
300 return cnSnDataOutput;