104eb9a8225d7074ca6db9503c3d07741bde3b98
[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.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.PutResult;
32 import org.opendaylight.netconf.sal.restconf.impl.RestconfDocumentedException;
33 import org.opendaylight.netconf.sal.restconf.impl.RestconfError;
34 import org.opendaylight.netconf.sal.restconf.impl.RestconfError.ErrorTag;
35 import org.opendaylight.netconf.sal.restconf.impl.RestconfError.ErrorType;
36 import org.opendaylight.netconf.sal.restconf.impl.RestconfImpl;
37 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
38 import org.opendaylight.restconf.common.context.NormalizedNodeContext;
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      * <p>
102      * The exception should be raised from validation method
103      * {@code RestconfImpl#validateListEqualityOfListInDataAndUri}
104      */
105     @Test
106     @Ignore // RestconfDocumentedExceptionMapper needs update
107     public void testUriAndPayloadKeysDifferent() {
108         try {
109             putListDataTest("key1value", "15", "key1value", (short) 16);
110             fail("RestconfDocumentedException expected");
111         } catch (final RestconfDocumentedException e) {
112             verifyException(e, ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
113         }
114
115         try {
116             putListDataTest("key1value", "15", "key1value1", (short) 16);
117             fail("RestconfDocumentedException expected");
118         } catch (final RestconfDocumentedException e) {
119             verifyException(e, ErrorType.PROTOCOL, ErrorTag.INVALID_VALUE);
120         }
121     }
122
123     /**
124      * Tests whether an exception is raised if URI contains less key values then
125      * payload.
126      *
127      * <p>
128      * The exception is raised during {@code InstanceIdentifier} instance is
129      * built from URI
130      */
131     @Test
132     @Ignore
133     public void testMissingKeysInUri() {
134         try {
135             putListDataTest("key1value", null, "key1value", (short) 15);
136             fail("RestconfDocumentedException expected");
137         } catch (final RestconfDocumentedException e) {
138             verifyException(e, ErrorType.PROTOCOL, ErrorTag.DATA_MISSING);
139         }
140     }
141
142     /**
143      * Tests whether an exception is raised if URI contains more key values then
144      * payload.
145      *
146      * <p>
147      * The exception should be raised from validation method
148      * {@code RestconfImpl#validateListEqualityOfListInDataAndUri}
149      */
150     @Test
151     public void testMissingKeysInPayload() {
152         try {
153             putListDataTest("key1value", "15", "key1value", null);
154             fail("RestconfDocumentedException expected");
155         } catch (final DataValidationException e) {
156             // FIXME: thing about different approach for testing the Exception states
157             // RestconfDocumentedException is not rise in new API because you get
158             // DataValidationException from putListDataTest before you call the real rest service
159 //            verifyException(e, ErrorType.PROTOCOL, ErrorTag.DATA_MISSING);
160         }
161     }
162
163     private static void verifyException(final RestconfDocumentedException restDocumentedException,
164                                         final ErrorType errorType, final ErrorTag errorTag) {
165         final List<RestconfError> errors = restDocumentedException.getErrors();
166         assertEquals("getErrors() size", 1, errors.size());
167         assertEquals("RestconfError getErrorType()", errorType, errors.get(0).getErrorType());
168         assertEquals("RestconfError getErrorTag()", errorTag, errors.get(0).getErrorTag());
169     }
170
171     public void putListDataTest(final String uriKey1, final String uriKey2, final String payloadKey1,
172             final Short payloadKey2) {
173         final QName lstWithCompositeKey =
174                 QName.create(TEST_MODULE_NS_STRING, TEST_MODULE_REVISION, "lst-with-composite-key");
175         final QName key1 = QName.create(TEST_MODULE_NS_STRING, TEST_MODULE_REVISION, "key1");
176         final QName key2 = QName.create(TEST_MODULE_NS_STRING, TEST_MODULE_REVISION, "key2");
177
178         final DataSchemaNode testNodeSchemaNode = schemaContextTestModule.getDataChildByName(lstWithCompositeKey);
179         assertTrue(testNodeSchemaNode != null);
180         assertTrue(testNodeSchemaNode instanceof ListSchemaNode);
181         final DataContainerNodeAttrBuilder<NodeIdentifierWithPredicates, MapEntryNode> testNodeContainer =
182                 Builders.mapEntryBuilder((ListSchemaNode) testNodeSchemaNode);
183
184         List<DataSchemaNode> testChildren = ControllerContext.findInstanceDataChildrenByName(
185                 (ListSchemaNode) testNodeSchemaNode, key1.getLocalName());
186         assertTrue(testChildren != null);
187         final DataSchemaNode testLeafKey1SchemaNode = Iterables.getFirst(testChildren, null);
188         assertTrue(testLeafKey1SchemaNode != null);
189         assertTrue(testLeafKey1SchemaNode instanceof LeafSchemaNode);
190         final NormalizedNodeAttrBuilder<NodeIdentifier, Object, LeafNode<Object>> leafKey1 =
191                 Builders.leafBuilder((LeafSchemaNode) testLeafKey1SchemaNode);
192         leafKey1.withValue(payloadKey1);
193         testNodeContainer.withChild(leafKey1.build());
194
195         if (payloadKey2 != null) {
196             testChildren = ControllerContext.findInstanceDataChildrenByName(
197                     (ListSchemaNode) testNodeSchemaNode, key2.getLocalName());
198             assertTrue(testChildren != null);
199             final DataSchemaNode testLeafKey2SchemaNode = Iterables.getFirst(testChildren, null);
200             assertTrue(testLeafKey2SchemaNode != null);
201             assertTrue(testLeafKey2SchemaNode instanceof LeafSchemaNode);
202             final NormalizedNodeAttrBuilder<NodeIdentifier, Object, LeafNode<Object>> leafKey2 =
203                     Builders.leafBuilder((LeafSchemaNode) testLeafKey2SchemaNode);
204             leafKey2.withValue(payloadKey2);
205             testNodeContainer.withChild(leafKey2.build());
206         }
207
208         final NormalizedNodeContext testCompositeContext = new NormalizedNodeContext(new InstanceIdentifierContext<>(
209                 null, testNodeSchemaNode, null, schemaContextTestModule), testNodeContainer.build());
210
211         final UriInfo uriInfo = Mockito.mock(UriInfo.class);
212         restconfImpl.updateConfigurationData(toUri(uriKey1, uriKey2), testCompositeContext, uriInfo);
213     }
214
215     public void putListDataWithWrapperTest(final String uriKey1, final String uriKey2, final String payloadKey1,
216             final Short payloadKey2) {
217         putListDataTest(uriKey1, uriKey2, payloadKey1, payloadKey2);
218     }
219
220     private static String toUri(final String uriKey1, final String uriKey2) {
221         final StringBuilder uriBuilder = new StringBuilder("/test-module:lst-with-composite-key/");
222         uriBuilder.append(uriKey1);
223         if (uriKey2 != null) {
224             uriBuilder.append("/");
225             uriBuilder.append(uriKey2);
226         }
227         return uriBuilder.toString();
228     }
229
230 }