Added test cases for each .proto file to ensure
[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.mock;
13 import static org.mockito.Mockito.when;
14
15 import java.io.FileNotFoundException;
16 import java.io.IOException;
17 import java.io.InputStream;
18 import java.io.UnsupportedEncodingException;
19 import java.net.URISyntaxException;
20 import java.util.concurrent.Future;
21 import javax.ws.rs.client.Entity;
22 import javax.ws.rs.core.Application;
23 import javax.ws.rs.core.MediaType;
24 import javax.ws.rs.core.Response;
25 import org.glassfish.jersey.server.ResourceConfig;
26 import org.glassfish.jersey.test.JerseyTest;
27 import org.junit.BeforeClass;
28 import org.junit.Test;
29 import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
30 import org.opendaylight.controller.sal.core.api.mount.MountInstance;
31 import org.opendaylight.controller.sal.core.api.mount.MountService;
32 import org.opendaylight.controller.sal.rest.impl.JsonToCompositeNodeProvider;
33 import org.opendaylight.controller.sal.rest.impl.RestconfDocumentedExceptionMapper;
34 import org.opendaylight.controller.sal.rest.impl.StructuredDataToJsonProvider;
35 import org.opendaylight.controller.sal.rest.impl.StructuredDataToXmlProvider;
36 import org.opendaylight.controller.sal.rest.impl.XmlToCompositeNodeProvider;
37 import org.opendaylight.controller.sal.restconf.impl.BrokerFacade;
38 import org.opendaylight.controller.sal.restconf.impl.ControllerContext;
39 import org.opendaylight.controller.sal.restconf.impl.RestconfImpl;
40 import org.opendaylight.yangtools.yang.common.RpcResult;
41 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
42 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
43 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
44
45 public class RestPutOperationTest extends JerseyTest {
46
47     private static String xmlData;
48     private static String xmlData2;
49     private static String xmlData3;
50
51     private static BrokerFacade brokerFacade;
52     private static RestconfImpl restconfImpl;
53     private static SchemaContext schemaContextYangsIetf;
54     private static SchemaContext schemaContextTestModule;
55
56     @BeforeClass
57     public static void init() throws IOException {
58         schemaContextYangsIetf = TestUtils.loadSchemaContext("/full-versions/yangs");
59         schemaContextTestModule = TestUtils.loadSchemaContext("/full-versions/test-module");
60         ControllerContext controllerContext = ControllerContext.getInstance();
61         controllerContext.setSchemas(schemaContextYangsIetf);
62         brokerFacade = mock(BrokerFacade.class);
63         restconfImpl = RestconfImpl.getInstance();
64         restconfImpl.setBroker(brokerFacade);
65         restconfImpl.setControllerContext(controllerContext);
66         loadData();
67     }
68
69     private static void loadData() throws IOException {
70         InputStream xmlStream = RestconfImplTest.class.getResourceAsStream("/parts/ietf-interfaces_interfaces.xml");
71         xmlData = TestUtils.getDocumentInPrintableForm(TestUtils.loadDocumentFrom(xmlStream));
72         InputStream xmlStream2 = RestconfImplTest.class.getResourceAsStream("/full-versions/test-data2/data2.xml");
73         xmlData2 = TestUtils.getDocumentInPrintableForm(TestUtils.loadDocumentFrom(xmlStream2));
74         InputStream xmlStream3 = RestconfImplTest.class.getResourceAsStream("/full-versions/test-data2/data7.xml");
75         xmlData3 = TestUtils.getDocumentInPrintableForm(TestUtils.loadDocumentFrom(xmlStream3));
76     }
77
78     @Override
79     protected Application configure() {
80         /* enable/disable Jersey logs to console */
81         // enable(TestProperties.LOG_TRAFFIC);
82         // enable(TestProperties.DUMP_ENTITY);
83         // enable(TestProperties.RECORD_LOG_LEVEL);
84         // set(TestProperties.RECORD_LOG_LEVEL, Level.ALL.intValue());
85         ResourceConfig resourceConfig = new ResourceConfig();
86         resourceConfig = resourceConfig.registerInstances(restconfImpl, StructuredDataToXmlProvider.INSTANCE,
87                 StructuredDataToJsonProvider.INSTANCE, XmlToCompositeNodeProvider.INSTANCE,
88                 JsonToCompositeNodeProvider.INSTANCE);
89         resourceConfig.registerClasses(RestconfDocumentedExceptionMapper.class);
90         return resourceConfig;
91     }
92
93     /**
94      * Tests of status codes for "/config/{identifier}".
95      */
96     @Test
97     public void putConfigStatusCodes() throws UnsupportedEncodingException {
98         String uri = "/config/ietf-interfaces:interfaces/interface/eth0";
99         mockCommitConfigurationDataPutMethod(TransactionStatus.COMMITED);
100         assertEquals(200, put(uri, MediaType.APPLICATION_XML, xmlData));
101
102         mockCommitConfigurationDataPutMethod(TransactionStatus.FAILED);
103         assertEquals(500, put(uri, MediaType.APPLICATION_XML, xmlData));
104
105         assertEquals(400, put(uri, MediaType.APPLICATION_JSON, ""));
106     }
107
108     @Test
109     public void putConfigStatusCodesEmptyBody() throws UnsupportedEncodingException {
110         String uri = "/config/ietf-interfaces:interfaces/interface/eth0";
111         Response resp = target(uri).request(MediaType.APPLICATION_JSON).put(
112                 Entity.entity("", MediaType.APPLICATION_JSON));
113         assertEquals(400, put(uri, MediaType.APPLICATION_JSON, ""));
114     }
115
116     @Test
117     public void testRpcResultCommitedToStatusCodesWithMountPoint() throws UnsupportedEncodingException,
118             FileNotFoundException, URISyntaxException {
119
120         RpcResult<TransactionStatus> rpcResult = new DummyRpcResult.Builder<TransactionStatus>().result(
121                 TransactionStatus.COMMITED).build();
122         Future<RpcResult<TransactionStatus>> dummyFuture = new DummyFuture.Builder<TransactionStatus>().rpcResult(
123                 rpcResult).build();
124         when(
125                 brokerFacade.commitConfigurationDataPutBehindMountPoint(any(MountInstance.class),
126                         any(YangInstanceIdentifier.class), any(CompositeNode.class))).thenReturn(dummyFuture);
127
128         MountInstance mountInstance = mock(MountInstance.class);
129         when(mountInstance.getSchemaContext()).thenReturn(schemaContextTestModule);
130         MountService mockMountService = mock(MountService.class);
131         when(mockMountService.getMountPoint(any(YangInstanceIdentifier.class))).thenReturn(mountInstance);
132
133         ControllerContext.getInstance().setMountService(mockMountService);
134
135         String uri = "/config/ietf-interfaces:interfaces/interface/0/yang-ext:mount/test-module:cont";
136         assertEquals(200, put(uri, MediaType.APPLICATION_XML, xmlData2));
137
138         uri = "/config/ietf-interfaces:interfaces/yang-ext:mount/test-module:cont";
139         assertEquals(200, put(uri, MediaType.APPLICATION_XML, xmlData2));
140     }
141
142     @Test
143     public void putDataMountPointIntoHighestElement() throws UnsupportedEncodingException, URISyntaxException {
144         RpcResult<TransactionStatus> rpcResult = new DummyRpcResult.Builder<TransactionStatus>().result(
145                 TransactionStatus.COMMITED).build();
146         Future<RpcResult<TransactionStatus>> dummyFuture = new DummyFuture.Builder<TransactionStatus>().rpcResult(
147                 rpcResult).build();
148         when(
149                 brokerFacade.commitConfigurationDataPutBehindMountPoint(any(MountInstance.class),
150                         any(YangInstanceIdentifier.class), any(CompositeNode.class))).thenReturn(dummyFuture);
151
152         MountInstance mountInstance = mock(MountInstance.class);
153         when(mountInstance.getSchemaContext()).thenReturn(schemaContextTestModule);
154         MountService mockMountService = mock(MountService.class);
155         when(mockMountService.getMountPoint(any(YangInstanceIdentifier.class))).thenReturn(mountInstance);
156
157         ControllerContext.getInstance().setMountService(mockMountService);
158
159         String uri = "/config/ietf-interfaces:interfaces/yang-ext:mount";
160         assertEquals(200, put(uri, MediaType.APPLICATION_XML, xmlData3));
161     }
162
163     private int put(String uri, String mediaType, String data) throws UnsupportedEncodingException {
164         return target(uri).request(mediaType).put(Entity.entity(data, mediaType)).getStatus();
165     }
166
167     private void mockCommitConfigurationDataPutMethod(TransactionStatus statusName) {
168         RpcResult<TransactionStatus> rpcResult = new DummyRpcResult.Builder<TransactionStatus>().result(statusName)
169                 .build();
170         Future<RpcResult<TransactionStatus>> dummyFuture = new DummyFuture.Builder<TransactionStatus>().rpcResult(
171                 rpcResult).build();
172         when(brokerFacade.commitConfigurationDataPut(any(YangInstanceIdentifier.class), any(CompositeNode.class)))
173                 .thenReturn(dummyFuture);
174     }
175
176 }