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.server.ResourceConfig;
28 import org.glassfish.jersey.test.JerseyTest;
29 import org.glassfish.jersey.test.TestProperties;
30 import org.junit.Before;
31 import org.junit.BeforeClass;
32 import org.junit.Test;
33 import org.opendaylight.controller.sal.rest.api.RestconfService;
34 import org.opendaylight.controller.sal.rest.impl.StructuredDataToXmlProvider;
35 import org.opendaylight.controller.sal.rest.impl.XmlToCompositeNodeProvider;
36 import org.opendaylight.controller.sal.restconf.impl.BrokerFacade;
37 import org.opendaylight.controller.sal.restconf.impl.ControllerContext;
38 import org.opendaylight.controller.sal.restconf.impl.MediaTypes;
39 import org.opendaylight.controller.sal.restconf.impl.RestconfImpl;
40 import org.opendaylight.yangtools.yang.common.QName;
41 import org.opendaylight.yangtools.yang.common.RpcError;
42 import org.opendaylight.yangtools.yang.common.RpcResult;
43 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
44 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
45 import org.opendaylight.yangtools.yang.model.api.Module;
46 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
47 import org.w3c.dom.Document;
48 import org.xml.sax.SAXException;
50 import com.google.common.base.Charsets;
52 public class XmlProvidersTest extends JerseyTest {
54 private static ControllerContext controllerContext;
55 private static BrokerFacade brokerFacade;
56 private static RestconfImpl restconfImpl;
59 public static void init() {
60 Set<Module> allModules = null;
62 allModules = TestUtils.loadModules(RestconfImplTest.class.getResource("/full-versions/yangs").getPath());
63 } catch (FileNotFoundException e) {
66 SchemaContext schemaContext = TestUtils.loadSchemaContext(allModules);
67 controllerContext = ControllerContext.getInstance();
68 controllerContext.setSchemas(schemaContext);
69 brokerFacade = mock(BrokerFacade.class);
70 restconfImpl = RestconfImpl.getInstance();
71 restconfImpl.setBroker(brokerFacade);
72 restconfImpl.setControllerContext(controllerContext);
77 List<LogRecord> loggedRecords = getLoggedRecords();
78 for (LogRecord l : loggedRecords) {
79 System.out.println(l.getMessage());
84 public void testStructuredDataToXmlProvider() throws FileNotFoundException {
87 uri = new URI("/restconf/datastore/" + URLEncoder.encode("ietf-interfaces:interfaces/interface/eth0", Charsets.US_ASCII.name()).toString());
88 } catch (UnsupportedEncodingException | URISyntaxException e) {
92 InputStream xmlStream = RestconfImplTest.class.getResourceAsStream("/parts/ietf-interfaces_interfaces.xml");
93 CompositeNode loadedCompositeNode = TestUtils.loadCompositeNode(xmlStream);
94 when(brokerFacade.readOperationalData(any(InstanceIdentifier.class))).thenReturn(loadedCompositeNode);
96 Response response = target(uri.toASCIIString()).request(MediaTypes.API+RestconfService.XML).get();
97 assertEquals(200, response.getStatus());
101 public void testXmlToCompositeNodeProvider() throws ParserConfigurationException, SAXException, IOException {
104 uri = new URI("/restconf/operations/" + URLEncoder.encode("ietf-interfaces:interfaces/interface/eth0", Charsets.US_ASCII.name()).toString());
105 } catch (UnsupportedEncodingException | URISyntaxException e) {
108 InputStream xmlStream = RestconfImplTest.class.getResourceAsStream("/parts/ietf-interfaces_interfaces.xml");
109 final CompositeNode loadedCompositeNode = TestUtils.loadCompositeNode(xmlStream);
110 when(brokerFacade.invokeRpc(any(QName.class), any(CompositeNode.class))).thenReturn(new RpcResult<CompositeNode>() {
113 public boolean isSuccessful() {
118 public CompositeNode getResult() {
119 return loadedCompositeNode;
123 public Collection<RpcError> getErrors() {
128 DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
129 DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
130 xmlStream = RestconfImplTest.class.getResourceAsStream("/parts/ietf-interfaces_interfaces.xml");
131 Document doc = docBuilder.parse(xmlStream);
133 Response response = target(uri.toASCIIString()).request(MediaTypes.API+RestconfService.XML).post(Entity.entity(TestUtils.getDocumentInPrintableForm(doc), new MediaType("application","vnd.yang.api+xml")));
134 assertEquals(200, response.getStatus());
138 public void testXmlToCompositeNodeProviderExceptions() {
141 uri = new URI("/restconf/operations/" + URLEncoder.encode("ietf-interfaces:interfaces/interface/eth0", Charsets.US_ASCII.name()).toString());
142 } catch (UnsupportedEncodingException | URISyntaxException e) {
146 Response response = target(uri.toASCIIString()).request(MediaTypes.API + RestconfService.XML).post(
147 Entity.entity("<SimpleNode/>", new MediaType("application", "vnd.yang.api+xml")));
148 assertEquals(400, response.getStatus());
150 response = target(uri.toASCIIString()).request(MediaTypes.API + RestconfService.XML).post(
151 Entity.entity("<SimpleNode>", new MediaType("application", "vnd.yang.api+xml")));
152 assertEquals(400, response.getStatus());
156 protected Application configure() {
157 enable(TestProperties.LOG_TRAFFIC);
158 enable(TestProperties.DUMP_ENTITY);
159 enable(TestProperties.RECORD_LOG_LEVEL);
160 set(TestProperties.RECORD_LOG_LEVEL, Level.ALL.intValue());
162 ResourceConfig resourceConfig = new ResourceConfig();
163 resourceConfig = resourceConfig.registerInstances(restconfImpl, StructuredDataToXmlProvider.INSTANCE, XmlToCompositeNodeProvider.INSTANCE);
164 return resourceConfig;