8c04e50f09712ed151d7086712810c533be2e924
[netconf.git] / restconf / restconf-nb-bierman02 / 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.ArgumentMatchers.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 java.io.FileNotFoundException;
18 import java.io.IOException;
19 import java.io.InputStream;
20 import java.io.UnsupportedEncodingException;
21 import java.net.URISyntaxException;
22 import java.util.Optional;
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 javax.ws.rs.core.Response.Status;
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.rest.common.TestRestconfUtils;
34 import org.opendaylight.mdsal.common.api.CommitInfo;
35 import org.opendaylight.mdsal.common.api.OptimisticLockFailedException;
36 import org.opendaylight.mdsal.common.api.TransactionCommitFailedException;
37 import org.opendaylight.mdsal.dom.api.DOMMountPoint;
38 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
39 import org.opendaylight.mdsal.dom.spi.FixedDOMSchemaService;
40 import org.opendaylight.netconf.sal.rest.impl.JsonNormalizedNodeBodyReader;
41 import org.opendaylight.netconf.sal.rest.impl.NormalizedNodeJsonBodyWriter;
42 import org.opendaylight.netconf.sal.rest.impl.NormalizedNodeXmlBodyWriter;
43 import org.opendaylight.netconf.sal.rest.impl.RestconfDocumentedExceptionMapper;
44 import org.opendaylight.netconf.sal.rest.impl.XmlNormalizedNodeBodyReader;
45 import org.opendaylight.netconf.sal.restconf.impl.BrokerFacade;
46 import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
47 import org.opendaylight.netconf.sal.restconf.impl.PutResult;
48 import org.opendaylight.netconf.sal.restconf.impl.RestconfImpl;
49 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
50 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
51 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
52 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
53 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
54
55 //TODO UNSTABLE TESTS - FIX ME
56 @Ignore
57 public class RestPutOperationTest extends JerseyTest {
58
59     private static String xmlData;
60     private static String xmlData2;
61     private static String xmlData3;
62
63     private static EffectiveModelContext schemaContextYangsIetf;
64     private static EffectiveModelContext schemaContextTestModule;
65
66     private BrokerFacade brokerFacade;
67     private RestconfImpl restconfImpl;
68     private DOMMountPoint mountInstance;
69
70     @BeforeClass
71     public static void init() throws IOException, ReactorException {
72         schemaContextYangsIetf = TestUtils.loadSchemaContext("/full-versions/yangs");
73         schemaContextTestModule = TestUtils.loadSchemaContext("/full-versions/test-module");
74         loadData();
75     }
76
77     private static void loadData() throws IOException {
78         final InputStream xmlStream =
79                 RestconfImplTest.class.getResourceAsStream("/parts/ietf-interfaces_interfaces.xml");
80         xmlData = TestUtils.getDocumentInPrintableForm(TestUtils.loadDocumentFrom(xmlStream));
81         final InputStream xmlStream2 =
82                 RestconfImplTest.class.getResourceAsStream("/full-versions/test-data2/data2.xml");
83         xmlData2 = TestUtils.getDocumentInPrintableForm(TestUtils.loadDocumentFrom(xmlStream2));
84         final InputStream xmlStream3 =
85                 RestconfImplTest.class.getResourceAsStream("/full-versions/test-data2/data7.xml");
86         xmlData3 = TestUtils.getDocumentInPrintableForm(TestUtils.loadDocumentFrom(xmlStream3));
87     }
88
89     @Override
90     protected Application configure() {
91         /* enable/disable Jersey logs to console */
92         // enable(TestProperties.LOG_TRAFFIC);
93         // enable(TestProperties.DUMP_ENTITY);
94         // enable(TestProperties.RECORD_LOG_LEVEL);
95         // set(TestProperties.RECORD_LOG_LEVEL, Level.ALL.intValue());
96
97         mountInstance = mock(DOMMountPoint.class);
98         final ControllerContext controllerContext =
99                 TestRestconfUtils.newControllerContext(schemaContextYangsIetf, mountInstance);
100         brokerFacade = mock(BrokerFacade.class);
101         restconfImpl = RestconfImpl.newInstance(brokerFacade, controllerContext);
102
103         ResourceConfig resourceConfig = new ResourceConfig();
104         resourceConfig = resourceConfig.registerInstances(restconfImpl,
105                 new XmlNormalizedNodeBodyReader(controllerContext), new NormalizedNodeXmlBodyWriter(),
106                 new JsonNormalizedNodeBodyReader(controllerContext), new NormalizedNodeJsonBodyWriter(),
107                 new RestconfDocumentedExceptionMapper(controllerContext));
108         return resourceConfig;
109     }
110
111     /**
112      * Tests of status codes for "/config/{identifier}".
113      */
114     @Test
115     public void putConfigStatusCodes() throws UnsupportedEncodingException {
116         final String uri = "/config/ietf-interfaces:interfaces/interface/eth0";
117         mockCommitConfigurationDataPutMethod(true);
118         assertEquals(500, put(uri, MediaType.APPLICATION_XML, xmlData));
119
120         mockCommitConfigurationDataPutMethod(false);
121         assertEquals(500, put(uri, MediaType.APPLICATION_XML, xmlData));
122
123         assertEquals(400, put(uri, MediaType.APPLICATION_JSON, ""));
124     }
125
126     @Test
127     public void putConfigStatusCodesEmptyBody() throws UnsupportedEncodingException {
128         final String uri = "/config/ietf-interfaces:interfaces/interface/eth0";
129         @SuppressWarnings("unused")
130         final Response resp = target(uri).request(MediaType.APPLICATION_JSON).put(
131                 Entity.entity("", MediaType.APPLICATION_JSON));
132         assertEquals(400, put(uri, MediaType.APPLICATION_JSON, ""));
133     }
134
135     @Test
136     public void testRpcResultCommitedToStatusCodesWithMountPoint() throws UnsupportedEncodingException,
137             FileNotFoundException, URISyntaxException {
138         final PutResult result = mock(PutResult.class);
139         when(brokerFacade.commitMountPointDataPut(any(DOMMountPoint.class), any(YangInstanceIdentifier.class),
140             any(NormalizedNode.class), null, null)).thenReturn(result);
141         doReturn(CommitInfo.emptyFluentFuture()).when(result).getFutureOfPutData();
142         when(result.getStatus()).thenReturn(Status.OK);
143
144         mockMountPoint();
145
146         String uri = "/config/ietf-interfaces:interfaces/interface/0/yang-ext:mount/test-module:cont";
147         assertEquals(200, put(uri, MediaType.APPLICATION_XML, xmlData2));
148
149         uri = "/config/ietf-interfaces:interfaces/yang-ext:mount/test-module:cont";
150         assertEquals(200, put(uri, MediaType.APPLICATION_XML, xmlData2));
151     }
152
153     @Test
154     public void putDataMountPointIntoHighestElement() throws UnsupportedEncodingException, URISyntaxException {
155         final PutResult result = mock(PutResult.class);
156         doReturn(result).when(brokerFacade).commitMountPointDataPut(any(DOMMountPoint.class),
157                 any(YangInstanceIdentifier.class), any(NormalizedNode.class), null, null);
158         doReturn(CommitInfo.emptyFluentFuture()).when(result).getFutureOfPutData();
159         when(result.getStatus()).thenReturn(Status.OK);
160
161         mockMountPoint();
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).when(brokerFacade).commitConfigurationDataPut(
173                 any(EffectiveModelContext.class), any(YangInstanceIdentifier.class), any(NormalizedNode.class), null,
174                 null);
175
176         assertEquals(500, put(uri, MediaType.APPLICATION_XML, xmlData));
177
178         doThrow(OptimisticLockFailedException.class).doReturn(mock(PutResult.class)).when(brokerFacade)
179                 .commitConfigurationDataPut(any(EffectiveModelContext.class), any(YangInstanceIdentifier.class),
180                         any(NormalizedNode.class), null, null);
181
182         assertEquals(500, put(uri, MediaType.APPLICATION_XML, xmlData));
183     }
184
185     @Test
186     public void putWithTransactionCommitFailedException() throws UnsupportedEncodingException {
187
188         final String uri = "/config/ietf-interfaces:interfaces/interface/eth0";
189
190         doThrow(TransactionCommitFailedException.class)
191                 .when(brokerFacade).commitConfigurationDataPut(
192                     any(EffectiveModelContext.class), any(YangInstanceIdentifier.class), any(NormalizedNode.class),
193                     null, null);
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 mockMountPoint() {
203         when(mountInstance.getService(DOMSchemaService.class))
204             .thenReturn(Optional.of(FixedDOMSchemaService.of(schemaContextTestModule)));
205     }
206
207     private void mockCommitConfigurationDataPutMethod(final boolean noErrors) {
208         final PutResult putResMock = mock(PutResult.class);
209         if (noErrors) {
210             doReturn(putResMock).when(brokerFacade).commitConfigurationDataPut(
211                     any(EffectiveModelContext.class), any(YangInstanceIdentifier.class), any(NormalizedNode.class),
212                     null, null);
213         } else {
214             doThrow(RestconfDocumentedException.class).when(brokerFacade).commitConfigurationDataPut(
215                     any(EffectiveModelContext.class), any(YangInstanceIdentifier.class), any(NormalizedNode.class),
216                     null, null);
217         }
218     }
219
220 }