Merge "Fix for Bug 3"
[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.XML;
8 import static org.opendaylight.controller.sal.restconf.impl.test.RestOperationUtils.createUri;
9
10 import java.io.FileNotFoundException;
11 import java.io.IOException;
12 import java.io.InputStream;
13 import java.io.UnsupportedEncodingException;
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 import javax.ws.rs.core.Response;
21
22 import org.glassfish.jersey.server.ResourceConfig;
23 import org.glassfish.jersey.test.JerseyTest;
24 import org.junit.BeforeClass;
25 import org.junit.Test;
26 import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
27 import org.opendaylight.controller.sal.core.api.mount.MountInstance;
28 import org.opendaylight.controller.sal.core.api.mount.MountService;
29 import org.opendaylight.controller.sal.rest.api.Draft02;
30 import org.opendaylight.controller.sal.rest.impl.JsonToCompositeNodeProvider;
31 import org.opendaylight.controller.sal.rest.impl.StructuredDataToJsonProvider;
32 import org.opendaylight.controller.sal.rest.impl.StructuredDataToXmlProvider;
33 import org.opendaylight.controller.sal.rest.impl.XmlToCompositeNodeProvider;
34 import org.opendaylight.controller.sal.restconf.impl.BrokerFacade;
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.RpcResult;
38 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
39 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
40 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
41
42 public class RestPutOperationTest extends JerseyTest {
43
44     private static String xmlData;
45
46     private static BrokerFacade brokerFacade;
47     private static RestconfImpl restconfImpl;
48     private static SchemaContext schemaContextYangsIetf;
49     private static SchemaContext schemaContextTestModule;
50
51     @BeforeClass
52     public static void init() throws IOException {
53         schemaContextYangsIetf = TestUtils.loadSchemaContext("/full-versions/yangs");
54         schemaContextTestModule = TestUtils.loadSchemaContext("/full-versions/test-module");
55         ControllerContext controllerContext = ControllerContext.getInstance();
56         controllerContext.setSchemas(schemaContextYangsIetf);
57         brokerFacade = mock(BrokerFacade.class);
58         restconfImpl = RestconfImpl.getInstance();
59         restconfImpl.setBroker(brokerFacade);
60         restconfImpl.setControllerContext(controllerContext);
61         loadData();
62     }
63
64     private static void loadData() throws IOException {
65         InputStream xmlStream = RestconfImplTest.class.getResourceAsStream("/parts/ietf-interfaces_interfaces.xml");
66         xmlData = TestUtils.getDocumentInPrintableForm(TestUtils.loadDocumentFrom(xmlStream));
67     }
68
69     @Override
70     protected Application configure() {
71         /* enable/disable Jersey logs to console */
72 //        enable(TestProperties.LOG_TRAFFIC);
73 //        enable(TestProperties.DUMP_ENTITY);
74 //        enable(TestProperties.RECORD_LOG_LEVEL);
75 //        set(TestProperties.RECORD_LOG_LEVEL, Level.ALL.intValue());
76         ResourceConfig resourceConfig = new ResourceConfig();
77         resourceConfig = resourceConfig.registerInstances(restconfImpl, StructuredDataToXmlProvider.INSTANCE,
78                 StructuredDataToJsonProvider.INSTANCE, XmlToCompositeNodeProvider.INSTANCE,
79                 JsonToCompositeNodeProvider.INSTANCE);
80         return resourceConfig;
81     }
82
83     /**
84      * Tests of status codes for "/config/{identifier}".
85      */
86     @Test
87     public void putConfigStatusCodes() throws UnsupportedEncodingException {
88         String uri = createUri("/config/", "ietf-interfaces:interfaces/interface/eth0");
89         mockCommitConfigurationDataPutMethod(TransactionStatus.COMMITED);
90         assertEquals(200, put(uri, MediaType.APPLICATION_XML, xmlData));
91         
92         mockCommitConfigurationDataPutMethod(TransactionStatus.FAILED);
93         assertEquals(500, put(uri, MediaType.APPLICATION_XML, xmlData));
94     }
95
96
97     /**
98      * Tests of status codes for "/datastore/{identifier}".
99      */
100     @Test
101     public void putDatastoreStatusCodes() throws UnsupportedEncodingException {
102         String uri = createUri("/datastore/", "ietf-interfaces:interfaces/interface/eth0");
103         mockCommitConfigurationDataPutMethod(TransactionStatus.COMMITED);
104         assertEquals(200, put(uri, MediaType.APPLICATION_XML, xmlData));
105         
106         mockCommitConfigurationDataPutMethod(TransactionStatus.FAILED);
107         assertEquals(500, put(uri, MediaType.APPLICATION_XML, xmlData));
108     }
109
110     @Test
111     public void testRpcResultCommitedToStatusCodesWithMountPoint() throws UnsupportedEncodingException,
112             FileNotFoundException, URISyntaxException {
113
114         RpcResult<TransactionStatus> rpcResult = new DummyRpcResult.Builder<TransactionStatus>().result(TransactionStatus.COMMITED)
115                 .build();
116         Future<RpcResult<TransactionStatus>> dummyFuture = DummyFuture.builder().rpcResult(rpcResult).build();
117         when(brokerFacade.commitConfigurationDataPutBehindMountPoint(any(MountInstance.class),
118                         any(InstanceIdentifier.class), any(CompositeNode.class))).thenReturn(dummyFuture);
119         
120
121         InputStream xmlStream = RestconfImplTest.class.getResourceAsStream("/full-versions/test-data2/data2.xml");
122         String xml = TestUtils.getDocumentInPrintableForm(TestUtils.loadDocumentFrom(xmlStream));
123         Entity<String> entity = Entity.entity(xml, Draft02.MediaTypes.DATA + XML);
124
125         MountInstance mountInstance = mock(MountInstance.class);
126         when(mountInstance.getSchemaContext()).thenReturn(schemaContextTestModule);
127         MountService mockMountService = mock(MountService.class);
128         when(mockMountService.getMountPoint(any(InstanceIdentifier.class))).thenReturn(mountInstance);
129
130         ControllerContext.getInstance().setMountService(mockMountService);
131
132         String uri = createUri("/config/", "ietf-interfaces:interfaces/interface/0/yang-ext:mount/test-module:cont");
133         Response response = target(uri).request(Draft02.MediaTypes.DATA + XML).put(entity);
134         assertEquals(200, response.getStatus());
135         
136         uri = createUri("/config/", "ietf-interfaces:interfaces/yang-ext:mount/test-module:cont");
137         response = target(uri).request(Draft02.MediaTypes.DATA + XML).put(entity);
138         assertEquals(200, response.getStatus());
139     }
140
141     private int put(String uri, String mediaType, String data) throws UnsupportedEncodingException {
142         return target(uri).request(mediaType).put(Entity.entity(data, mediaType)).getStatus();
143     }
144
145     private void mockCommitConfigurationDataPutMethod(TransactionStatus statusName) {
146         RpcResult<TransactionStatus> rpcResult = new DummyRpcResult.Builder<TransactionStatus>().result(statusName)
147                 .build();
148         Future<RpcResult<TransactionStatus>> dummyFuture = DummyFuture.builder().rpcResult(rpcResult).build();
149         when(brokerFacade.commitConfigurationDataPut(any(InstanceIdentifier.class), any(CompositeNode.class)))
150                 .thenReturn(dummyFuture);
151     }
152
153 }