Cleanup: Remove passing around of DataPersistenceProvider
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / test / java / org / opendaylight / controller / sal / restconf / impl / input / to / cnsn / test / RestPutListDataTest.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.input.to.cnsn.test;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertTrue;
12 import static org.junit.Assert.fail;
13 import static org.mockito.Matchers.any;
14 import static org.mockito.Mockito.mock;
15 import static org.mockito.Mockito.when;
16 import com.google.common.collect.Iterables;
17 import com.google.common.util.concurrent.CheckedFuture;
18 import java.io.FileNotFoundException;
19 import java.net.URI;
20 import java.util.List;
21 import org.junit.Before;
22 import org.junit.Ignore;
23 import org.junit.Test;
24 import org.opendaylight.controller.sal.restconf.impl.BrokerFacade;
25 import org.opendaylight.controller.sal.restconf.impl.ControllerContext;
26 import org.opendaylight.controller.sal.restconf.impl.InstanceIdentifierContext;
27 import org.opendaylight.controller.sal.restconf.impl.NormalizedNodeContext;
28 import org.opendaylight.controller.sal.restconf.impl.RestconfDocumentedException;
29 import org.opendaylight.controller.sal.restconf.impl.RestconfError;
30 import org.opendaylight.controller.sal.restconf.impl.RestconfError.ErrorTag;
31 import org.opendaylight.controller.sal.restconf.impl.RestconfError.ErrorType;
32 import org.opendaylight.controller.sal.restconf.impl.RestconfImpl;
33 import org.opendaylight.controller.sal.restconf.impl.test.TestUtils;
34 import org.opendaylight.yangtools.yang.common.QName;
35 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
36 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
37 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
38 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
39 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
40 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
41 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
42 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeAttrBuilder;
43 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeAttrBuilder;
44 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.valid.DataValidationException;
45 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
46 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
47 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
48 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
49
50 public class RestPutListDataTest {
51
52     private static BrokerFacade brokerFacade;
53     private static RestconfImpl restconfImpl;
54     private static SchemaContext schemaContextTestModule;
55
56     private static final String TEST_MODULE_NS_STRING = "test:module";
57     private static final URI TEST_MODULE_NS;
58     private static final String TEST_MODULE_REVISION = "2014-01-09";
59
60     static {
61         TEST_MODULE_NS = URI.create("test:module");
62     }
63
64     @Before
65     public void initialize() throws FileNotFoundException {
66         final ControllerContext controllerContext = ControllerContext.getInstance();
67         schemaContextTestModule = TestUtils.loadSchemaContext("/full-versions/test-module");
68         controllerContext.setSchemas(schemaContextTestModule);
69         brokerFacade = mock(BrokerFacade.class);
70         restconfImpl = RestconfImpl.getInstance();
71         restconfImpl.setBroker(brokerFacade);
72         restconfImpl.setControllerContext(controllerContext);
73         when(brokerFacade.commitConfigurationDataPut(any(YangInstanceIdentifier.class), any(NormalizedNode.class)))
74                 .thenReturn(mock(CheckedFuture.class));
75     }
76
77     /**
78      * Tests whether no exception is raised if number and values of keys in URI
79      * and payload are equal
80      */
81     @Test
82     @Ignore
83     public void testValidKeys() {
84         putListDataTest("key1value", "15", "key1value", (short) 15);
85     }
86
87     /**
88      * Tests whether an exception is raised if key values in URI and payload are
89      * different.
90      *
91      * The exception should be raised from validation method
92      * {@code RestconfImpl#validateListEqualityOfListInDataAndUri}
93      */
94     @Test
95     @Ignore // RestconfDocumentedExceptionMapper needs update
96     public void testUriAndPayloadKeysDifferent() {
97         try {
98             putListDataTest("key1value", "15", "key1value", (short) 16);
99             fail("RestconfDocumentedException expected");
100         } catch (final RestconfDocumentedException e) {
101             verifyException(e, ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
102         }
103
104         try {
105             putListDataTest("key1value", "15", "key1value1", (short) 16);
106             fail("RestconfDocumentedException expected");
107         } catch (final RestconfDocumentedException e) {
108             verifyException(e, ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
109         }
110     }
111
112     /**
113      * Tests whether an exception is raised if URI contains less key values then
114      * payload.
115      *
116      * The exception is raised during {@code InstanceIdentifier} instance is
117      * built from URI
118      */
119     @Test
120     @Ignore
121     public void testMissingKeysInUri() {
122         try {
123             putListDataTest("key1value", null, "key1value", (short) 15);
124             fail("RestconfDocumentedException expected");
125         } catch (final RestconfDocumentedException e) {
126             verifyException(e, ErrorType.PROTOCOL, ErrorTag.DATA_MISSING);
127         }
128     }
129
130     /**
131      * Tests whether an exception is raised if URI contains more key values then
132      * payload.
133      *
134      * The exception should be raised from validation method
135      * {@code RestconfImpl#validateListEqualityOfListInDataAndUri}
136      */
137     @Test
138     public void testMissingKeysInPayload() {
139         try {
140             putListDataTest("key1value", "15", "key1value", null);
141             fail("RestconfDocumentedException expected");
142         } catch (final DataValidationException e) {
143             // FIXME: thing about different approach for testing the Exception states
144             // RestconfDocumentedException is not rise in new API because you get
145             // DataValidationException from putListDataTest before you call the real rest service
146 //            verifyException(e, ErrorType.PROTOCOL, ErrorTag.DATA_MISSING);
147         }
148     }
149
150     private void verifyException(final RestconfDocumentedException e, final ErrorType errorType, final ErrorTag errorTag) {
151         final List<RestconfError> errors = e.getErrors();
152         assertEquals("getErrors() size", 1, errors.size());
153         assertEquals("RestconfError getErrorType()", errorType, errors.get(0).getErrorType());
154         assertEquals("RestconfError getErrorTag()", errorTag, errors.get(0).getErrorTag());
155     }
156
157     public void putListDataTest(final String uriKey1, final String uriKey2, final String payloadKey1,
158             final Short payloadKey2) {
159         final QName lstWithCompositeKey = QName.create(TEST_MODULE_NS_STRING, TEST_MODULE_REVISION, "lst-with-composite-key");
160         final QName key1 = QName.create(TEST_MODULE_NS_STRING, TEST_MODULE_REVISION, "key1");
161         final QName key2 = QName.create(TEST_MODULE_NS_STRING, TEST_MODULE_REVISION, "key2");
162
163         final DataSchemaNode testNodeSchemaNode = schemaContextTestModule.getDataChildByName(lstWithCompositeKey);
164         assertTrue(testNodeSchemaNode != null);
165         assertTrue(testNodeSchemaNode instanceof ListSchemaNode);
166         final DataContainerNodeAttrBuilder<NodeIdentifierWithPredicates, MapEntryNode> testNodeContainer =
167                 Builders.mapEntryBuilder((ListSchemaNode) testNodeSchemaNode);
168
169         List<DataSchemaNode> testChildren = ControllerContext.findInstanceDataChildrenByName(
170                 (ListSchemaNode) testNodeSchemaNode, key1.getLocalName());
171         assertTrue(testChildren != null);
172         final DataSchemaNode testLeafKey1SchemaNode = Iterables.getFirst(testChildren, null);
173         assertTrue(testLeafKey1SchemaNode != null);
174         assertTrue(testLeafKey1SchemaNode instanceof LeafSchemaNode);
175         final NormalizedNodeAttrBuilder<NodeIdentifier, Object, LeafNode<Object>> leafKey1 =
176                 Builders.leafBuilder((LeafSchemaNode) testLeafKey1SchemaNode);
177         leafKey1.withValue(payloadKey1);
178         testNodeContainer.withChild(leafKey1.build());
179
180         if (payloadKey2 != null) {
181             testChildren = ControllerContext.findInstanceDataChildrenByName(
182                     (ListSchemaNode) testNodeSchemaNode, key2.getLocalName());
183             assertTrue(testChildren != null);
184             final DataSchemaNode testLeafKey2SchemaNode = Iterables.getFirst(testChildren, null);
185             assertTrue(testLeafKey2SchemaNode != null);
186             assertTrue(testLeafKey2SchemaNode instanceof LeafSchemaNode);
187             final NormalizedNodeAttrBuilder<NodeIdentifier, Object, LeafNode<Object>> leafKey2 =
188                     Builders.leafBuilder((LeafSchemaNode) testLeafKey2SchemaNode);
189             leafKey2.withValue(payloadKey2);
190             testNodeContainer.withChild(leafKey2.build());
191         }
192
193         final NormalizedNodeContext testCompositeContext = new NormalizedNodeContext(new InstanceIdentifierContext(
194                 null, testNodeSchemaNode, null, schemaContextTestModule), testNodeContainer.build());
195
196         restconfImpl.updateConfigurationData(toUri(uriKey1, uriKey2), testCompositeContext);
197     }
198
199     public void putListDataWithWrapperTest(final String uriKey1, final String uriKey2, final String payloadKey1,
200             final Short payloadKey2) {
201         putListDataTest(uriKey1, uriKey2, payloadKey1, payloadKey2);
202     }
203
204     private String toUri(final String uriKey1, final String uriKey2) {
205         final StringBuilder uriBuilder = new StringBuilder("/test-module:lst-with-composite-key/");
206         uriBuilder.append(uriKey1);
207         if (uriKey2 != null) {
208             uriBuilder.append("/");
209             uriBuilder.append(uriKey2);
210         }
211         return uriBuilder.toString();
212     }
213
214 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.