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;
17 import com.google.common.collect.Iterables;
18 import com.google.common.util.concurrent.CheckedFuture;
19 import java.io.FileNotFoundException;
21 import java.util.List;
22 import javax.ws.rs.core.Response.Status;
23 import javax.ws.rs.core.UriInfo;
24 import org.junit.Before;
25 import org.junit.Ignore;
26 import org.junit.Test;
27 import org.mockito.Mockito;
28 import org.opendaylight.controller.sal.restconf.impl.test.TestUtils;
29 import org.opendaylight.netconf.sal.restconf.impl.BrokerFacade;
30 import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
31 import org.opendaylight.netconf.sal.restconf.impl.PutResult;
32 import org.opendaylight.netconf.sal.restconf.impl.RestconfImpl;
33 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
34 import org.opendaylight.restconf.common.context.NormalizedNodeContext;
35 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
36 import org.opendaylight.restconf.common.errors.RestconfError;
37 import org.opendaylight.restconf.common.errors.RestconfError.ErrorTag;
38 import org.opendaylight.restconf.common.errors.RestconfError.ErrorType;
39 import org.opendaylight.yangtools.yang.common.QName;
40 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
41 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
42 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
43 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
44 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
45 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
46 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
47 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeAttrBuilder;
48 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeAttrBuilder;
49 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.valid.DataValidationException;
50 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
51 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
52 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
53 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
55 public class RestPutListDataTest {
57 private static BrokerFacade brokerFacade;
58 private static RestconfImpl restconfImpl;
59 private static SchemaContext schemaContextTestModule;
61 private static final String TEST_MODULE_NS_STRING = "test:module";
62 private static final URI TEST_MODULE_NS;
63 private static final String TEST_MODULE_REVISION = "2014-01-09";
66 TEST_MODULE_NS = URI.create("test:module");
70 public void initialize() throws FileNotFoundException {
71 final ControllerContext controllerContext = ControllerContext.getInstance();
72 schemaContextTestModule = TestUtils.loadSchemaContext("/full-versions/test-module");
73 controllerContext.setSchemas(schemaContextTestModule);
74 brokerFacade = mock(BrokerFacade.class);
75 restconfImpl = RestconfImpl.getInstance();
76 restconfImpl.setBroker(brokerFacade);
77 restconfImpl.setControllerContext(controllerContext);
78 final PutResult result = mock(PutResult.class);
79 when(brokerFacade.commitConfigurationDataPut(any(SchemaContext.class), any(YangInstanceIdentifier.class),
80 any(NormalizedNode.class), Mockito.anyString(), Mockito.anyString()))
82 when(result.getFutureOfPutData()).thenReturn(mock(CheckedFuture.class));
83 when(result.getStatus()).thenReturn(Status.OK);
87 * Tests whether no exception is raised if number and values of keys in URI
88 * and payload are equal.
92 public void testValidKeys() {
93 putListDataTest("key1value", "15", "key1value", (short) 15);
97 * Tests whether an exception is raised if key values in URI and payload are
101 * The exception should be raised from validation method
102 * {@code RestconfImpl#validateListEqualityOfListInDataAndUri}
105 @Ignore // RestconfDocumentedExceptionMapper needs update
106 public void testUriAndPayloadKeysDifferent() {
108 putListDataTest("key1value", "15", "key1value", (short) 16);
109 fail("RestconfDocumentedException expected");
110 } catch (final RestconfDocumentedException e) {
111 verifyException(e, ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
115 putListDataTest("key1value", "15", "key1value1", (short) 16);
116 fail("RestconfDocumentedException expected");
117 } catch (final RestconfDocumentedException e) {
118 verifyException(e, ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
123 * Tests whether an exception is raised if URI contains less key values then
127 * The exception is raised during {@code InstanceIdentifier} instance is
132 public void testMissingKeysInUri() {
134 putListDataTest("key1value", null, "key1value", (short) 15);
135 fail("RestconfDocumentedException expected");
136 } catch (final RestconfDocumentedException e) {
137 verifyException(e, ErrorType.PROTOCOL, ErrorTag.DATA_MISSING);
142 * Tests whether an exception is raised if URI contains more key values then
146 * The exception should be raised from validation method
147 * {@code RestconfImpl#validateListEqualityOfListInDataAndUri}
150 public void testMissingKeysInPayload() {
152 putListDataTest("key1value", "15", "key1value", null);
153 fail("RestconfDocumentedException expected");
154 } catch (final DataValidationException e) {
155 // FIXME: thing about different approach for testing the Exception states
156 // RestconfDocumentedException is not rise in new API because you get
157 // DataValidationException from putListDataTest before you call the real rest service
158 // verifyException(e, ErrorType.PROTOCOL, ErrorTag.DATA_MISSING);
162 private static void verifyException(final RestconfDocumentedException restDocumentedException,
163 final ErrorType errorType, final ErrorTag errorTag) {
164 final List<RestconfError> errors = restDocumentedException.getErrors();
165 assertEquals("getErrors() size", 1, errors.size());
166 assertEquals("RestconfError getErrorType()", errorType, errors.get(0).getErrorType());
167 assertEquals("RestconfError getErrorTag()", errorTag, errors.get(0).getErrorTag());
170 public void putListDataTest(final String uriKey1, final String uriKey2, final String payloadKey1,
171 final Short payloadKey2) {
172 final QName lstWithCompositeKey =
173 QName.create(TEST_MODULE_NS_STRING, TEST_MODULE_REVISION, "lst-with-composite-key");
174 final QName key1 = QName.create(TEST_MODULE_NS_STRING, TEST_MODULE_REVISION, "key1");
175 final QName key2 = QName.create(TEST_MODULE_NS_STRING, TEST_MODULE_REVISION, "key2");
177 final DataSchemaNode testNodeSchemaNode = schemaContextTestModule.getDataChildByName(lstWithCompositeKey);
178 assertTrue(testNodeSchemaNode != null);
179 assertTrue(testNodeSchemaNode instanceof ListSchemaNode);
180 final DataContainerNodeAttrBuilder<NodeIdentifierWithPredicates, MapEntryNode> testNodeContainer =
181 Builders.mapEntryBuilder((ListSchemaNode) testNodeSchemaNode);
183 List<DataSchemaNode> testChildren = ControllerContext.findInstanceDataChildrenByName(
184 (ListSchemaNode) testNodeSchemaNode, key1.getLocalName());
185 assertTrue(testChildren != null);
186 final DataSchemaNode testLeafKey1SchemaNode = Iterables.getFirst(testChildren, null);
187 assertTrue(testLeafKey1SchemaNode != null);
188 assertTrue(testLeafKey1SchemaNode instanceof LeafSchemaNode);
189 final NormalizedNodeAttrBuilder<NodeIdentifier, Object, LeafNode<Object>> leafKey1 =
190 Builders.leafBuilder((LeafSchemaNode) testLeafKey1SchemaNode);
191 leafKey1.withValue(payloadKey1);
192 testNodeContainer.withChild(leafKey1.build());
194 if (payloadKey2 != null) {
195 testChildren = ControllerContext.findInstanceDataChildrenByName(
196 (ListSchemaNode) testNodeSchemaNode, key2.getLocalName());
197 assertTrue(testChildren != null);
198 final DataSchemaNode testLeafKey2SchemaNode = Iterables.getFirst(testChildren, null);
199 assertTrue(testLeafKey2SchemaNode != null);
200 assertTrue(testLeafKey2SchemaNode instanceof LeafSchemaNode);
201 final NormalizedNodeAttrBuilder<NodeIdentifier, Object, LeafNode<Object>> leafKey2 =
202 Builders.leafBuilder((LeafSchemaNode) testLeafKey2SchemaNode);
203 leafKey2.withValue(payloadKey2);
204 testNodeContainer.withChild(leafKey2.build());
207 final NormalizedNodeContext testCompositeContext = new NormalizedNodeContext(new InstanceIdentifierContext<>(
208 null, testNodeSchemaNode, null, schemaContextTestModule), 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();