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.assertTrue;
12 import static org.junit.Assert.fail;
13 import static org.mockito.ArgumentMatchers.any;
14 import static org.mockito.Mockito.mock;
15 import static org.mockito.Mockito.when;
17 import com.google.common.collect.Iterables;
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.restconf.impl.BrokerFacade;
31 import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
32 import org.opendaylight.netconf.sal.restconf.impl.PutResult;
33 import org.opendaylight.netconf.sal.restconf.impl.RestconfImpl;
34 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
35 import org.opendaylight.restconf.common.context.NormalizedNodeContext;
36 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
37 import org.opendaylight.restconf.common.errors.RestconfError;
38 import org.opendaylight.restconf.common.errors.RestconfError.ErrorTag;
39 import org.opendaylight.restconf.common.errors.RestconfError.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.impl.schema.Builders;
48 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeBuilder;
49 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeBuilder;
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.LeafSchemaNode;
53 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
54 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
56 public class RestPutListDataTest {
57 private static SchemaContext schemaContextTestModule;
59 private static BrokerFacade brokerFacade;
60 private static RestconfImpl restconfImpl;
62 private static final String TEST_MODULE_NS_STRING = "test:module";
63 private static final String TEST_MODULE_REVISION = "2014-01-09";
66 public static void staticSetup() throws FileNotFoundException {
67 schemaContextTestModule = TestUtils.loadSchemaContext("/full-versions/test-module");
71 public void initialize() throws FileNotFoundException {
72 final ControllerContext controllerContext = TestRestconfUtils.newControllerContext(schemaContextTestModule);
73 brokerFacade = mock(BrokerFacade.class);
74 restconfImpl = RestconfImpl.newInstance(brokerFacade, controllerContext);
75 final PutResult result = mock(PutResult.class);
76 when(brokerFacade.commitConfigurationDataPut(any(SchemaContext.class), any(YangInstanceIdentifier.class),
77 any(NormalizedNode.class), Mockito.anyString(), Mockito.anyString()))
79 when(result.getFutureOfPutData()).thenReturn(mock(FluentFuture.class));
80 when(result.getStatus()).thenReturn(Status.OK);
84 * Tests whether no exception is raised if number and values of keys in URI
85 * and payload are equal.
89 public void testValidKeys() {
90 putListDataTest("key1value", "15", "key1value", (short) 15);
94 * Tests whether an exception is raised if key values in URI and payload are
98 * The exception should be raised from validation method
99 * {@code RestconfImpl#validateListEqualityOfListInDataAndUri}
102 @Ignore // RestconfDocumentedExceptionMapper needs update
103 public void testUriAndPayloadKeysDifferent() {
105 putListDataTest("key1value", "15", "key1value", (short) 16);
106 fail("RestconfDocumentedException expected");
107 } catch (final RestconfDocumentedException e) {
108 verifyException(e, ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
112 putListDataTest("key1value", "15", "key1value1", (short) 16);
113 fail("RestconfDocumentedException expected");
114 } catch (final RestconfDocumentedException e) {
115 verifyException(e, ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
120 * Tests whether an exception is raised if URI contains less key values then
124 * The exception is raised during {@code InstanceIdentifier} instance is
129 public void testMissingKeysInUri() {
131 putListDataTest("key1value", null, "key1value", (short) 15);
132 fail("RestconfDocumentedException expected");
133 } catch (final RestconfDocumentedException e) {
134 verifyException(e, ErrorType.PROTOCOL, ErrorTag.DATA_MISSING);
139 * Tests whether an exception is raised if URI contains more key values then
143 * The exception should be raised from validation method
144 * {@code RestconfImpl#validateListEqualityOfListInDataAndUri}
147 public void testMissingKeysInPayload() {
149 putListDataTest("key1value", "15", "key1value", null);
150 fail("RestconfDocumentedException expected");
151 } catch (final DataValidationException e) {
152 // FIXME: thing about different approach for testing the Exception states
153 // RestconfDocumentedException is not rise in new API because you get
154 // DataValidationException from putListDataTest before you call the real rest service
155 // verifyException(e, ErrorType.PROTOCOL, ErrorTag.DATA_MISSING);
159 private static void verifyException(final RestconfDocumentedException restDocumentedException,
160 final ErrorType errorType, final ErrorTag errorTag) {
161 final List<RestconfError> errors = restDocumentedException.getErrors();
162 assertEquals("getErrors() size", 1, errors.size());
163 assertEquals("RestconfError getErrorType()", errorType, errors.get(0).getErrorType());
164 assertEquals("RestconfError getErrorTag()", errorTag, errors.get(0).getErrorTag());
167 public void putListDataTest(final String uriKey1, final String uriKey2, final String payloadKey1,
168 final Short payloadKey2) {
169 final QName lstWithCompositeKey =
170 QName.create(TEST_MODULE_NS_STRING, TEST_MODULE_REVISION, "lst-with-composite-key");
171 final QName key1 = QName.create(TEST_MODULE_NS_STRING, TEST_MODULE_REVISION, "key1");
172 final QName key2 = QName.create(TEST_MODULE_NS_STRING, TEST_MODULE_REVISION, "key2");
174 final DataSchemaNode testNodeSchemaNode = schemaContextTestModule.getDataChildByName(lstWithCompositeKey);
175 assertTrue(testNodeSchemaNode != null);
176 assertTrue(testNodeSchemaNode instanceof ListSchemaNode);
177 final DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> testNodeContainer =
178 Builders.mapEntryBuilder((ListSchemaNode) testNodeSchemaNode);
180 List<DataSchemaNode> testChildren = ControllerContext.findInstanceDataChildrenByName(
181 (ListSchemaNode) testNodeSchemaNode, key1.getLocalName());
182 assertTrue(testChildren != null);
183 final DataSchemaNode testLeafKey1SchemaNode = Iterables.getFirst(testChildren, null);
184 assertTrue(testLeafKey1SchemaNode != null);
185 assertTrue(testLeafKey1SchemaNode instanceof LeafSchemaNode);
186 final NormalizedNodeBuilder<NodeIdentifier, Object, LeafNode<Object>> leafKey1 =
187 Builders.leafBuilder((LeafSchemaNode) testLeafKey1SchemaNode);
188 leafKey1.withValue(payloadKey1);
189 testNodeContainer.withChild(leafKey1.build());
191 if (payloadKey2 != null) {
192 testChildren = ControllerContext.findInstanceDataChildrenByName(
193 (ListSchemaNode) testNodeSchemaNode, key2.getLocalName());
194 assertTrue(testChildren != null);
195 final DataSchemaNode testLeafKey2SchemaNode = Iterables.getFirst(testChildren, null);
196 assertTrue(testLeafKey2SchemaNode != null);
197 assertTrue(testLeafKey2SchemaNode instanceof LeafSchemaNode);
198 final NormalizedNodeBuilder<NodeIdentifier, Object, LeafNode<Object>> leafKey2 =
199 Builders.leafBuilder((LeafSchemaNode) testLeafKey2SchemaNode);
200 leafKey2.withValue(payloadKey2);
201 testNodeContainer.withChild(leafKey2.build());
204 final NormalizedNodeContext testCompositeContext = new NormalizedNodeContext(new InstanceIdentifierContext<>(
205 null, testNodeSchemaNode, null, schemaContextTestModule), testNodeContainer.build());
207 final UriInfo uriInfo = Mockito.mock(UriInfo.class);
208 restconfImpl.updateConfigurationData(toUri(uriKey1, uriKey2), testCompositeContext, uriInfo);
211 public void putListDataWithWrapperTest(final String uriKey1, final String uriKey2, final String payloadKey1,
212 final Short payloadKey2) {
213 putListDataTest(uriKey1, uriKey2, payloadKey1, payloadKey2);
216 private static String toUri(final String uriKey1, final String uriKey2) {
217 final StringBuilder uriBuilder = new StringBuilder("/test-module:lst-with-composite-key/");
218 uriBuilder.append(uriKey1);
219 if (uriKey2 != null) {
220 uriBuilder.append("/");
221 uriBuilder.append(uriKey2);
223 return uriBuilder.toString();