Merge "Switch to using yangtools version of mockito-configuration"
[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 xmlDataRpcInput;
47     private static CompositeNodeWrapper cnSnDataOutput;
48     private static String xmlData2;
49
50     private static ControllerContext controllerContext;
51     private static BrokerFacade brokerFacade;
52     private static RestconfImpl restconfImpl;
53     private static SchemaContext schemaContextYangsIetf;
54     private static SchemaContext schemaContextTestModule;
55
56     @BeforeClass
57     public static void init() throws URISyntaxException, IOException {
58         schemaContextYangsIetf = TestUtils.loadSchemaContext("/full-versions/yangs");
59         schemaContextTestModule = TestUtils.loadSchemaContext("/full-versions/test-module");
60         controllerContext = ControllerContext.getInstance();
61         brokerFacade = mock(BrokerFacade.class);
62         restconfImpl = RestconfImpl.getInstance();
63         restconfImpl.setBroker(brokerFacade);
64         restconfImpl.setControllerContext(controllerContext);
65         loadData();
66     }
67
68     @Override
69     protected Application configure() {
70         /* enable/disable Jersey logs to console */
71 //        enable(TestProperties.LOG_TRAFFIC);
72 //        enable(TestProperties.DUMP_ENTITY);
73 //        enable(TestProperties.RECORD_LOG_LEVEL);
74 //        set(TestProperties.RECORD_LOG_LEVEL, Level.ALL.intValue());
75         ResourceConfig resourceConfig = new ResourceConfig();
76         resourceConfig = resourceConfig.registerInstances(restconfImpl, StructuredDataToXmlProvider.INSTANCE,
77                 StructuredDataToJsonProvider.INSTANCE, XmlToCompositeNodeProvider.INSTANCE,
78                 JsonToCompositeNodeProvider.INSTANCE);
79         return resourceConfig;
80     }
81
82     @Test
83     public void postOperationsStatusCodes() throws UnsupportedEncodingException {
84         controllerContext.setSchemas(schemaContextTestModule);
85         mockInvokeRpc(cnSnDataOutput, true);
86         String uri = createUri("/operations/", "test-module:rpc-test");
87         assertEquals(200, post(uri, MediaType.APPLICATION_XML, xmlDataRpcInput));
88         
89         mockInvokeRpc(null, true);
90         assertEquals(204, post(uri, MediaType.APPLICATION_XML, xmlDataRpcInput));
91         
92         mockInvokeRpc(null, false);
93         assertEquals(500, post(uri, MediaType.APPLICATION_XML, xmlDataRpcInput));
94         
95         uri = createUri("/operations/", "test-module:rpc-wrongtest");
96         assertEquals(404, post(uri, MediaType.APPLICATION_XML, xmlDataRpcInput));
97     }
98
99     @Test
100     public void postConfigOnlyStatusCodes() throws UnsupportedEncodingException {
101         controllerContext.setSchemas(schemaContextYangsIetf);
102         mockCommitConfigurationDataPostMethod(TransactionStatus.COMMITED);
103         String uri = createUri("/config", "");
104         assertEquals(204, post(uri, MediaType.APPLICATION_XML, xmlDataAbsolutePath));
105         
106         mockCommitConfigurationDataPostMethod(null);
107         assertEquals(202, post(uri, MediaType.APPLICATION_XML, xmlDataAbsolutePath));
108         
109         mockCommitConfigurationDataPostMethod(TransactionStatus.FAILED);
110         assertEquals(500, post(uri, MediaType.APPLICATION_XML, xmlDataAbsolutePath));
111     }
112
113     @Test
114     public void postConfigStatusCodes() throws UnsupportedEncodingException {
115         controllerContext.setSchemas(schemaContextYangsIetf);
116         mockCommitConfigurationDataPostMethod(TransactionStatus.COMMITED);
117         String uri = createUri("/config/", "ietf-interfaces:interfaces");
118         assertEquals(204, post(uri, MediaType.APPLICATION_XML, xmlDataAbsolutePath));
119         
120         mockCommitConfigurationDataPostMethod(null);
121         assertEquals(202, post(uri, MediaType.APPLICATION_XML, xmlDataAbsolutePath));
122         
123         mockCommitConfigurationDataPostMethod(TransactionStatus.FAILED);
124         assertEquals(500, post(uri, MediaType.APPLICATION_XML, xmlDataAbsolutePath));
125     }
126
127     @Test
128     public void postDatastoreStatusCodes() throws UnsupportedEncodingException {
129         controllerContext.setSchemas(schemaContextYangsIetf);
130         mockCommitConfigurationDataPostMethod(TransactionStatus.COMMITED);
131         String uri = createUri("/datastore/", "ietf-interfaces:interfaces");
132         assertEquals(204, post(uri, MediaType.APPLICATION_XML, xmlDataAbsolutePath));
133         
134         mockCommitConfigurationDataPostMethod(null);
135         assertEquals(202, post(uri, MediaType.APPLICATION_XML, xmlDataAbsolutePath));
136         
137         mockCommitConfigurationDataPostMethod(TransactionStatus.FAILED);
138         assertEquals(500, post(uri, MediaType.APPLICATION_XML, xmlDataAbsolutePath));
139     }
140
141     @Test
142     public void postDataViaUrlMountPoint() throws UnsupportedEncodingException {
143         controllerContext.setSchemas(schemaContextYangsIetf);
144         RpcResult<TransactionStatus> rpcResult = new DummyRpcResult.Builder<TransactionStatus>().result(TransactionStatus.COMMITED)
145                 .build();
146         Future<RpcResult<TransactionStatus>> dummyFuture = DummyFuture.builder().rpcResult(rpcResult).build();
147         when(brokerFacade.commitConfigurationDataPostBehindMountPoint(any(MountInstance.class),
148                         any(InstanceIdentifier.class), any(CompositeNode.class))).thenReturn(dummyFuture);
149
150         MountInstance mountInstance = mock(MountInstance.class);
151         when(mountInstance.getSchemaContext()).thenReturn(schemaContextTestModule);
152         MountService mockMountService = mock(MountService.class);
153         when(mockMountService.getMountPoint(any(InstanceIdentifier.class))).thenReturn(mountInstance);
154
155         ControllerContext.getInstance().setMountService(mockMountService);
156
157         String uri = createUri("/config/", "ietf-interfaces:interfaces/interface/0/yang-ext:mount/test-module:cont/cont1");
158         assertEquals(204, post(uri, Draft02.MediaTypes.DATA + XML, xmlData2));
159     }
160     
161     private void mockInvokeRpc(CompositeNode result, boolean sucessful) {
162         RpcResult<CompositeNode> rpcResult = new DummyRpcResult.Builder<CompositeNode>().result(result)
163                 .isSuccessful(sucessful).build();
164         when(brokerFacade.invokeRpc(any(QName.class), any(CompositeNode.class))).thenReturn(rpcResult);
165     }
166
167     private void mockCommitConfigurationDataPostMethod(TransactionStatus statusName) {
168         RpcResult<TransactionStatus> rpcResult = new DummyRpcResult.Builder<TransactionStatus>().result(statusName)
169                 .build();
170         Future<RpcResult<TransactionStatus>> dummyFuture = null;
171         if (statusName != null) {
172             dummyFuture = DummyFuture.builder().rpcResult(rpcResult).build();
173         } else {
174             dummyFuture = DummyFuture.builder().build();
175         }
176
177         when(brokerFacade.commitConfigurationDataPost(any(InstanceIdentifier.class), any(CompositeNode.class)))
178                 .thenReturn(dummyFuture);
179     }
180     
181     private int post(String uri, String mediaType, String data) {
182         return target(uri).request(mediaType).post(Entity.entity(data, mediaType)).getStatus();
183     }
184
185     private static void loadData() throws IOException, URISyntaxException {
186         InputStream xmlStream = RestconfImplTest.class.getResourceAsStream("/parts/ietf-interfaces_interfaces_absolute_path.xml");
187         xmlDataAbsolutePath = TestUtils.getDocumentInPrintableForm(TestUtils.loadDocumentFrom(xmlStream));
188         String xmlPathRpcInput = RestconfImplTest.class.getResource("/full-versions/test-data2/data-rpc-input.xml")
189                 .getPath();
190         xmlDataRpcInput = TestUtils.loadTextFile(xmlPathRpcInput);
191         cnSnDataOutput = prepareCnSnRpcOutput();
192         String data2Input = RestconfImplTest.class.getResource("/full-versions/test-data2/data2.xml").getPath();
193         xmlData2 = TestUtils.loadTextFile(data2Input);
194     }
195
196     private static CompositeNodeWrapper prepareCnSnRpcOutput() throws URISyntaxException {
197         CompositeNodeWrapper cnSnDataOutput = new CompositeNodeWrapper(new URI("test:module"), "output");
198         CompositeNodeWrapper cont = new CompositeNodeWrapper(new URI("test:module"), "cont-output");
199         cnSnDataOutput.addValue(cont);
200         cnSnDataOutput.unwrap();
201         return cnSnDataOutput;
202     }
203 }