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

©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.