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 javax.ws.rs.core.Response.Status;
22 import org.junit.Before;
23 import org.junit.Ignore;
24 import org.junit.Test;
25 import org.opendaylight.controller.sal.restconf.impl.test.TestUtils;
26 import org.opendaylight.netconf.sal.restconf.impl.BrokerFacade;
27 import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
28 import org.opendaylight.netconf.sal.restconf.impl.InstanceIdentifierContext;
29 import org.opendaylight.netconf.sal.restconf.impl.NormalizedNodeContext;
30 import org.opendaylight.netconf.sal.restconf.impl.PutResult;
31 import org.opendaylight.netconf.sal.restconf.impl.RestconfDocumentedException;
32 import org.opendaylight.netconf.sal.restconf.impl.RestconfError;
33 import org.opendaylight.netconf.sal.restconf.impl.RestconfError.ErrorTag;
34 import org.opendaylight.netconf.sal.restconf.impl.RestconfError.ErrorType;
35 import org.opendaylight.netconf.sal.restconf.impl.RestconfImpl;
36 import org.opendaylight.yangtools.yang.common.QName;
37 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
38 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
39 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
40 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
41 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
42 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
43 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
44 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeAttrBuilder;
45 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeAttrBuilder;
46 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.valid.DataValidationException;
47 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
48 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
49 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
50 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
51 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
53 public class RestPutListDataTest {
55 private static BrokerFacade brokerFacade;
56 private static RestconfImpl restconfImpl;
57 private static SchemaContext schemaContextTestModule;
59 private static final String TEST_MODULE_NS_STRING = "test:module";
60 private static final URI TEST_MODULE_NS;
61 private static final String TEST_MODULE_REVISION = "2014-01-09";
64 TEST_MODULE_NS = URI.create("test:module");
68 public void initialize() throws FileNotFoundException, ReactorException {
69 final ControllerContext controllerContext = ControllerContext.getInstance();
70 schemaContextTestModule = TestUtils.loadSchemaContext("/full-versions/test-module");
71 controllerContext.setSchemas(schemaContextTestModule);
72 brokerFacade = mock(BrokerFacade.class);
73 restconfImpl = RestconfImpl.getInstance();
74 restconfImpl.setBroker(brokerFacade);
75 restconfImpl.setControllerContext(controllerContext);
76 final PutResult result = mock(PutResult.class);
77 when(brokerFacade.commitConfigurationDataPut(any(SchemaContext.class), any(YangInstanceIdentifier.class),
78 any(NormalizedNode.class)))
80 when(result.getFutureOfPutData()).thenReturn(mock(CheckedFuture.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
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
123 * The exception is raised during {@code InstanceIdentifier} instance is
128 public void testMissingKeysInUri() {
130 putListDataTest("key1value", null, "key1value", (short) 15);
131 fail("RestconfDocumentedException expected");
132 } catch (final RestconfDocumentedException e) {
133 verifyException(e, ErrorType.PROTOCOL, ErrorTag.DATA_MISSING);
138 * Tests whether an exception is raised if URI contains more key values then
141 * The exception should be raised from validation method
142 * {@code RestconfImpl#validateListEqualityOfListInDataAndUri}
145 public void testMissingKeysInPayload() {
147 putListDataTest("key1value", "15", "key1value", null);
148 fail("RestconfDocumentedException expected");
149 } catch (final DataValidationException e) {
150 // FIXME: thing about different approach for testing the Exception states
151 // RestconfDocumentedException is not rise in new API because you get
152 // DataValidationException from putListDataTest before you call the real rest service
153 // verifyException(e, ErrorType.PROTOCOL, ErrorTag.DATA_MISSING);
157 private void verifyException(final RestconfDocumentedException e, final ErrorType errorType, final ErrorTag errorTag) {
158 final List<RestconfError> errors = e.getErrors();
159 assertEquals("getErrors() size", 1, errors.size());
160 assertEquals("RestconfError getErrorType()", errorType, errors.get(0).getErrorType());
161 assertEquals("RestconfError getErrorTag()", errorTag, errors.get(0).getErrorTag());
164 public void putListDataTest(final String uriKey1, final String uriKey2, final String payloadKey1,
165 final Short payloadKey2) {
166 final QName lstWithCompositeKey = QName.create(TEST_MODULE_NS_STRING, TEST_MODULE_REVISION, "lst-with-composite-key");
167 final QName key1 = QName.create(TEST_MODULE_NS_STRING, TEST_MODULE_REVISION, "key1");
168 final QName key2 = QName.create(TEST_MODULE_NS_STRING, TEST_MODULE_REVISION, "key2");
170 final DataSchemaNode testNodeSchemaNode = schemaContextTestModule.getDataChildByName(lstWithCompositeKey);
171 assertTrue(testNodeSchemaNode != null);
172 assertTrue(testNodeSchemaNode instanceof ListSchemaNode);
173 final DataContainerNodeAttrBuilder<NodeIdentifierWithPredicates, MapEntryNode> testNodeContainer =
174 Builders.mapEntryBuilder((ListSchemaNode) testNodeSchemaNode);
176 List<DataSchemaNode> testChildren = ControllerContext.findInstanceDataChildrenByName(
177 (ListSchemaNode) testNodeSchemaNode, key1.getLocalName());
178 assertTrue(testChildren != null);
179 final DataSchemaNode testLeafKey1SchemaNode = Iterables.getFirst(testChildren, null);
180 assertTrue(testLeafKey1SchemaNode != null);
181 assertTrue(testLeafKey1SchemaNode instanceof LeafSchemaNode);
182 final NormalizedNodeAttrBuilder<NodeIdentifier, Object, LeafNode<Object>> leafKey1 =
183 Builders.leafBuilder((LeafSchemaNode) testLeafKey1SchemaNode);
184 leafKey1.withValue(payloadKey1);
185 testNodeContainer.withChild(leafKey1.build());
187 if (payloadKey2 != null) {
188 testChildren = ControllerContext.findInstanceDataChildrenByName(
189 (ListSchemaNode) testNodeSchemaNode, key2.getLocalName());
190 assertTrue(testChildren != null);
191 final DataSchemaNode testLeafKey2SchemaNode = Iterables.getFirst(testChildren, null);
192 assertTrue(testLeafKey2SchemaNode != null);
193 assertTrue(testLeafKey2SchemaNode instanceof LeafSchemaNode);
194 final NormalizedNodeAttrBuilder<NodeIdentifier, Object, LeafNode<Object>> leafKey2 =
195 Builders.leafBuilder((LeafSchemaNode) testLeafKey2SchemaNode);
196 leafKey2.withValue(payloadKey2);
197 testNodeContainer.withChild(leafKey2.build());
200 final NormalizedNodeContext testCompositeContext = new NormalizedNodeContext(new InstanceIdentifierContext<>(
201 null, testNodeSchemaNode, null, schemaContextTestModule), testNodeContainer.build());
203 restconfImpl.updateConfigurationData(toUri(uriKey1, uriKey2), testCompositeContext);
206 public void putListDataWithWrapperTest(final String uriKey1, final String uriKey2, final String payloadKey1,
207 final Short payloadKey2) {
208 putListDataTest(uriKey1, uriKey2, payloadKey1, payloadKey2);
211 private String toUri(final String uriKey1, final String uriKey2) {
212 final StringBuilder uriBuilder = new StringBuilder("/test-module:lst-with-composite-key/");
213 uriBuilder.append(uriKey1);
214 if (uriKey2 != null) {
215 uriBuilder.append("/");
216 uriBuilder.append(uriKey2);
218 return uriBuilder.toString();