Mass conversion to static methods
[netconf.git] / restconf / 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.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
17 import com.google.common.collect.Iterables;
18 import com.google.common.util.concurrent.CheckedFuture;
19 import java.io.FileNotFoundException;
20 import java.net.URI;
21 import java.util.List;
22 import javax.ws.rs.core.Response.Status;
23 import javax.ws.rs.core.UriInfo;
24 import org.junit.Before;
25 import org.junit.Ignore;
26 import org.junit.Test;
27 import org.mockito.Mockito;
28 import org.opendaylight.controller.sal.restconf.impl.test.TestUtils;
29 import org.opendaylight.netconf.sal.restconf.impl.BrokerFacade;
30 import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
31 import org.opendaylight.netconf.sal.restconf.impl.InstanceIdentifierContext;
32 import org.opendaylight.netconf.sal.restconf.impl.NormalizedNodeContext;
33 import org.opendaylight.netconf.sal.restconf.impl.PutResult;
34 import org.opendaylight.netconf.sal.restconf.impl.RestconfDocumentedException;
35 import org.opendaylight.netconf.sal.restconf.impl.RestconfError;
36 import org.opendaylight.netconf.sal.restconf.impl.RestconfError.ErrorTag;
37 import org.opendaylight.netconf.sal.restconf.impl.RestconfError.ErrorType;
38 import org.opendaylight.netconf.sal.restconf.impl.RestconfImpl;
39 import org.opendaylight.yangtools.yang.common.QName;
40 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
41 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
42 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
43 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
44 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
45 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
46 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
47 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeAttrBuilder;
48 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeAttrBuilder;
49 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.valid.DataValidationException;
50 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
51 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
52 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
53 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
54 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
55
56 public class RestPutListDataTest {
57
58     private static BrokerFacade brokerFacade;
59     private static RestconfImpl restconfImpl;
60     private static SchemaContext schemaContextTestModule;
61
62     private static final String TEST_MODULE_NS_STRING = "test:module";
63     private static final URI TEST_MODULE_NS;
64     private static final String TEST_MODULE_REVISION = "2014-01-09";
65
66     static {
67         TEST_MODULE_NS = URI.create("test:module");
68     }
69
70     @Before
71     public void initialize() throws FileNotFoundException, ReactorException {
72         final ControllerContext controllerContext = ControllerContext.getInstance();
73         schemaContextTestModule = TestUtils.loadSchemaContext("/full-versions/test-module");
74         controllerContext.setSchemas(schemaContextTestModule);
75         brokerFacade = mock(BrokerFacade.class);
76         restconfImpl = RestconfImpl.getInstance();
77         restconfImpl.setBroker(brokerFacade);
78         restconfImpl.setControllerContext(controllerContext);
79         final PutResult result = mock(PutResult.class);
80         when(brokerFacade.commitConfigurationDataPut(any(SchemaContext.class), any(YangInstanceIdentifier.class),
81                 any(NormalizedNode.class), Mockito.anyString(), Mockito.anyString()))
82                         .thenReturn(result);
83         when(result.getFutureOfPutData()).thenReturn(mock(CheckedFuture.class));
84         when(result.getStatus()).thenReturn(Status.OK);
85     }
86
87     /**
88      * Tests whether no exception is raised if number and values of keys in URI
89      * and payload are equal
90      */
91     @Test
92     @Ignore
93     public void testValidKeys() {
94         putListDataTest("key1value", "15", "key1value", (short) 15);
95     }
96
97     /**
98      * Tests whether an exception is raised if key values in URI and payload are
99      * different.
100      *
101      * The exception should be raised from validation method
102      * {@code RestconfImpl#validateListEqualityOfListInDataAndUri}
103      */
104     @Test
105     @Ignore // RestconfDocumentedExceptionMapper needs update
106     public void testUriAndPayloadKeysDifferent() {
107         try {
108             putListDataTest("key1value", "15", "key1value", (short) 16);
109             fail("RestconfDocumentedException expected");
110         } catch (final RestconfDocumentedException e) {
111             verifyException(e, ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
112         }
113
114         try {
115             putListDataTest("key1value", "15", "key1value1", (short) 16);
116             fail("RestconfDocumentedException expected");
117         } catch (final RestconfDocumentedException e) {
118             verifyException(e, ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
119         }
120     }
121
122     /**
123      * Tests whether an exception is raised if URI contains less key values then
124      * payload.
125      *
126      * The exception is raised during {@code InstanceIdentifier} instance is
127      * built from URI
128      */
129     @Test
130     @Ignore
131     public void testMissingKeysInUri() {
132         try {
133             putListDataTest("key1value", null, "key1value", (short) 15);
134             fail("RestconfDocumentedException expected");
135         } catch (final RestconfDocumentedException e) {
136             verifyException(e, ErrorType.PROTOCOL, ErrorTag.DATA_MISSING);
137         }
138     }
139
140     /**
141      * Tests whether an exception is raised if URI contains more key values then
142      * payload.
143      *
144      * The exception should be raised from validation method
145      * {@code RestconfImpl#validateListEqualityOfListInDataAndUri}
146      */
147     @Test
148     public void testMissingKeysInPayload() {
149         try {
150             putListDataTest("key1value", "15", "key1value", null);
151             fail("RestconfDocumentedException expected");
152         } catch (final DataValidationException e) {
153             // FIXME: thing about different approach for testing the Exception states
154             // RestconfDocumentedException is not rise in new API because you get
155             // DataValidationException from putListDataTest before you call the real rest service
156 //            verifyException(e, ErrorType.PROTOCOL, ErrorTag.DATA_MISSING);
157         }
158     }
159
160     private static void verifyException(final RestconfDocumentedException e, final ErrorType errorType,
161             final ErrorTag errorTag) {
162         final List<RestconfError> errors = e.getErrors();
163         assertEquals("getErrors() size", 1, errors.size());
164         assertEquals("RestconfError getErrorType()", errorType, errors.get(0).getErrorType());
165         assertEquals("RestconfError getErrorTag()", errorTag, errors.get(0).getErrorTag());
166     }
167
168     public void putListDataTest(final String uriKey1, final String uriKey2, final String payloadKey1,
169             final Short payloadKey2) {
170         final QName lstWithCompositeKey = QName.create(TEST_MODULE_NS_STRING, TEST_MODULE_REVISION, "lst-with-composite-key");
171         final QName key1 = QName.create(TEST_MODULE_NS_STRING, TEST_MODULE_REVISION, "key1");
172         final QName key2 = QName.create(TEST_MODULE_NS_STRING, TEST_MODULE_REVISION, "key2");
173
174         final DataSchemaNode testNodeSchemaNode = schemaContextTestModule.getDataChildByName(lstWithCompositeKey);
175         assertTrue(testNodeSchemaNode != null);
176         assertTrue(testNodeSchemaNode instanceof ListSchemaNode);
177         final DataContainerNodeAttrBuilder<NodeIdentifierWithPredicates, MapEntryNode> testNodeContainer =
178                 Builders.mapEntryBuilder((ListSchemaNode) testNodeSchemaNode);
179
180         List<DataSchemaNode> testChildren = ControllerContext.findInstanceDataChildrenByName(
181                 (ListSchemaNode) testNodeSchemaNode, key1.getLocalName());
182         assertTrue(testChildren != null);
183         final DataSchemaNode testLeafKey1SchemaNode = Iterables.getFirst(testChildren, null);
184         assertTrue(testLeafKey1SchemaNode != null);
185         assertTrue(testLeafKey1SchemaNode instanceof LeafSchemaNode);
186         final NormalizedNodeAttrBuilder<NodeIdentifier, Object, LeafNode<Object>> leafKey1 =
187                 Builders.leafBuilder((LeafSchemaNode) testLeafKey1SchemaNode);
188         leafKey1.withValue(payloadKey1);
189         testNodeContainer.withChild(leafKey1.build());
190
191         if (payloadKey2 != null) {
192             testChildren = ControllerContext.findInstanceDataChildrenByName(
193                     (ListSchemaNode) testNodeSchemaNode, key2.getLocalName());
194             assertTrue(testChildren != null);
195             final DataSchemaNode testLeafKey2SchemaNode = Iterables.getFirst(testChildren, null);
196             assertTrue(testLeafKey2SchemaNode != null);
197             assertTrue(testLeafKey2SchemaNode instanceof LeafSchemaNode);
198             final NormalizedNodeAttrBuilder<NodeIdentifier, Object, LeafNode<Object>> leafKey2 =
199                     Builders.leafBuilder((LeafSchemaNode) testLeafKey2SchemaNode);
200             leafKey2.withValue(payloadKey2);
201             testNodeContainer.withChild(leafKey2.build());
202         }
203
204         final NormalizedNodeContext testCompositeContext = new NormalizedNodeContext(new InstanceIdentifierContext<>(
205                 null, testNodeSchemaNode, null, schemaContextTestModule), testNodeContainer.build());
206
207         final UriInfo uriInfo = Mockito.mock(UriInfo.class);
208         restconfImpl.updateConfigurationData(toUri(uriKey1, uriKey2), testCompositeContext, uriInfo);
209     }
210
211     public void putListDataWithWrapperTest(final String uriKey1, final String uriKey2, final String payloadKey1,
212             final Short payloadKey2) {
213         putListDataTest(uriKey1, uriKey2, payloadKey1, payloadKey2);
214     }
215
216     private static String toUri(final String uriKey1, final String uriKey2) {
217         final StringBuilder uriBuilder = new StringBuilder("/test-module:lst-with-composite-key/");
218         uriBuilder.append(uriKey1);
219         if (uriKey2 != null) {
220             uriBuilder.append("/");
221             uriBuilder.append(uriKey2);
222         }
223         return uriBuilder.toString();
224     }
225
226 }