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

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.