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