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

©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.