Merge "Add shutdown hook."
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / test / java / org / opendaylight / controller / sal / restconf / impl / test / RestPutOperationTest.java
1 package org.opendaylight.controller.sal.restconf.impl.test;
2
3 import static org.junit.Assert.assertEquals;
4 import static org.mockito.Matchers.any;
5 import static org.mockito.Mockito.mock;
6 import static org.mockito.Mockito.when;
7 import static org.opendaylight.controller.sal.restconf.impl.test.RestOperationUtils.JSON;
8 import static org.opendaylight.controller.sal.restconf.impl.test.RestOperationUtils.XML;
9 import static org.opendaylight.controller.sal.restconf.impl.test.RestOperationUtils.createUri;
10 import static org.opendaylight.controller.sal.restconf.impl.test.RestOperationUtils.entity;
11
12 import java.io.FileNotFoundException;
13 import java.io.IOException;
14 import java.io.InputStream;
15 import java.io.UnsupportedEncodingException;
16 import java.net.URISyntaxException;
17 import java.util.concurrent.Future;
18 import java.util.logging.Level;
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.core.api.mount.MountService;
33 import org.opendaylight.controller.sal.rest.api.Draft01;
34 import org.opendaylight.controller.sal.rest.api.Draft02;
35 import org.opendaylight.controller.sal.rest.impl.JsonToCompositeNodeProvider;
36 import org.opendaylight.controller.sal.rest.impl.StructuredDataToJsonProvider;
37 import org.opendaylight.controller.sal.rest.impl.StructuredDataToXmlProvider;
38 import org.opendaylight.controller.sal.rest.impl.XmlToCompositeNodeProvider;
39 import org.opendaylight.controller.sal.restconf.impl.BrokerFacade;
40 import org.opendaylight.controller.sal.restconf.impl.ControllerContext;
41 import org.opendaylight.controller.sal.restconf.impl.RestconfImpl;
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.SchemaContext;
46
47 public class RestPutOperationTest extends JerseyTest {
48
49     private static String xmlData;
50     private static String jsonData;
51
52     private static BrokerFacade brokerFacade;
53     private static RestconfImpl restconfImpl;
54     private static SchemaContext schemaContextYangsIetf;
55     private static SchemaContext schemaContextTestModule;
56
57     @BeforeClass
58     public static void init() throws IOException {
59         schemaContextYangsIetf = TestUtils.loadSchemaContext("/full-versions/yangs");
60         schemaContextTestModule = TestUtils.loadSchemaContext("/full-versions/test-module");
61         ControllerContext controllerContext = ControllerContext.getInstance();
62         controllerContext.setSchemas(schemaContextYangsIetf);
63         brokerFacade = mock(BrokerFacade.class);
64         restconfImpl = RestconfImpl.getInstance();
65         restconfImpl.setBroker(brokerFacade);
66         restconfImpl.setControllerContext(controllerContext);
67         loadData();
68     }
69
70     @Before
71     public void logs() throws IOException {
72         /* enable/disable Jersey logs to console */
73         /*
74          * List<LogRecord> loggedRecords = getLoggedRecords(); for (LogRecord l
75          * : loggedRecords) { System.out.println(l.getMessage()); }
76          */
77     }
78
79     @Override
80     protected Application configure() {
81         /* enable/disable Jersey logs to console */
82         /*
83          * enable(TestProperties.LOG_TRAFFIC);
84          */
85         enable(TestProperties.DUMP_ENTITY);
86         enable(TestProperties.RECORD_LOG_LEVEL);
87         set(TestProperties.RECORD_LOG_LEVEL, Level.ALL.intValue());
88
89         ResourceConfig resourceConfig = new ResourceConfig();
90         resourceConfig = resourceConfig.registerInstances(restconfImpl, StructuredDataToXmlProvider.INSTANCE,
91                 StructuredDataToJsonProvider.INSTANCE, XmlToCompositeNodeProvider.INSTANCE,
92                 JsonToCompositeNodeProvider.INSTANCE);
93         return resourceConfig;
94     }
95
96     /**
97      * Test method
98      * {@link RestconfImpl#updateConfigurationData(String, CompositeNode)} of
99      * RestconfImpl for "/config/...identifier..." URL. Return status code is
100      * 200.
101      * 
102      */
103     @Test
104     public void putConfigDataViaUrlTest200() throws UnsupportedEncodingException {
105         mockCommitConfigurationDataPutMethod(TransactionStatus.COMMITED);
106         putDataViaUrlTest("/config/", Draft02.MediaTypes.DATA + JSON, jsonData, 200);
107         putDataViaUrlTest("/config/", Draft02.MediaTypes.DATA + XML, xmlData, 200);
108         putDataViaUrlTest("/config/", MediaType.APPLICATION_JSON, jsonData, 200);
109         putDataViaUrlTest("/config/", MediaType.APPLICATION_XML, xmlData, 200);
110         putDataViaUrlTest("/config/", MediaType.TEXT_XML, xmlData, 200);
111
112     }
113
114     /**
115      * Test method
116      * {@link RestconfImpl#updateConfigurationData(String, CompositeNode)} of
117      * RestconfImpl for "/config/...identifier..." URL. Return status code is
118      * 500.
119      * 
120      */
121     @Test
122     public void putConfigDataViaUrlTest500() throws UnsupportedEncodingException {
123         mockCommitConfigurationDataPutMethod(TransactionStatus.FAILED);
124         putDataViaUrlTest("/config/", Draft02.MediaTypes.DATA + JSON, jsonData, 500);
125         putDataViaUrlTest("/config/", Draft02.MediaTypes.DATA + XML, xmlData, 500);
126         putDataViaUrlTest("/config/", MediaType.APPLICATION_JSON, jsonData, 500);
127         putDataViaUrlTest("/config/", MediaType.APPLICATION_XML, xmlData, 500);
128         putDataViaUrlTest("/config/", MediaType.TEXT_XML, xmlData, 500);
129
130     }
131
132     /**
133      * Test method
134      * {@link RestconfImpl#updateConfigurationData(String, CompositeNode)} of
135      * RestconfImpl for "/datastore/...identifier..." URL. Return status code is
136      * 200.
137      * 
138      */
139     @Test
140     public void putDatastoreDataViaUrlTest200() throws UnsupportedEncodingException {
141         mockCommitConfigurationDataPutMethod(TransactionStatus.COMMITED);
142         putDataViaUrlTest("/datastore/", Draft01.MediaTypes.DATA + JSON, jsonData, 200);
143         putDataViaUrlTest("/datastore/", Draft01.MediaTypes.DATA + XML, xmlData, 200);
144         putDataViaUrlTest("/datastore/", MediaType.APPLICATION_JSON, jsonData, 200);
145         putDataViaUrlTest("/datastore/", MediaType.APPLICATION_XML, xmlData, 200);
146         putDataViaUrlTest("/datastore/", MediaType.TEXT_XML, xmlData, 200);
147     }
148
149     /**
150      * Test method
151      * {@link RestconfImpl#updateConfigurationData(String, CompositeNode)} of
152      * RestconfImpl for "/datastore/...identifier..." URL. Return status code is
153      * 500.
154      * 
155      */
156     @Test
157     public void putDatastoreDataViaUrlTest500() throws UnsupportedEncodingException {
158         mockCommitConfigurationDataPutMethod(TransactionStatus.FAILED);
159         putDataViaUrlTest("/datastore/", Draft01.MediaTypes.DATA + JSON, jsonData, 500);
160         putDataViaUrlTest("/datastore/", Draft01.MediaTypes.DATA + XML, xmlData, 500);
161         putDataViaUrlTest("/datastore/", MediaType.APPLICATION_JSON, jsonData, 500);
162         putDataViaUrlTest("/datastore/", MediaType.APPLICATION_XML, xmlData, 500);
163         putDataViaUrlTest("/datastore/", MediaType.TEXT_XML, xmlData, 500);
164     }
165
166     @Test
167     public void testRpcResultCommitedToStatusCodesWithMountPoint() throws UnsupportedEncodingException,
168             FileNotFoundException, URISyntaxException {
169
170         mockCommitConfigurationDataPutMethod(TransactionStatus.COMMITED);
171
172         InputStream xmlStream = RestconfImplTest.class.getResourceAsStream("/full-versions/test-data2/data2.xml");
173         String xml = TestUtils.getDocumentInPrintableForm(TestUtils.loadDocumentFrom(xmlStream));
174         Entity<String> entity = Entity.entity(xml, Draft02.MediaTypes.DATA + XML);
175
176         MountService mockMountService = mock(MountService.class);
177         when(mockMountService.getMountPoint(any(InstanceIdentifier.class))).thenReturn(
178                 new DummyMountInstanceImpl.Builder().setSchemaContext(schemaContextTestModule).build());
179
180         ControllerContext.getInstance().setMountService(mockMountService);
181
182         String uri = createUri("/config/", "ietf-interfaces:interfaces/interface/0/test-module:cont");
183         Response response = target(uri).request(Draft02.MediaTypes.DATA + XML).put(entity);
184         assertEquals(200, response.getStatus());
185     }
186
187     private void putDataViaUrlTest(String uriPrefix, String mediaType, String data, int responseStatus)
188             throws UnsupportedEncodingException {
189         String uri = createUri(uriPrefix, "ietf-interfaces:interfaces/interface/eth0");
190         Response response = target(uri).request(mediaType).put(entity(data, mediaType));
191         assertEquals(responseStatus, response.getStatus());
192     }
193
194     private static void loadData() throws IOException {
195         InputStream xmlStream = RestconfImplTest.class.getResourceAsStream("/parts/ietf-interfaces_interfaces.xml");
196         xmlData = TestUtils.getDocumentInPrintableForm(TestUtils.loadDocumentFrom(xmlStream));
197
198         String jsonPath = RestconfImplTest.class.getResource("/parts/ietf-interfaces_interfaces.json").getPath();
199         jsonData = TestUtils.loadTextFile(jsonPath);
200     }
201
202     private void mockCommitConfigurationDataPutMethod(TransactionStatus statusName) {
203         RpcResult<TransactionStatus> rpcResult = new DummyRpcResult.Builder<TransactionStatus>().result(statusName)
204                 .build();
205         Future<RpcResult<TransactionStatus>> dummyFuture = DummyFuture.builder().rpcResult(rpcResult).build();
206         when(brokerFacade.commitConfigurationDataPut(any(InstanceIdentifier.class), any(CompositeNode.class)))
207                 .thenReturn(dummyFuture);
208     }
209
210 }