Bug 6947 / Bug 6948 - implement point and insert query params
[netconf.git] / restconf / 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 import com.google.common.base.Optional;
18 import com.google.common.collect.ImmutableList;
19 import com.google.common.util.concurrent.CheckedFuture;
20 import com.google.common.util.concurrent.Futures;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.io.UnsupportedEncodingException;
24 import java.net.URISyntaxException;
25 import java.text.ParseException;
26 import java.util.Set;
27 import javax.ws.rs.client.Entity;
28 import javax.ws.rs.core.Application;
29 import javax.ws.rs.core.MediaType;
30 import org.glassfish.jersey.server.ResourceConfig;
31 import org.glassfish.jersey.test.JerseyTest;
32 import org.junit.BeforeClass;
33 import org.junit.Ignore;
34 import org.junit.Test;
35 import org.mockito.ArgumentCaptor;
36 import org.mockito.Mockito;
37 import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
38 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
39 import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
40 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
41 import org.opendaylight.netconf.sal.rest.api.Draft02;
42 import org.opendaylight.netconf.sal.rest.impl.JsonNormalizedNodeBodyReader;
43 import org.opendaylight.netconf.sal.rest.impl.NormalizedNodeJsonBodyWriter;
44 import org.opendaylight.netconf.sal.rest.impl.NormalizedNodeXmlBodyWriter;
45 import org.opendaylight.netconf.sal.rest.impl.RestconfDocumentedExceptionMapper;
46 import org.opendaylight.netconf.sal.rest.impl.XmlNormalizedNodeBodyReader;
47 import org.opendaylight.netconf.sal.restconf.impl.BrokerFacade;
48 import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
49 import org.opendaylight.netconf.sal.restconf.impl.RestconfImpl;
50 import org.opendaylight.yangtools.yang.common.RpcResult;
51 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
52 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
53 import org.opendaylight.yangtools.yang.model.api.Module;
54 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
55 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
56
57 public class RestPostOperationTest extends JerseyTest {
58
59     private static String xmlDataAbsolutePath;
60     private static String xmlDataInterfaceAbsolutePath;
61     private static String xmlDataRpcInput;
62     private static String xmlBlockData;
63     private static String xmlTestInterface;
64     private static String xmlData3;
65     private static String xmlData4;
66
67     private static BrokerFacade brokerFacade;
68     private static RestconfImpl restconfImpl;
69     private static SchemaContext schemaContextYangsIetf;
70     private static SchemaContext schemaContextTestModule;
71     private static SchemaContext schemaContext;
72
73     private static DOMMountPointService mountService;
74
75     @BeforeClass
76     public static void init() throws URISyntaxException, IOException, ReactorException {
77         schemaContextYangsIetf = TestUtils.loadSchemaContext("/full-versions/yangs");
78         schemaContextTestModule = TestUtils.loadSchemaContext("/full-versions/test-module");
79         brokerFacade = mock(BrokerFacade.class);
80         restconfImpl = RestconfImpl.getInstance();
81         restconfImpl.setBroker(brokerFacade);
82
83         schemaContext = TestUtils.loadSchemaContext("/test-config-data/yang1");
84         final Set<Module> modules = schemaContext.getModules();
85
86         loadData();
87     }
88
89     @Override
90     protected Application configure() {
91         /* enable/disable Jersey logs to console */
92         // enable(TestProperties.LOG_TRAFFIC);
93         // enable(TestProperties.DUMP_ENTITY);
94         // enable(TestProperties.RECORD_LOG_LEVEL);
95         // set(TestProperties.RECORD_LOG_LEVEL, Level.ALL.intValue());
96         ResourceConfig resourceConfig = new ResourceConfig();
97         resourceConfig = resourceConfig.registerInstances(restconfImpl, new XmlNormalizedNodeBodyReader(),
98                 new NormalizedNodeXmlBodyWriter(), new JsonNormalizedNodeBodyReader(), new NormalizedNodeJsonBodyWriter());
99         resourceConfig.registerClasses(RestconfDocumentedExceptionMapper.class);
100         return resourceConfig;
101     }
102
103     private void setSchemaControllerContext(final SchemaContext schema) {
104         final ControllerContext context = ControllerContext.getInstance();
105         context.setSchemas(schema);
106         restconfImpl.setControllerContext(context);
107     }
108
109     @SuppressWarnings("unchecked")
110     @Test
111     @Ignore /// xmlData* need netconf-yang
112     public void postDataViaUrlMountPoint() throws UnsupportedEncodingException {
113         setSchemaControllerContext(schemaContextYangsIetf);
114         when(
115                 brokerFacade.commitConfigurationDataPost(any(DOMMountPoint.class), any(YangInstanceIdentifier.class),
116                         any(NormalizedNode.class), null, null)).thenReturn(mock(CheckedFuture.class));
117
118         final DOMMountPoint mountInstance = mock(DOMMountPoint.class);
119         when(mountInstance.getSchemaContext()).thenReturn(schemaContextTestModule);
120         final DOMMountPointService mockMountService = mock(DOMMountPointService.class);
121         when(mockMountService.getMountPoint(any(YangInstanceIdentifier.class))).thenReturn(Optional.of(mountInstance));
122
123         ControllerContext.getInstance().setMountService(mockMountService);
124
125         String uri = "/config/ietf-interfaces:interfaces/interface/0/";
126         assertEquals(204, post(uri, Draft02.MediaTypes.DATA + XML, xmlData4));
127         uri = "/config/ietf-interfaces:interfaces/interface/0/yang-ext:mount/test-module:cont";
128         assertEquals(204, post(uri, Draft02.MediaTypes.DATA + XML, xmlData3));
129
130         assertEquals(400, post(uri, MediaType.APPLICATION_JSON, ""));
131     }
132
133     @SuppressWarnings("unchecked")
134     @Test
135     @Ignore //jenkins has problem with JerseyTest - we expecting problems with singletons ControllerContext as schemaContext holder
136     public void createConfigurationDataTest() throws UnsupportedEncodingException, ParseException {
137         initMocking();
138         final RpcResult<TransactionStatus> rpcResult = new DummyRpcResult.Builder<TransactionStatus>().result(
139                 TransactionStatus.COMMITED).build();
140
141         when(brokerFacade.commitConfigurationDataPost((SchemaContext) null, any(YangInstanceIdentifier.class),
142                 any(NormalizedNode.class), null, null))
143                 .thenReturn(mock(CheckedFuture.class));
144
145         final ArgumentCaptor<YangInstanceIdentifier> instanceIdCaptor = ArgumentCaptor.forClass(YangInstanceIdentifier.class);
146         @SuppressWarnings("rawtypes")
147         final ArgumentCaptor<NormalizedNode> compNodeCaptor = ArgumentCaptor.forClass(NormalizedNode.class);
148
149
150         // FIXME : identify who is set the schemaContext
151 //        final String URI_1 = "/config";
152 //        assertEquals(204, post(URI_1, Draft02.MediaTypes.DATA + XML, xmlTestInterface));
153 //        verify(brokerFacade).commitConfigurationDataPost(instanceIdCaptor.capture(), compNodeCaptor.capture());
154         final String identifier = "[(urn:ietf:params:xml:ns:yang:test-interface?revision=2014-07-01)interfaces]";
155 //        assertEquals(identifier, ImmutableList.copyOf(instanceIdCaptor.getValue().getPathArguments()).toString());
156
157         final String URI_2 = "/config/test-interface:interfaces";
158         assertEquals(204, post(URI_2, Draft02.MediaTypes.DATA + XML, xmlBlockData));
159         // FIXME : NEVER test a nr. of call some service in complex test suite
160 //        verify(brokerFacade, times(2))
161         verify(brokerFacade, times(1))
162                 .commitConfigurationDataPost((SchemaContext) null, instanceIdCaptor.capture(), compNodeCaptor.capture(),
163                         null, null);
164 //        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]";
165         assertEquals(identifier, ImmutableList.copyOf(instanceIdCaptor.getValue().getPathArguments()).toString());
166     }
167
168     @Test
169     public void createConfigurationDataNullTest() throws UnsupportedEncodingException {
170         initMocking();
171
172         when(brokerFacade.commitConfigurationDataPost(any(SchemaContext.class), any(YangInstanceIdentifier.class),
173                 any(NormalizedNode.class), Mockito.anyString(), Mockito.anyString()))
174                 .thenReturn(Futures.<Void, TransactionCommitFailedException>immediateCheckedFuture(null));
175
176         //FIXME : find who is set schemaContext
177 //        final String URI_1 = "/config";
178 //        assertEquals(204, post(URI_1, Draft02.MediaTypes.DATA + XML, xmlTestInterface));
179
180         final String URI_2 = "/config/test-interface:interfaces";
181         assertEquals(204, post(URI_2, Draft02.MediaTypes.DATA + XML, xmlBlockData));
182     }
183
184     private static void initMocking() {
185         final ControllerContext controllerContext = ControllerContext.getInstance();
186         controllerContext.setSchemas(schemaContext);
187         mountService = mock(DOMMountPointService.class);
188         controllerContext.setMountService(mountService);
189         brokerFacade = mock(BrokerFacade.class);
190         restconfImpl = RestconfImpl.getInstance();
191         restconfImpl.setBroker(brokerFacade);
192         restconfImpl.setControllerContext(controllerContext);
193     }
194
195     private int post(final String uri, final String mediaType, final String data) {
196         return target(uri).request(mediaType).post(Entity.entity(data, mediaType)).getStatus();
197     }
198
199     private static void loadData() throws IOException, URISyntaxException {
200         InputStream xmlStream = RestconfImplTest.class
201                 .getResourceAsStream("/parts/ietf-interfaces_interfaces_absolute_path.xml");
202         xmlDataAbsolutePath = TestUtils.getDocumentInPrintableForm(TestUtils.loadDocumentFrom(xmlStream));
203         xmlStream = RestconfImplTest.class
204                 .getResourceAsStream("/parts/ietf-interfaces_interfaces_interface_absolute_path.xml");
205         xmlDataInterfaceAbsolutePath = TestUtils.getDocumentInPrintableForm(TestUtils.loadDocumentFrom(xmlStream));
206         final String xmlPathRpcInput = RestconfImplTest.class.getResource("/full-versions/test-data2/data-rpc-input.xml")
207                 .getPath();
208         xmlDataRpcInput = TestUtils.loadTextFile(xmlPathRpcInput);
209         final String xmlPathBlockData = RestconfImplTest.class.getResource("/test-config-data/xml/block-data.xml").getPath();
210         xmlBlockData = TestUtils.loadTextFile(xmlPathBlockData);
211         final String xmlPathTestInterface = RestconfImplTest.class.getResource("/test-config-data/xml/test-interface.xml")
212                 .getPath();
213         xmlTestInterface = TestUtils.loadTextFile(xmlPathTestInterface);
214 //        cnSnDataOutput = prepareCnSnRpcOutput();
215         final String data3Input = RestconfImplTest.class.getResource("/full-versions/test-data2/data3.xml").getPath();
216         xmlData3 = TestUtils.loadTextFile(data3Input);
217         final String data4Input = RestconfImplTest.class.getResource("/full-versions/test-data2/data7.xml").getPath();
218         xmlData4 = TestUtils.loadTextFile(data4Input);
219     }
220
221 }