2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.sal.restconf.impl.test;
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;
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;
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;
58 public class RestPostOperationTest extends JerseyTest {
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;
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;
74 private static DOMMountPointService mountService;
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);
84 schemaContext = TestUtils.loadSchemaContext("/test-config-data/yang1");
85 final Set<Module> modules = schemaContext.getModules();
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;
104 private static void setSchemaControllerContext(final SchemaContext schema) {
105 final ControllerContext context = ControllerContext.getInstance();
106 context.setSchemas(schema);
107 restconfImpl.setControllerContext(context);
110 @SuppressWarnings("unchecked")
112 @Ignore /// xmlData* need netconf-yang
113 public void postDataViaUrlMountPoint() throws UnsupportedEncodingException {
114 setSchemaControllerContext(schemaContextYangsIetf);
116 brokerFacade.commitConfigurationDataPost(any(DOMMountPoint.class), any(YangInstanceIdentifier.class),
117 any(NormalizedNode.class), null, null)).thenReturn(mock(CheckedFuture.class));
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));
124 ControllerContext.getInstance().setMountService(mockMountService);
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));
131 assertEquals(400, post(uri, MediaType.APPLICATION_JSON, ""));
134 @SuppressWarnings("unchecked")
136 @Ignore //jenkins has problem with JerseyTest - we expecting problems with singletons ControllerContext as schemaContext holder
137 public void createConfigurationDataTest() throws UnsupportedEncodingException, ParseException {
139 final RpcResult<TransactionStatus> rpcResult = new DummyRpcResult.Builder<TransactionStatus>().result(
140 TransactionStatus.COMMITED).build();
142 when(brokerFacade.commitConfigurationDataPost((SchemaContext) null, any(YangInstanceIdentifier.class),
143 any(NormalizedNode.class), null, null))
144 .thenReturn(mock(CheckedFuture.class));
146 final ArgumentCaptor<YangInstanceIdentifier> instanceIdCaptor = ArgumentCaptor.forClass(YangInstanceIdentifier.class);
147 @SuppressWarnings("rawtypes")
148 final ArgumentCaptor<NormalizedNode> compNodeCaptor = ArgumentCaptor.forClass(NormalizedNode.class);
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());
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(),
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());
170 public void createConfigurationDataNullTest() throws UnsupportedEncodingException {
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));
177 //FIXME : find who is set schemaContext
178 // final String URI_1 = "/config";
179 // assertEquals(204, post(URI_1, Draft02.MediaTypes.DATA + XML, xmlTestInterface));
181 final String URI_2 = "/config/test-interface:interfaces";
182 assertEquals(204, post(URI_2, Draft02.MediaTypes.DATA + XML, xmlBlockData));
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);
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();
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")
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")
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);