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