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.ArgumentMatchers.any;
12 import static org.mockito.ArgumentMatchers.isNull;
13 import static org.mockito.Mockito.mock;
14 import static org.mockito.Mockito.times;
15 import static org.mockito.Mockito.verify;
16 import static org.mockito.Mockito.when;
17 import static org.opendaylight.controller.sal.restconf.impl.test.RestOperationUtils.XML;
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.UnsupportedEncodingException;
24 import java.net.URISyntaxException;
25 import java.text.ParseException;
26 import javax.ws.rs.client.Entity;
27 import javax.ws.rs.core.Application;
28 import javax.ws.rs.core.MediaType;
29 import org.glassfish.jersey.server.ResourceConfig;
30 import org.glassfish.jersey.test.JerseyTest;
31 import org.junit.BeforeClass;
32 import org.junit.Ignore;
33 import org.junit.Test;
34 import org.mockito.ArgumentCaptor;
35 import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
36 import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
37 import org.opendaylight.netconf.sal.rest.api.Draft02;
38 import org.opendaylight.netconf.sal.rest.impl.JsonNormalizedNodeBodyReader;
39 import org.opendaylight.netconf.sal.rest.impl.NormalizedNodeJsonBodyWriter;
40 import org.opendaylight.netconf.sal.rest.impl.NormalizedNodeXmlBodyWriter;
41 import org.opendaylight.netconf.sal.rest.impl.RestconfDocumentedExceptionMapper;
42 import org.opendaylight.netconf.sal.rest.impl.XmlNormalizedNodeBodyReader;
43 import org.opendaylight.netconf.sal.restconf.impl.BrokerFacade;
44 import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
45 import org.opendaylight.netconf.sal.restconf.impl.RestconfImpl;
46 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
47 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
48 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
49 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
51 public class RestPostOperationTest extends JerseyTest {
53 private static String xmlBlockData;
54 private static String xmlData3;
55 private static String xmlData4;
57 private static SchemaContext schemaContextYangsIetf;
58 private static SchemaContext schemaContextTestModule;
59 private static SchemaContext schemaContext;
61 private BrokerFacade brokerFacade;
62 private RestconfImpl restconfImpl;
63 private ControllerContext controllerContext;
64 private DOMMountPoint mountInstance;
67 public static void init() throws URISyntaxException, IOException, ReactorException {
68 schemaContextYangsIetf = TestUtils.loadSchemaContext("/full-versions/yangs");
69 schemaContextTestModule = TestUtils.loadSchemaContext("/full-versions/test-module");
70 schemaContext = TestUtils.loadSchemaContext("/test-config-data/yang1");
75 protected Application configure() {
76 /* enable/disable Jersey logs to console */
77 // enable(TestProperties.LOG_TRAFFIC);
78 // enable(TestProperties.DUMP_ENTITY);
79 // enable(TestProperties.RECORD_LOG_LEVEL);
80 // set(TestProperties.RECORD_LOG_LEVEL, Level.ALL.intValue());
82 mountInstance = mock(DOMMountPoint.class);
83 controllerContext = TestRestconfUtils.newControllerContext(schemaContext, mountInstance);
84 brokerFacade = mock(BrokerFacade.class);
85 restconfImpl = RestconfImpl.newInstance(brokerFacade, controllerContext);
87 ResourceConfig resourceConfig = new ResourceConfig();
88 resourceConfig = resourceConfig.registerInstances(restconfImpl,
89 new XmlNormalizedNodeBodyReader(controllerContext), new NormalizedNodeXmlBodyWriter(),
90 new JsonNormalizedNodeBodyReader(controllerContext), new NormalizedNodeJsonBodyWriter(),
91 new RestconfDocumentedExceptionMapper(controllerContext));
92 return resourceConfig;
95 private void setSchemaControllerContext(final SchemaContext schema) {
96 controllerContext.setSchemas(schema);
99 @SuppressWarnings("unchecked")
101 @Ignore /// xmlData* need netconf-yang
102 public void postDataViaUrlMountPoint() throws UnsupportedEncodingException {
103 setSchemaControllerContext(schemaContextYangsIetf);
104 when(brokerFacade.commitConfigurationDataPost(any(DOMMountPoint.class), any(YangInstanceIdentifier.class),
105 any(NormalizedNode.class), null, null)).thenReturn(mock(CheckedFuture.class));
107 when(mountInstance.getSchemaContext()).thenReturn(schemaContextTestModule);
109 String uri = "/config/ietf-interfaces:interfaces/interface/0/";
110 assertEquals(204, post(uri, Draft02.MediaTypes.DATA + XML, xmlData4));
111 uri = "/config/ietf-interfaces:interfaces/interface/0/yang-ext:mount/test-module:cont";
112 assertEquals(204, post(uri, Draft02.MediaTypes.DATA + XML, xmlData3));
114 assertEquals(400, post(uri, MediaType.APPLICATION_JSON, ""));
117 @SuppressWarnings("unchecked")
119 @Ignore //jenkins has problem with JerseyTest
120 // - we expecting problems with singletons ControllerContext as schemaContext holder
121 public void createConfigurationDataTest() throws UnsupportedEncodingException, ParseException {
122 when(brokerFacade.commitConfigurationDataPost((SchemaContext) null, any(YangInstanceIdentifier.class),
123 any(NormalizedNode.class), null, null))
124 .thenReturn(mock(CheckedFuture.class));
126 final ArgumentCaptor<YangInstanceIdentifier> instanceIdCaptor =
127 ArgumentCaptor.forClass(YangInstanceIdentifier.class);
128 @SuppressWarnings("rawtypes")
129 final ArgumentCaptor<NormalizedNode> compNodeCaptor = ArgumentCaptor.forClass(NormalizedNode.class);
132 // FIXME : identify who is set the schemaContext
133 // final String URI_1 = "/config";
134 // assertEquals(204, post(URI_1, Draft02.MediaTypes.DATA + XML, xmlTestInterface));
135 // verify(brokerFacade).commitConfigurationDataPost(instanceIdCaptor.capture(), compNodeCaptor.capture());
136 final String identifier = "[(urn:ietf:params:xml:ns:yang:test-interface?revision=2014-07-01)interfaces]";
137 // assertEquals(identifier, ImmutableList.copyOf(instanceIdCaptor.getValue().getPathArguments()).toString());
139 final String URI_2 = "/config/test-interface:interfaces";
140 assertEquals(204, post(URI_2, Draft02.MediaTypes.DATA + XML, xmlBlockData));
141 // FIXME : NEVER test a nr. of call some service in complex test suite
142 // verify(brokerFacade, times(2))
143 verify(brokerFacade, times(1))
144 .commitConfigurationDataPost((SchemaContext) null, instanceIdCaptor.capture(), compNodeCaptor.capture(),
146 // identifier = "[(urn:ietf:params:xml:ns:yang:test-interface?revision=2014-07-01)interfaces," +
147 // "(urn:ietf:params:xml:ns:yang:test-interface?revision=2014-07-01)block]";
148 assertEquals(identifier, ImmutableList.copyOf(instanceIdCaptor.getValue().getPathArguments()).toString());
152 public void createConfigurationDataNullTest() throws UnsupportedEncodingException {
153 when(brokerFacade.commitConfigurationDataPost(any(SchemaContext.class), any(YangInstanceIdentifier.class),
154 any(NormalizedNode.class), isNull(), isNull()))
155 .thenReturn(Futures.immediateCheckedFuture(null));
157 //FIXME : find who is set schemaContext
158 // final String URI_1 = "/config";
159 // assertEquals(204, post(URI_1, Draft02.MediaTypes.DATA + XML, xmlTestInterface));
161 final String URI_2 = "/config/test-interface:interfaces";
162 assertEquals(204, post(URI_2, Draft02.MediaTypes.DATA + XML, xmlBlockData));
165 private int post(final String uri, final String mediaType, final String data) {
166 return target(uri).request(mediaType).post(Entity.entity(data, mediaType)).getStatus();
169 private static void loadData() throws IOException, URISyntaxException {
170 final String xmlPathBlockData =
171 RestconfImplTest.class.getResource("/test-config-data/xml/block-data.xml").getPath();
172 xmlBlockData = TestUtils.loadTextFile(xmlPathBlockData);
173 final String data3Input = RestconfImplTest.class.getResource("/full-versions/test-data2/data3.xml").getPath();
174 xmlData3 = TestUtils.loadTextFile(data3Input);
175 final String data4Input = RestconfImplTest.class.getResource("/full-versions/test-data2/data7.xml").getPath();
176 xmlData4 = TestUtils.loadTextFile(data4Input);