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 java.io.FileNotFoundException;
18 import java.util.List;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
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.DummyFuture;
32 import org.opendaylight.controller.sal.restconf.impl.test.DummyFuture.Builder;
33 import org.opendaylight.controller.sal.restconf.impl.test.DummyRpcResult;
34 import org.opendaylight.controller.sal.restconf.impl.test.TestUtils;
35 import org.opendaylight.yangtools.yang.common.QName;
36 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
37 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
38 import org.opendaylight.yangtools.yang.data.impl.ImmutableCompositeNode;
39 import org.opendaylight.yangtools.yang.data.impl.util.CompositeNodeBuilder;
40 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
42 public class RestPutListDataTest {
44 private static BrokerFacade brokerFacade;
45 private static RestconfImpl restconfImpl;
46 private static SchemaContext schemaContextTestModule;
48 private static final String TEST_MODULE_NS_STRING = "test:module";
49 private static final URI TEST_MODULE_NS;
50 private static final String TEST_MODULE_REVISION = "2014-01-09";
53 TEST_MODULE_NS = URI.create("test:module");
57 public void initialize() throws FileNotFoundException {
58 ControllerContext controllerContext = ControllerContext.getInstance();
59 schemaContextTestModule = TestUtils.loadSchemaContext("/full-versions/test-module");
60 controllerContext.setSchemas(schemaContextTestModule);
61 brokerFacade = mock(BrokerFacade.class);
62 restconfImpl = RestconfImpl.getInstance();
63 restconfImpl.setBroker(brokerFacade);
64 restconfImpl.setControllerContext(controllerContext);
65 Builder<TransactionStatus> futureBuilder = new DummyFuture.Builder<TransactionStatus>();
66 futureBuilder.rpcResult(new DummyRpcResult.Builder<TransactionStatus>().result(TransactionStatus.COMMITED)
68 when(brokerFacade.commitConfigurationDataPut(any(InstanceIdentifier.class), any(CompositeNode.class)))
69 .thenReturn(futureBuilder.build());
73 * Tests whether no exception is raised if number and values of keys in URI
74 * and payload are equal
77 public void testValidKeys() {
78 putListDataTest("key1value", "15", "key1value", (short) 15);
82 * Tests whether an exception is raised if key values in URI and payload are
85 * The exception should be raised from validation method
86 * {@code RestconfImpl#validateListEqualityOfListInDataAndUri}
89 public void testUriAndPayloadKeysDifferent() {
91 putListDataTest("key1value", "15", "key1value", (short) 16);
92 fail("RestconfDocumentedException expected");
93 } catch (RestconfDocumentedException e) {
94 verifyException(e, ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
98 putListDataTest("key1value", "15", "key1value1", (short) 16);
99 fail("RestconfDocumentedException expected");
100 } catch (RestconfDocumentedException e) {
101 verifyException(e, ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
106 * Tests whether an exception is raised if URI contains less key values then
109 * The exception is raised during {@code InstanceIdentifier} instance is
113 public void testMissingKeysInUri() {
115 putListDataTest("key1value", null, "key1value", (short) 15);
116 fail("RestconfDocumentedException expected");
117 } catch (RestconfDocumentedException e) {
118 verifyException(e, ErrorType.PROTOCOL, ErrorTag.DATA_MISSING);
123 * Tests whether an exception is raised if URI contains more key values then
126 * The exception should be raised from validation method
127 * {@code RestconfImpl#validateListEqualityOfListInDataAndUri}
130 public void testMissingKeysInPayload() {
132 putListDataTest("key1value", "15", "key1value", null);
133 fail("RestconfDocumentedException expected");
134 } catch (RestconfDocumentedException e) {
135 verifyException(e, ErrorType.PROTOCOL, ErrorTag.DATA_MISSING);
138 putListDataWithWrapperTest("key1value", "15", "key1value", null);
139 fail("RestconfDocumentedException expected");
140 } catch (RestconfDocumentedException e) {
141 // this exception is raised from RestconfImpl.normalizeCompositeNode()
142 verifyException(e, ErrorType.PROTOCOL, ErrorTag.DATA_MISSING);
147 private void verifyException(final RestconfDocumentedException e, final ErrorType errorType, final ErrorTag errorTag) {
148 List<RestconfError> errors = e.getErrors();
149 assertEquals("getErrors() size", 1, errors.size());
150 assertEquals("RestconfError getErrorType()", errorType, errors.get(0).getErrorType());
151 assertEquals("RestconfError getErrorTag()", errorTag, errors.get(0).getErrorTag());
154 public void putListDataTest(final String uriKey1, final String uriKey2, final String payloadKey1,
155 final Short payloadKey2) {
156 QName lstWithCompositeKey = QName.create(TEST_MODULE_NS_STRING, TEST_MODULE_REVISION, "lst-with-composite-key");
157 QName key1 = QName.create(TEST_MODULE_NS_STRING, TEST_MODULE_REVISION, "key1");
158 QName key2 = QName.create(TEST_MODULE_NS_STRING, TEST_MODULE_REVISION, "key2");
160 CompositeNodeBuilder<ImmutableCompositeNode> payloadBuilder = ImmutableCompositeNode.builder();
161 payloadBuilder.setQName(lstWithCompositeKey).addLeaf(key1, payloadKey1);
162 if (payloadKey2 != null) {
163 payloadBuilder.addLeaf(key2, payloadKey2);
166 restconfImpl.updateConfigurationData(toUri(uriKey1, uriKey2), payloadBuilder.toInstance());
169 public void putListDataWithWrapperTest(final String uriKey1, final String uriKey2, final String payloadKey1,
170 final Short payloadKey2) {
171 CompositeNodeWrapper payloadBuilder = new CompositeNodeWrapper(TEST_MODULE_NS, "lst-with-composite-key");
172 payloadBuilder.addValue(new SimpleNodeWrapper(TEST_MODULE_NS, "key1", payloadKey1));
173 if (payloadKey2 != null) {
174 payloadBuilder.addValue(new SimpleNodeWrapper(TEST_MODULE_NS, "key2", payloadKey2));
176 restconfImpl.updateConfigurationData(toUri(uriKey1, uriKey2), payloadBuilder);
179 private String toUri(final String uriKey1, final String uriKey2) {
180 final StringBuilder uriBuilder = new StringBuilder("/test-module:lst-with-composite-key/");
181 uriBuilder.append(uriKey1);
182 if (uriKey2 != null) {
183 uriBuilder.append("/");
184 uriBuilder.append(uriKey2);
186 return uriBuilder.toString();