Merge "Initial implementation of the ClusteredDataStore"
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / test / java / org / opendaylight / controller / sal / restconf / impl / test / XmlProvidersTest.java
1 package org.opendaylight.controller.sal.restconf.impl.test;
2
3 import static org.mockito.Mockito.*;
4 import static org.junit.Assert.*;
5
6 import java.io.FileNotFoundException;
7 import java.io.IOException;
8 import java.io.InputStream;
9 import java.io.UnsupportedEncodingException;
10 import java.net.URI;
11 import java.net.URISyntaxException;
12 import java.net.URLEncoder;
13 import java.util.Collection;
14 import java.util.List;
15 import java.util.Set;
16 import java.util.logging.Level;
17 import java.util.logging.LogRecord;
18
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;
26
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;
49
50 import com.google.common.base.Charsets;
51
52 public class XmlProvidersTest extends JerseyTest {
53
54     private static ControllerContext controllerContext;
55     private static BrokerFacade brokerFacade;
56     private static RestconfImpl restconfImpl;
57
58     @BeforeClass
59     public static void init() {
60         Set<Module> allModules = null;
61         try {
62             allModules = TestUtils.loadModules(RestconfImplTest.class.getResource("/full-versions/yangs").getPath());
63         } catch (FileNotFoundException e) {
64             e.printStackTrace();
65         }
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);
73     }
74
75 //    @Before
76     public void logs() {
77         List<LogRecord> loggedRecords = getLoggedRecords();
78         for (LogRecord l : loggedRecords) {
79             System.out.println(l.getMessage());
80         }
81     }
82
83     @Test
84     public void testStructuredDataToXmlProvider() throws FileNotFoundException {
85         URI uri = null;
86         try {
87             uri = new URI("/datastore/" + URLEncoder.encode("ietf-interfaces:interfaces/interface/eth0", Charsets.US_ASCII.name()).toString());
88         } catch (UnsupportedEncodingException | URISyntaxException e) {
89             e.printStackTrace();
90         }
91         
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);
95         
96         Response response = target(uri.toASCIIString()).request(MediaTypes.API+RestconfService.XML).get();
97         assertEquals(200, response.getStatus());
98     }
99
100     @Test
101     public void testXmlToCompositeNodeProvider() throws ParserConfigurationException, SAXException, IOException {
102         URI uri = null;
103         try {
104             uri = new URI("/operations/" + URLEncoder.encode("ietf-interfaces:interfaces/interface/eth0", Charsets.US_ASCII.name()).toString());
105         } catch (UnsupportedEncodingException | URISyntaxException e) {
106             e.printStackTrace();
107         }
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>() {
111             
112             @Override
113             public boolean isSuccessful() {
114                 return true;
115             }
116             
117             @Override
118             public CompositeNode getResult() {
119                 return loadedCompositeNode;
120             }
121             
122             @Override
123             public Collection<RpcError> getErrors() {
124                 return null;
125             }
126         });
127         
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);
132         
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());
135     }
136     
137     @Test
138     public void testXmlToCompositeNodeProviderExceptions() {
139         URI uri = null;
140         try {
141             uri = new URI("/operations/" + URLEncoder.encode("ietf-interfaces:interfaces/interface/eth0", Charsets.US_ASCII.name()).toString());
142         } catch (UnsupportedEncodingException | URISyntaxException e) {
143             e.printStackTrace();
144         }
145         
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());
149         
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());
153     }
154
155     @Override
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());
161         
162         ResourceConfig resourceConfig = new ResourceConfig();
163         resourceConfig = resourceConfig.registerInstances(restconfImpl, StructuredDataToXmlProvider.INSTANCE, XmlToCompositeNodeProvider.INSTANCE);
164         return resourceConfig;
165     }
166
167 }