fix of Bug 314
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / test / java / org / opendaylight / controller / sal / restconf / impl / test / RestPostOperationTest.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.XML;
8 import static org.opendaylight.controller.sal.restconf.impl.test.RestOperationUtils.createUri;
9
10 import java.io.IOException;
11 import java.io.InputStream;
12 import java.io.UnsupportedEncodingException;
13 import java.net.URI;
14 import java.net.URISyntaxException;
15 import java.util.concurrent.Future;
16
17 import javax.ws.rs.client.Entity;
18 import javax.ws.rs.core.Application;
19 import javax.ws.rs.core.MediaType;
20
21 import org.glassfish.jersey.server.ResourceConfig;
22 import org.glassfish.jersey.test.JerseyTest;
23 import org.junit.BeforeClass;
24 import org.junit.Test;
25 import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
26 import org.opendaylight.controller.sal.core.api.mount.MountInstance;
27 import org.opendaylight.controller.sal.core.api.mount.MountService;
28 import org.opendaylight.controller.sal.rest.api.Draft02;
29 import org.opendaylight.controller.sal.rest.impl.JsonToCompositeNodeProvider;
30 import org.opendaylight.controller.sal.rest.impl.StructuredDataToJsonProvider;
31 import org.opendaylight.controller.sal.rest.impl.StructuredDataToXmlProvider;
32 import org.opendaylight.controller.sal.rest.impl.XmlToCompositeNodeProvider;
33 import org.opendaylight.controller.sal.restconf.impl.BrokerFacade;
34 import org.opendaylight.controller.sal.restconf.impl.CompositeNodeWrapper;
35 import org.opendaylight.controller.sal.restconf.impl.ControllerContext;
36 import org.opendaylight.controller.sal.restconf.impl.RestconfImpl;
37 import org.opendaylight.yangtools.yang.common.QName;
38 import org.opendaylight.yangtools.yang.common.RpcResult;
39 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
40 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
41 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
42
43 public class RestPostOperationTest extends JerseyTest {
44
45     private static String xmlDataAbsolutePath;
46     private static String xmlDataInterfaceAbsolutePath;
47     private static String xmlDataRpcInput;
48     private static CompositeNodeWrapper cnSnDataOutput;
49     private static String xmlData2;
50     private static String xmlData3;
51
52     private static ControllerContext controllerContext;
53     private static BrokerFacade brokerFacade;
54     private static RestconfImpl restconfImpl;
55     private static SchemaContext schemaContextYangsIetf;
56     private static SchemaContext schemaContextTestModule;
57
58     @BeforeClass
59     public static void init() throws URISyntaxException, IOException {
60         schemaContextYangsIetf = TestUtils.loadSchemaContext("/full-versions/yangs");
61         schemaContextTestModule = TestUtils.loadSchemaContext("/full-versions/test-module");
62         controllerContext = ControllerContext.getInstance();
63         brokerFacade = mock(BrokerFacade.class);
64         restconfImpl = RestconfImpl.getInstance();
65         restconfImpl.setBroker(brokerFacade);
66         restconfImpl.setControllerContext(controllerContext);
67         loadData();
68     }
69
70     @Override
71     protected Application configure() {
72         /* enable/disable Jersey logs to console */
73 //        enable(TestProperties.LOG_TRAFFIC);
74 //        enable(TestProperties.DUMP_ENTITY);
75 //        enable(TestProperties.RECORD_LOG_LEVEL);
76 //        set(TestProperties.RECORD_LOG_LEVEL, Level.ALL.intValue());
77         ResourceConfig resourceConfig = new ResourceConfig();
78         resourceConfig = resourceConfig.registerInstances(restconfImpl, StructuredDataToXmlProvider.INSTANCE,
79                 StructuredDataToJsonProvider.INSTANCE, XmlToCompositeNodeProvider.INSTANCE,
80                 JsonToCompositeNodeProvider.INSTANCE);
81         return resourceConfig;
82     }
83
84     @Test
85     public void postOperationsStatusCodes() throws UnsupportedEncodingException {
86         controllerContext.setSchemas(schemaContextTestModule);
87         mockInvokeRpc(cnSnDataOutput, true);
88         String uri = createUri("/operations/", "test-module:rpc-test");
89         assertEquals(200, post(uri, MediaType.APPLICATION_XML, xmlDataRpcInput));
90         
91         mockInvokeRpc(null, true);
92         assertEquals(204, post(uri, MediaType.APPLICATION_XML, xmlDataRpcInput));
93         
94         mockInvokeRpc(null, false);
95         assertEquals(500, post(uri, MediaType.APPLICATION_XML, xmlDataRpcInput));
96         
97         uri = createUri("/operations/", "test-module:rpc-wrongtest");
98         assertEquals(404, post(uri, MediaType.APPLICATION_XML, xmlDataRpcInput));
99     }
100
101     @Test
102     public void postConfigOnlyStatusCodes() throws UnsupportedEncodingException {
103         controllerContext.setSchemas(schemaContextYangsIetf);
104         mockCommitConfigurationDataPostMethod(TransactionStatus.COMMITED);
105         String uri = createUri("/config", "");
106         assertEquals(204, post(uri, MediaType.APPLICATION_XML, xmlDataAbsolutePath));
107         
108         mockCommitConfigurationDataPostMethod(null);
109         assertEquals(202, post(uri, MediaType.APPLICATION_XML, xmlDataAbsolutePath));
110         
111         mockCommitConfigurationDataPostMethod(TransactionStatus.FAILED);
112         assertEquals(500, post(uri, MediaType.APPLICATION_XML, xmlDataAbsolutePath));
113     }
114
115     @Test
116     public void postConfigStatusCodes() throws UnsupportedEncodingException {
117         controllerContext.setSchemas(schemaContextYangsIetf);
118         mockCommitConfigurationDataPostMethod(TransactionStatus.COMMITED);
119         String uri = createUri("/config/", "ietf-interfaces:interfaces");
120         assertEquals(204, post(uri, MediaType.APPLICATION_XML, xmlDataInterfaceAbsolutePath));
121         
122         mockCommitConfigurationDataPostMethod(null);
123         assertEquals(202, post(uri, MediaType.APPLICATION_XML, xmlDataInterfaceAbsolutePath));
124         
125         mockCommitConfigurationDataPostMethod(TransactionStatus.FAILED);
126         assertEquals(500, post(uri, MediaType.APPLICATION_XML, xmlDataInterfaceAbsolutePath));
127     }
128
129     @Test
130     public void postDatastoreStatusCodes() throws UnsupportedEncodingException {
131         controllerContext.setSchemas(schemaContextYangsIetf);
132         mockCommitConfigurationDataPostMethod(TransactionStatus.COMMITED);
133         String uri = createUri("/datastore/", "ietf-interfaces:interfaces");
134         assertEquals(204, post(uri, MediaType.APPLICATION_XML, xmlDataInterfaceAbsolutePath));
135         
136         mockCommitConfigurationDataPostMethod(null);
137         assertEquals(202, post(uri, MediaType.APPLICATION_XML, xmlDataInterfaceAbsolutePath));
138         
139         mockCommitConfigurationDataPostMethod(TransactionStatus.FAILED);
140         assertEquals(500, post(uri, MediaType.APPLICATION_XML, xmlDataInterfaceAbsolutePath));
141     }
142
143     @Test
144     public void postDataViaUrlMountPoint() throws UnsupportedEncodingException {
145         controllerContext.setSchemas(schemaContextYangsIetf);
146         RpcResult<TransactionStatus> rpcResult = new DummyRpcResult.Builder<TransactionStatus>().result(TransactionStatus.COMMITED)
147                 .build();
148         Future<RpcResult<TransactionStatus>> dummyFuture = DummyFuture.builder().rpcResult(rpcResult).build();
149         when(brokerFacade.commitConfigurationDataPostBehindMountPoint(any(MountInstance.class),
150                         any(InstanceIdentifier.class), any(CompositeNode.class))).thenReturn(dummyFuture);
151
152         MountInstance mountInstance = mock(MountInstance.class);
153         when(mountInstance.getSchemaContext()).thenReturn(schemaContextTestModule);
154         MountService mockMountService = mock(MountService.class);
155         when(mockMountService.getMountPoint(any(InstanceIdentifier.class))).thenReturn(mountInstance);
156
157         ControllerContext.getInstance().setMountService(mockMountService);
158
159         String uri = createUri("/config/", "ietf-interfaces:interfaces/interface/0/yang-ext:mount");
160         assertEquals(204, post(uri, Draft02.MediaTypes.DATA + XML, xmlData2));
161         uri = createUri("/config/", "ietf-interfaces:interfaces/interface/0/yang-ext:mount/test-module:cont");
162         assertEquals(204, post(uri, Draft02.MediaTypes.DATA + XML, xmlData3));
163     }
164     
165     private void mockInvokeRpc(CompositeNode result, boolean sucessful) {
166         RpcResult<CompositeNode> rpcResult = new DummyRpcResult.Builder<CompositeNode>().result(result)
167                 .isSuccessful(sucessful).build();
168         when(brokerFacade.invokeRpc(any(QName.class), any(CompositeNode.class))).thenReturn(rpcResult);
169     }
170
171     private void mockCommitConfigurationDataPostMethod(TransactionStatus statusName) {
172         RpcResult<TransactionStatus> rpcResult = new DummyRpcResult.Builder<TransactionStatus>().result(statusName)
173                 .build();
174         Future<RpcResult<TransactionStatus>> dummyFuture = null;
175         if (statusName != null) {
176             dummyFuture = DummyFuture.builder().rpcResult(rpcResult).build();
177         } else {
178             dummyFuture = DummyFuture.builder().build();
179         }
180
181         when(brokerFacade.commitConfigurationDataPost(any(InstanceIdentifier.class), any(CompositeNode.class)))
182                 .thenReturn(dummyFuture);
183     }
184     
185     private int post(String uri, String mediaType, String data) {
186         return target(uri).request(mediaType).post(Entity.entity(data, mediaType)).getStatus();
187     }
188
189     private static void loadData() throws IOException, URISyntaxException {
190         InputStream xmlStream = RestconfImplTest.class.getResourceAsStream("/parts/ietf-interfaces_interfaces_absolute_path.xml");
191         xmlDataAbsolutePath = TestUtils.getDocumentInPrintableForm(TestUtils.loadDocumentFrom(xmlStream));
192         xmlStream = RestconfImplTest.class.getResourceAsStream("/parts/ietf-interfaces_interfaces_interface_absolute_path.xml");
193         xmlDataInterfaceAbsolutePath = TestUtils.getDocumentInPrintableForm(TestUtils.loadDocumentFrom(xmlStream));
194         String xmlPathRpcInput = RestconfImplTest.class.getResource("/full-versions/test-data2/data-rpc-input.xml")
195                 .getPath();
196         xmlDataRpcInput = TestUtils.loadTextFile(xmlPathRpcInput);
197         cnSnDataOutput = prepareCnSnRpcOutput();
198         String data2Input = RestconfImplTest.class.getResource("/full-versions/test-data2/data2.xml").getPath();
199         xmlData2 = TestUtils.loadTextFile(data2Input);
200         String data3Input = RestconfImplTest.class.getResource("/full-versions/test-data2/data3.xml").getPath();
201         xmlData3 = TestUtils.loadTextFile(data3Input);
202     }
203
204     private static CompositeNodeWrapper prepareCnSnRpcOutput() throws URISyntaxException {
205         CompositeNodeWrapper cnSnDataOutput = new CompositeNodeWrapper(new URI("test:module"), "output");
206         CompositeNodeWrapper cont = new CompositeNodeWrapper(new URI("test:module"), "cont-output");
207         cnSnDataOutput.addValue(cont);
208         cnSnDataOutput.unwrap();
209         return cnSnDataOutput;
210     }
211 }