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.fail;
12 import static org.mockito.Matchers.any;
13 import static org.mockito.Mockito.mock;
14 import static org.mockito.Mockito.when;
16 import com.google.common.util.concurrent.CheckedFuture;
17 import java.io.FileNotFoundException;
19 import java.util.List;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.opendaylight.controller.sal.restconf.impl.BrokerFacade;
23 import org.opendaylight.controller.sal.restconf.impl.CompositeNodeWrapper;
24 import org.opendaylight.controller.sal.restconf.impl.ControllerContext;
25 import org.opendaylight.controller.sal.restconf.impl.RestconfDocumentedException;
26 import org.opendaylight.controller.sal.restconf.impl.RestconfError;
27 import org.opendaylight.controller.sal.restconf.impl.RestconfError.ErrorTag;
28 import org.opendaylight.controller.sal.restconf.impl.RestconfError.ErrorType;
29 import org.opendaylight.controller.sal.restconf.impl.RestconfImpl;
30 import org.opendaylight.controller.sal.restconf.impl.SimpleNodeWrapper;
31 import org.opendaylight.controller.sal.restconf.impl.test.TestUtils;
32 import org.opendaylight.yangtools.yang.common.QName;
33 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
34 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
35 import org.opendaylight.yangtools.yang.data.impl.ImmutableCompositeNode;
36 import org.opendaylight.yangtools.yang.data.impl.util.CompositeNodeBuilder;
37 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
39 public class RestPutListDataTest {
41 private static BrokerFacade brokerFacade;
42 private static RestconfImpl restconfImpl;
43 private static SchemaContext schemaContextTestModule;
45 private static final String TEST_MODULE_NS_STRING = "test:module";
46 private static final URI TEST_MODULE_NS;
47 private static final String TEST_MODULE_REVISION = "2014-01-09";
50 TEST_MODULE_NS = URI.create("test:module");
54 public void initialize() throws FileNotFoundException {
55 ControllerContext controllerContext = ControllerContext.getInstance();
56 schemaContextTestModule = TestUtils.loadSchemaContext("/full-versions/test-module");
57 controllerContext.setSchemas(schemaContextTestModule);
58 brokerFacade = mock(BrokerFacade.class);
59 restconfImpl = RestconfImpl.getInstance();
60 restconfImpl.setBroker(brokerFacade);
61 restconfImpl.setControllerContext(controllerContext);
62 when(brokerFacade.commitConfigurationDataPut(any(YangInstanceIdentifier.class), any(NormalizedNode.class)))
63 .thenReturn(mock(CheckedFuture.class));
67 * Tests whether no exception is raised if number and values of keys in URI
68 * and payload are equal
71 public void testValidKeys() {
72 putListDataTest("key1value", "15", "key1value", (short) 15);
76 * Tests whether an exception is raised if key values in URI and payload are
79 * The exception should be raised from validation method
80 * {@code RestconfImpl#validateListEqualityOfListInDataAndUri}
83 public void testUriAndPayloadKeysDifferent() {
85 putListDataTest("key1value", "15", "key1value", (short) 16);
86 fail("RestconfDocumentedException expected");
87 } catch (RestconfDocumentedException e) {
88 verifyException(e, ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
92 putListDataTest("key1value", "15", "key1value1", (short) 16);
93 fail("RestconfDocumentedException expected");
94 } catch (RestconfDocumentedException e) {
95 verifyException(e, ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
100 * Tests whether an exception is raised if URI contains less key values then
103 * The exception is raised during {@code InstanceIdentifier} instance is
107 public void testMissingKeysInUri() {
109 putListDataTest("key1value", null, "key1value", (short) 15);
110 fail("RestconfDocumentedException expected");
111 } catch (RestconfDocumentedException e) {
112 verifyException(e, ErrorType.PROTOCOL, ErrorTag.DATA_MISSING);
117 * Tests whether an exception is raised if URI contains more key values then
120 * The exception should be raised from validation method
121 * {@code RestconfImpl#validateListEqualityOfListInDataAndUri}
124 public void testMissingKeysInPayload() {
126 putListDataTest("key1value", "15", "key1value", null);
127 fail("RestconfDocumentedException expected");
128 } catch (RestconfDocumentedException e) {
129 verifyException(e, ErrorType.PROTOCOL, ErrorTag.DATA_MISSING);
132 putListDataWithWrapperTest("key1value", "15", "key1value", null);
133 fail("RestconfDocumentedException expected");
134 } catch (RestconfDocumentedException e) {
135 // this exception is raised from RestconfImpl.normalizeCompositeNode()
136 verifyException(e, ErrorType.PROTOCOL, ErrorTag.DATA_MISSING);
141 private void verifyException(final RestconfDocumentedException e, final ErrorType errorType, final ErrorTag errorTag) {
142 List<RestconfError> errors = e.getErrors();
143 assertEquals("getErrors() size", 1, errors.size());
144 assertEquals("RestconfError getErrorType()", errorType, errors.get(0).getErrorType());
145 assertEquals("RestconfError getErrorTag()", errorTag, errors.get(0).getErrorTag());
148 public void putListDataTest(final String uriKey1, final String uriKey2, final String payloadKey1,
149 final Short payloadKey2) {
150 QName lstWithCompositeKey = QName.create(TEST_MODULE_NS_STRING, TEST_MODULE_REVISION, "lst-with-composite-key");
151 QName key1 = QName.create(TEST_MODULE_NS_STRING, TEST_MODULE_REVISION, "key1");
152 QName key2 = QName.create(TEST_MODULE_NS_STRING, TEST_MODULE_REVISION, "key2");
154 CompositeNodeBuilder<ImmutableCompositeNode> payloadBuilder = ImmutableCompositeNode.builder();
155 payloadBuilder.setQName(lstWithCompositeKey).addLeaf(key1, payloadKey1);
156 if (payloadKey2 != null) {
157 payloadBuilder.addLeaf(key2, payloadKey2);
160 restconfImpl.updateConfigurationData(toUri(uriKey1, uriKey2), payloadBuilder.toInstance());
163 public void putListDataWithWrapperTest(final String uriKey1, final String uriKey2, final String payloadKey1,
164 final Short payloadKey2) {
165 CompositeNodeWrapper payloadBuilder = new CompositeNodeWrapper(TEST_MODULE_NS, "lst-with-composite-key");
166 payloadBuilder.addValue(new SimpleNodeWrapper(TEST_MODULE_NS, "key1", payloadKey1));
167 if (payloadKey2 != null) {
168 payloadBuilder.addValue(new SimpleNodeWrapper(TEST_MODULE_NS, "key2", payloadKey2));
170 restconfImpl.updateConfigurationData(toUri(uriKey1, uriKey2), payloadBuilder);
173 private String toUri(final String uriKey1, final String uriKey2) {
174 final StringBuilder uriBuilder = new StringBuilder("/test-module:lst-with-composite-key/");
175 uriBuilder.append(uriKey1);
176 if (uriKey2 != null) {
177 uriBuilder.append("/");
178 uriBuilder.append(uriKey2);
180 return uriBuilder.toString();