Merge "Fix end of TLV in LLDP packet"
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / test / java / org / opendaylight / controller / sal / restconf / impl / test / RestPostOperationTest.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.sal.restconf.impl.test;
9
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
18 import java.io.IOException;
19 import java.io.InputStream;
20 import java.io.UnsupportedEncodingException;
21 import java.net.URI;
22 import java.net.URISyntaxException;
23 import java.net.URLEncoder;
24 import java.text.ParseException;
25 import java.util.Set;
26 import java.util.concurrent.Future;
27
28 import javax.ws.rs.client.Entity;
29 import javax.ws.rs.core.Application;
30 import javax.ws.rs.core.MediaType;
31
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;
55
56 import com.google.common.base.Charsets;
57
58 public class RestPostOperationTest extends JerseyTest {
59
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;
68
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;
75
76     private static MountService mountService;
77
78     @BeforeClass
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);
87
88         Set<Module> modules = TestUtils.loadModulesFrom("/test-config-data/yang1");
89         schemaContext = TestUtils.loadSchemaContext(modules);
90
91         loadData();
92     }
93
94     @Override
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;
106     }
107
108     @Test
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));
114
115         mockInvokeRpc(null, true);
116         assertEquals(204, post(uri, MediaType.APPLICATION_XML, xmlDataRpcInput));
117
118         mockInvokeRpc(null, false);
119         assertEquals(500, post(uri, MediaType.APPLICATION_XML, xmlDataRpcInput));
120
121         uri = createUri("/operations/", "test-module:rpc-wrongtest");
122         assertEquals(404, post(uri, MediaType.APPLICATION_XML, xmlDataRpcInput));
123     }
124
125     @Test
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));
131
132         mockCommitConfigurationDataPostMethod(null);
133         assertEquals(202, post(uri, MediaType.APPLICATION_XML, xmlDataAbsolutePath));
134
135         mockCommitConfigurationDataPostMethod(TransactionStatus.FAILED);
136         assertEquals(500, post(uri, MediaType.APPLICATION_XML, xmlDataAbsolutePath));
137     }
138
139     @Test
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));
145
146         mockCommitConfigurationDataPostMethod(null);
147         assertEquals(202, post(uri, MediaType.APPLICATION_XML, xmlDataInterfaceAbsolutePath));
148
149         mockCommitConfigurationDataPostMethod(TransactionStatus.FAILED);
150         assertEquals(500, post(uri, MediaType.APPLICATION_XML, xmlDataInterfaceAbsolutePath));
151     }
152
153     @Test
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();
159         when(
160                 brokerFacade.commitConfigurationDataPostBehindMountPoint(any(MountInstance.class),
161                         any(InstanceIdentifier.class), any(CompositeNode.class))).thenReturn(dummyFuture);
162
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);
167
168         ControllerContext.getInstance().setMountService(mockMountService);
169
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));
174     }
175
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);
180     }
181
182     private void mockCommitConfigurationDataPostMethod(TransactionStatus statusName) {
183         RpcResult<TransactionStatus> rpcResult = new DummyRpcResult.Builder<TransactionStatus>().result(statusName)
184                 .build();
185         Future<RpcResult<TransactionStatus>> dummyFuture = null;
186         if (statusName != null) {
187             dummyFuture = DummyFuture.builder().rpcResult(rpcResult).build();
188         } else {
189             dummyFuture = DummyFuture.builder().build();
190         }
191
192         when(brokerFacade.commitConfigurationDataPost(any(InstanceIdentifier.class), any(CompositeNode.class)))
193                 .thenReturn(dummyFuture);
194     }
195
196     @Test
197     public void createConfigurationDataTest() throws UnsupportedEncodingException, ParseException {
198         initMocking();
199         RpcResult<TransactionStatus> rpcResult = new DummyRpcResult.Builder<TransactionStatus>().result(
200                 TransactionStatus.COMMITED).build();
201         Future<RpcResult<TransactionStatus>> dummyFuture = DummyFuture.builder().rpcResult(rpcResult).build();
202
203         when(brokerFacade.commitConfigurationDataPost(any(InstanceIdentifier.class), any(CompositeNode.class)))
204                 .thenReturn(dummyFuture);
205
206         ArgumentCaptor<InstanceIdentifier> instanceIdCaptor = ArgumentCaptor.forClass(InstanceIdentifier.class);
207         ArgumentCaptor<CompositeNode> compNodeCaptor = ArgumentCaptor.forClass(CompositeNode.class);
208
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());
214
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());
221     }
222
223     @Test
224     public void createConfigurationDataNullTest() throws UnsupportedEncodingException {
225         initMocking();
226
227         when(brokerFacade.commitConfigurationDataPost(any(InstanceIdentifier.class), any(CompositeNode.class)))
228                 .thenReturn(null);
229
230         String URI_1 = createUri("/config", "");
231         assertEquals(202, post(URI_1, Draft02.MediaTypes.DATA + XML, xmlTestInterface));
232
233         String URI_2 = createUri("/config/", "test-interface:interfaces");
234         assertEquals(202, post(URI_2, Draft02.MediaTypes.DATA + XML, xmlBlockData));
235     }
236
237     private String createUri(String prefix, String encodedPart) throws UnsupportedEncodingException {
238         return URI.create(prefix + URLEncoder.encode(encodedPart, Charsets.US_ASCII.name()).toString()).toASCIIString();
239     }
240
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);
250     }
251
252     private int post(String uri, String mediaType, String data) {
253         return target(uri).request(mediaType).post(Entity.entity(data, mediaType)).getStatus();
254     }
255
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")
264                 .getPath();
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")
269                 .getPath();
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);
276     }
277
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;
284     }
285 }