71406ca7742590c94374721e1880589ecac7ca33
[netconf.git] / restconf / restconf-nb-bierman02 / 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.assertNotNull;
12 import static org.junit.Assert.assertTrue;
13 import static org.junit.Assert.fail;
14 import static org.mockito.ArgumentMatchers.any;
15 import static org.mockito.Mockito.mock;
16 import static org.mockito.Mockito.when;
17
18 import com.google.common.util.concurrent.FluentFuture;
19 import java.io.FileNotFoundException;
20 import java.util.List;
21 import javax.ws.rs.core.Response.Status;
22 import javax.ws.rs.core.UriInfo;
23 import org.junit.Before;
24 import org.junit.BeforeClass;
25 import org.junit.Ignore;
26 import org.junit.Test;
27 import org.mockito.Mockito;
28 import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
29 import org.opendaylight.controller.sal.restconf.impl.test.TestUtils;
30 import org.opendaylight.netconf.sal.rest.impl.NormalizedNodeContext;
31 import org.opendaylight.netconf.sal.restconf.impl.BrokerFacade;
32 import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
33 import org.opendaylight.netconf.sal.restconf.impl.PutResult;
34 import org.opendaylight.netconf.sal.restconf.impl.RestconfImpl;
35 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
36 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
37 import org.opendaylight.restconf.common.errors.RestconfError;
38 import org.opendaylight.yangtools.yang.common.ErrorTag;
39 import org.opendaylight.yangtools.yang.common.ErrorType;
40 import org.opendaylight.yangtools.yang.common.QName;
41 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
42 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
43 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
44 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
45 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
46 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
47 import org.opendaylight.yangtools.yang.data.api.schema.builder.DataContainerNodeBuilder;
48 import org.opendaylight.yangtools.yang.data.api.schema.builder.NormalizedNodeBuilder;
49 import org.opendaylight.yangtools.yang.data.impl.schema.SchemaAwareBuilders;
50 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.valid.DataValidationException;
51 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
52 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
53 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
54 import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
55 import org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack;
56
57 public class RestPutListDataTest {
58     private static EffectiveModelContext schemaContextTestModule;
59
60     private static BrokerFacade brokerFacade;
61     private static RestconfImpl restconfImpl;
62
63     private static final String TEST_MODULE_NS_STRING = "test:module";
64     private static final String TEST_MODULE_REVISION = "2014-01-09";
65
66     @BeforeClass
67     public static void staticSetup() throws FileNotFoundException {
68         schemaContextTestModule = TestUtils.loadSchemaContext("/full-versions/test-module");
69     }
70
71     @Before
72     public void initialize() throws FileNotFoundException {
73         final ControllerContext controllerContext = TestRestconfUtils.newControllerContext(schemaContextTestModule);
74         brokerFacade = mock(BrokerFacade.class);
75         restconfImpl = RestconfImpl.newInstance(brokerFacade, controllerContext);
76         final PutResult result = mock(PutResult.class);
77         when(brokerFacade.commitConfigurationDataPut(any(EffectiveModelContext.class),
78                 any(YangInstanceIdentifier.class), any(NormalizedNode.class), Mockito.anyString(), Mockito.anyString()))
79                         .thenReturn(result);
80         when(result.getFutureOfPutData()).thenReturn(mock(FluentFuture.class));
81         when(result.getStatus()).thenReturn(Status.OK);
82     }
83
84     /**
85      * Tests whether no exception is raised if number and values of keys in URI
86      * and payload are equal.
87      */
88     @Test
89     @Ignore
90     public void testValidKeys() {
91         putListDataTest("key1value", "15", "key1value", (short) 15);
92     }
93
94     /**
95      * Tests whether an exception is raised if key values in URI and payload are
96      * different.
97      *
98      * <p>
99      * The exception should be raised from validation method
100      * {@code RestconfImpl#validateListEqualityOfListInDataAndUri}
101      */
102     @Test
103     @Ignore // RestconfDocumentedExceptionMapper needs update
104     public void testUriAndPayloadKeysDifferent() {
105         try {
106             putListDataTest("key1value", "15", "key1value", (short) 16);
107             fail("RestconfDocumentedException expected");
108         } catch (final RestconfDocumentedException e) {
109             verifyException(e, ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
110         }
111
112         try {
113             putListDataTest("key1value", "15", "key1value1", (short) 16);
114             fail("RestconfDocumentedException expected");
115         } catch (final RestconfDocumentedException e) {
116             verifyException(e, ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
117         }
118     }
119
120     /**
121      * Tests whether an exception is raised if URI contains less key values then
122      * payload.
123      *
124      * <p>
125      * The exception is raised during {@code InstanceIdentifier} instance is
126      * built from URI
127      */
128     @Test
129     @Ignore
130     public void testMissingKeysInUri() {
131         try {
132             putListDataTest("key1value", null, "key1value", (short) 15);
133             fail("RestconfDocumentedException expected");
134         } catch (final RestconfDocumentedException e) {
135             verifyException(e, ErrorType.PROTOCOL, ErrorTag.DATA_MISSING);
136         }
137     }
138
139     /**
140      * Tests whether an exception is raised if URI contains more key values then
141      * payload.
142      *
143      * <p>
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 restDocumentedException,
161                                         final ErrorType errorType, final ErrorTag errorTag) {
162         final List<RestconfError> errors = restDocumentedException.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 =
171                 QName.create(TEST_MODULE_NS_STRING, TEST_MODULE_REVISION, "lst-with-composite-key");
172         final QName key1 = QName.create(TEST_MODULE_NS_STRING, TEST_MODULE_REVISION, "key1");
173         final QName key2 = QName.create(TEST_MODULE_NS_STRING, TEST_MODULE_REVISION, "key2");
174
175         final DataSchemaNode testNodeSchemaNode = schemaContextTestModule.getDataChildByName(lstWithCompositeKey);
176         assertTrue(testNodeSchemaNode != null);
177         assertTrue(testNodeSchemaNode instanceof ListSchemaNode);
178         final DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> testNodeContainer =
179             SchemaAwareBuilders.mapEntryBuilder((ListSchemaNode) testNodeSchemaNode);
180
181         var testChildren = ControllerContext.findInstanceDataChildrenByName(
182                 (ListSchemaNode) testNodeSchemaNode, key1.getLocalName());
183         assertTrue(testChildren != null);
184         final DataSchemaNode testLeafKey1SchemaNode = testChildren.get(0).child;
185         assertTrue(testLeafKey1SchemaNode != null);
186         assertTrue(testLeafKey1SchemaNode instanceof LeafSchemaNode);
187         final NormalizedNodeBuilder<NodeIdentifier, Object, LeafNode<Object>> leafKey1 =
188             SchemaAwareBuilders.leafBuilder((LeafSchemaNode) testLeafKey1SchemaNode);
189         leafKey1.withValue(payloadKey1);
190         testNodeContainer.withChild(leafKey1.build());
191
192         if (payloadKey2 != null) {
193             testChildren = ControllerContext.findInstanceDataChildrenByName(
194                     (ListSchemaNode) testNodeSchemaNode, key2.getLocalName());
195             assertTrue(testChildren != null);
196             final DataSchemaNode testLeafKey2SchemaNode = testChildren.get(0).child;
197             assertNotNull(testLeafKey2SchemaNode);
198             assertTrue(testLeafKey2SchemaNode instanceof LeafSchemaNode);
199             final NormalizedNodeBuilder<NodeIdentifier, Object, LeafNode<Object>> leafKey2 =
200                 SchemaAwareBuilders.leafBuilder((LeafSchemaNode) testLeafKey2SchemaNode);
201             leafKey2.withValue(payloadKey2);
202             testNodeContainer.withChild(leafKey2.build());
203         }
204
205         final NormalizedNodeContext testCompositeContext = new NormalizedNodeContext(
206             InstanceIdentifierContext.ofStack(
207                 SchemaInferenceStack.ofDataTreePath(schemaContextTestModule, lstWithCompositeKey)),
208             testNodeContainer.build());
209
210         final UriInfo uriInfo = Mockito.mock(UriInfo.class);
211         restconfImpl.updateConfigurationData(toUri(uriKey1, uriKey2), testCompositeContext, uriInfo);
212     }
213
214     public void putListDataWithWrapperTest(final String uriKey1, final String uriKey2, final String payloadKey1,
215             final Short payloadKey2) {
216         putListDataTest(uriKey1, uriKey2, payloadKey1, payloadKey2);
217     }
218
219     private static String toUri(final String uriKey1, final String uriKey2) {
220         final StringBuilder uriBuilder = new StringBuilder("/test-module:lst-with-composite-key/");
221         uriBuilder.append(uriKey1);
222         if (uriKey2 != null) {
223             uriBuilder.append("/");
224             uriBuilder.append(uriKey2);
225         }
226         return uriBuilder.toString();
227     }
228 }