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