Mass conversion to static methods
[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 - we expecting problems with singletons ControllerContext as schemaContext holder
137     public void createConfigurationDataTest() throws UnsupportedEncodingException, ParseException {
138         initMocking();
139         final RpcResult<TransactionStatus> rpcResult = new DummyRpcResult.Builder<TransactionStatus>().result(
140                 TransactionStatus.COMMITED).build();
141
142         when(brokerFacade.commitConfigurationDataPost((SchemaContext) null, any(YangInstanceIdentifier.class),
143                 any(NormalizedNode.class), null, null))
144                 .thenReturn(mock(CheckedFuture.class));
145
146         final ArgumentCaptor<YangInstanceIdentifier> instanceIdCaptor = ArgumentCaptor.forClass(YangInstanceIdentifier.class);
147         @SuppressWarnings("rawtypes")
148         final ArgumentCaptor<NormalizedNode> compNodeCaptor = ArgumentCaptor.forClass(NormalizedNode.class);
149
150
151         // FIXME : identify who is set the schemaContext
152 //        final String URI_1 = "/config";
153 //        assertEquals(204, post(URI_1, Draft02.MediaTypes.DATA + XML, xmlTestInterface));
154 //        verify(brokerFacade).commitConfigurationDataPost(instanceIdCaptor.capture(), compNodeCaptor.capture());
155         final String identifier = "[(urn:ietf:params:xml:ns:yang:test-interface?revision=2014-07-01)interfaces]";
156 //        assertEquals(identifier, ImmutableList.copyOf(instanceIdCaptor.getValue().getPathArguments()).toString());
157
158         final String URI_2 = "/config/test-interface:interfaces";
159         assertEquals(204, post(URI_2, Draft02.MediaTypes.DATA + XML, xmlBlockData));
160         // FIXME : NEVER test a nr. of call some service in complex test suite
161 //        verify(brokerFacade, times(2))
162         verify(brokerFacade, times(1))
163                 .commitConfigurationDataPost((SchemaContext) null, instanceIdCaptor.capture(), compNodeCaptor.capture(),
164                         null, null);
165 //        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]";
166         assertEquals(identifier, ImmutableList.copyOf(instanceIdCaptor.getValue().getPathArguments()).toString());
167     }
168
169     @Test
170     public void createConfigurationDataNullTest() throws UnsupportedEncodingException {
171         initMocking();
172
173         when(brokerFacade.commitConfigurationDataPost(any(SchemaContext.class), any(YangInstanceIdentifier.class),
174                 any(NormalizedNode.class), Mockito.anyString(), Mockito.anyString()))
175                 .thenReturn(Futures.<Void, TransactionCommitFailedException>immediateCheckedFuture(null));
176
177         //FIXME : find who is set schemaContext
178 //        final String URI_1 = "/config";
179 //        assertEquals(204, post(URI_1, Draft02.MediaTypes.DATA + XML, xmlTestInterface));
180
181         final String URI_2 = "/config/test-interface:interfaces";
182         assertEquals(204, post(URI_2, Draft02.MediaTypes.DATA + XML, xmlBlockData));
183     }
184
185     private static void initMocking() {
186         final ControllerContext controllerContext = ControllerContext.getInstance();
187         controllerContext.setSchemas(schemaContext);
188         mountService = mock(DOMMountPointService.class);
189         controllerContext.setMountService(mountService);
190         brokerFacade = mock(BrokerFacade.class);
191         restconfImpl = RestconfImpl.getInstance();
192         restconfImpl.setBroker(brokerFacade);
193         restconfImpl.setControllerContext(controllerContext);
194     }
195
196     private int post(final String uri, final String mediaType, final String data) {
197         return target(uri).request(mediaType).post(Entity.entity(data, mediaType)).getStatus();
198     }
199
200     private static void loadData() throws IOException, URISyntaxException {
201         InputStream xmlStream = RestconfImplTest.class
202                 .getResourceAsStream("/parts/ietf-interfaces_interfaces_absolute_path.xml");
203         xmlDataAbsolutePath = TestUtils.getDocumentInPrintableForm(TestUtils.loadDocumentFrom(xmlStream));
204         xmlStream = RestconfImplTest.class
205                 .getResourceAsStream("/parts/ietf-interfaces_interfaces_interface_absolute_path.xml");
206         xmlDataInterfaceAbsolutePath = TestUtils.getDocumentInPrintableForm(TestUtils.loadDocumentFrom(xmlStream));
207         final String xmlPathRpcInput = RestconfImplTest.class.getResource("/full-versions/test-data2/data-rpc-input.xml")
208                 .getPath();
209         xmlDataRpcInput = TestUtils.loadTextFile(xmlPathRpcInput);
210         final String xmlPathBlockData = RestconfImplTest.class.getResource("/test-config-data/xml/block-data.xml").getPath();
211         xmlBlockData = TestUtils.loadTextFile(xmlPathBlockData);
212         final String xmlPathTestInterface = RestconfImplTest.class.getResource("/test-config-data/xml/test-interface.xml")
213                 .getPath();
214         xmlTestInterface = TestUtils.loadTextFile(xmlPathTestInterface);
215 //        cnSnDataOutput = prepareCnSnRpcOutput();
216         final String data3Input = RestconfImplTest.class.getResource("/full-versions/test-data2/data3.xml").getPath();
217         xmlData3 = TestUtils.loadTextFile(data3Input);
218         final String data4Input = RestconfImplTest.class.getResource("/full-versions/test-data2/data7.xml").getPath();
219         xmlData4 = TestUtils.loadTextFile(data4Input);
220     }
221
222 }