2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.sal.restconf.impl.input.to.cnsn.test;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertTrue;
13 import static org.junit.Assert.fail;
14 import static org.mockito.ArgumentMatchers.any;
15 import static org.mockito.Mockito.mock;
16 import static org.mockito.Mockito.when;
18 import com.google.common.util.concurrent.FluentFuture;
19 import java.io.FileNotFoundException;
20 import java.util.List;
21 import javax.ws.rs.core.Response.Status;
22 import javax.ws.rs.core.UriInfo;
23 import org.junit.Before;
24 import org.junit.BeforeClass;
25 import org.junit.Ignore;
26 import org.junit.Test;
27 import org.mockito.Mockito;
28 import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
29 import org.opendaylight.controller.sal.restconf.impl.test.TestUtils;
30 import org.opendaylight.netconf.sal.rest.impl.NormalizedNodeContext;
31 import org.opendaylight.netconf.sal.restconf.impl.BrokerFacade;
32 import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
33 import org.opendaylight.netconf.sal.restconf.impl.PutResult;
34 import org.opendaylight.netconf.sal.restconf.impl.RestconfImpl;
35 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
36 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
37 import org.opendaylight.restconf.common.errors.RestconfError;
38 import org.opendaylight.yangtools.yang.common.ErrorTag;
39 import org.opendaylight.yangtools.yang.common.ErrorType;
40 import org.opendaylight.yangtools.yang.common.QName;
41 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
42 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
43 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
44 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
45 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
46 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
47 import org.opendaylight.yangtools.yang.data.api.schema.builder.DataContainerNodeBuilder;
48 import org.opendaylight.yangtools.yang.data.api.schema.builder.NormalizedNodeBuilder;
49 import org.opendaylight.yangtools.yang.data.impl.schema.SchemaAwareBuilders;
50 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.valid.DataValidationException;
51 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
52 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
53 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
54 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
55 import org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack;
57 public class RestPutListDataTest {
58 private static EffectiveModelContext schemaContextTestModule;
60 private static BrokerFacade brokerFacade;
61 private static RestconfImpl restconfImpl;
63 private static final String TEST_MODULE_NS_STRING = "test:module";
64 private static final String TEST_MODULE_REVISION = "2014-01-09";
67 public static void staticSetup() throws FileNotFoundException {
68 schemaContextTestModule = TestUtils.loadSchemaContext("/full-versions/test-module");
72 public void initialize() throws FileNotFoundException {
73 final ControllerContext controllerContext = TestRestconfUtils.newControllerContext(schemaContextTestModule);
74 brokerFacade = mock(BrokerFacade.class);
75 restconfImpl = RestconfImpl.newInstance(brokerFacade, controllerContext);
76 final PutResult result = mock(PutResult.class);
77 when(brokerFacade.commitConfigurationDataPut(any(EffectiveModelContext.class),
78 any(YangInstanceIdentifier.class), any(NormalizedNode.class), Mockito.anyString(), Mockito.anyString()))
80 when(result.getFutureOfPutData()).thenReturn(mock(FluentFuture.class));
81 when(result.getStatus()).thenReturn(Status.OK);
85 * Tests whether no exception is raised if number and values of keys in URI
86 * and payload are equal.
90 public void testValidKeys() {
91 putListDataTest("key1value", "15", "key1value", (short) 15);
95 * Tests whether an exception is raised if key values in URI and payload are
99 * The exception should be raised from validation method
100 * {@code RestconfImpl#validateListEqualityOfListInDataAndUri}
103 @Ignore // RestconfDocumentedExceptionMapper needs update
104 public void testUriAndPayloadKeysDifferent() {
106 putListDataTest("key1value", "15", "key1value", (short) 16);
107 fail("RestconfDocumentedException expected");
108 } catch (final RestconfDocumentedException e) {
109 verifyException(e, ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
113 putListDataTest("key1value", "15", "key1value1", (short) 16);
114 fail("RestconfDocumentedException expected");
115 } catch (final RestconfDocumentedException e) {
116 verifyException(e, ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
121 * Tests whether an exception is raised if URI contains less key values then
125 * The exception is raised during {@code InstanceIdentifier} instance is
130 public void testMissingKeysInUri() {
132 putListDataTest("key1value", null, "key1value", (short) 15);
133 fail("RestconfDocumentedException expected");
134 } catch (final RestconfDocumentedException e) {
135 verifyException(e, ErrorType.PROTOCOL, ErrorTag.DATA_MISSING);
140 * Tests whether an exception is raised if URI contains more key values then
144 * The exception should be raised from validation method
145 * {@code RestconfImpl#validateListEqualityOfListInDataAndUri}
148 public void testMissingKeysInPayload() {
150 putListDataTest("key1value", "15", "key1value", null);
151 fail("RestconfDocumentedException expected");
152 } catch (final DataValidationException e) {
153 // FIXME: thing about different approach for testing the Exception states
154 // RestconfDocumentedException is not rise in new API because you get
155 // DataValidationException from putListDataTest before you call the real rest service
156 // verifyException(e, ErrorType.PROTOCOL, ErrorTag.DATA_MISSING);
160 private static void verifyException(final RestconfDocumentedException restDocumentedException,
161 final ErrorType errorType, final ErrorTag errorTag) {
162 final List<RestconfError> errors = restDocumentedException.getErrors();
163 assertEquals("getErrors() size", 1, errors.size());
164 assertEquals("RestconfError getErrorType()", errorType, errors.get(0).getErrorType());
165 assertEquals("RestconfError getErrorTag()", errorTag, errors.get(0).getErrorTag());
168 public void putListDataTest(final String uriKey1, final String uriKey2, final String payloadKey1,
169 final Short payloadKey2) {
170 final QName lstWithCompositeKey =
171 QName.create(TEST_MODULE_NS_STRING, TEST_MODULE_REVISION, "lst-with-composite-key");
172 final QName key1 = QName.create(TEST_MODULE_NS_STRING, TEST_MODULE_REVISION, "key1");
173 final QName key2 = QName.create(TEST_MODULE_NS_STRING, TEST_MODULE_REVISION, "key2");
175 final DataSchemaNode testNodeSchemaNode = schemaContextTestModule.getDataChildByName(lstWithCompositeKey);
176 assertTrue(testNodeSchemaNode != null);
177 assertTrue(testNodeSchemaNode instanceof ListSchemaNode);
178 final DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> testNodeContainer =
179 SchemaAwareBuilders.mapEntryBuilder((ListSchemaNode) testNodeSchemaNode);
181 var testChildren = ControllerContext.findInstanceDataChildrenByName(
182 (ListSchemaNode) testNodeSchemaNode, key1.getLocalName());
183 assertTrue(testChildren != null);
184 final DataSchemaNode testLeafKey1SchemaNode = testChildren.get(0).child;
185 assertTrue(testLeafKey1SchemaNode != null);
186 assertTrue(testLeafKey1SchemaNode instanceof LeafSchemaNode);
187 final NormalizedNodeBuilder<NodeIdentifier, Object, LeafNode<Object>> leafKey1 =
188 SchemaAwareBuilders.leafBuilder((LeafSchemaNode) testLeafKey1SchemaNode);
189 leafKey1.withValue(payloadKey1);
190 testNodeContainer.withChild(leafKey1.build());
192 if (payloadKey2 != null) {
193 testChildren = ControllerContext.findInstanceDataChildrenByName(
194 (ListSchemaNode) testNodeSchemaNode, key2.getLocalName());
195 assertTrue(testChildren != null);
196 final DataSchemaNode testLeafKey2SchemaNode = testChildren.get(0).child;
197 assertNotNull(testLeafKey2SchemaNode);
198 assertTrue(testLeafKey2SchemaNode instanceof LeafSchemaNode);
199 final NormalizedNodeBuilder<NodeIdentifier, Object, LeafNode<Object>> leafKey2 =
200 SchemaAwareBuilders.leafBuilder((LeafSchemaNode) testLeafKey2SchemaNode);
201 leafKey2.withValue(payloadKey2);
202 testNodeContainer.withChild(leafKey2.build());
205 final NormalizedNodeContext testCompositeContext = new NormalizedNodeContext(
206 InstanceIdentifierContext.ofStack(
207 SchemaInferenceStack.ofDataTreePath(schemaContextTestModule, lstWithCompositeKey)),
208 testNodeContainer.build());
210 final UriInfo uriInfo = Mockito.mock(UriInfo.class);
211 restconfImpl.updateConfigurationData(toUri(uriKey1, uriKey2), testCompositeContext, uriInfo);
214 public void putListDataWithWrapperTest(final String uriKey1, final String uriKey2, final String payloadKey1,
215 final Short payloadKey2) {
216 putListDataTest(uriKey1, uriKey2, payloadKey1, payloadKey2);
219 private static String toUri(final String uriKey1, final String uriKey2) {
220 final StringBuilder uriBuilder = new StringBuilder("/test-module:lst-with-composite-key/");
221 uriBuilder.append(uriKey1);
222 if (uriKey2 != null) {
223 uriBuilder.append("/");
224 uriBuilder.append(uriKey2);
226 return uriBuilder.toString();