Merge "BUG-190 Simplify reconnect logic in protocol-framework."
[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 java.io.FileNotFoundException;
17 import java.net.URI;
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.YangInstanceIdentifier;
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;
41
42 public class RestPutListDataTest {
43
44     private static BrokerFacade brokerFacade;
45     private static RestconfImpl restconfImpl;
46     private static SchemaContext schemaContextTestModule;
47
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";
51
52     static {
53         TEST_MODULE_NS = URI.create("test:module");
54     }
55
56     @Before
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)
67                 .build());
68         when(brokerFacade.commitConfigurationDataPut(any(YangInstanceIdentifier.class), any(CompositeNode.class)))
69                 .thenReturn(futureBuilder.build());
70     }
71
72     /**
73      * Tests whether no exception is raised if number and values of keys in URI
74      * and payload are equal
75      */
76     @Test
77     public void testValidKeys() {
78         putListDataTest("key1value", "15", "key1value", (short) 15);
79     }
80
81     /**
82      * Tests whether an exception is raised if key values in URI and payload are
83      * different.
84      *
85      * The exception should be raised from validation method
86      * {@code RestconfImpl#validateListEqualityOfListInDataAndUri}
87      */
88     @Test
89     public void testUriAndPayloadKeysDifferent() {
90         try {
91             putListDataTest("key1value", "15", "key1value", (short) 16);
92             fail("RestconfDocumentedException expected");
93         } catch (RestconfDocumentedException e) {
94             verifyException(e, ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
95         }
96
97         try {
98             putListDataTest("key1value", "15", "key1value1", (short) 16);
99             fail("RestconfDocumentedException expected");
100         } catch (RestconfDocumentedException e) {
101             verifyException(e, ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
102         }
103     }
104
105     /**
106      * Tests whether an exception is raised if URI contains less key values then
107      * payload.
108      *
109      * The exception is raised during {@code InstanceIdentifier} instance is
110      * built from URI
111      */
112     @Test
113     public void testMissingKeysInUri() {
114         try {
115             putListDataTest("key1value", null, "key1value", (short) 15);
116             fail("RestconfDocumentedException expected");
117         } catch (RestconfDocumentedException e) {
118             verifyException(e, ErrorType.PROTOCOL, ErrorTag.DATA_MISSING);
119         }
120     }
121
122     /**
123      * Tests whether an exception is raised if URI contains more key values then
124      * payload.
125      *
126      * The exception should be raised from validation method
127      * {@code RestconfImpl#validateListEqualityOfListInDataAndUri}
128      */
129     @Test
130     public void testMissingKeysInPayload() {
131         try {
132             putListDataTest("key1value", "15", "key1value", null);
133             fail("RestconfDocumentedException expected");
134         } catch (RestconfDocumentedException e) {
135             verifyException(e, ErrorType.PROTOCOL, ErrorTag.DATA_MISSING);
136         }
137         try {
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);
143         }
144
145     }
146
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());
152     }
153
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");
159
160         CompositeNodeBuilder<ImmutableCompositeNode> payloadBuilder = ImmutableCompositeNode.builder();
161         payloadBuilder.setQName(lstWithCompositeKey).addLeaf(key1, payloadKey1);
162         if (payloadKey2 != null) {
163             payloadBuilder.addLeaf(key2, payloadKey2);
164         }
165
166         restconfImpl.updateConfigurationData(toUri(uriKey1, uriKey2), payloadBuilder.toInstance());
167     }
168
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));
175         }
176         restconfImpl.updateConfigurationData(toUri(uriKey1, uriKey2), payloadBuilder);
177     }
178
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);
185         }
186         return uriBuilder.toString();
187     }
188
189 }