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;
18 import java.io.IOException;
19 import java.io.InputStream;
20 import java.io.UnsupportedEncodingException;
22 import java.net.URISyntaxException;
23 import java.net.URLEncoder;
24 import java.text.ParseException;
26 import java.util.concurrent.Future;
28 import javax.ws.rs.client.Entity;
29 import javax.ws.rs.core.Application;
30 import javax.ws.rs.core.MediaType;
32 import org.glassfish.jersey.server.ResourceConfig;
33 import org.glassfish.jersey.test.JerseyTest;
34 import org.junit.BeforeClass;
35 import org.junit.Test;
36 import org.mockito.ArgumentCaptor;
37 import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
38 import org.opendaylight.controller.sal.core.api.mount.MountInstance;
39 import org.opendaylight.controller.sal.core.api.mount.MountService;
40 import org.opendaylight.controller.sal.rest.api.Draft02;
41 import org.opendaylight.controller.sal.rest.impl.JsonToCompositeNodeProvider;
42 import org.opendaylight.controller.sal.rest.impl.StructuredDataToJsonProvider;
43 import org.opendaylight.controller.sal.rest.impl.StructuredDataToXmlProvider;
44 import org.opendaylight.controller.sal.rest.impl.XmlToCompositeNodeProvider;
45 import org.opendaylight.controller.sal.restconf.impl.BrokerFacade;
46 import org.opendaylight.controller.sal.restconf.impl.CompositeNodeWrapper;
47 import org.opendaylight.controller.sal.restconf.impl.ControllerContext;
48 import org.opendaylight.controller.sal.restconf.impl.RestconfImpl;
49 import org.opendaylight.yangtools.yang.common.QName;
50 import org.opendaylight.yangtools.yang.common.RpcResult;
51 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
52 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
53 import org.opendaylight.yangtools.yang.model.api.Module;
54 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
56 import com.google.common.base.Charsets;
58 public class RestPostOperationTest extends JerseyTest {
60 private static String xmlDataAbsolutePath;
61 private static String xmlDataInterfaceAbsolutePath;
62 private static String xmlDataRpcInput;
63 private static String xmlBlockData;
64 private static String xmlTestInterface;
65 private static CompositeNodeWrapper cnSnDataOutput;
66 private static String xmlData3;
67 private static String xmlData4;
69 private static ControllerContext controllerContext;
70 private static BrokerFacade brokerFacade;
71 private static RestconfImpl restconfImpl;
72 private static SchemaContext schemaContextYangsIetf;
73 private static SchemaContext schemaContextTestModule;
74 private static SchemaContext schemaContext;
76 private static MountService mountService;
79 public static void init() throws URISyntaxException, IOException {
80 schemaContextYangsIetf = TestUtils.loadSchemaContext("/full-versions/yangs");
81 schemaContextTestModule = TestUtils.loadSchemaContext("/full-versions/test-module");
82 controllerContext = ControllerContext.getInstance();
83 brokerFacade = mock(BrokerFacade.class);
84 restconfImpl = RestconfImpl.getInstance();
85 restconfImpl.setBroker(brokerFacade);
86 restconfImpl.setControllerContext(controllerContext);
88 Set<Module> modules = TestUtils.loadModulesFrom("/test-config-data/yang1");
89 schemaContext = TestUtils.loadSchemaContext(modules);
95 protected Application configure() {
96 /* enable/disable Jersey logs to console */
97 // enable(TestProperties.LOG_TRAFFIC);
98 // enable(TestProperties.DUMP_ENTITY);
99 // enable(TestProperties.RECORD_LOG_LEVEL);
100 // set(TestProperties.RECORD_LOG_LEVEL, Level.ALL.intValue());
101 ResourceConfig resourceConfig = new ResourceConfig();
102 resourceConfig = resourceConfig.registerInstances(restconfImpl, StructuredDataToXmlProvider.INSTANCE,
103 StructuredDataToJsonProvider.INSTANCE, XmlToCompositeNodeProvider.INSTANCE,
104 JsonToCompositeNodeProvider.INSTANCE);
105 return resourceConfig;
109 public void postOperationsStatusCodes() throws UnsupportedEncodingException {
110 controllerContext.setSchemas(schemaContextTestModule);
111 mockInvokeRpc(cnSnDataOutput, true);
112 String uri = createUri("/operations/", "test-module:rpc-test");
113 assertEquals(200, post(uri, MediaType.APPLICATION_XML, xmlDataRpcInput));
115 mockInvokeRpc(null, true);
116 assertEquals(204, post(uri, MediaType.APPLICATION_XML, xmlDataRpcInput));
118 mockInvokeRpc(null, false);
119 assertEquals(500, post(uri, MediaType.APPLICATION_XML, xmlDataRpcInput));
121 uri = createUri("/operations/", "test-module:rpc-wrongtest");
122 assertEquals(404, post(uri, MediaType.APPLICATION_XML, xmlDataRpcInput));
126 public void postConfigOnlyStatusCodes() throws UnsupportedEncodingException {
127 controllerContext.setSchemas(schemaContextYangsIetf);
128 mockCommitConfigurationDataPostMethod(TransactionStatus.COMMITED);
129 String uri = createUri("/config", "");
130 assertEquals(204, post(uri, MediaType.APPLICATION_XML, xmlDataAbsolutePath));
132 mockCommitConfigurationDataPostMethod(null);
133 assertEquals(202, post(uri, MediaType.APPLICATION_XML, xmlDataAbsolutePath));
135 mockCommitConfigurationDataPostMethod(TransactionStatus.FAILED);
136 assertEquals(500, post(uri, MediaType.APPLICATION_XML, xmlDataAbsolutePath));
140 public void postConfigStatusCodes() throws UnsupportedEncodingException {
141 controllerContext.setSchemas(schemaContextYangsIetf);
142 mockCommitConfigurationDataPostMethod(TransactionStatus.COMMITED);
143 String uri = createUri("/config/", "ietf-interfaces:interfaces");
144 assertEquals(204, post(uri, MediaType.APPLICATION_XML, xmlDataInterfaceAbsolutePath));
146 mockCommitConfigurationDataPostMethod(null);
147 assertEquals(202, post(uri, MediaType.APPLICATION_XML, xmlDataInterfaceAbsolutePath));
149 mockCommitConfigurationDataPostMethod(TransactionStatus.FAILED);
150 assertEquals(500, post(uri, MediaType.APPLICATION_XML, xmlDataInterfaceAbsolutePath));
154 public void postDataViaUrlMountPoint() throws UnsupportedEncodingException {
155 controllerContext.setSchemas(schemaContextYangsIetf);
156 RpcResult<TransactionStatus> rpcResult = new DummyRpcResult.Builder<TransactionStatus>().result(
157 TransactionStatus.COMMITED).build();
158 Future<RpcResult<TransactionStatus>> dummyFuture = DummyFuture.builder().rpcResult(rpcResult).build();
160 brokerFacade.commitConfigurationDataPostBehindMountPoint(any(MountInstance.class),
161 any(InstanceIdentifier.class), any(CompositeNode.class))).thenReturn(dummyFuture);
163 MountInstance mountInstance = mock(MountInstance.class);
164 when(mountInstance.getSchemaContext()).thenReturn(schemaContextTestModule);
165 MountService mockMountService = mock(MountService.class);
166 when(mockMountService.getMountPoint(any(InstanceIdentifier.class))).thenReturn(mountInstance);
168 ControllerContext.getInstance().setMountService(mockMountService);
170 String uri = createUri("/config/", "ietf-interfaces:interfaces/interface/0/");
171 assertEquals(204, post(uri, Draft02.MediaTypes.DATA + XML, xmlData4));
172 uri = createUri("/config/", "ietf-interfaces:interfaces/interface/0/yang-ext:mount/test-module:cont");
173 assertEquals(204, post(uri, Draft02.MediaTypes.DATA + XML, xmlData3));
176 private void mockInvokeRpc(CompositeNode result, boolean sucessful) {
177 RpcResult<CompositeNode> rpcResult = new DummyRpcResult.Builder<CompositeNode>().result(result)
178 .isSuccessful(sucessful).build();
179 when(brokerFacade.invokeRpc(any(QName.class), any(CompositeNode.class))).thenReturn(rpcResult);
182 private void mockCommitConfigurationDataPostMethod(TransactionStatus statusName) {
183 RpcResult<TransactionStatus> rpcResult = new DummyRpcResult.Builder<TransactionStatus>().result(statusName)
185 Future<RpcResult<TransactionStatus>> dummyFuture = null;
186 if (statusName != null) {
187 dummyFuture = DummyFuture.builder().rpcResult(rpcResult).build();
189 dummyFuture = DummyFuture.builder().build();
192 when(brokerFacade.commitConfigurationDataPost(any(InstanceIdentifier.class), any(CompositeNode.class)))
193 .thenReturn(dummyFuture);
197 public void createConfigurationDataTest() throws UnsupportedEncodingException, ParseException {
199 RpcResult<TransactionStatus> rpcResult = new DummyRpcResult.Builder<TransactionStatus>().result(
200 TransactionStatus.COMMITED).build();
201 Future<RpcResult<TransactionStatus>> dummyFuture = DummyFuture.builder().rpcResult(rpcResult).build();
203 when(brokerFacade.commitConfigurationDataPost(any(InstanceIdentifier.class), any(CompositeNode.class)))
204 .thenReturn(dummyFuture);
206 ArgumentCaptor<InstanceIdentifier> instanceIdCaptor = ArgumentCaptor.forClass(InstanceIdentifier.class);
207 ArgumentCaptor<CompositeNode> compNodeCaptor = ArgumentCaptor.forClass(CompositeNode.class);
209 String URI_1 = createUri("/config", "");
210 assertEquals(204, post(URI_1, Draft02.MediaTypes.DATA + XML, xmlTestInterface));
211 verify(brokerFacade).commitConfigurationDataPost(instanceIdCaptor.capture(), compNodeCaptor.capture());
212 String identifier = "[(urn:ietf:params:xml:ns:yang:test-interface?revision=2014-07-01)interfaces]";
213 assertEquals(identifier, instanceIdCaptor.getValue().getPath().toString());
215 String URI_2 = createUri("/config/", "test-interface:interfaces");
216 assertEquals(204, post(URI_2, Draft02.MediaTypes.DATA + XML, xmlBlockData));
217 verify(brokerFacade, times(2))
218 .commitConfigurationDataPost(instanceIdCaptor.capture(), compNodeCaptor.capture());
219 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]";
220 assertEquals(identifier, instanceIdCaptor.getValue().getPath().toString());
224 public void createConfigurationDataNullTest() throws UnsupportedEncodingException {
227 when(brokerFacade.commitConfigurationDataPost(any(InstanceIdentifier.class), any(CompositeNode.class)))
230 String URI_1 = createUri("/config", "");
231 assertEquals(202, post(URI_1, Draft02.MediaTypes.DATA + XML, xmlTestInterface));
233 String URI_2 = createUri("/config/", "test-interface:interfaces");
234 assertEquals(202, post(URI_2, Draft02.MediaTypes.DATA + XML, xmlBlockData));
237 private String createUri(String prefix, String encodedPart) throws UnsupportedEncodingException {
238 return URI.create(prefix + URLEncoder.encode(encodedPart, Charsets.US_ASCII.name()).toString()).toASCIIString();
241 private static void initMocking() {
242 controllerContext = ControllerContext.getInstance();
243 controllerContext.setSchemas(schemaContext);
244 mountService = mock(MountService.class);
245 controllerContext.setMountService(mountService);
246 brokerFacade = mock(BrokerFacade.class);
247 restconfImpl = RestconfImpl.getInstance();
248 restconfImpl.setBroker(brokerFacade);
249 restconfImpl.setControllerContext(controllerContext);
252 private int post(String uri, String mediaType, String data) {
253 return target(uri).request(mediaType).post(Entity.entity(data, mediaType)).getStatus();
256 private static void loadData() throws IOException, URISyntaxException {
257 InputStream xmlStream = RestconfImplTest.class
258 .getResourceAsStream("/parts/ietf-interfaces_interfaces_absolute_path.xml");
259 xmlDataAbsolutePath = TestUtils.getDocumentInPrintableForm(TestUtils.loadDocumentFrom(xmlStream));
260 xmlStream = RestconfImplTest.class
261 .getResourceAsStream("/parts/ietf-interfaces_interfaces_interface_absolute_path.xml");
262 xmlDataInterfaceAbsolutePath = TestUtils.getDocumentInPrintableForm(TestUtils.loadDocumentFrom(xmlStream));
263 String xmlPathRpcInput = RestconfImplTest.class.getResource("/full-versions/test-data2/data-rpc-input.xml")
265 xmlDataRpcInput = TestUtils.loadTextFile(xmlPathRpcInput);
266 String xmlPathBlockData = RestconfImplTest.class.getResource("/test-config-data/xml/block-data.xml").getPath();
267 xmlBlockData = TestUtils.loadTextFile(xmlPathBlockData);
268 String xmlPathTestInterface = RestconfImplTest.class.getResource("/test-config-data/xml/test-interface.xml")
270 xmlTestInterface = TestUtils.loadTextFile(xmlPathTestInterface);
271 cnSnDataOutput = prepareCnSnRpcOutput();
272 String data3Input = RestconfImplTest.class.getResource("/full-versions/test-data2/data3.xml").getPath();
273 xmlData3 = TestUtils.loadTextFile(data3Input);
274 String data4Input = RestconfImplTest.class.getResource("/full-versions/test-data2/data7.xml").getPath();
275 xmlData4 = TestUtils.loadTextFile(data4Input);
278 private static CompositeNodeWrapper prepareCnSnRpcOutput() throws URISyntaxException {
279 CompositeNodeWrapper cnSnDataOutput = new CompositeNodeWrapper(new URI("test:module"), "output");
280 CompositeNodeWrapper cont = new CompositeNodeWrapper(new URI("test:module"), "cont-output");
281 cnSnDataOutput.addValue(cont);
282 cnSnDataOutput.unwrap();
283 return cnSnDataOutput;