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 java.util.Optional;
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.rest.common.TestRestconfUtils;
37 import org.opendaylight.mdsal.common.api.CommitInfo;
38 import org.opendaylight.mdsal.dom.api.DOMMountPoint;
39 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
40 import org.opendaylight.mdsal.dom.spi.FixedDOMSchemaService;
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.data.api.YangInstanceIdentifier;
51 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
52 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
54 public class RestPostOperationTest extends JerseyTest {
56 private static String xmlBlockData;
57 private static String xmlData3;
58 private static String xmlData4;
60 private static EffectiveModelContext schemaContextYangsIetf;
61 private static EffectiveModelContext schemaContextTestModule;
62 private static EffectiveModelContext schemaContext;
64 private BrokerFacade brokerFacade;
65 private RestconfImpl restconfImpl;
66 private ControllerContext controllerContext;
67 private DOMMountPoint mountInstance;
70 public static void init() throws URISyntaxException, IOException {
71 schemaContextYangsIetf = TestUtils.loadSchemaContext("/full-versions/yangs");
72 schemaContextTestModule = TestUtils.loadSchemaContext("/full-versions/test-module");
73 schemaContext = TestUtils.loadSchemaContext("/test-config-data/yang1");
78 protected Application configure() {
79 /* enable/disable Jersey logs to console */
80 // enable(TestProperties.LOG_TRAFFIC);
81 // enable(TestProperties.DUMP_ENTITY);
82 // enable(TestProperties.RECORD_LOG_LEVEL);
83 // set(TestProperties.RECORD_LOG_LEVEL, Level.ALL.intValue());
85 mountInstance = mock(DOMMountPoint.class);
86 controllerContext = TestRestconfUtils.newControllerContext(schemaContext, mountInstance);
87 brokerFacade = mock(BrokerFacade.class);
88 restconfImpl = RestconfImpl.newInstance(brokerFacade, controllerContext);
90 ResourceConfig resourceConfig = new ResourceConfig();
91 resourceConfig = resourceConfig.registerInstances(restconfImpl,
92 new XmlNormalizedNodeBodyReader(controllerContext), new NormalizedNodeXmlBodyWriter(),
93 new JsonNormalizedNodeBodyReader(controllerContext), new NormalizedNodeJsonBodyWriter(),
94 new RestconfDocumentedExceptionMapper(controllerContext));
95 return resourceConfig;
98 private void setSchemaControllerContext(final EffectiveModelContext schema) {
99 controllerContext.setSchemas(schema);
102 @SuppressWarnings("unchecked")
104 @Ignore /// xmlData* need netconf-yang
105 public void postDataViaUrlMountPoint() throws UnsupportedEncodingException {
106 setSchemaControllerContext(schemaContextYangsIetf);
107 when(brokerFacade.commitConfigurationDataPost(any(DOMMountPoint.class), any(YangInstanceIdentifier.class),
108 any(NormalizedNode.class), null, null)).thenReturn(mock(FluentFuture.class));
110 when(mountInstance.getService(DOMSchemaService.class))
111 .thenReturn(Optional.of(FixedDOMSchemaService.of(schemaContextTestModule)));
113 String uri = "/config/ietf-interfaces:interfaces/interface/0/";
114 assertEquals(204, post(uri, Draft02.MediaTypes.DATA + XML, xmlData4));
115 uri = "/config/ietf-interfaces:interfaces/interface/0/yang-ext:mount/test-module:cont";
116 assertEquals(204, post(uri, Draft02.MediaTypes.DATA + XML, xmlData3));
118 assertEquals(400, post(uri, MediaType.APPLICATION_JSON, ""));
121 @SuppressWarnings("unchecked")
123 @Ignore //jenkins has problem with JerseyTest
124 // - we expecting problems with singletons ControllerContext as schemaContext holder
125 public void createConfigurationDataTest() throws UnsupportedEncodingException, ParseException {
126 when(brokerFacade.commitConfigurationDataPost((EffectiveModelContext) null, any(YangInstanceIdentifier.class),
127 any(NormalizedNode.class), null, null))
128 .thenReturn(mock(FluentFuture.class));
130 final ArgumentCaptor<YangInstanceIdentifier> instanceIdCaptor =
131 ArgumentCaptor.forClass(YangInstanceIdentifier.class);
132 @SuppressWarnings("rawtypes")
133 final ArgumentCaptor<NormalizedNode> compNodeCaptor = ArgumentCaptor.forClass(NormalizedNode.class);
136 // FIXME : identify who is set the schemaContext
137 // final String URI_1 = "/config";
138 // assertEquals(204, post(URI_1, Draft02.MediaTypes.DATA + XML, xmlTestInterface));
139 // verify(brokerFacade).commitConfigurationDataPost(instanceIdCaptor.capture(), compNodeCaptor.capture());
140 final String identifier = "[(urn:ietf:params:xml:ns:yang:test-interface?revision=2014-07-01)interfaces]";
141 // assertEquals(identifier, ImmutableList.copyOf(instanceIdCaptor.getValue().getPathArguments()).toString());
143 final String URI_2 = "/config/test-interface:interfaces";
144 assertEquals(204, post(URI_2, Draft02.MediaTypes.DATA + XML, xmlBlockData));
145 // FIXME : NEVER test a nr. of call some service in complex test suite
146 // verify(brokerFacade, times(2))
147 verify(brokerFacade, times(1))
148 .commitConfigurationDataPost((EffectiveModelContext) null, instanceIdCaptor.capture(),
149 compNodeCaptor.capture(), null, null);
150 // identifier = "[(urn:ietf:params:xml:ns:yang:test-interface?revision=2014-07-01)interfaces," +
151 // "(urn:ietf:params:xml:ns:yang:test-interface?revision=2014-07-01)block]";
152 assertEquals(identifier, ImmutableList.copyOf(instanceIdCaptor.getValue().getPathArguments()).toString());
156 public void createConfigurationDataNullTest() throws UnsupportedEncodingException {
157 doReturn(CommitInfo.emptyFluentFuture()).when(brokerFacade).commitConfigurationDataPost(
158 any(EffectiveModelContext.class), any(YangInstanceIdentifier.class), any(NormalizedNode.class), isNull(),
161 //FIXME : find who is set schemaContext
162 // final String URI_1 = "/config";
163 // assertEquals(204, post(URI_1, Draft02.MediaTypes.DATA + XML, xmlTestInterface));
165 final String URI_2 = "/config/test-interface:interfaces";
166 assertEquals(204, post(URI_2, Draft02.MediaTypes.DATA + XML, xmlBlockData));
169 private int post(final String uri, final String mediaType, final String data) {
170 return target(uri).request(mediaType).post(Entity.entity(data, mediaType)).getStatus();
173 private static void loadData() throws IOException, URISyntaxException {
174 final String xmlPathBlockData =
175 RestconfImplTest.class.getResource("/test-config-data/xml/block-data.xml").getPath();
176 xmlBlockData = TestUtils.loadTextFile(xmlPathBlockData);
177 final String data3Input = RestconfImplTest.class.getResource("/full-versions/test-data2/data3.xml").getPath();
178 xmlData3 = TestUtils.loadTextFile(data3Input);
179 final String data4Input = RestconfImplTest.class.getResource("/full-versions/test-data2/data7.xml").getPath();
180 xmlData4 = TestUtils.loadTextFile(data4Input);