sal-rest-connector test refactoring
[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.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertNotNull;
5 import static org.mockito.Matchers.any;
6 import static org.mockito.Mockito.mock;
7 import static org.mockito.Mockito.when;
8
9 import java.io.FileNotFoundException;
10 import java.io.InputStream;
11 import java.io.UnsupportedEncodingException;
12 import java.net.URI;
13 import java.net.URISyntaxException;
14 import java.net.URLEncoder;
15 import java.util.*;
16 import java.util.concurrent.Future;
17 import java.util.logging.Level;
18 import java.util.logging.LogRecord;
19
20 import javax.ws.rs.client.Entity;
21 import javax.ws.rs.core.Application;
22 import javax.ws.rs.core.MediaType;
23 import javax.ws.rs.core.Response;
24
25 import org.glassfish.jersey.server.ResourceConfig;
26 import org.glassfish.jersey.test.JerseyTest;
27 import org.glassfish.jersey.test.TestProperties;
28 import org.junit.Before;
29 import org.junit.BeforeClass;
30 import org.junit.Test;
31 import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
32 import org.opendaylight.controller.sal.rest.api.Draft01;
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.*;
37 import org.opendaylight.yangtools.yang.common.RpcResult;
38 import org.opendaylight.yangtools.yang.data.api.*;
39 import org.opendaylight.yangtools.yang.model.api.Module;
40 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
41
42 import com.google.common.base.Charsets;
43
44 public class XmlProvidersTest extends JerseyTest {
45
46     private static ControllerContext controllerContext;
47     private static BrokerFacade brokerFacade;
48     private static RestconfImpl restconfImpl;
49     private static final MediaType MEDIA_TYPE = new MediaType("application", "vnd.yang.data+xml");
50     private static final MediaType MEDIA_TYPE_DRAFT02 = new MediaType("application", "yang.data+xml");
51
52     @BeforeClass
53     public static void init() throws FileNotFoundException {
54         Set<Module> allModules = TestUtils.loadModulesFrom("/full-versions/yangs");
55         assertNotNull(allModules);
56         SchemaContext schemaContext = TestUtils.loadSchemaContext(allModules);
57         controllerContext = ControllerContext.getInstance();
58         controllerContext.setSchemas(schemaContext);
59         brokerFacade = mock(BrokerFacade.class);
60         restconfImpl = RestconfImpl.getInstance();
61         restconfImpl.setBroker(brokerFacade);
62         restconfImpl.setControllerContext(controllerContext);
63     }
64
65     @Before
66     public void logs() {
67         List<LogRecord> loggedRecords = getLoggedRecords();
68         for (LogRecord l : loggedRecords) {
69             System.out.println(l.getMessage());
70         }
71     }
72
73     @Test
74     public void testStructuredDataToXmlProvider() throws FileNotFoundException, UnsupportedEncodingException {
75         String uri = createUri("/datastore/", "ietf-interfaces:interfaces/interface/eth0");
76
77         CompositeNode loadedCompositeNode = prepareCompositeNodeWithIetfInterfacesInterfacesData();
78         when(brokerFacade.readOperationalData(any(InstanceIdentifier.class))).thenReturn(loadedCompositeNode);
79
80         Response response = target(uri).request(MEDIA_TYPE).get();
81         assertEquals(200, response.getStatus());
82     }
83
84     private CompositeNode prepareCompositeNodeWithIetfInterfacesInterfacesData() {
85         CompositeNode intface;
86         try {
87             intface = new CompositeNodeWrapper(new URI("interface"), "interface");
88             List<Node<?>> childs = new ArrayList<>();
89
90             childs.add(new SimpleNodeWrapper(new URI("name"), "name", "eth0"));
91             childs.add(new SimpleNodeWrapper(new URI("type"), "type", "ethernetCsmacd"));
92             childs.add(new SimpleNodeWrapper(new URI("enabled"), "enabled", Boolean.FALSE));
93             childs.add(new SimpleNodeWrapper(new URI("description"), "description", "some interface"));
94             intface.setValue(childs);
95             return intface;
96         } catch (URISyntaxException e) {
97         }
98
99         return null;
100     }
101
102     @Test
103     public void testBadFormatXmlToCompositeNodeProvider() throws UnsupportedEncodingException, URISyntaxException {
104         String uri = createUri("/operations/", "ietf-interfaces:interfaces/interface/eth0");
105
106         Response response = target(uri).request(Draft01.MediaTypes.DATA + RestconfService.XML).post(
107                 Entity.entity("<SimpleNode/>", MEDIA_TYPE));
108         assertEquals(400, response.getStatus());
109
110         response = target(uri).request(Draft01.MediaTypes.DATA + RestconfService.XML).post(
111                 Entity.entity("<SimpleNode>", MEDIA_TYPE));
112         assertEquals(400, response.getStatus());
113     }
114
115     @Test
116     public void testXmlToCompositeNode404NotFound() throws UnsupportedEncodingException, URISyntaxException {
117         String uri = createUri("/datastore/", "ietf-interfaces:interfaces/interface/eth0");
118
119         when(brokerFacade.readOperationalData(any(InstanceIdentifier.class))).thenReturn(null);
120
121         Response response = target(uri).request(Draft01.MediaTypes.DATA + RestconfService.XML).get();
122         assertEquals(404, response.getStatus());
123     }
124
125     @Test
126     public void testXmlToCompositeNode400() throws UnsupportedEncodingException, URISyntaxException {
127         String uri = createUri("/datastore/", "simple-nodes:user/name");
128
129         when(brokerFacade.readOperationalData(any(InstanceIdentifier.class))).thenReturn(null);
130
131         Response response = target(uri).request(Draft01.MediaTypes.DATA + RestconfService.XML).get();
132         assertEquals(400, response.getStatus());
133     }
134
135     @Test
136     public void testRpcResultCommitedToStatusCodes() throws UnsupportedEncodingException {
137         InputStream xmlStream = RestconfImplTest.class.getResourceAsStream("/parts/ietf-interfaces_interfaces.xml");
138         String xml = TestUtils.getDocumentInPrintableForm(TestUtils.loadDocumentFrom(xmlStream));
139         Entity<String> entity = Entity.entity(xml, MEDIA_TYPE_DRAFT02);
140         RpcResult<TransactionStatus> rpcResult = new DummyRpcResult.Builder<TransactionStatus>().result(
141                 TransactionStatus.COMMITED).build();
142         Future<RpcResult<TransactionStatus>> dummyFuture = DummyFuture.builder().rpcResult(rpcResult).build();
143         when(brokerFacade.commitOperationalDataPut(any(InstanceIdentifier.class), any(CompositeNode.class)))
144                 .thenReturn(dummyFuture);
145         when(brokerFacade.commitConfigurationDataPut(any(InstanceIdentifier.class), any(CompositeNode.class)))
146                 .thenReturn(dummyFuture);
147
148         String uri = createUri("/config/", "ietf-interfaces:interfaces/interface/eth0");
149         Response response = target(uri).request(MEDIA_TYPE_DRAFT02).put(entity);
150         assertEquals(200, response.getStatus());
151         response = target(uri).request(MEDIA_TYPE_DRAFT02).post(entity);
152         assertEquals(204, response.getStatus());
153
154         uri = createUri("/config/", "ietf-interfaces:interfaces/interface/eth0");
155         response = target(uri).request(MEDIA_TYPE_DRAFT02).put(entity);
156         assertEquals(200, response.getStatus());
157         response = target(uri).request(MEDIA_TYPE_DRAFT02).post(entity);
158         assertEquals(204, response.getStatus());
159
160         uri = createUri("/datastore/", "ietf-interfaces:interfaces/interface/eth0");
161         response = target(uri).request(MEDIA_TYPE).put(entity);
162         assertEquals(200, response.getStatus());
163         response = target(uri).request(MEDIA_TYPE).post(entity);
164         assertEquals(204, response.getStatus());
165     }
166
167     @Test
168     public void testRpcResultOtherToStatusCodes() throws UnsupportedEncodingException {
169         InputStream xmlStream = RestconfImplTest.class.getResourceAsStream("/parts/ietf-interfaces_interfaces.xml");
170         String xml = TestUtils.getDocumentInPrintableForm(TestUtils.loadDocumentFrom(xmlStream));
171         Entity<String> entity = Entity.entity(xml, MEDIA_TYPE_DRAFT02);
172         RpcResult<TransactionStatus> rpcResult = new DummyRpcResult.Builder<TransactionStatus>().result(
173                 TransactionStatus.FAILED).build();
174         Future<RpcResult<TransactionStatus>> dummyFuture = DummyFuture.builder().rpcResult(rpcResult).build();
175         when(brokerFacade.commitOperationalDataPut(any(InstanceIdentifier.class), any(CompositeNode.class)))
176                 .thenReturn(dummyFuture);
177         when(brokerFacade.commitConfigurationDataPut(any(InstanceIdentifier.class), any(CompositeNode.class)))
178                 .thenReturn(dummyFuture);
179
180         String uri = createUri("/config/", "ietf-interfaces:interfaces/interface/eth0");
181         Response response = target(uri).request(MEDIA_TYPE_DRAFT02).put(entity);
182         assertEquals(500, response.getStatus());
183         response = target(uri).request(MEDIA_TYPE_DRAFT02).post(entity);
184         assertEquals(500, response.getStatus());
185
186         uri = createUri("/config/", "ietf-interfaces:interfaces/interface/eth0");
187         response = target(uri).request(MEDIA_TYPE_DRAFT02).put(entity);
188         assertEquals(500, response.getStatus());
189         response = target(uri).request(MEDIA_TYPE_DRAFT02).post(entity);
190         assertEquals(500, response.getStatus());
191
192         uri = createUri("/datastore/", "ietf-interfaces:interfaces/interface/eth0");
193         response = target(uri).request(MEDIA_TYPE).put(entity);
194         assertEquals(500, response.getStatus());
195         response = target(uri).request(MEDIA_TYPE).post(entity);
196         assertEquals(500, response.getStatus());
197     }
198
199     private String createUri(String prefix, String encodedPart) throws UnsupportedEncodingException {
200         return URI.create(prefix + URLEncoder.encode(encodedPart, Charsets.US_ASCII.name()).toString()).toASCIIString();
201     }
202
203     @Override
204     protected Application configure() {
205         enable(TestProperties.LOG_TRAFFIC);
206         enable(TestProperties.DUMP_ENTITY);
207         enable(TestProperties.RECORD_LOG_LEVEL);
208         set(TestProperties.RECORD_LOG_LEVEL, Level.ALL.intValue());
209
210         ResourceConfig resourceConfig = new ResourceConfig();
211         resourceConfig = resourceConfig.registerInstances(restconfImpl, StructuredDataToXmlProvider.INSTANCE,
212                 XmlToCompositeNodeProvider.INSTANCE);
213         return resourceConfig;
214     }
215
216 }