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 javax.ws.rs.core.UriInfo;
23 import org.junit.Before;
24 import org.junit.Ignore;
25 import org.junit.Test;
26 import org.mockito.Mockito;
27 import org.opendaylight.controller.sal.restconf.impl.test.TestUtils;
28 import org.opendaylight.netconf.sal.restconf.impl.BrokerFacade;
29 import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
30 import org.opendaylight.netconf.sal.restconf.impl.InstanceIdentifierContext;
31 import org.opendaylight.netconf.sal.restconf.impl.NormalizedNodeContext;
32 import org.opendaylight.netconf.sal.restconf.impl.PutResult;
33 import org.opendaylight.netconf.sal.restconf.impl.RestconfDocumentedException;
34 import org.opendaylight.netconf.sal.restconf.impl.RestconfError;
35 import org.opendaylight.netconf.sal.restconf.impl.RestconfError.ErrorTag;
36 import org.opendaylight.netconf.sal.restconf.impl.RestconfError.ErrorType;
37 import org.opendaylight.netconf.sal.restconf.impl.RestconfImpl;
38 import org.opendaylight.yangtools.yang.common.QName;
39 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
40 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
41 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
42 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
43 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
44 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
45 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
46 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeAttrBuilder;
47 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeAttrBuilder;
48 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.valid.DataValidationException;
49 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
50 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
51 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
52 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
53 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
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, ReactorException {
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
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
125 * The exception is raised during {@code InstanceIdentifier} instance is
130 public void testMissingKeysInUri() {
132 putListDataTest("key1value", null, "key1value", (short) 15);
133 fail("RestconfDocumentedException expected");
134 } catch (final RestconfDocumentedException e) {
135 verifyException(e, ErrorType.PROTOCOL, ErrorTag.DATA_MISSING);
140 * 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 void verifyException(final RestconfDocumentedException e, final ErrorType errorType, final ErrorTag errorTag) {
160 final List<RestconfError> errors = e.getErrors();
161 assertEquals("getErrors() size", 1, errors.size());
162 assertEquals("RestconfError getErrorType()", errorType, errors.get(0).getErrorType());
163 assertEquals("RestconfError getErrorTag()", errorTag, errors.get(0).getErrorTag());
166 public void putListDataTest(final String uriKey1, final String uriKey2, final String payloadKey1,
167 final Short payloadKey2) {
168 final QName lstWithCompositeKey = QName.create(TEST_MODULE_NS_STRING, TEST_MODULE_REVISION, "lst-with-composite-key");
169 final QName key1 = QName.create(TEST_MODULE_NS_STRING, TEST_MODULE_REVISION, "key1");
170 final QName key2 = QName.create(TEST_MODULE_NS_STRING, TEST_MODULE_REVISION, "key2");
172 final DataSchemaNode testNodeSchemaNode = schemaContextTestModule.getDataChildByName(lstWithCompositeKey);
173 assertTrue(testNodeSchemaNode != null);
174 assertTrue(testNodeSchemaNode instanceof ListSchemaNode);
175 final DataContainerNodeAttrBuilder<NodeIdentifierWithPredicates, MapEntryNode> testNodeContainer =
176 Builders.mapEntryBuilder((ListSchemaNode) testNodeSchemaNode);
178 List<DataSchemaNode> testChildren = ControllerContext.findInstanceDataChildrenByName(
179 (ListSchemaNode) testNodeSchemaNode, key1.getLocalName());
180 assertTrue(testChildren != null);
181 final DataSchemaNode testLeafKey1SchemaNode = Iterables.getFirst(testChildren, null);
182 assertTrue(testLeafKey1SchemaNode != null);
183 assertTrue(testLeafKey1SchemaNode instanceof LeafSchemaNode);
184 final NormalizedNodeAttrBuilder<NodeIdentifier, Object, LeafNode<Object>> leafKey1 =
185 Builders.leafBuilder((LeafSchemaNode) testLeafKey1SchemaNode);
186 leafKey1.withValue(payloadKey1);
187 testNodeContainer.withChild(leafKey1.build());
189 if (payloadKey2 != null) {
190 testChildren = ControllerContext.findInstanceDataChildrenByName(
191 (ListSchemaNode) testNodeSchemaNode, key2.getLocalName());
192 assertTrue(testChildren != null);
193 final DataSchemaNode testLeafKey2SchemaNode = Iterables.getFirst(testChildren, null);
194 assertTrue(testLeafKey2SchemaNode != null);
195 assertTrue(testLeafKey2SchemaNode instanceof LeafSchemaNode);
196 final NormalizedNodeAttrBuilder<NodeIdentifier, Object, LeafNode<Object>> leafKey2 =
197 Builders.leafBuilder((LeafSchemaNode) testLeafKey2SchemaNode);
198 leafKey2.withValue(payloadKey2);
199 testNodeContainer.withChild(leafKey2.build());
202 final NormalizedNodeContext testCompositeContext = new NormalizedNodeContext(new InstanceIdentifierContext<>(
203 null, testNodeSchemaNode, null, schemaContextTestModule), testNodeContainer.build());
205 final UriInfo uriInfo = Mockito.mock(UriInfo.class);
206 restconfImpl.updateConfigurationData(toUri(uriKey1, uriKey2), testCompositeContext, uriInfo);
209 public void putListDataWithWrapperTest(final String uriKey1, final String uriKey2, final String payloadKey1,
210 final Short payloadKey2) {
211 putListDataTest(uriKey1, uriKey2, payloadKey1, payloadKey2);
214 private String toUri(final String uriKey1, final String uriKey2) {
215 final StringBuilder uriBuilder = new StringBuilder("/test-module:lst-with-composite-key/");
216 uriBuilder.append(uriKey1);
217 if (uriKey2 != null) {
218 uriBuilder.append("/");
219 uriBuilder.append(uriKey2);
221 return uriBuilder.toString();