Merge changes I4ec69712,I8348002a,I7e6abbf1,Idc2294da
[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     public void testValidKeys() {
83         putListDataTest("key1value", "15", "key1value", (short) 15);
84     }
85
86     /**
87      * Tests whether an exception is raised if key values in URI and payload are
88      * different.
89      *
90      * The exception should be raised from validation method
91      * {@code RestconfImpl#validateListEqualityOfListInDataAndUri}
92      */
93     @Test
94     @Ignore // RestconfDocumentedExceptionMapper needs update
95     public void testUriAndPayloadKeysDifferent() {
96         try {
97             putListDataTest("key1value", "15", "key1value", (short) 16);
98             fail("RestconfDocumentedException expected");
99         } catch (final RestconfDocumentedException e) {
100             verifyException(e, ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
101         }
102
103         try {
104             putListDataTest("key1value", "15", "key1value1", (short) 16);
105             fail("RestconfDocumentedException expected");
106         } catch (final RestconfDocumentedException e) {
107             verifyException(e, ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
108         }
109     }
110
111     /**
112      * Tests whether an exception is raised if URI contains less key values then
113      * payload.
114      *
115      * The exception is raised during {@code InstanceIdentifier} instance is
116      * built from URI
117      */
118     @Test
119     public void testMissingKeysInUri() {
120         try {
121             putListDataTest("key1value", null, "key1value", (short) 15);
122             fail("RestconfDocumentedException expected");
123         } catch (final RestconfDocumentedException e) {
124             verifyException(e, ErrorType.PROTOCOL, ErrorTag.DATA_MISSING);
125         }
126     }
127
128     /**
129      * Tests whether an exception is raised if URI contains more key values then
130      * payload.
131      *
132      * The exception should be raised from validation method
133      * {@code RestconfImpl#validateListEqualityOfListInDataAndUri}
134      */
135     @Test
136     public void testMissingKeysInPayload() {
137         try {
138             putListDataTest("key1value", "15", "key1value", null);
139             fail("RestconfDocumentedException expected");
140         } catch (final DataValidationException e) {
141             // FIXME: thing about different approach for testing the Exception states
142             // RestconfDocumentedException is not rise in new API because you get
143             // DataValidationException from putListDataTest before you call the real rest service
144 //            verifyException(e, ErrorType.PROTOCOL, ErrorTag.DATA_MISSING);
145         }
146     }
147
148     private void verifyException(final RestconfDocumentedException e, final ErrorType errorType, final ErrorTag errorTag) {
149         final List<RestconfError> errors = e.getErrors();
150         assertEquals("getErrors() size", 1, errors.size());
151         assertEquals("RestconfError getErrorType()", errorType, errors.get(0).getErrorType());
152         assertEquals("RestconfError getErrorTag()", errorTag, errors.get(0).getErrorTag());
153     }
154
155     public void putListDataTest(final String uriKey1, final String uriKey2, final String payloadKey1,
156             final Short payloadKey2) {
157         final QName lstWithCompositeKey = QName.create(TEST_MODULE_NS_STRING, TEST_MODULE_REVISION, "lst-with-composite-key");
158         final QName key1 = QName.create(TEST_MODULE_NS_STRING, TEST_MODULE_REVISION, "key1");
159         final QName key2 = QName.create(TEST_MODULE_NS_STRING, TEST_MODULE_REVISION, "key2");
160
161         final DataSchemaNode testNodeSchemaNode = schemaContextTestModule.getDataChildByName(lstWithCompositeKey);
162         assertTrue(testNodeSchemaNode != null);
163         assertTrue(testNodeSchemaNode instanceof ListSchemaNode);
164         final DataContainerNodeAttrBuilder<NodeIdentifierWithPredicates, MapEntryNode> testNodeContainer =
165                 Builders.mapEntryBuilder((ListSchemaNode) testNodeSchemaNode);
166
167         List<DataSchemaNode> testChildren = ControllerContext.findInstanceDataChildrenByName(
168                 (ListSchemaNode) testNodeSchemaNode, key1.getLocalName());
169         assertTrue(testChildren != null);
170         final DataSchemaNode testLeafKey1SchemaNode = Iterables.getFirst(testChildren, null);
171         assertTrue(testLeafKey1SchemaNode != null);
172         assertTrue(testLeafKey1SchemaNode instanceof LeafSchemaNode);
173         final NormalizedNodeAttrBuilder<NodeIdentifier, Object, LeafNode<Object>> leafKey1 =
174                 Builders.leafBuilder((LeafSchemaNode) testLeafKey1SchemaNode);
175         leafKey1.withValue(payloadKey1);
176         testNodeContainer.withChild(leafKey1.build());
177
178         if (payloadKey2 != null) {
179             testChildren = ControllerContext.findInstanceDataChildrenByName(
180                     (ListSchemaNode) testNodeSchemaNode, key2.getLocalName());
181             assertTrue(testChildren != null);
182             final DataSchemaNode testLeafKey2SchemaNode = Iterables.getFirst(testChildren, null);
183             assertTrue(testLeafKey2SchemaNode != null);
184             assertTrue(testLeafKey2SchemaNode instanceof LeafSchemaNode);
185             final NormalizedNodeAttrBuilder<NodeIdentifier, Object, LeafNode<Object>> leafKey2 =
186                     Builders.leafBuilder((LeafSchemaNode) testLeafKey2SchemaNode);
187             leafKey2.withValue(payloadKey2);
188             testNodeContainer.withChild(leafKey2.build());
189         }
190
191         final NormalizedNodeContext testCompositeContext = new NormalizedNodeContext(new InstanceIdentifierContext(
192                 null, testNodeSchemaNode, null, schemaContextTestModule), testNodeContainer.build());
193
194         restconfImpl.updateConfigurationData(toUri(uriKey1, uriKey2), testCompositeContext);
195     }
196
197     public void putListDataWithWrapperTest(final String uriKey1, final String uriKey2, final String payloadKey1,
198             final Short payloadKey2) {
199         putListDataTest(uriKey1, uriKey2, payloadKey1, payloadKey2);
200     }
201
202     private String toUri(final String uriKey1, final String uriKey2) {
203         final StringBuilder uriBuilder = new StringBuilder("/test-module:lst-with-composite-key/");
204         uriBuilder.append(uriKey1);
205         if (uriKey2 != null) {
206             uriBuilder.append("/");
207             uriBuilder.append(uriKey2);
208         }
209         return uriBuilder.toString();
210     }
211
212 }