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;
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.DataContainerNodeAttrBuilder;
49 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeAttrBuilder;
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.getInstance();
75 restconfImpl.setBroker(brokerFacade);
76 restconfImpl.setControllerContext(controllerContext);
77 final PutResult result = mock(PutResult.class);
78 when(brokerFacade.commitConfigurationDataPut(any(SchemaContext.class), any(YangInstanceIdentifier.class),
79 any(NormalizedNode.class), Mockito.anyString(), Mockito.anyString()))
81 when(result.getFutureOfPutData()).thenReturn(mock(CheckedFuture.class));
82 when(result.getStatus()).thenReturn(Status.OK);
86 * Tests whether no exception is raised if number and values of keys in URI
87 * and payload are equal.
91 public void testValidKeys() {
92 putListDataTest("key1value", "15", "key1value", (short) 15);
96 * Tests whether an exception is raised if key values in URI and payload are
100 * The exception should be raised from validation method
101 * {@code RestconfImpl#validateListEqualityOfListInDataAndUri}
104 @Ignore // RestconfDocumentedExceptionMapper needs update
105 public void testUriAndPayloadKeysDifferent() {
107 putListDataTest("key1value", "15", "key1value", (short) 16);
108 fail("RestconfDocumentedException expected");
109 } catch (final RestconfDocumentedException e) {
110 verifyException(e, ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
114 putListDataTest("key1value", "15", "key1value1", (short) 16);
115 fail("RestconfDocumentedException expected");
116 } catch (final RestconfDocumentedException e) {
117 verifyException(e, ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
122 * Tests whether an exception is raised if URI contains less key values then
126 * The exception is raised during {@code InstanceIdentifier} instance is
131 public void testMissingKeysInUri() {
133 putListDataTest("key1value", null, "key1value", (short) 15);
134 fail("RestconfDocumentedException expected");
135 } catch (final RestconfDocumentedException e) {
136 verifyException(e, ErrorType.PROTOCOL, ErrorTag.DATA_MISSING);
141 * Tests whether an exception is raised if URI contains more key values then
145 * The exception should be raised from validation method
146 * {@code RestconfImpl#validateListEqualityOfListInDataAndUri}
149 public void testMissingKeysInPayload() {
151 putListDataTest("key1value", "15", "key1value", null);
152 fail("RestconfDocumentedException expected");
153 } catch (final DataValidationException e) {
154 // FIXME: thing about different approach for testing the Exception states
155 // RestconfDocumentedException is not rise in new API because you get
156 // DataValidationException from putListDataTest before you call the real rest service
157 // verifyException(e, ErrorType.PROTOCOL, ErrorTag.DATA_MISSING);
161 private static void verifyException(final RestconfDocumentedException restDocumentedException,
162 final ErrorType errorType, final ErrorTag errorTag) {
163 final List<RestconfError> errors = restDocumentedException.getErrors();
164 assertEquals("getErrors() size", 1, errors.size());
165 assertEquals("RestconfError getErrorType()", errorType, errors.get(0).getErrorType());
166 assertEquals("RestconfError getErrorTag()", errorTag, errors.get(0).getErrorTag());
169 public void putListDataTest(final String uriKey1, final String uriKey2, final String payloadKey1,
170 final Short payloadKey2) {
171 final QName lstWithCompositeKey =
172 QName.create(TEST_MODULE_NS_STRING, TEST_MODULE_REVISION, "lst-with-composite-key");
173 final QName key1 = QName.create(TEST_MODULE_NS_STRING, TEST_MODULE_REVISION, "key1");
174 final QName key2 = QName.create(TEST_MODULE_NS_STRING, TEST_MODULE_REVISION, "key2");
176 final DataSchemaNode testNodeSchemaNode = schemaContextTestModule.getDataChildByName(lstWithCompositeKey);
177 assertTrue(testNodeSchemaNode != null);
178 assertTrue(testNodeSchemaNode instanceof ListSchemaNode);
179 final DataContainerNodeAttrBuilder<NodeIdentifierWithPredicates, MapEntryNode> testNodeContainer =
180 Builders.mapEntryBuilder((ListSchemaNode) testNodeSchemaNode);
182 List<DataSchemaNode> testChildren = ControllerContext.findInstanceDataChildrenByName(
183 (ListSchemaNode) testNodeSchemaNode, key1.getLocalName());
184 assertTrue(testChildren != null);
185 final DataSchemaNode testLeafKey1SchemaNode = Iterables.getFirst(testChildren, null);
186 assertTrue(testLeafKey1SchemaNode != null);
187 assertTrue(testLeafKey1SchemaNode instanceof LeafSchemaNode);
188 final NormalizedNodeAttrBuilder<NodeIdentifier, Object, LeafNode<Object>> leafKey1 =
189 Builders.leafBuilder((LeafSchemaNode) testLeafKey1SchemaNode);
190 leafKey1.withValue(payloadKey1);
191 testNodeContainer.withChild(leafKey1.build());
193 if (payloadKey2 != null) {
194 testChildren = ControllerContext.findInstanceDataChildrenByName(
195 (ListSchemaNode) testNodeSchemaNode, key2.getLocalName());
196 assertTrue(testChildren != null);
197 final DataSchemaNode testLeafKey2SchemaNode = Iterables.getFirst(testChildren, null);
198 assertTrue(testLeafKey2SchemaNode != null);
199 assertTrue(testLeafKey2SchemaNode instanceof LeafSchemaNode);
200 final NormalizedNodeAttrBuilder<NodeIdentifier, Object, LeafNode<Object>> leafKey2 =
201 Builders.leafBuilder((LeafSchemaNode) testLeafKey2SchemaNode);
202 leafKey2.withValue(payloadKey2);
203 testNodeContainer.withChild(leafKey2.build());
206 final NormalizedNodeContext testCompositeContext = new NormalizedNodeContext(new InstanceIdentifierContext<>(
207 null, testNodeSchemaNode, null, schemaContextTestModule), testNodeContainer.build());
209 final UriInfo uriInfo = Mockito.mock(UriInfo.class);
210 restconfImpl.updateConfigurationData(toUri(uriKey1, uriKey2), testCompositeContext, uriInfo);
213 public void putListDataWithWrapperTest(final String uriKey1, final String uriKey2, final String payloadKey1,
214 final Short payloadKey2) {
215 putListDataTest(uriKey1, uriKey2, payloadKey1, payloadKey2);
218 private static String toUri(final String uriKey1, final String uriKey2) {
219 final StringBuilder uriBuilder = new StringBuilder("/test-module:lst-with-composite-key/");
220 uriBuilder.append(uriKey1);
221 if (uriKey2 != null) {
222 uriBuilder.append("/");
223 uriBuilder.append(uriKey2);
225 return uriBuilder.toString();