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.doReturn;
14 import static org.mockito.Mockito.mock;
15 import static org.mockito.Mockito.times;
16 import static org.mockito.Mockito.verify;
17 import static org.mockito.Mockito.when;
18 import static org.opendaylight.controller.sal.restconf.impl.test.RestOperationUtils.XML;
20 import com.google.common.collect.ImmutableList;
21 import com.google.common.util.concurrent.FluentFuture;
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.rest.common.TestRestconfUtils;
36 import org.opendaylight.mdsal.common.api.CommitInfo;
37 import org.opendaylight.mdsal.dom.api.DOMMountPoint;
38 import org.opendaylight.netconf.sal.rest.api.Draft02;
39 import org.opendaylight.netconf.sal.rest.impl.JsonNormalizedNodeBodyReader;
40 import org.opendaylight.netconf.sal.rest.impl.NormalizedNodeJsonBodyWriter;
41 import org.opendaylight.netconf.sal.rest.impl.NormalizedNodeXmlBodyWriter;
42 import org.opendaylight.netconf.sal.rest.impl.RestconfDocumentedExceptionMapper;
43 import org.opendaylight.netconf.sal.rest.impl.XmlNormalizedNodeBodyReader;
44 import org.opendaylight.netconf.sal.restconf.impl.BrokerFacade;
45 import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
46 import org.opendaylight.netconf.sal.restconf.impl.RestconfImpl;
47 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
48 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
49 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
50 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
52 public class RestPostOperationTest extends JerseyTest {
54 private static String xmlBlockData;
55 private static String xmlData3;
56 private static String xmlData4;
58 private static SchemaContext schemaContextYangsIetf;
59 private static SchemaContext schemaContextTestModule;
60 private static SchemaContext schemaContext;
62 private BrokerFacade brokerFacade;
63 private RestconfImpl restconfImpl;
64 private ControllerContext controllerContext;
65 private DOMMountPoint mountInstance;
68 public static void init() throws URISyntaxException, IOException, ReactorException {
69 schemaContextYangsIetf = TestUtils.loadSchemaContext("/full-versions/yangs");
70 schemaContextTestModule = TestUtils.loadSchemaContext("/full-versions/test-module");
71 schemaContext = TestUtils.loadSchemaContext("/test-config-data/yang1");
76 protected Application configure() {
77 /* enable/disable Jersey logs to console */
78 // enable(TestProperties.LOG_TRAFFIC);
79 // enable(TestProperties.DUMP_ENTITY);
80 // enable(TestProperties.RECORD_LOG_LEVEL);
81 // set(TestProperties.RECORD_LOG_LEVEL, Level.ALL.intValue());
83 mountInstance = mock(DOMMountPoint.class);
84 controllerContext = TestRestconfUtils.newControllerContext(schemaContext, mountInstance);
85 brokerFacade = mock(BrokerFacade.class);
86 restconfImpl = RestconfImpl.newInstance(brokerFacade, controllerContext);
88 ResourceConfig resourceConfig = new ResourceConfig();
89 resourceConfig = resourceConfig.registerInstances(restconfImpl,
90 new XmlNormalizedNodeBodyReader(controllerContext), new NormalizedNodeXmlBodyWriter(),
91 new JsonNormalizedNodeBodyReader(controllerContext), new NormalizedNodeJsonBodyWriter(),
92 new RestconfDocumentedExceptionMapper(controllerContext));
93 return resourceConfig;
96 private void setSchemaControllerContext(final SchemaContext schema) {
97 controllerContext.setSchemas(schema);
100 @SuppressWarnings("unchecked")
102 @Ignore /// xmlData* need netconf-yang
103 public void postDataViaUrlMountPoint() throws UnsupportedEncodingException {
104 setSchemaControllerContext(schemaContextYangsIetf);
105 when(brokerFacade.commitConfigurationDataPost(any(DOMMountPoint.class), any(YangInstanceIdentifier.class),
106 any(NormalizedNode.class), null, null)).thenReturn(mock(FluentFuture.class));
108 when(mountInstance.getSchemaContext()).thenReturn(schemaContextTestModule);
110 String uri = "/config/ietf-interfaces:interfaces/interface/0/";
111 assertEquals(204, post(uri, Draft02.MediaTypes.DATA + XML, xmlData4));
112 uri = "/config/ietf-interfaces:interfaces/interface/0/yang-ext:mount/test-module:cont";
113 assertEquals(204, post(uri, Draft02.MediaTypes.DATA + XML, xmlData3));
115 assertEquals(400, post(uri, MediaType.APPLICATION_JSON, ""));
118 @SuppressWarnings("unchecked")
120 @Ignore //jenkins has problem with JerseyTest
121 // - we expecting problems with singletons ControllerContext as schemaContext holder
122 public void createConfigurationDataTest() throws UnsupportedEncodingException, ParseException {
123 when(brokerFacade.commitConfigurationDataPost((SchemaContext) null, any(YangInstanceIdentifier.class),
124 any(NormalizedNode.class), null, null))
125 .thenReturn(mock(FluentFuture.class));
127 final ArgumentCaptor<YangInstanceIdentifier> instanceIdCaptor =
128 ArgumentCaptor.forClass(YangInstanceIdentifier.class);
129 @SuppressWarnings("rawtypes")
130 final ArgumentCaptor<NormalizedNode> compNodeCaptor = ArgumentCaptor.forClass(NormalizedNode.class);
133 // FIXME : identify who is set the schemaContext
134 // final String URI_1 = "/config";
135 // assertEquals(204, post(URI_1, Draft02.MediaTypes.DATA + XML, xmlTestInterface));
136 // verify(brokerFacade).commitConfigurationDataPost(instanceIdCaptor.capture(), compNodeCaptor.capture());
137 final String identifier = "[(urn:ietf:params:xml:ns:yang:test-interface?revision=2014-07-01)interfaces]";
138 // assertEquals(identifier, ImmutableList.copyOf(instanceIdCaptor.getValue().getPathArguments()).toString());
140 final String URI_2 = "/config/test-interface:interfaces";
141 assertEquals(204, post(URI_2, Draft02.MediaTypes.DATA + XML, xmlBlockData));
142 // FIXME : NEVER test a nr. of call some service in complex test suite
143 // verify(brokerFacade, times(2))
144 verify(brokerFacade, times(1))
145 .commitConfigurationDataPost((SchemaContext) null, instanceIdCaptor.capture(), compNodeCaptor.capture(),
147 // identifier = "[(urn:ietf:params:xml:ns:yang:test-interface?revision=2014-07-01)interfaces," +
148 // "(urn:ietf:params:xml:ns:yang:test-interface?revision=2014-07-01)block]";
149 assertEquals(identifier, ImmutableList.copyOf(instanceIdCaptor.getValue().getPathArguments()).toString());
153 public void createConfigurationDataNullTest() throws UnsupportedEncodingException {
154 doReturn(CommitInfo.emptyFluentFuture()).when(brokerFacade).commitConfigurationDataPost(
155 any(SchemaContext.class), any(YangInstanceIdentifier.class), any(NormalizedNode.class), isNull(),
158 //FIXME : find who is set schemaContext
159 // final String URI_1 = "/config";
160 // assertEquals(204, post(URI_1, Draft02.MediaTypes.DATA + XML, xmlTestInterface));
162 final String URI_2 = "/config/test-interface:interfaces";
163 assertEquals(204, post(URI_2, Draft02.MediaTypes.DATA + XML, xmlBlockData));
166 private int post(final String uri, final String mediaType, final String data) {
167 return target(uri).request(mediaType).post(Entity.entity(data, mediaType)).getStatus();
170 private static void loadData() throws IOException, URISyntaxException {
171 final String xmlPathBlockData =
172 RestconfImplTest.class.getResource("/test-config-data/xml/block-data.xml").getPath();
173 xmlBlockData = TestUtils.loadTextFile(xmlPathBlockData);
174 final String data3Input = RestconfImplTest.class.getResource("/full-versions/test-data2/data3.xml").getPath();
175 xmlData3 = TestUtils.loadTextFile(data3Input);
176 final String data4Input = RestconfImplTest.class.getResource("/full-versions/test-data2/data7.xml").getPath();
177 xmlData4 = TestUtils.loadTextFile(data4Input);