baf226712ffbb255550948c3c5f36160d4c30262
[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.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;
22
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;
30
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;
55
56 import com.google.common.base.Charsets;
57
58 public class XmlProvidersTest extends JerseyTest {
59
60     private static ControllerContext controllerContext;
61     private static BrokerFacade brokerFacade;
62     private static RestconfImpl restconfImpl;
63
64     @BeforeClass
65     public static void init() {
66         Set<Module> allModules = null;
67         try {
68             allModules = TestUtils.loadModules(RestconfImplTest.class.getResource("/full-versions/yangs").getPath());
69         } catch (FileNotFoundException e) {
70             e.printStackTrace();
71         }
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);
79     }
80
81     @Before
82     public void logs() {
83         List<LogRecord> loggedRecords = getLoggedRecords();
84         for (LogRecord l : loggedRecords) {
85             System.out.println(l.getMessage());
86         }
87     }
88
89     @Test
90     public void testStructuredDataToXmlProvider() throws FileNotFoundException {
91         URI uri = null;
92         try {
93             uri = new URI("/datastore/" + URLEncoder.encode("ietf-interfaces:interfaces/interface/eth0", Charsets.US_ASCII.name()).toString());
94         } catch (UnsupportedEncodingException | URISyntaxException e) {
95             e.printStackTrace();
96         }
97         
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);
101         
102         Response response = target(uri.toASCIIString()).request(MediaTypes.API+RestconfService.XML).get();
103         assertEquals(200, response.getStatus());
104     }
105
106     @Test
107     public void testXmlToCompositeNodeProvider() throws ParserConfigurationException, SAXException, IOException {
108         URI uri = null;
109         try {
110             uri = new URI("/config/" + URLEncoder.encode("ietf-interfaces:interfaces/interface/eth0", Charsets.US_ASCII.name()).toString());
111         } catch (UnsupportedEncodingException | URISyntaxException e) {
112             e.printStackTrace();
113         }
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>>() {
117             @Override
118             public boolean cancel(boolean mayInterruptIfRunning) {
119                 return false;
120             }
121             @Override
122             public boolean isCancelled() {
123                 return false;
124             }
125             @Override
126             public boolean isDone() {
127                 return false;
128             }
129             @Override
130             public RpcResult<TransactionStatus> get() throws InterruptedException, ExecutionException {
131                 return null;
132             }
133             @Override
134             public RpcResult<TransactionStatus> get(long timeout, TimeUnit unit) throws InterruptedException,
135                     ExecutionException, TimeoutException {
136                 return null;
137             }
138         });
139         
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);
144         
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());
147     }
148     
149     @Test
150     public void testXmlToCompositeNodeProviderExceptions() {
151         URI uri = null;
152         try {
153             uri = new URI("/operations/" + URLEncoder.encode("ietf-interfaces:interfaces/interface/eth0", Charsets.US_ASCII.name()).toString());
154         } catch (UnsupportedEncodingException | URISyntaxException e) {
155             e.printStackTrace();
156         }
157         
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());
161         
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());
165     }
166     
167     @Test
168     public void testXmlToCompositeNode404NotFound() {
169         URI uri = null;
170         try {
171             uri = new URI("/datastore/" + URLEncoder.encode("ietf-interfaces:interfaces/interface/eth0", Charsets.US_ASCII.name()).toString());
172         } catch (UnsupportedEncodingException | URISyntaxException e) {
173             e.printStackTrace();
174         }
175         
176         when(brokerFacade.readOperationalData(any(InstanceIdentifier.class))).thenReturn(null);
177         
178         Response response = target(uri.toASCIIString()).request(MediaTypes.API+RestconfService.XML).get();
179         assertEquals(404, response.getStatus());
180     }
181
182     @Override
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());
188         
189         ResourceConfig resourceConfig = new ResourceConfig();
190         resourceConfig = resourceConfig.registerInstances(restconfImpl, StructuredDataToXmlProvider.INSTANCE, XmlToCompositeNodeProvider.INSTANCE);
191         return resourceConfig;
192     }
193
194 }