BUG 1082 Migrate sal-rest-connector to Async Data Broker API
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / test / java / org / opendaylight / controller / sal / restconf / impl / input / to / cnsn / test / RestPutListDataTest.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.sal.restconf.impl.input.to.cnsn.test;
9
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;
15
16 import com.google.common.util.concurrent.CheckedFuture;
17 import java.io.FileNotFoundException;
18 import java.net.URI;
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;
38
39 public class RestPutListDataTest {
40
41     private static BrokerFacade brokerFacade;
42     private static RestconfImpl restconfImpl;
43     private static SchemaContext schemaContextTestModule;
44
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";
48
49     static {
50         TEST_MODULE_NS = URI.create("test:module");
51     }
52
53     @Before
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));
64     }
65
66     /**
67      * Tests whether no exception is raised if number and values of keys in URI
68      * and payload are equal
69      */
70     @Test
71     public void testValidKeys() {
72         putListDataTest("key1value", "15", "key1value", (short) 15);
73     }
74
75     /**
76      * Tests whether an exception is raised if key values in URI and payload are
77      * different.
78      *
79      * The exception should be raised from validation method
80      * {@code RestconfImpl#validateListEqualityOfListInDataAndUri}
81      */
82     @Test
83     public void testUriAndPayloadKeysDifferent() {
84         try {
85             putListDataTest("key1value", "15", "key1value", (short) 16);
86             fail("RestconfDocumentedException expected");
87         } catch (RestconfDocumentedException e) {
88             verifyException(e, ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
89         }
90
91         try {
92             putListDataTest("key1value", "15", "key1value1", (short) 16);
93             fail("RestconfDocumentedException expected");
94         } catch (RestconfDocumentedException e) {
95             verifyException(e, ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
96         }
97     }
98
99     /**
100      * Tests whether an exception is raised if URI contains less key values then
101      * payload.
102      *
103      * The exception is raised during {@code InstanceIdentifier} instance is
104      * built from URI
105      */
106     @Test
107     public void testMissingKeysInUri() {
108         try {
109             putListDataTest("key1value", null, "key1value", (short) 15);
110             fail("RestconfDocumentedException expected");
111         } catch (RestconfDocumentedException e) {
112             verifyException(e, ErrorType.PROTOCOL, ErrorTag.DATA_MISSING);
113         }
114     }
115
116     /**
117      * Tests whether an exception is raised if URI contains more key values then
118      * payload.
119      *
120      * The exception should be raised from validation method
121      * {@code RestconfImpl#validateListEqualityOfListInDataAndUri}
122      */
123     @Test
124     public void testMissingKeysInPayload() {
125         try {
126             putListDataTest("key1value", "15", "key1value", null);
127             fail("RestconfDocumentedException expected");
128         } catch (RestconfDocumentedException e) {
129             verifyException(e, ErrorType.PROTOCOL, ErrorTag.DATA_MISSING);
130         }
131         try {
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);
137         }
138
139     }
140
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());
146     }
147
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");
153
154         CompositeNodeBuilder<ImmutableCompositeNode> payloadBuilder = ImmutableCompositeNode.builder();
155         payloadBuilder.setQName(lstWithCompositeKey).addLeaf(key1, payloadKey1);
156         if (payloadKey2 != null) {
157             payloadBuilder.addLeaf(key2, payloadKey2);
158         }
159
160         restconfImpl.updateConfigurationData(toUri(uriKey1, uriKey2), payloadBuilder.toInstance());
161     }
162
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));
169         }
170         restconfImpl.updateConfigurationData(toUri(uriKey1, uriKey2), payloadBuilder);
171     }
172
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);
179         }
180         return uriBuilder.toString();
181     }
182
183 }