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;
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;
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.netconf.sal.rest.api.Draft02;
41 import org.opendaylight.netconf.sal.rest.impl.JsonNormalizedNodeBodyReader;
42 import org.opendaylight.netconf.sal.rest.impl.NormalizedNodeJsonBodyWriter;
43 import org.opendaylight.netconf.sal.rest.impl.NormalizedNodeXmlBodyWriter;
44 import org.opendaylight.netconf.sal.rest.impl.RestconfDocumentedExceptionMapper;
45 import org.opendaylight.netconf.sal.rest.impl.XmlNormalizedNodeBodyReader;
46 import org.opendaylight.netconf.sal.restconf.impl.BrokerFacade;
47 import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
48 import org.opendaylight.netconf.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 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
56 public class RestPostOperationTest extends JerseyTest {
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;
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;
72 private static DOMMountPointService mountService;
75 public static void init() throws URISyntaxException, IOException, ReactorException {
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);
82 schemaContext = TestUtils.loadSchemaContext("/test-config-data/yang1");
83 final Set<Module> modules = schemaContext.getModules();
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;
102 private void setSchemaControllerContext(final SchemaContext schema) {
103 final ControllerContext context = ControllerContext.getInstance();
104 context.setSchemas(schema);
105 restconfImpl.setControllerContext(context);
108 @SuppressWarnings("unchecked")
110 @Ignore /// xmlData* need netconf-yang
111 public void postDataViaUrlMountPoint() throws UnsupportedEncodingException {
112 setSchemaControllerContext(schemaContextYangsIetf);
114 brokerFacade.commitConfigurationDataPost(any(DOMMountPoint.class), any(YangInstanceIdentifier.class),
115 any(NormalizedNode.class))).thenReturn(mock(CheckedFuture.class));
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));
122 ControllerContext.getInstance().setMountService(mockMountService);
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));
129 assertEquals(400, post(uri, MediaType.APPLICATION_JSON, ""));
132 @SuppressWarnings("unchecked")
134 @Ignore //jenkins has problem with JerseyTest - we expecting problems with singletons ControllerContext as schemaContext holder
135 public void createConfigurationDataTest() throws UnsupportedEncodingException, ParseException {
137 final RpcResult<TransactionStatus> rpcResult = new DummyRpcResult.Builder<TransactionStatus>().result(
138 TransactionStatus.COMMITED).build();
140 when(brokerFacade.commitConfigurationDataPost((SchemaContext)null, any(YangInstanceIdentifier.class), any(NormalizedNode.class)))
141 .thenReturn(mock(CheckedFuture.class));
143 final ArgumentCaptor<YangInstanceIdentifier> instanceIdCaptor = ArgumentCaptor.forClass(YangInstanceIdentifier.class);
144 @SuppressWarnings("rawtypes")
145 final ArgumentCaptor<NormalizedNode> compNodeCaptor = ArgumentCaptor.forClass(NormalizedNode.class);
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());
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((SchemaContext)null, 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());
166 public void createConfigurationDataNullTest() throws UnsupportedEncodingException {
169 when(brokerFacade.commitConfigurationDataPost(any(SchemaContext.class), any(YangInstanceIdentifier.class),any(NormalizedNode.class)))
170 .thenReturn(Futures.<Void, TransactionCommitFailedException>immediateCheckedFuture(null));
172 //FIXME : find who is set schemaContext
173 // final String URI_1 = "/config";
174 // assertEquals(204, post(URI_1, Draft02.MediaTypes.DATA + XML, xmlTestInterface));
176 final String URI_2 = "/config/test-interface:interfaces";
177 assertEquals(204, post(URI_2, Draft02.MediaTypes.DATA + XML, xmlBlockData));
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);
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();
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")
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")
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);