Merge "BUG-362: add some diagnostic information Changed Remote RPC Server Implementat...
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / test / java / org / opendaylight / controller / sal / restconf / impl / test / RestPutOperationTest.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.controller.sal.restconf.impl.test;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.mockito.Matchers.any;
12 import static org.mockito.Mockito.mock;
13 import static org.mockito.Mockito.when;
14 import static org.opendaylight.controller.sal.restconf.impl.test.RestOperationUtils.createUri;
15
16 import java.io.FileNotFoundException;
17 import java.io.IOException;
18 import java.io.InputStream;
19 import java.io.UnsupportedEncodingException;
20 import java.net.URISyntaxException;
21 import java.util.concurrent.Future;
22
23 import javax.ws.rs.client.Entity;
24 import javax.ws.rs.core.Application;
25 import javax.ws.rs.core.MediaType;
26
27 import org.glassfish.jersey.server.ResourceConfig;
28 import org.glassfish.jersey.test.JerseyTest;
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.MountInstance;
33 import org.opendaylight.controller.sal.core.api.mount.MountService;
34 import org.opendaylight.controller.sal.rest.impl.JsonToCompositeNodeProvider;
35 import org.opendaylight.controller.sal.rest.impl.StructuredDataToJsonProvider;
36 import org.opendaylight.controller.sal.rest.impl.StructuredDataToXmlProvider;
37 import org.opendaylight.controller.sal.rest.impl.XmlToCompositeNodeProvider;
38 import org.opendaylight.controller.sal.restconf.impl.BrokerFacade;
39 import org.opendaylight.controller.sal.restconf.impl.ControllerContext;
40 import org.opendaylight.controller.sal.restconf.impl.RestconfImpl;
41 import org.opendaylight.yangtools.yang.common.RpcResult;
42 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
43 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
44 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
45
46 public class RestPutOperationTest extends JerseyTest {
47
48     private static String xmlData;
49     private static String xmlData2;
50     private static String xmlData3;
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     private static void loadData() throws IOException {
71         InputStream xmlStream = RestconfImplTest.class.getResourceAsStream("/parts/ietf-interfaces_interfaces.xml");
72         xmlData = TestUtils.getDocumentInPrintableForm(TestUtils.loadDocumentFrom(xmlStream));
73         InputStream xmlStream2 = RestconfImplTest.class.getResourceAsStream("/full-versions/test-data2/data2.xml");
74         xmlData2 = TestUtils.getDocumentInPrintableForm(TestUtils.loadDocumentFrom(xmlStream2));
75         InputStream xmlStream3 = RestconfImplTest.class.getResourceAsStream("/full-versions/test-data2/data7.xml");
76         xmlData3 = TestUtils.getDocumentInPrintableForm(TestUtils.loadDocumentFrom(xmlStream3));
77     }
78
79     @Override
80     protected Application configure() {
81         /* enable/disable Jersey logs to console */
82         // enable(TestProperties.LOG_TRAFFIC);
83         // enable(TestProperties.DUMP_ENTITY);
84         // enable(TestProperties.RECORD_LOG_LEVEL);
85         // set(TestProperties.RECORD_LOG_LEVEL, Level.ALL.intValue());
86         ResourceConfig resourceConfig = new ResourceConfig();
87         resourceConfig = resourceConfig.registerInstances(restconfImpl, StructuredDataToXmlProvider.INSTANCE,
88                 StructuredDataToJsonProvider.INSTANCE, XmlToCompositeNodeProvider.INSTANCE,
89                 JsonToCompositeNodeProvider.INSTANCE);
90         return resourceConfig;
91     }
92
93     /**
94      * Tests of status codes for "/config/{identifier}".
95      */
96     @Test
97     public void putConfigStatusCodes() throws UnsupportedEncodingException {
98         String uri = createUri("/config/", "ietf-interfaces:interfaces/interface/eth0");
99         mockCommitConfigurationDataPutMethod(TransactionStatus.COMMITED);
100         assertEquals(200, put(uri, MediaType.APPLICATION_XML, xmlData));
101
102         mockCommitConfigurationDataPutMethod(TransactionStatus.FAILED);
103         assertEquals(500, put(uri, MediaType.APPLICATION_XML, xmlData));
104     }
105
106     @Test
107     public void testRpcResultCommitedToStatusCodesWithMountPoint() throws UnsupportedEncodingException,
108             FileNotFoundException, URISyntaxException {
109
110         RpcResult<TransactionStatus> rpcResult = new DummyRpcResult.Builder<TransactionStatus>().result(
111                 TransactionStatus.COMMITED).build();
112         Future<RpcResult<TransactionStatus>> dummyFuture = DummyFuture.builder().rpcResult(rpcResult).build();
113         when(
114                 brokerFacade.commitConfigurationDataPutBehindMountPoint(any(MountInstance.class),
115                         any(InstanceIdentifier.class), any(CompositeNode.class))).thenReturn(dummyFuture);
116
117         MountInstance mountInstance = mock(MountInstance.class);
118         when(mountInstance.getSchemaContext()).thenReturn(schemaContextTestModule);
119         MountService mockMountService = mock(MountService.class);
120         when(mockMountService.getMountPoint(any(InstanceIdentifier.class))).thenReturn(mountInstance);
121
122         ControllerContext.getInstance().setMountService(mockMountService);
123
124         String uri = createUri("/config/", "ietf-interfaces:interfaces/interface/0/yang-ext:mount/test-module:cont");
125         assertEquals(200, put(uri, MediaType.APPLICATION_XML, xmlData2));
126
127         uri = createUri("/config/", "ietf-interfaces:interfaces/yang-ext:mount/test-module:cont");
128         assertEquals(200, put(uri, MediaType.APPLICATION_XML, xmlData2));
129     }
130
131     @Test
132     public void putDataMountPointIntoHighestElement() throws UnsupportedEncodingException, URISyntaxException {
133         RpcResult<TransactionStatus> rpcResult = new DummyRpcResult.Builder<TransactionStatus>().result(
134                 TransactionStatus.COMMITED).build();
135         Future<RpcResult<TransactionStatus>> dummyFuture = DummyFuture.builder().rpcResult(rpcResult).build();
136         when(
137                 brokerFacade.commitConfigurationDataPutBehindMountPoint(any(MountInstance.class),
138                         any(InstanceIdentifier.class), any(CompositeNode.class))).thenReturn(dummyFuture);
139
140         MountInstance mountInstance = mock(MountInstance.class);
141         when(mountInstance.getSchemaContext()).thenReturn(schemaContextTestModule);
142         MountService mockMountService = mock(MountService.class);
143         when(mockMountService.getMountPoint(any(InstanceIdentifier.class))).thenReturn(mountInstance);
144
145         ControllerContext.getInstance().setMountService(mockMountService);
146
147         String uri = createUri("/config/", "ietf-interfaces:interfaces/yang-ext:mount");
148         assertEquals(200, put(uri, MediaType.APPLICATION_XML, xmlData3));
149     }
150
151     private int put(String uri, String mediaType, String data) throws UnsupportedEncodingException {
152         return target(uri).request(mediaType).put(Entity.entity(data, mediaType)).getStatus();
153     }
154
155     private void mockCommitConfigurationDataPutMethod(TransactionStatus statusName) {
156         RpcResult<TransactionStatus> rpcResult = new DummyRpcResult.Builder<TransactionStatus>().result(statusName)
157                 .build();
158         Future<RpcResult<TransactionStatus>> dummyFuture = DummyFuture.builder().rpcResult(rpcResult).build();
159         when(brokerFacade.commitConfigurationDataPut(any(InstanceIdentifier.class), any(CompositeNode.class)))
160                 .thenReturn(dummyFuture);
161     }
162
163 }