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.logging.Level;
17 import java.util.logging.LogRecord;
19 import javax.ws.rs.client.Entity;
20 import javax.ws.rs.core.Application;
21 import javax.ws.rs.core.MediaType;
22 import javax.ws.rs.core.Response;
23 import javax.xml.parsers.DocumentBuilder;
24 import javax.xml.parsers.DocumentBuilderFactory;
25 import javax.xml.parsers.ParserConfigurationException;
27 import org.glassfish.jersey.client.ClientConfig;
28 import org.glassfish.jersey.server.ResourceConfig;
29 import org.glassfish.jersey.test.JerseyTest;
30 import org.glassfish.jersey.test.TestProperties;
31 import org.junit.Before;
32 import org.junit.BeforeClass;
33 import org.junit.Test;
34 import org.opendaylight.controller.sal.rest.api.RestconfService;
35 import org.opendaylight.controller.sal.rest.impl.StructuredDataToXmlProvider;
36 import org.opendaylight.controller.sal.rest.impl.XmlToCompositeNodeProvider;
37 import org.opendaylight.controller.sal.restconf.impl.BrokerFacade;
38 import org.opendaylight.controller.sal.restconf.impl.ControllerContext;
39 import org.opendaylight.controller.sal.restconf.impl.MediaTypes;
40 import org.opendaylight.controller.sal.restconf.impl.RestconfImpl;
41 import org.opendaylight.yangtools.yang.common.QName;
42 import org.opendaylight.yangtools.yang.common.RpcError;
43 import org.opendaylight.yangtools.yang.common.RpcResult;
44 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
45 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
46 import org.opendaylight.yangtools.yang.model.api.Module;
47 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
48 import org.w3c.dom.Document;
49 import org.xml.sax.SAXException;
51 import com.google.common.base.Charsets;
53 public class XmlProvidersTest extends JerseyTest {
55 private static ControllerContext controllerContext;
56 private static BrokerFacade brokerFacade;
57 private static RestconfImpl restconfImpl;
60 public static void init() {
61 Set<Module> allModules = null;
63 allModules = TestUtils.loadModules(RestconfImplTest.class.getResource("/full-versions/yangs").getPath());
64 } catch (FileNotFoundException e) {
67 SchemaContext schemaContext = TestUtils.loadSchemaContext(allModules);
68 controllerContext = ControllerContext.getInstance();
69 controllerContext.setSchemas(schemaContext);
70 brokerFacade = mock(BrokerFacade.class);
71 restconfImpl = RestconfImpl.getInstance();
72 restconfImpl.setBroker(brokerFacade);
73 restconfImpl.setControllerContext(controllerContext);
78 List<LogRecord> loggedRecords = getLoggedRecords();
79 for (LogRecord l : loggedRecords) {
80 System.out.println(l.getMessage());
85 public void testStructuredDataToXmlProvider() throws FileNotFoundException {
88 uri = new URI("/datastore/" + URLEncoder.encode("ietf-interfaces:interfaces/interface/eth0", Charsets.US_ASCII.name()).toString());
89 } catch (UnsupportedEncodingException | URISyntaxException e) {
93 InputStream xmlStream = RestconfImplTest.class.getResourceAsStream("/parts/ietf-interfaces_interfaces.xml");
94 CompositeNode loadedCompositeNode = TestUtils.loadCompositeNode(xmlStream);
95 when(brokerFacade.readOperationalData(any(InstanceIdentifier.class))).thenReturn(loadedCompositeNode);
97 Response response = target(uri.toASCIIString()).request(MediaTypes.API+RestconfService.XML).get();
98 assertEquals(200, response.getStatus());
102 public void testXmlToCompositeNodeProvider() throws ParserConfigurationException, SAXException, IOException {
105 uri = new URI("/operations/" + URLEncoder.encode("ietf-interfaces:interfaces/interface/eth0", Charsets.US_ASCII.name()).toString());
106 } catch (UnsupportedEncodingException | URISyntaxException e) {
109 InputStream xmlStream = RestconfImplTest.class.getResourceAsStream("/parts/ietf-interfaces_interfaces.xml");
110 final CompositeNode loadedCompositeNode = TestUtils.loadCompositeNode(xmlStream);
111 when(brokerFacade.invokeRpc(any(QName.class), any(CompositeNode.class))).thenReturn(new RpcResult<CompositeNode>() {
114 public boolean isSuccessful() {
119 public CompositeNode getResult() {
120 return loadedCompositeNode;
124 public Collection<RpcError> getErrors() {
129 DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
130 DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
131 xmlStream = RestconfImplTest.class.getResourceAsStream("/parts/ietf-interfaces_interfaces.xml");
132 Document doc = docBuilder.parse(xmlStream);
134 Response response = target(uri.toASCIIString()).request(MediaTypes.API+RestconfService.XML).post(Entity.entity(TestUtils.getDocumentInPrintableForm(doc), new MediaType("application","vnd.yang.api+xml")));
135 assertEquals(200, response.getStatus());
139 public void testXmlToCompositeNodeProviderExceptions() {
142 uri = new URI("/operations/" + URLEncoder.encode("ietf-interfaces:interfaces/interface/eth0", Charsets.US_ASCII.name()).toString());
143 } catch (UnsupportedEncodingException | URISyntaxException e) {
147 Response response = target(uri.toASCIIString()).request(MediaTypes.API + RestconfService.XML).post(
148 Entity.entity("<SimpleNode/>", new MediaType("application", "vnd.yang.api+xml")));
149 assertEquals(400, response.getStatus());
151 response = target(uri.toASCIIString()).request(MediaTypes.API + RestconfService.XML).post(
152 Entity.entity("<SimpleNode>", new MediaType("application", "vnd.yang.api+xml")));
153 assertEquals(400, response.getStatus());
157 public void testXmlToCompositeNode404NotFound() {
160 uri = new URI("/datastore/" + URLEncoder.encode("ietf-interfaces:interfaces/interface/eth0", Charsets.US_ASCII.name()).toString());
161 } catch (UnsupportedEncodingException | URISyntaxException e) {
165 when(brokerFacade.readOperationalData(any(InstanceIdentifier.class))).thenReturn(null);
167 Response response = target(uri.toASCIIString()).request(MediaTypes.API+RestconfService.XML).get();
168 assertEquals(404, response.getStatus());
172 protected Application configure() {
173 enable(TestProperties.LOG_TRAFFIC);
174 enable(TestProperties.DUMP_ENTITY);
175 enable(TestProperties.RECORD_LOG_LEVEL);
176 set(TestProperties.RECORD_LOG_LEVEL, Level.ALL.intValue());
178 ResourceConfig resourceConfig = new ResourceConfig();
179 resourceConfig = resourceConfig.registerInstances(restconfImpl, StructuredDataToXmlProvider.INSTANCE, XmlToCompositeNodeProvider.INSTANCE);
180 return resourceConfig;