1 package org.opendaylight.controller.sal.restconf.impl.test;
3 import static org.mockito.Mockito.*;
4 import static org.junit.Assert.*;
6 import java.io.FileNotFoundException;
7 import java.io.IOException;
8 import java.io.InputStream;
9 import java.io.UnsupportedEncodingException;
11 import java.net.URISyntaxException;
12 import java.net.URLEncoder;
13 import java.util.Collection;
14 import java.util.List;
16 import java.util.concurrent.ExecutionException;
17 import java.util.concurrent.Future;
18 import java.util.concurrent.TimeUnit;
19 import java.util.concurrent.TimeoutException;
20 import java.util.logging.Level;
21 import java.util.logging.LogRecord;
23 import javax.ws.rs.client.Entity;
24 import javax.ws.rs.core.Application;
25 import javax.ws.rs.core.MediaType;
26 import javax.ws.rs.core.Response;
27 import javax.xml.parsers.DocumentBuilder;
28 import javax.xml.parsers.DocumentBuilderFactory;
29 import javax.xml.parsers.ParserConfigurationException;
31 import org.glassfish.jersey.client.ClientConfig;
32 import org.glassfish.jersey.server.ResourceConfig;
33 import org.glassfish.jersey.test.JerseyTest;
34 import org.glassfish.jersey.test.TestProperties;
35 import org.junit.Before;
36 import org.junit.BeforeClass;
37 import org.junit.Test;
38 import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
39 import org.opendaylight.controller.sal.rest.api.RestconfService;
40 import org.opendaylight.controller.sal.rest.impl.StructuredDataToXmlProvider;
41 import org.opendaylight.controller.sal.rest.impl.XmlToCompositeNodeProvider;
42 import org.opendaylight.controller.sal.restconf.impl.BrokerFacade;
43 import org.opendaylight.controller.sal.restconf.impl.ControllerContext;
44 import org.opendaylight.controller.sal.restconf.impl.MediaTypes;
45 import org.opendaylight.controller.sal.restconf.impl.RestconfImpl;
46 import org.opendaylight.yangtools.yang.common.QName;
47 import org.opendaylight.yangtools.yang.common.RpcError;
48 import org.opendaylight.yangtools.yang.common.RpcResult;
49 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
50 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
51 import org.opendaylight.yangtools.yang.model.api.Module;
52 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
53 import org.w3c.dom.Document;
54 import org.xml.sax.SAXException;
56 import com.google.common.base.Charsets;
58 public class XmlProvidersTest extends JerseyTest {
60 private static ControllerContext controllerContext;
61 private static BrokerFacade brokerFacade;
62 private static RestconfImpl restconfImpl;
65 public static void init() {
66 Set<Module> allModules = null;
68 allModules = TestUtils.loadModules(RestconfImplTest.class.getResource("/full-versions/yangs").getPath());
69 } catch (FileNotFoundException e) {
72 SchemaContext schemaContext = TestUtils.loadSchemaContext(allModules);
73 controllerContext = ControllerContext.getInstance();
74 controllerContext.setSchemas(schemaContext);
75 brokerFacade = mock(BrokerFacade.class);
76 restconfImpl = RestconfImpl.getInstance();
77 restconfImpl.setBroker(brokerFacade);
78 restconfImpl.setControllerContext(controllerContext);
83 List<LogRecord> loggedRecords = getLoggedRecords();
84 for (LogRecord l : loggedRecords) {
85 System.out.println(l.getMessage());
90 public void testStructuredDataToXmlProvider() throws FileNotFoundException {
93 uri = new URI("/datastore/" + URLEncoder.encode("ietf-interfaces:interfaces/interface/eth0", Charsets.US_ASCII.name()).toString());
94 } catch (UnsupportedEncodingException | URISyntaxException e) {
98 InputStream xmlStream = RestconfImplTest.class.getResourceAsStream("/parts/ietf-interfaces_interfaces.xml");
99 CompositeNode loadedCompositeNode = TestUtils.loadCompositeNode(xmlStream);
100 when(brokerFacade.readOperationalData(any(InstanceIdentifier.class))).thenReturn(loadedCompositeNode);
102 Response response = target(uri.toASCIIString()).request(MediaTypes.API+RestconfService.XML).get();
103 assertEquals(200, response.getStatus());
107 public void testXmlToCompositeNodeProvider() throws ParserConfigurationException, SAXException, IOException {
110 uri = new URI("/config/" + URLEncoder.encode("ietf-interfaces:interfaces/interface/eth0", Charsets.US_ASCII.name()).toString());
111 } catch (UnsupportedEncodingException | URISyntaxException e) {
114 InputStream xmlStream = RestconfImplTest.class.getResourceAsStream("/parts/ietf-interfaces_interfaces.xml");
115 final CompositeNode loadedCompositeNode = TestUtils.loadCompositeNode(xmlStream);
116 when(brokerFacade.commitConfigurationDataPut(any(InstanceIdentifier.class), any(CompositeNode.class))).thenReturn(new Future<RpcResult<TransactionStatus>>() {
118 public boolean cancel(boolean mayInterruptIfRunning) {
122 public boolean isCancelled() {
126 public boolean isDone() {
130 public RpcResult<TransactionStatus> get() throws InterruptedException, ExecutionException {
134 public RpcResult<TransactionStatus> get(long timeout, TimeUnit unit) throws InterruptedException,
135 ExecutionException, TimeoutException {
140 DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
141 DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
142 xmlStream = RestconfImplTest.class.getResourceAsStream("/parts/ietf-interfaces_interfaces.xml");
143 Document doc = docBuilder.parse(xmlStream);
145 Response response = target(uri.toASCIIString()).request(MediaTypes.API+RestconfService.XML).post(Entity.entity(TestUtils.getDocumentInPrintableForm(doc), new MediaType("application","vnd.yang.api+xml")));
146 assertEquals(204, response.getStatus());
150 public void testXmlToCompositeNodeProviderExceptions() {
153 uri = new URI("/operations/" + URLEncoder.encode("ietf-interfaces:interfaces/interface/eth0", Charsets.US_ASCII.name()).toString());
154 } catch (UnsupportedEncodingException | URISyntaxException e) {
158 Response response = target(uri.toASCIIString()).request(MediaTypes.API + RestconfService.XML).post(
159 Entity.entity("<SimpleNode/>", new MediaType("application", "vnd.yang.api+xml")));
160 assertEquals(400, response.getStatus());
162 response = target(uri.toASCIIString()).request(MediaTypes.API + RestconfService.XML).post(
163 Entity.entity("<SimpleNode>", new MediaType("application", "vnd.yang.api+xml")));
164 assertEquals(400, response.getStatus());
168 public void testXmlToCompositeNode404NotFound() {
171 uri = new URI("/datastore/" + URLEncoder.encode("ietf-interfaces:interfaces/interface/eth0", Charsets.US_ASCII.name()).toString());
172 } catch (UnsupportedEncodingException | URISyntaxException e) {
176 when(brokerFacade.readOperationalData(any(InstanceIdentifier.class))).thenReturn(null);
178 Response response = target(uri.toASCIIString()).request(MediaTypes.API+RestconfService.XML).get();
179 assertEquals(404, response.getStatus());
183 protected Application configure() {
184 enable(TestProperties.LOG_TRAFFIC);
185 enable(TestProperties.DUMP_ENTITY);
186 enable(TestProperties.RECORD_LOG_LEVEL);
187 set(TestProperties.RECORD_LOG_LEVEL, Level.ALL.intValue());
189 ResourceConfig resourceConfig = new ResourceConfig();
190 resourceConfig = resourceConfig.registerInstances(restconfImpl, StructuredDataToXmlProvider.INSTANCE, XmlToCompositeNodeProvider.INSTANCE);
191 return resourceConfig;