BUG 1082 Migrate sal-rest-connector to Async Data Broker API
[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.Test;
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.JsonToCompositeNodeProvider;
36 import org.opendaylight.controller.sal.rest.impl.RestconfDocumentedExceptionMapper;
37 import org.opendaylight.controller.sal.rest.impl.StructuredDataToJsonProvider;
38 import org.opendaylight.controller.sal.rest.impl.StructuredDataToXmlProvider;
39 import org.opendaylight.controller.sal.rest.impl.XmlToCompositeNodeProvider;
40 import org.opendaylight.controller.sal.restconf.impl.BrokerFacade;
41 import org.opendaylight.controller.sal.restconf.impl.ControllerContext;
42 import org.opendaylight.controller.sal.restconf.impl.RestconfDocumentedException;
43 import org.opendaylight.controller.sal.restconf.impl.RestconfImpl;
44 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
45 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
46 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
47
48 public class RestPutOperationTest extends JerseyTest {
49
50     private static String xmlData;
51     private static String xmlData2;
52     private static String xmlData3;
53
54     private static BrokerFacade brokerFacade;
55     private static RestconfImpl restconfImpl;
56     private static SchemaContext schemaContextYangsIetf;
57     private static SchemaContext schemaContextTestModule;
58
59     @BeforeClass
60     public static void init() throws IOException {
61         schemaContextYangsIetf = TestUtils.loadSchemaContext("/full-versions/yangs");
62         schemaContextTestModule = TestUtils.loadSchemaContext("/full-versions/test-module");
63         ControllerContext controllerContext = ControllerContext.getInstance();
64         controllerContext.setSchemas(schemaContextYangsIetf);
65         brokerFacade = mock(BrokerFacade.class);
66         restconfImpl = RestconfImpl.getInstance();
67         restconfImpl.setBroker(brokerFacade);
68         restconfImpl.setControllerContext(controllerContext);
69         loadData();
70     }
71
72     private static void loadData() throws IOException {
73         InputStream xmlStream = RestconfImplTest.class.getResourceAsStream("/parts/ietf-interfaces_interfaces.xml");
74         xmlData = TestUtils.getDocumentInPrintableForm(TestUtils.loadDocumentFrom(xmlStream));
75         InputStream xmlStream2 = RestconfImplTest.class.getResourceAsStream("/full-versions/test-data2/data2.xml");
76         xmlData2 = TestUtils.getDocumentInPrintableForm(TestUtils.loadDocumentFrom(xmlStream2));
77         InputStream xmlStream3 = RestconfImplTest.class.getResourceAsStream("/full-versions/test-data2/data7.xml");
78         xmlData3 = TestUtils.getDocumentInPrintableForm(TestUtils.loadDocumentFrom(xmlStream3));
79     }
80
81     @Override
82     protected Application configure() {
83         /* enable/disable Jersey logs to console */
84         // enable(TestProperties.LOG_TRAFFIC);
85         // enable(TestProperties.DUMP_ENTITY);
86         // enable(TestProperties.RECORD_LOG_LEVEL);
87         // set(TestProperties.RECORD_LOG_LEVEL, Level.ALL.intValue());
88         ResourceConfig resourceConfig = new ResourceConfig();
89         resourceConfig = resourceConfig.registerInstances(restconfImpl, StructuredDataToXmlProvider.INSTANCE,
90                 StructuredDataToJsonProvider.INSTANCE, XmlToCompositeNodeProvider.INSTANCE,
91                 JsonToCompositeNodeProvider.INSTANCE);
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         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         String uri = "/config/ietf-interfaces:interfaces/interface/eth0";
114         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     public void testRpcResultCommitedToStatusCodesWithMountPoint() throws UnsupportedEncodingException,
121             FileNotFoundException, URISyntaxException {
122
123         CheckedFuture<Void, TransactionCommitFailedException> dummyFuture = mock(CheckedFuture.class);
124
125         when(
126                 brokerFacade.commitConfigurationDataPut(any(DOMMountPoint.class), any(YangInstanceIdentifier.class),
127                         any(NormalizedNode.class))).thenReturn(dummyFuture);
128
129         DOMMountPoint mountInstance = mock(DOMMountPoint.class);
130         when(mountInstance.getSchemaContext()).thenReturn(schemaContextTestModule);
131         DOMMountPointService mockMountService = mock(DOMMountPointService.class);
132         when(mockMountService.getMountPoint(any(YangInstanceIdentifier.class))).thenReturn(Optional.of(mountInstance));
133
134         ControllerContext.getInstance().setMountService(mockMountService);
135
136         String uri = "/config/ietf-interfaces:interfaces/interface/0/yang-ext:mount/test-module:cont";
137         assertEquals(200, put(uri, MediaType.APPLICATION_XML, xmlData2));
138
139         uri = "/config/ietf-interfaces:interfaces/yang-ext:mount/test-module:cont";
140         assertEquals(200, put(uri, MediaType.APPLICATION_XML, xmlData2));
141     }
142
143     @Test
144     public void putDataMountPointIntoHighestElement() throws UnsupportedEncodingException, URISyntaxException {
145         CheckedFuture<Void, TransactionCommitFailedException> dummyFuture = mock(CheckedFuture.class);
146         when(
147                 brokerFacade.commitConfigurationDataPut(any(DOMMountPoint.class), any(YangInstanceIdentifier.class),
148                         any(NormalizedNode.class))).thenReturn(dummyFuture);
149
150         DOMMountPoint mountInstance = mock(DOMMountPoint.class);
151         when(mountInstance.getSchemaContext()).thenReturn(schemaContextTestModule);
152         DOMMountPointService mockMountService = mock(DOMMountPointService.class);
153         when(mockMountService.getMountPoint(any(YangInstanceIdentifier.class))).thenReturn(Optional.of(mountInstance));
154
155         ControllerContext.getInstance().setMountService(mockMountService);
156
157         String uri = "/config/ietf-interfaces:interfaces/yang-ext:mount";
158         assertEquals(200, put(uri, MediaType.APPLICATION_XML, xmlData3));
159     }
160
161     private int put(String uri, String mediaType, String data) throws UnsupportedEncodingException {
162         return target(uri).request(mediaType).put(Entity.entity(data, mediaType)).getStatus();
163     }
164
165     private void mockCommitConfigurationDataPutMethod(final boolean noErrors) {
166         if (noErrors) {
167             doReturn(mock(CheckedFuture.class)).when(brokerFacade).commitConfigurationDataPut(
168                     any(YangInstanceIdentifier.class), any(NormalizedNode.class));
169         } else {
170             doThrow(RestconfDocumentedException.class).when(brokerFacade).commitConfigurationDataPut(
171                     any(YangInstanceIdentifier.class), any(NormalizedNode.class));
172         }
173     }
174
175 }