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.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;
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.test.TestUtils;
25 import org.opendaylight.netconf.sal.restconf.impl.BrokerFacade;
26 import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
27 import org.opendaylight.netconf.sal.restconf.impl.InstanceIdentifierContext;
28 import org.opendaylight.netconf.sal.restconf.impl.NormalizedNodeContext;
29 import org.opendaylight.netconf.sal.restconf.impl.RestconfDocumentedException;
30 import org.opendaylight.netconf.sal.restconf.impl.RestconfError;
31 import org.opendaylight.netconf.sal.restconf.impl.RestconfError.ErrorTag;
32 import org.opendaylight.netconf.sal.restconf.impl.RestconfError.ErrorType;
33 import org.opendaylight.netconf.sal.restconf.impl.RestconfImpl;
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 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
51 public class RestPutListDataTest {
53 private static BrokerFacade brokerFacade;
54 private static RestconfImpl restconfImpl;
55 private static SchemaContext schemaContextTestModule;
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";
62 TEST_MODULE_NS = URI.create("test:module");
66 public void initialize() throws FileNotFoundException, ReactorException {
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(SchemaContext.class), any(YangInstanceIdentifier.class), any(NormalizedNode.class)))
75 .thenReturn(mock(CheckedFuture.class));
79 * Tests whether no exception is raised if number and values of keys in URI
80 * and payload are equal
84 public void testValidKeys() {
85 putListDataTest("key1value", "15", "key1value", (short) 15);
89 * Tests whether an exception is raised if key values in URI and payload are
92 * The exception should be raised from validation method
93 * {@code RestconfImpl#validateListEqualityOfListInDataAndUri}
96 @Ignore // RestconfDocumentedExceptionMapper needs update
97 public void testUriAndPayloadKeysDifferent() {
99 putListDataTest("key1value", "15", "key1value", (short) 16);
100 fail("RestconfDocumentedException expected");
101 } catch (final RestconfDocumentedException e) {
102 verifyException(e, ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
106 putListDataTest("key1value", "15", "key1value1", (short) 16);
107 fail("RestconfDocumentedException expected");
108 } catch (final RestconfDocumentedException e) {
109 verifyException(e, ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
114 * Tests whether an exception is raised if URI contains less key values then
117 * The exception is raised during {@code InstanceIdentifier} instance is
122 public void testMissingKeysInUri() {
124 putListDataTest("key1value", null, "key1value", (short) 15);
125 fail("RestconfDocumentedException expected");
126 } catch (final RestconfDocumentedException e) {
127 verifyException(e, ErrorType.PROTOCOL, ErrorTag.DATA_MISSING);
132 * Tests whether an exception is raised if URI contains more key values then
135 * The exception should be raised from validation method
136 * {@code RestconfImpl#validateListEqualityOfListInDataAndUri}
139 public void testMissingKeysInPayload() {
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);
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());
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");
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);
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());
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());
194 final NormalizedNodeContext testCompositeContext = new NormalizedNodeContext(new InstanceIdentifierContext<>(
195 null, testNodeSchemaNode, null, schemaContextTestModule), testNodeContainer.build());
197 restconfImpl.updateConfigurationData(toUri(uriKey1, uriKey2), testCompositeContext);
200 public void putListDataWithWrapperTest(final String uriKey1, final String uriKey2, final String payloadKey1,
201 final Short payloadKey2) {
202 putListDataTest(uriKey1, uriKey2, payloadKey1, payloadKey2);
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);
212 return uriBuilder.toString();