Merge "Cleanup: Remove passing around of DataPersistenceProvider"
[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.doReturn;
13 import static org.mockito.Mockito.doThrow;
14 import static org.mockito.Mockito.mock;
15 import static org.mockito.Mockito.when;
16
17 import com.google.common.base.Optional;
18 import com.google.common.util.concurrent.CheckedFuture;
19 import java.io.FileNotFoundException;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.io.UnsupportedEncodingException;
23 import java.net.URISyntaxException;
24 import javax.ws.rs.client.Entity;
25 import javax.ws.rs.core.Application;
26 import javax.ws.rs.core.MediaType;
27 import javax.ws.rs.core.Response;
28 import org.glassfish.jersey.server.ResourceConfig;
29 import org.glassfish.jersey.test.JerseyTest;
30 import org.junit.BeforeClass;
31 import org.junit.Ignore;
32 import org.junit.Test;
33 import org.opendaylight.controller.md.sal.common.api.data.OptimisticLockFailedException;
34 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
35 import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
36 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
37 import org.opendaylight.controller.sal.rest.impl.JsonNormalizedNodeBodyReader;
38 import org.opendaylight.controller.sal.rest.impl.NormalizedNodeJsonBodyWriter;
39 import org.opendaylight.controller.sal.rest.impl.NormalizedNodeXmlBodyWriter;
40 import org.opendaylight.controller.sal.rest.impl.RestconfDocumentedExceptionMapper;
41 import org.opendaylight.controller.sal.rest.impl.XmlNormalizedNodeBodyReader;
42 import org.opendaylight.controller.sal.restconf.impl.BrokerFacade;
43 import org.opendaylight.controller.sal.restconf.impl.ControllerContext;
44 import org.opendaylight.controller.sal.restconf.impl.RestconfDocumentedException;
45 import org.opendaylight.controller.sal.restconf.impl.RestconfImpl;
46 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
47 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
48 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
49
50 @Ignore
51 public class RestPutOperationTest extends JerseyTest {
52
53     private static String xmlData;
54     private static String xmlData2;
55     private static String xmlData3;
56
57     private static BrokerFacade brokerFacade;
58     private static RestconfImpl restconfImpl;
59     private static SchemaContext schemaContextYangsIetf;
60     private static SchemaContext schemaContextTestModule;
61
62     @BeforeClass
63     public static void init() throws IOException {
64         schemaContextYangsIetf = TestUtils.loadSchemaContext("/full-versions/yangs");
65         schemaContextTestModule = TestUtils.loadSchemaContext("/full-versions/test-module");
66         final ControllerContext controllerContext = ControllerContext.getInstance();
67         controllerContext.setSchemas(schemaContextYangsIetf);
68         brokerFacade = mock(BrokerFacade.class);
69         restconfImpl = RestconfImpl.getInstance();
70         restconfImpl.setBroker(brokerFacade);
71         restconfImpl.setControllerContext(controllerContext);
72         loadData();
73     }
74
75     private static void loadData() throws IOException {
76         final InputStream xmlStream = RestconfImplTest.class.getResourceAsStream("/parts/ietf-interfaces_interfaces.xml");
77         xmlData = TestUtils.getDocumentInPrintableForm(TestUtils.loadDocumentFrom(xmlStream));
78         final InputStream xmlStream2 = RestconfImplTest.class.getResourceAsStream("/full-versions/test-data2/data2.xml");
79         xmlData2 = TestUtils.getDocumentInPrintableForm(TestUtils.loadDocumentFrom(xmlStream2));
80         final InputStream xmlStream3 = RestconfImplTest.class.getResourceAsStream("/full-versions/test-data2/data7.xml");
81         xmlData3 = TestUtils.getDocumentInPrintableForm(TestUtils.loadDocumentFrom(xmlStream3));
82     }
83
84     @Override
85     protected Application configure() {
86         /* enable/disable Jersey logs to console */
87         // enable(TestProperties.LOG_TRAFFIC);
88         // enable(TestProperties.DUMP_ENTITY);
89         // enable(TestProperties.RECORD_LOG_LEVEL);
90         // set(TestProperties.RECORD_LOG_LEVEL, Level.ALL.intValue());
91         ResourceConfig resourceConfig = new ResourceConfig();
92         resourceConfig = resourceConfig.registerInstances(restconfImpl,new XmlNormalizedNodeBodyReader(),
93                 new NormalizedNodeXmlBodyWriter(), new JsonNormalizedNodeBodyReader(), new NormalizedNodeJsonBodyWriter());
94         resourceConfig.registerClasses(RestconfDocumentedExceptionMapper.class);
95         return resourceConfig;
96     }
97
98     /**
99      * Tests of status codes for "/config/{identifier}".
100      */
101     @Test
102     public void putConfigStatusCodes() throws UnsupportedEncodingException {
103         final String uri = "/config/ietf-interfaces:interfaces/interface/eth0";
104         mockCommitConfigurationDataPutMethod(true);
105         assertEquals(200, put(uri, MediaType.APPLICATION_XML, xmlData));
106
107         mockCommitConfigurationDataPutMethod(false);
108         assertEquals(500, put(uri, MediaType.APPLICATION_XML, xmlData));
109
110         assertEquals(400, put(uri, MediaType.APPLICATION_JSON, ""));
111     }
112
113     @Test
114     public void putConfigStatusCodesEmptyBody() throws UnsupportedEncodingException {
115         final String uri = "/config/ietf-interfaces:interfaces/interface/eth0";
116         @SuppressWarnings("unused")
117         final Response resp = target(uri).request(MediaType.APPLICATION_JSON).put(
118                 Entity.entity("", MediaType.APPLICATION_JSON));
119         assertEquals(400, put(uri, MediaType.APPLICATION_JSON, ""));
120     }
121
122     @Test
123     @Ignore // jenkins has problem with JerseyTest - we expecting problems with singletons ControllerContext as schemaContext holder
124     public void testRpcResultCommitedToStatusCodesWithMountPoint() throws UnsupportedEncodingException,
125             FileNotFoundException, URISyntaxException {
126
127         @SuppressWarnings("unchecked")
128         final CheckedFuture<Void, TransactionCommitFailedException> dummyFuture = mock(CheckedFuture.class);
129
130         when(
131                 brokerFacade.commitConfigurationDataPut(any(DOMMountPoint.class), any(YangInstanceIdentifier.class),
132                         any(NormalizedNode.class))).thenReturn(dummyFuture);
133
134         final DOMMountPoint mountInstance = mock(DOMMountPoint.class);
135         when(mountInstance.getSchemaContext()).thenReturn(schemaContextTestModule);
136         final DOMMountPointService mockMountService = mock(DOMMountPointService.class);
137         when(mockMountService.getMountPoint(any(YangInstanceIdentifier.class))).thenReturn(Optional.of(mountInstance));
138
139         ControllerContext.getInstance().setMountService(mockMountService);
140
141         String uri = "/config/ietf-interfaces:interfaces/interface/0/yang-ext:mount/test-module:cont";
142         assertEquals(200, put(uri, MediaType.APPLICATION_XML, xmlData2));
143
144         uri = "/config/ietf-interfaces:interfaces/yang-ext:mount/test-module:cont";
145         assertEquals(200, put(uri, MediaType.APPLICATION_XML, xmlData2));
146     }
147
148     @Test
149     public void putDataMountPointIntoHighestElement() throws UnsupportedEncodingException, URISyntaxException {
150         @SuppressWarnings("unchecked")
151         final CheckedFuture<Void, TransactionCommitFailedException> dummyFuture = mock(CheckedFuture.class);
152         when(
153                 brokerFacade.commitConfigurationDataPut(any(DOMMountPoint.class), any(YangInstanceIdentifier.class),
154                         any(NormalizedNode.class))).thenReturn(dummyFuture);
155
156         final DOMMountPoint mountInstance = mock(DOMMountPoint.class);
157         when(mountInstance.getSchemaContext()).thenReturn(schemaContextTestModule);
158         final DOMMountPointService mockMountService = mock(DOMMountPointService.class);
159         when(mockMountService.getMountPoint(any(YangInstanceIdentifier.class))).thenReturn(Optional.of(mountInstance));
160
161         ControllerContext.getInstance().setMountService(mockMountService);
162
163         final String uri = "/config/ietf-interfaces:interfaces/yang-ext:mount";
164         assertEquals(200, put(uri, MediaType.APPLICATION_XML, xmlData3));
165     }
166
167     @Test
168     public void putWithOptimisticLockFailedException() throws UnsupportedEncodingException {
169
170         final String uri = "/config/ietf-interfaces:interfaces/interface/eth0";
171
172         doThrow(OptimisticLockFailedException.class).
173             when(brokerFacade).commitConfigurationDataPut(
174                 any(YangInstanceIdentifier.class), any(NormalizedNode.class));
175
176         assertEquals(500, put(uri, MediaType.APPLICATION_XML, xmlData));
177
178         doThrow(OptimisticLockFailedException.class).doReturn(mock(CheckedFuture.class)).
179             when(brokerFacade).commitConfigurationDataPut(
180                 any(YangInstanceIdentifier.class), any(NormalizedNode.class));
181
182         assertEquals(200, put(uri, MediaType.APPLICATION_XML, xmlData));
183     }
184
185     @Test
186     @Ignore // jenkins has problem with JerseyTest - we expecting problems with singletons ControllerContext as schemaContext holder
187     public void putWithTransactionCommitFailedException() throws UnsupportedEncodingException {
188
189         final String uri = "/config/ietf-interfaces:interfaces/interface/eth0";
190
191         doThrow(TransactionCommitFailedException.class).
192             when(brokerFacade).commitConfigurationDataPut(
193                 any(YangInstanceIdentifier.class), any(NormalizedNode.class));
194
195         assertEquals(500, put(uri, MediaType.APPLICATION_XML, xmlData));
196     }
197
198     private int put(final String uri, final String mediaType, final String data) throws UnsupportedEncodingException {
199         return target(uri).request(mediaType).put(Entity.entity(data, mediaType)).getStatus();
200     }
201
202     private void mockCommitConfigurationDataPutMethod(final boolean noErrors) {
203         if (noErrors) {
204             doReturn(mock(CheckedFuture.class)).when(brokerFacade).commitConfigurationDataPut(
205                     any(YangInstanceIdentifier.class), any(NormalizedNode.class));
206         } else {
207             doThrow(RestconfDocumentedException.class).when(brokerFacade).commitConfigurationDataPut(
208                     any(YangInstanceIdentifier.class), any(NormalizedNode.class));
209         }
210     }
211
212 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.