1 package org.opendaylight.controller.sal.restconf.impl.test;
3 import static org.junit.Assert.assertEquals;
4 import static org.mockito.Matchers.any;
5 import static org.mockito.Mockito.mock;
6 import static org.mockito.Mockito.times;
7 import static org.mockito.Mockito.verify;
8 import static org.mockito.Mockito.when;
9 import static org.opendaylight.controller.sal.restconf.impl.test.RestOperationUtils.XML;
10 import static org.opendaylight.controller.sal.restconf.impl.test.RestOperationUtils.createUri;
12 import java.io.IOException;
13 import java.io.InputStream;
14 import java.io.UnsupportedEncodingException;
16 import java.net.URISyntaxException;
17 import java.net.URLEncoder;
18 import java.text.ParseException;
20 import java.util.concurrent.Future;
22 import javax.ws.rs.client.Entity;
23 import javax.ws.rs.core.Application;
24 import javax.ws.rs.core.MediaType;
25 import javax.ws.rs.core.Response;
27 import org.glassfish.jersey.server.ResourceConfig;
28 import org.glassfish.jersey.test.JerseyTest;
29 import org.junit.BeforeClass;
30 import org.junit.Test;
31 import org.mockito.ArgumentCaptor;
32 import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
33 import org.opendaylight.controller.sal.core.api.mount.MountInstance;
34 import org.opendaylight.controller.sal.core.api.mount.MountService;
35 import org.opendaylight.controller.sal.rest.api.Draft02;
36 import org.opendaylight.controller.sal.rest.impl.JsonToCompositeNodeProvider;
37 import org.opendaylight.controller.sal.rest.impl.StructuredDataToJsonProvider;
38 import org.opendaylight.controller.sal.rest.impl.StructuredDataToXmlProvider;
39 import org.opendaylight.controller.sal.rest.impl.XmlMapper;
40 import org.opendaylight.controller.sal.rest.impl.XmlToCompositeNodeProvider;
41 import org.opendaylight.controller.sal.restconf.impl.BrokerFacade;
42 import org.opendaylight.controller.sal.restconf.impl.CompositeNodeWrapper;
43 import org.opendaylight.controller.sal.restconf.impl.ControllerContext;
44 import org.opendaylight.controller.sal.restconf.impl.RestconfImpl;
45 import org.opendaylight.yangtools.yang.common.QName;
46 import org.opendaylight.yangtools.yang.common.RpcResult;
47 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
48 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
49 import org.opendaylight.yangtools.yang.model.api.Module;
50 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
52 import com.google.common.base.Charsets;
54 public class RestPostOperationTest extends JerseyTest {
56 private static String xmlDataAbsolutePath;
57 private static String xmlDataInterfaceAbsolutePath;
58 private static String xmlDataRpcInput;
59 private static String xmlBlockData;
60 private static String xmlTestInterface;
61 private static CompositeNodeWrapper cnSnDataOutput;
62 private static String xmlData3;
63 private static String xmlData4;
65 private static ControllerContext controllerContext;
66 private static BrokerFacade brokerFacade;
67 private static RestconfImpl restconfImpl;
68 private static SchemaContext schemaContextYangsIetf;
69 private static SchemaContext schemaContextTestModule;
70 private static SchemaContext schemaContext;
72 private static MountService mountService;
75 public static void init() throws URISyntaxException, IOException {
76 schemaContextYangsIetf = TestUtils.loadSchemaContext("/full-versions/yangs");
77 schemaContextTestModule = TestUtils.loadSchemaContext("/full-versions/test-module");
78 controllerContext = ControllerContext.getInstance();
79 brokerFacade = mock(BrokerFacade.class);
80 restconfImpl = RestconfImpl.getInstance();
81 restconfImpl.setBroker(brokerFacade);
82 restconfImpl.setControllerContext(controllerContext);
84 Set<Module> modules = TestUtils.loadModulesFrom("/test-config-data/yang1");
85 schemaContext = TestUtils.loadSchemaContext(modules);
91 protected Application configure() {
92 /* enable/disable Jersey logs to console */
93 // enable(TestProperties.LOG_TRAFFIC);
94 // enable(TestProperties.DUMP_ENTITY);
95 // enable(TestProperties.RECORD_LOG_LEVEL);
96 // set(TestProperties.RECORD_LOG_LEVEL, Level.ALL.intValue());
97 ResourceConfig resourceConfig = new ResourceConfig();
98 resourceConfig = resourceConfig.registerInstances(restconfImpl, StructuredDataToXmlProvider.INSTANCE,
99 StructuredDataToJsonProvider.INSTANCE, XmlToCompositeNodeProvider.INSTANCE,
100 JsonToCompositeNodeProvider.INSTANCE);
101 return resourceConfig;
105 public void postOperationsStatusCodes() throws UnsupportedEncodingException {
106 controllerContext.setSchemas(schemaContextTestModule);
107 mockInvokeRpc(cnSnDataOutput, true);
108 String uri = createUri("/operations/", "test-module:rpc-test");
109 assertEquals(200, post(uri, MediaType.APPLICATION_XML, xmlDataRpcInput));
111 mockInvokeRpc(null, true);
112 assertEquals(204, post(uri, MediaType.APPLICATION_XML, xmlDataRpcInput));
114 mockInvokeRpc(null, false);
115 assertEquals(500, post(uri, MediaType.APPLICATION_XML, xmlDataRpcInput));
117 uri = createUri("/operations/", "test-module:rpc-wrongtest");
118 assertEquals(404, post(uri, MediaType.APPLICATION_XML, xmlDataRpcInput));
122 public void postConfigOnlyStatusCodes() throws UnsupportedEncodingException {
123 controllerContext.setSchemas(schemaContextYangsIetf);
124 mockCommitConfigurationDataPostMethod(TransactionStatus.COMMITED);
125 String uri = createUri("/config", "");
126 assertEquals(204, post(uri, MediaType.APPLICATION_XML, xmlDataAbsolutePath));
128 mockCommitConfigurationDataPostMethod(null);
129 assertEquals(202, post(uri, MediaType.APPLICATION_XML, xmlDataAbsolutePath));
131 mockCommitConfigurationDataPostMethod(TransactionStatus.FAILED);
132 assertEquals(500, post(uri, MediaType.APPLICATION_XML, xmlDataAbsolutePath));
136 public void postConfigStatusCodes() throws UnsupportedEncodingException {
137 controllerContext.setSchemas(schemaContextYangsIetf);
138 mockCommitConfigurationDataPostMethod(TransactionStatus.COMMITED);
139 String uri = createUri("/config/", "ietf-interfaces:interfaces");
140 assertEquals(204, post(uri, MediaType.APPLICATION_XML, xmlDataInterfaceAbsolutePath));
142 mockCommitConfigurationDataPostMethod(null);
143 assertEquals(202, post(uri, MediaType.APPLICATION_XML, xmlDataInterfaceAbsolutePath));
145 mockCommitConfigurationDataPostMethod(TransactionStatus.FAILED);
146 assertEquals(500, post(uri, MediaType.APPLICATION_XML, xmlDataInterfaceAbsolutePath));
150 public void postDatastoreStatusCodes() throws UnsupportedEncodingException {
151 controllerContext.setSchemas(schemaContextYangsIetf);
152 mockCommitConfigurationDataPostMethod(TransactionStatus.COMMITED);
153 String uri = createUri("/datastore/", "ietf-interfaces:interfaces");
154 assertEquals(204, post(uri, MediaType.APPLICATION_XML, xmlDataInterfaceAbsolutePath));
156 mockCommitConfigurationDataPostMethod(null);
157 assertEquals(202, post(uri, MediaType.APPLICATION_XML, xmlDataInterfaceAbsolutePath));
159 mockCommitConfigurationDataPostMethod(TransactionStatus.FAILED);
160 assertEquals(500, post(uri, MediaType.APPLICATION_XML, xmlDataInterfaceAbsolutePath));
164 public void postDataViaUrlMountPoint() throws UnsupportedEncodingException {
165 controllerContext.setSchemas(schemaContextYangsIetf);
166 RpcResult<TransactionStatus> rpcResult = new DummyRpcResult.Builder<TransactionStatus>().result(
167 TransactionStatus.COMMITED).build();
168 Future<RpcResult<TransactionStatus>> dummyFuture = DummyFuture.builder().rpcResult(rpcResult).build();
170 brokerFacade.commitConfigurationDataPostBehindMountPoint(any(MountInstance.class),
171 any(InstanceIdentifier.class), any(CompositeNode.class))).thenReturn(dummyFuture);
173 MountInstance mountInstance = mock(MountInstance.class);
174 when(mountInstance.getSchemaContext()).thenReturn(schemaContextTestModule);
175 MountService mockMountService = mock(MountService.class);
176 when(mockMountService.getMountPoint(any(InstanceIdentifier.class))).thenReturn(mountInstance);
178 ControllerContext.getInstance().setMountService(mockMountService);
180 String uri = createUri("/config/", "ietf-interfaces:interfaces/interface/0/");
181 assertEquals(204, post(uri, Draft02.MediaTypes.DATA + XML, xmlData4));
182 uri = createUri("/config/", "ietf-interfaces:interfaces/interface/0/yang-ext:mount/test-module:cont");
183 assertEquals(204, post(uri, Draft02.MediaTypes.DATA + XML, xmlData3));
186 private void mockInvokeRpc(CompositeNode result, boolean sucessful) {
187 RpcResult<CompositeNode> rpcResult = new DummyRpcResult.Builder<CompositeNode>().result(result)
188 .isSuccessful(sucessful).build();
189 when(brokerFacade.invokeRpc(any(QName.class), any(CompositeNode.class))).thenReturn(rpcResult);
192 private void mockCommitConfigurationDataPostMethod(TransactionStatus statusName) {
193 RpcResult<TransactionStatus> rpcResult = new DummyRpcResult.Builder<TransactionStatus>().result(statusName)
195 Future<RpcResult<TransactionStatus>> dummyFuture = null;
196 if (statusName != null) {
197 dummyFuture = DummyFuture.builder().rpcResult(rpcResult).build();
199 dummyFuture = DummyFuture.builder().build();
202 when(brokerFacade.commitConfigurationDataPost(any(InstanceIdentifier.class), any(CompositeNode.class)))
203 .thenReturn(dummyFuture);
207 public void createConfigurationDataTest() throws UnsupportedEncodingException, ParseException {
209 RpcResult<TransactionStatus> rpcResult = new DummyRpcResult.Builder<TransactionStatus>().result(
210 TransactionStatus.COMMITED).build();
211 Future<RpcResult<TransactionStatus>> dummyFuture = DummyFuture.builder().rpcResult(rpcResult).build();
213 when(brokerFacade.commitConfigurationDataPost(any(InstanceIdentifier.class), any(CompositeNode.class)))
214 .thenReturn(dummyFuture);
216 ArgumentCaptor<InstanceIdentifier> instanceIdCaptor = ArgumentCaptor.forClass(InstanceIdentifier.class);
217 ArgumentCaptor<CompositeNode> compNodeCaptor = ArgumentCaptor.forClass(CompositeNode.class);
219 String URI_1 = createUri("/config", "");
220 assertEquals(204, post(URI_1, Draft02.MediaTypes.DATA + XML, xmlTestInterface));
221 verify(brokerFacade).commitConfigurationDataPost(instanceIdCaptor.capture(), compNodeCaptor.capture());
222 String identifier = "[(urn:ietf:params:xml:ns:yang:test-interface?revision=2014-07-01)interfaces]";
223 assertEquals(identifier, instanceIdCaptor.getValue().getPath().toString());
225 String URI_2 = createUri("/config/", "test-interface:interfaces");
226 assertEquals(204, post(URI_2, Draft02.MediaTypes.DATA + XML, xmlBlockData));
227 verify(brokerFacade, times(2))
228 .commitConfigurationDataPost(instanceIdCaptor.capture(), compNodeCaptor.capture());
229 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]";
230 assertEquals(identifier, instanceIdCaptor.getValue().getPath().toString());
234 public void createConfigurationDataNullTest() throws UnsupportedEncodingException {
237 when(brokerFacade.commitConfigurationDataPost(any(InstanceIdentifier.class), any(CompositeNode.class)))
240 String URI_1 = createUri("/config", "");
241 assertEquals(202, post(URI_1, Draft02.MediaTypes.DATA + XML, xmlTestInterface));
243 String URI_2 = createUri("/config/", "test-interface:interfaces");
244 assertEquals(202, post(URI_2, Draft02.MediaTypes.DATA + XML, xmlBlockData));
247 private String createUri(String prefix, String encodedPart) throws UnsupportedEncodingException {
248 return URI.create(prefix + URLEncoder.encode(encodedPart, Charsets.US_ASCII.name()).toString()).toASCIIString();
251 private static void initMocking() {
252 controllerContext = ControllerContext.getInstance();
253 controllerContext.setSchemas(schemaContext);
254 mountService = mock(MountService.class);
255 controllerContext.setMountService(mountService);
256 brokerFacade = mock(BrokerFacade.class);
257 restconfImpl = RestconfImpl.getInstance();
258 restconfImpl.setBroker(brokerFacade);
259 restconfImpl.setControllerContext(controllerContext);
262 private int post(String uri, String mediaType, String data) {
263 return target(uri).request(mediaType).post(Entity.entity(data, mediaType)).getStatus();
266 private static void loadData() throws IOException, URISyntaxException {
267 InputStream xmlStream = RestconfImplTest.class
268 .getResourceAsStream("/parts/ietf-interfaces_interfaces_absolute_path.xml");
269 xmlDataAbsolutePath = TestUtils.getDocumentInPrintableForm(TestUtils.loadDocumentFrom(xmlStream));
270 xmlStream = RestconfImplTest.class
271 .getResourceAsStream("/parts/ietf-interfaces_interfaces_interface_absolute_path.xml");
272 xmlDataInterfaceAbsolutePath = TestUtils.getDocumentInPrintableForm(TestUtils.loadDocumentFrom(xmlStream));
273 String xmlPathRpcInput = RestconfImplTest.class.getResource("/full-versions/test-data2/data-rpc-input.xml")
275 xmlDataRpcInput = TestUtils.loadTextFile(xmlPathRpcInput);
276 String xmlPathBlockData = RestconfImplTest.class.getResource("/test-config-data/xml/block-data.xml").getPath();
277 xmlBlockData = TestUtils.loadTextFile(xmlPathBlockData);
278 String xmlPathTestInterface = RestconfImplTest.class.getResource("/test-config-data/xml/test-interface.xml")
280 xmlTestInterface = TestUtils.loadTextFile(xmlPathTestInterface);
281 cnSnDataOutput = prepareCnSnRpcOutput();
282 String data3Input = RestconfImplTest.class.getResource("/full-versions/test-data2/data3.xml").getPath();
283 xmlData3 = TestUtils.loadTextFile(data3Input);
284 String data4Input = RestconfImplTest.class.getResource("/full-versions/test-data2/data7.xml").getPath();
285 xmlData4 = TestUtils.loadTextFile(data4Input);
288 private static CompositeNodeWrapper prepareCnSnRpcOutput() throws URISyntaxException {
289 CompositeNodeWrapper cnSnDataOutput = new CompositeNodeWrapper(new URI("test:module"), "output");
290 CompositeNodeWrapper cont = new CompositeNodeWrapper(new URI("test:module"), "cont-output");
291 cnSnDataOutput.addValue(cont);
292 cnSnDataOutput.unwrap();
293 return cnSnDataOutput;