9d004362d29d0b64b9a8636f51430ccfa46d8ca3
[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.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;
50
51 import com.google.common.base.Charsets;
52
53 public class XmlProvidersTest extends JerseyTest {
54
55     private static ControllerContext controllerContext;
56     private static BrokerFacade brokerFacade;
57     private static RestconfImpl restconfImpl;
58
59     @BeforeClass
60     public static void init() {
61         Set<Module> allModules = null;
62         try {
63             allModules = TestUtils.loadModules(RestconfImplTest.class.getResource("/full-versions/yangs").getPath());
64         } catch (FileNotFoundException e) {
65             e.printStackTrace();
66         }
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);
74     }
75
76 //    @Before
77     public void logs() {
78         List<LogRecord> loggedRecords = getLoggedRecords();
79         for (LogRecord l : loggedRecords) {
80             System.out.println(l.getMessage());
81         }
82     }
83
84     @Test
85     public void testStructuredDataToXmlProvider() throws FileNotFoundException {
86         URI uri = null;
87         try {
88             uri = new URI("/datastore/" + URLEncoder.encode("ietf-interfaces:interfaces/interface/eth0", Charsets.US_ASCII.name()).toString());
89         } catch (UnsupportedEncodingException | URISyntaxException e) {
90             e.printStackTrace();
91         }
92         
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);
96         
97         Response response = target(uri.toASCIIString()).request(MediaTypes.API+RestconfService.XML).get();
98         assertEquals(200, response.getStatus());
99     }
100
101     @Test
102     public void testXmlToCompositeNodeProvider() throws ParserConfigurationException, SAXException, IOException {
103         URI uri = null;
104         try {
105             uri = new URI("/operations/" + URLEncoder.encode("ietf-interfaces:interfaces/interface/eth0", Charsets.US_ASCII.name()).toString());
106         } catch (UnsupportedEncodingException | URISyntaxException e) {
107             e.printStackTrace();
108         }
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>() {
112             
113             @Override
114             public boolean isSuccessful() {
115                 return true;
116             }
117             
118             @Override
119             public CompositeNode getResult() {
120                 return loadedCompositeNode;
121             }
122             
123             @Override
124             public Collection<RpcError> getErrors() {
125                 return null;
126             }
127         });
128         
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);
133         
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());
136     }
137     
138     @Test
139     public void testXmlToCompositeNodeProviderExceptions() {
140         URI uri = null;
141         try {
142             uri = new URI("/operations/" + URLEncoder.encode("ietf-interfaces:interfaces/interface/eth0", Charsets.US_ASCII.name()).toString());
143         } catch (UnsupportedEncodingException | URISyntaxException e) {
144             e.printStackTrace();
145         }
146         
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());
150         
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());
154     }
155     
156     @Test
157     public void testXmlToCompositeNode404NotFound() {
158         URI uri = null;
159         try {
160             uri = new URI("/datastore/" + URLEncoder.encode("ietf-interfaces:interfaces/interface/eth0", Charsets.US_ASCII.name()).toString());
161         } catch (UnsupportedEncodingException | URISyntaxException e) {
162             e.printStackTrace();
163         }
164         
165         when(brokerFacade.readOperationalData(any(InstanceIdentifier.class))).thenReturn(null);
166         
167         Response response = target(uri.toASCIIString()).request(MediaTypes.API+RestconfService.XML).get();
168         assertEquals(404, response.getStatus());
169     }
170
171     @Override
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());
177         
178         ResourceConfig resourceConfig = new ResourceConfig();
179         resourceConfig = resourceConfig.registerInstances(restconfImpl, StructuredDataToXmlProvider.INSTANCE, XmlToCompositeNodeProvider.INSTANCE);
180         return resourceConfig;
181     }
182
183 }