Merge "Added the Buffer-Id to packet-in."
[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
20 import java.io.FileNotFoundException;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.io.UnsupportedEncodingException;
24 import java.net.URISyntaxException;
25
26 import javax.ws.rs.client.Entity;
27 import javax.ws.rs.core.Application;
28 import javax.ws.rs.core.MediaType;
29 import javax.ws.rs.core.Response;
30
31 import org.glassfish.jersey.server.ResourceConfig;
32 import org.glassfish.jersey.test.JerseyTest;
33 import org.junit.BeforeClass;
34 import org.junit.Test;
35 import org.opendaylight.controller.md.sal.common.api.data.OptimisticLockFailedException;
36 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
37 import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
38 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
39 import org.opendaylight.controller.sal.rest.impl.JsonToCompositeNodeProvider;
40 import org.opendaylight.controller.sal.rest.impl.RestconfDocumentedExceptionMapper;
41 import org.opendaylight.controller.sal.rest.impl.StructuredDataToJsonProvider;
42 import org.opendaylight.controller.sal.rest.impl.StructuredDataToXmlProvider;
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         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         InputStream xmlStream = RestconfImplTest.class.getResourceAsStream("/parts/ietf-interfaces_interfaces.xml");
78         xmlData = TestUtils.getDocumentInPrintableForm(TestUtils.loadDocumentFrom(xmlStream));
79         InputStream xmlStream2 = RestconfImplTest.class.getResourceAsStream("/full-versions/test-data2/data2.xml");
80         xmlData2 = TestUtils.getDocumentInPrintableForm(TestUtils.loadDocumentFrom(xmlStream2));
81         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);
96         resourceConfig.registerClasses(RestconfDocumentedExceptionMapper.class);
97         return resourceConfig;
98     }
99
100     /**
101      * Tests of status codes for "/config/{identifier}".
102      */
103     @Test
104     public void putConfigStatusCodes() throws UnsupportedEncodingException {
105         String uri = "/config/ietf-interfaces:interfaces/interface/eth0";
106         mockCommitConfigurationDataPutMethod(true);
107         assertEquals(200, put(uri, MediaType.APPLICATION_XML, xmlData));
108
109         mockCommitConfigurationDataPutMethod(false);
110         assertEquals(500, put(uri, MediaType.APPLICATION_XML, xmlData));
111
112         assertEquals(400, put(uri, MediaType.APPLICATION_JSON, ""));
113     }
114
115     @Test
116     public void putConfigStatusCodesEmptyBody() throws UnsupportedEncodingException {
117         String uri = "/config/ietf-interfaces:interfaces/interface/eth0";
118         Response resp = target(uri).request(MediaType.APPLICATION_JSON).put(
119                 Entity.entity("", MediaType.APPLICATION_JSON));
120         assertEquals(400, put(uri, MediaType.APPLICATION_JSON, ""));
121     }
122
123     @Test
124     public void testRpcResultCommitedToStatusCodesWithMountPoint() throws UnsupportedEncodingException,
125             FileNotFoundException, URISyntaxException {
126
127         CheckedFuture<Void, TransactionCommitFailedException> dummyFuture = mock(CheckedFuture.class);
128
129         when(
130                 brokerFacade.commitConfigurationDataPut(any(DOMMountPoint.class), any(YangInstanceIdentifier.class),
131                         any(NormalizedNode.class))).thenReturn(dummyFuture);
132
133         DOMMountPoint mountInstance = mock(DOMMountPoint.class);
134         when(mountInstance.getSchemaContext()).thenReturn(schemaContextTestModule);
135         DOMMountPointService mockMountService = mock(DOMMountPointService.class);
136         when(mockMountService.getMountPoint(any(YangInstanceIdentifier.class))).thenReturn(Optional.of(mountInstance));
137
138         ControllerContext.getInstance().setMountService(mockMountService);
139
140         String uri = "/config/ietf-interfaces:interfaces/interface/0/yang-ext:mount/test-module:cont";
141         assertEquals(200, put(uri, MediaType.APPLICATION_XML, xmlData2));
142
143         uri = "/config/ietf-interfaces:interfaces/yang-ext:mount/test-module:cont";
144         assertEquals(200, put(uri, MediaType.APPLICATION_XML, xmlData2));
145     }
146
147     @Test
148     public void putDataMountPointIntoHighestElement() throws UnsupportedEncodingException, URISyntaxException {
149         CheckedFuture<Void, TransactionCommitFailedException> dummyFuture = mock(CheckedFuture.class);
150         when(
151                 brokerFacade.commitConfigurationDataPut(any(DOMMountPoint.class), any(YangInstanceIdentifier.class),
152                         any(NormalizedNode.class))).thenReturn(dummyFuture);
153
154         DOMMountPoint mountInstance = mock(DOMMountPoint.class);
155         when(mountInstance.getSchemaContext()).thenReturn(schemaContextTestModule);
156         DOMMountPointService mockMountService = mock(DOMMountPointService.class);
157         when(mockMountService.getMountPoint(any(YangInstanceIdentifier.class))).thenReturn(Optional.of(mountInstance));
158
159         ControllerContext.getInstance().setMountService(mockMountService);
160
161         String uri = "/config/ietf-interfaces:interfaces/yang-ext:mount";
162         assertEquals(200, put(uri, MediaType.APPLICATION_XML, xmlData3));
163     }
164
165     @Test
166     public void putWithOptimisticLockFailedException() throws UnsupportedEncodingException {
167
168         String uri = "/config/ietf-interfaces:interfaces/interface/eth0";
169
170         doThrow(OptimisticLockFailedException.class).
171             when(brokerFacade).commitConfigurationDataPut(
172                 any(YangInstanceIdentifier.class), any(NormalizedNode.class));
173
174         assertEquals(500, put(uri, MediaType.APPLICATION_XML, xmlData));
175
176         doThrow(OptimisticLockFailedException.class).doReturn(mock(CheckedFuture.class)).
177             when(brokerFacade).commitConfigurationDataPut(
178                 any(YangInstanceIdentifier.class), any(NormalizedNode.class));
179
180         assertEquals(200, put(uri, MediaType.APPLICATION_XML, xmlData));
181     }
182
183     @Test
184     public void putWithTransactionCommitFailedException() throws UnsupportedEncodingException {
185
186         String uri = "/config/ietf-interfaces:interfaces/interface/eth0";
187
188         doThrow(TransactionCommitFailedException.class).
189             when(brokerFacade).commitConfigurationDataPut(
190                 any(YangInstanceIdentifier.class), any(NormalizedNode.class));
191
192         assertEquals(500, put(uri, MediaType.APPLICATION_XML, xmlData));
193     }
194
195     private int put(String uri, String mediaType, String data) throws UnsupportedEncodingException {
196         return target(uri).request(mediaType).put(Entity.entity(data, mediaType)).getStatus();
197     }
198
199     private void mockCommitConfigurationDataPutMethod(final boolean noErrors) {
200         if (noErrors) {
201             doReturn(mock(CheckedFuture.class)).when(brokerFacade).commitConfigurationDataPut(
202                     any(YangInstanceIdentifier.class), any(NormalizedNode.class));
203         } else {
204             doThrow(RestconfDocumentedException.class).when(brokerFacade).commitConfigurationDataPut(
205                     any(YangInstanceIdentifier.class), any(NormalizedNode.class));
206         }
207     }
208
209 }