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