Remove DocumentedException.ErrorType
[netconf.git] / netconf / mdsal-netconf-connector / src / test / java / org / opendaylight / netconf / mdsal / connector / ops / NetconfMDSalMappingTest.java
1 /*
2  * Copyright (c) 2015 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.netconf.mdsal.connector.ops;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertThrows;
12 import static org.junit.Assert.assertTrue;
13
14 import java.net.URI;
15 import org.junit.Test;
16 import org.opendaylight.netconf.api.DocumentedException;
17 import org.opendaylight.netconf.api.DocumentedException.ErrorTag;
18 import org.opendaylight.netconf.api.xml.XmlElement;
19 import org.opendaylight.netconf.api.xml.XmlUtil;
20 import org.opendaylight.netconf.mdsal.connector.CurrentSchemaContext;
21 import org.opendaylight.netconf.mdsal.connector.TransactionProvider;
22 import org.opendaylight.netconf.mdsal.connector.ops.get.GetConfig;
23 import org.opendaylight.netconf.util.test.XmlFileLoader;
24 import org.opendaylight.yangtools.yang.common.ErrorSeverity;
25 import org.opendaylight.yangtools.yang.common.ErrorType;
26 import org.opendaylight.yangtools.yang.common.QName;
27 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
28 import org.w3c.dom.Document;
29 import org.w3c.dom.Element;
30 import org.w3c.dom.NodeList;
31
32 public class NetconfMDSalMappingTest extends AbstractNetconfOperationTest {
33     private static final String TARGET_KEY = "target";
34     private static final String FILTER_NODE = "filter";
35     private static final String GET_CONFIG = "get-config";
36     private static final QName TOP = QName.create("urn:opendaylight:mdsal:mapping:test", "2015-02-26", "top");
37     private static final QName USERS = QName.create("urn:opendaylight:mdsal:mapping:test", "2015-02-26", "users");
38     private static final QName USER = QName.create("urn:opendaylight:mdsal:mapping:test", "2015-02-26", "user");
39     private static final QName MODULES = QName.create("urn:opendaylight:mdsal:mapping:test", "2015-02-26", "modules");
40     private static final QName AUGMENTED_CONTAINER = QName.create("urn:opendaylight:mdsal:mapping:test",
41             "2015-02-26", "augmented-container");
42     private static final QName AUGMENTED_STRING_IN_CONT = QName.create("urn:opendaylight:mdsal:mapping:test",
43             "2015-02-26", "identifier");
44     private static final QName CHOICE_NODE = QName.create("urn:opendaylight:mdsal:mapping:test", "2015-02-26",
45             "choice-node");
46     private static final QName AUGMENTED_CASE = QName.create("urn:opendaylight:mdsal:mapping:test", "2015-02-26",
47             "augmented-case");
48     private static final QName CHOICE_WRAPPER = QName.create("urn:opendaylight:mdsal:mapping:test", "2015-02-26",
49             "choice-wrapper");
50     private static final QName INNER_CHOICE = QName.create("urn:opendaylight:mdsal:mapping:test", "2015-02-26",
51             "inner-choice");
52     private static final QName INNER_CHOICE_TEXT = QName.create("urn:opendaylight:mdsal:mapping:test", "2015-02-26",
53             "text");
54
55     private static final YangInstanceIdentifier AUGMENTED_CONTAINER_IN_MODULES =
56             YangInstanceIdentifier.builder().node(TOP).node(MODULES).build();
57
58     @Test
59     public void testEmptyDatastore() throws Exception {
60         assertEmptyDatastore(get());
61         assertEmptyDatastore(getConfigCandidate());
62         assertEmptyDatastore(getConfigRunning());
63     }
64
65     @Test
66     public void testIncorrectGet() throws Exception {
67         DocumentedException ex = assertThrows(DocumentedException.class,
68             () -> executeOperation(new GetConfig(SESSION_ID_FOR_REPORTING, getCurrentSchemaContext(),
69                     getTransactionProvider()), "messages/mapping/bad_getConfig.xml"));
70         assertEquals(ErrorSeverity.ERROR, ex.getErrorSeverity());
71         assertEquals(ErrorTag.OPERATION_FAILED, ex.getErrorTag());
72         assertEquals(ErrorType.APPLICATION, ex.getErrorType());
73
74         ex = assertThrows(DocumentedException.class,
75             () -> executeOperation(new GetConfig(SESSION_ID_FOR_REPORTING, getCurrentSchemaContext(),
76                     getTransactionProvider()), "messages/mapping/bad_namespace_getConfig.xml"));
77         assertEquals(ErrorSeverity.ERROR, ex.getErrorSeverity());
78         assertEquals(ErrorTag.OPERATION_FAILED, ex.getErrorTag());
79         assertEquals(ErrorType.APPLICATION, ex.getErrorType());
80     }
81
82     @Test
83     public void testConfigMissing() throws Exception {
84         final DocumentedException ex = assertThrows(DocumentedException.class,
85             () -> edit("messages/mapping/editConfigs/editConfig_no_config.xml"));
86         assertEquals(ErrorSeverity.ERROR, ex.getErrorSeverity());
87         assertEquals(ErrorTag.MISSING_ELEMENT, ex.getErrorTag());
88         assertEquals(ErrorType.PROTOCOL, ex.getErrorType());
89     }
90
91     @Test
92     public void testEditRunning() throws Exception {
93         final DocumentedException ex = assertThrows(DocumentedException.class,
94             () -> edit("messages/mapping/editConfigs/editConfig_running.xml"));
95         assertEquals(ErrorSeverity.ERROR, ex.getErrorSeverity());
96         assertEquals(ErrorTag.OPERATION_NOT_SUPPORTED, ex.getErrorTag());
97         assertEquals(ErrorType.PROTOCOL, ex.getErrorType());
98     }
99
100     @Test
101     public void testCommitWithoutOpenTransaction() throws Exception {
102         verifyResponse(commit(), RPC_REPLY_OK);
103         assertEmptyDatastore(getConfigCandidate());
104     }
105
106     @Test
107     public void testCandidateTransaction() throws Exception {
108         verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_n1.xml"), RPC_REPLY_OK);
109         verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument(
110                 "messages/mapping/editConfigs/editConfig_merge_n1_control.xml"));
111         assertEmptyDatastore(getConfigRunning());
112
113         verifyResponse(discardChanges(), RPC_REPLY_OK);
114         assertEmptyDatastore(getConfigCandidate());
115     }
116
117     @Test
118     public void testEditWithCommit() throws Exception {
119         verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_n1.xml"), RPC_REPLY_OK);
120         verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument(
121                 "messages/mapping/editConfigs/editConfig_merge_n1_control.xml"));
122
123         verifyResponse(commit(), RPC_REPLY_OK);
124         verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument(
125                 "messages/mapping/editConfigs/editConfig_merge_n1_control.xml"));
126
127         deleteDatastore();
128     }
129
130     @Test
131     public void testKeyOrder() throws Exception {
132         verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_keys_1.xml"), RPC_REPLY_OK);
133         verifyResponse(commit(), RPC_REPLY_OK);
134         final Document configRunning = getConfigRunning();
135         final String responseAsString = XmlUtil.toString(configRunning);
136         verifyResponse(configRunning, XmlFileLoader.xmlFileToDocument(
137                 "messages/mapping/editConfigs/editConfig_merge_multiple_keys_1_control.xml"));
138
139         final int key3 = responseAsString.indexOf("key3");
140         final int key1 = responseAsString.indexOf("key1");
141         final int key2 = responseAsString.indexOf("key2");
142
143         assertTrue(String.format("Key ordering invalid, should be key3(%d) < key1(%d) < key2(%d)", key3, key1, key2),
144                 key3 < key1 && key1 < key2);
145
146         deleteDatastore();
147     }
148
149     @Test
150     public void testMultipleEditsWithMerge() throws Exception {
151         verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_1.xml"), RPC_REPLY_OK);
152         verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument(
153                 "messages/mapping/editConfigs/editConfig_merge_multiple_control_1.xml"));
154         verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_single_1.xml"), RPC_REPLY_OK);
155         verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument(
156                 "messages/mapping/editConfigs/editConfig_merge_multiple_control_2.xml"));
157         assertEmptyDatastore(getConfigRunning());
158
159         verifyResponse(commit(), RPC_REPLY_OK);
160         verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument(
161                 "messages/mapping/editConfigs/editConfig_merge_multiple_control_2.xml"));
162
163         deleteDatastore();
164     }
165
166     @Test
167     public void testMoreComplexEditConfigs() throws Exception {
168         verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_1.xml"), RPC_REPLY_OK);
169         verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_single_1.xml"), RPC_REPLY_OK);
170
171         verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_2.xml"), RPC_REPLY_OK);
172         verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument(
173                 "messages/mapping/editConfigs/editConfig_merge_multiple_after_more_complex_merge.xml"));
174
175         verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_3.xml"), RPC_REPLY_OK);
176         verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument(
177                 "messages/mapping/editConfigs/editConfig_merge_multiple_after_more_complex_merge_2.xml"));
178
179         verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_4_replace.xml"), RPC_REPLY_OK);
180         verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument(
181                 "messages/mapping/editConfigs/editConfig_merge_multiple_after_replace.xml"));
182         verifyResponse(commit(), RPC_REPLY_OK);
183
184         verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument(
185                 "messages/mapping/editConfigs/editConfig_merge_multiple_after_replace.xml"));
186
187         verifyResponse(edit("messages/mapping/editConfigs/editConfig_replace_default.xml"), RPC_REPLY_OK);
188         verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument(
189                 "messages/mapping/editConfigs/editConfig_replace_default_control.xml"));
190         verifyResponse(commit(), RPC_REPLY_OK);
191
192         verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument(
193                 "messages/mapping/editConfigs/editConfig_replace_default_control.xml"));
194
195         deleteDatastore();
196     }
197
198     @Test
199     public void testOrderedListEdits() throws Exception {
200
201         verifyResponse(edit("messages/mapping/editConfigs/editConfig_ordered_list_create.xml"), RPC_REPLY_OK);
202         verifyResponse(commit(), RPC_REPLY_OK);
203
204         verifyResponse(edit("messages/mapping/editConfigs/editConfig_ordered_list_replace.xml"), RPC_REPLY_OK);
205         verifyResponse(commit(), RPC_REPLY_OK);
206
207         deleteDatastore();
208     }
209
210     @Test
211     public void testAugmentedOrderedListEdits() throws Exception {
212
213         verifyResponse(edit("messages/mapping/editConfigs/editConfig_augmented_ordered_list_create.xml"),
214                 RPC_REPLY_OK);
215         verifyResponse(commit(), RPC_REPLY_OK);
216
217         verifyResponse(edit("messages/mapping/editConfigs/editConfig_augmented_ordered_list_replace.xml"),
218                 RPC_REPLY_OK);
219         verifyResponse(commit(), RPC_REPLY_OK);
220
221         deleteDatastore();
222     }
223
224     @Test
225     public void testAugmentedContainerReplace() throws Exception {
226         verifyResponse(edit("messages/mapping/editConfigs/editConfig_empty_modules_create.xml"),
227                 RPC_REPLY_OK);
228         verifyResponse(commit(), RPC_REPLY_OK);
229
230         verifyResponse(edit("messages/mapping/editConfigs/editConfig_augmented_container_replace.xml"),
231                 RPC_REPLY_OK);
232         verifyResponse(commit(), RPC_REPLY_OK);
233
234         deleteDatastore();
235     }
236
237     @Test
238     public void testLeafFromAugmentReplace() throws Exception {
239         verifyResponse(edit("messages/mapping/editConfigs/editConfig_empty_modules_create.xml"),
240                 RPC_REPLY_OK);
241         verifyResponse(commit(), RPC_REPLY_OK);
242
243         verifyResponse(edit("messages/mapping/editConfigs/editConfig_leaf_from_augment_replace.xml"),
244                 RPC_REPLY_OK);
245         verifyResponse(commit(), RPC_REPLY_OK);
246
247         deleteDatastore();
248     }
249
250     @Test
251     public void testLock() throws Exception {
252         verifyResponse(lockCandidate(), RPC_REPLY_OK);
253
254         DocumentedException ex = assertThrows(DocumentedException.class, NetconfMDSalMappingTest::lock);
255         assertEquals(ErrorSeverity.ERROR, ex.getErrorSeverity());
256         assertEquals(ErrorTag.OPERATION_NOT_SUPPORTED, ex.getErrorTag());
257         assertEquals(ErrorType.APPLICATION, ex.getErrorType());
258
259         ex = assertThrows(DocumentedException.class, NetconfMDSalMappingTest::lockWithoutTarget);
260         assertEquals(ErrorSeverity.ERROR, ex.getErrorSeverity());
261         assertEquals(ErrorTag.INVALID_VALUE, ex.getErrorTag());
262         assertEquals(ErrorType.APPLICATION, ex.getErrorType());
263     }
264
265     @Test
266     public void testUnlock() throws Exception {
267         verifyResponse(unlockCandidate(), RPC_REPLY_OK);
268
269         DocumentedException ex = assertThrows(DocumentedException.class, NetconfMDSalMappingTest::unlock);
270         assertEquals(ErrorSeverity.ERROR, ex.getErrorSeverity());
271         assertEquals(ErrorTag.OPERATION_NOT_SUPPORTED, ex.getErrorTag());
272         assertEquals(ErrorType.APPLICATION, ex.getErrorType());
273
274         ex = assertThrows(DocumentedException.class, NetconfMDSalMappingTest::unlockWithoutTarget);
275         assertEquals(ErrorSeverity.ERROR, ex.getErrorSeverity());
276         assertEquals(ErrorTag.INVALID_VALUE, ex.getErrorTag());
277         assertEquals(ErrorType.APPLICATION, ex.getErrorType());
278     }
279
280     @Test
281     public void testEditWithCreate() throws Exception {
282         verifyResponse(edit("messages/mapping/editConfigs/editConfig_create.xml"), RPC_REPLY_OK);
283         verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument(
284                 "messages/mapping/editConfig_create_n1_control.xml"));
285
286         final DocumentedException ex = assertThrows(DocumentedException.class,
287             () -> edit("messages/mapping/editConfigs/editConfig_create.xml"));
288         assertEquals(ErrorSeverity.ERROR, ex.getErrorSeverity());
289         assertEquals(ErrorTag.DATA_EXISTS, ex.getErrorTag());
290         assertEquals(ErrorType.PROTOCOL, ex.getErrorType());
291
292         verifyResponse(discardChanges(), RPC_REPLY_OK);
293     }
294
295     @Test
296     public void testDeleteNonExisting() throws Exception {
297         assertEmptyDatastore(getConfigCandidate());
298         assertEmptyDatastore(getConfigRunning());
299
300         final DocumentedException ex = assertThrows(DocumentedException.class,
301             () -> edit("messages/mapping/editConfigs/editConfig_delete-top.xml"));
302         assertEquals(ErrorSeverity.ERROR, ex.getErrorSeverity());
303         assertEquals(ErrorTag.DATA_MISSING, ex.getErrorTag());
304         assertEquals(ErrorType.PROTOCOL, ex.getErrorType());
305     }
306
307     @Test
308     public void testEditMissingDefaultOperation() throws Exception {
309         verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_missing_default-operation_1.xml"),
310                 RPC_REPLY_OK);
311         verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_missing_default-operation_2.xml"),
312                 RPC_REPLY_OK);
313         verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument(
314                 "messages/mapping/editConfigs/editConfig_merge_missing_default-operation_control.xml"));
315
316         verifyResponse(commit(), RPC_REPLY_OK);
317         verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument(
318                 "messages/mapping/editConfigs/editConfig_merge_missing_default-operation_control.xml"));
319
320         deleteDatastore();
321     }
322
323     @Test
324     public void testEditConfigWithMultipleOperations() throws Exception {
325         deleteDatastore();
326
327         verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_setup.xml"),
328                 RPC_REPLY_OK);
329         verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_1.xml"), RPC_REPLY_OK);
330
331         verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_2.xml"), RPC_REPLY_OK);
332         verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument(
333                 "messages/mapping/editConfigs/editConfig_merge_multiple_operations_2_control.xml"));
334
335         verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_3_leaf_operations.xml"),
336                 RPC_REPLY_OK);
337         verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument(
338                 "messages/mapping/editConfigs/editConfig_merge_multiple_operations_3_control.xml"));
339
340         deleteDatastore();
341
342         verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_4_setup.xml"),
343                 RPC_REPLY_OK);
344         verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_4_default-replace.xml"),
345                 RPC_REPLY_OK);
346
347         DocumentedException ex = assertThrows(DocumentedException.class,
348             () -> edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_4_create_existing.xml"));
349         assertEquals(ErrorSeverity.ERROR, ex.getErrorSeverity());
350         assertEquals(ErrorTag.DATA_EXISTS, ex.getErrorTag());
351         assertEquals(ErrorType.PROTOCOL, ex.getErrorType());
352
353         verifyResponse(edit(
354                 "messages/mapping/editConfigs/editConfig_merge_multiple_operations_4_delete_children_operations.xml"),
355                 RPC_REPLY_OK);
356         verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument(
357                 "messages/mapping/editConfigs/"
358                         + "editConfig_merge_multiple_operations_4_delete_children_operations_control.xml"));
359         verifyResponse(edit(
360                 "messages/mapping/editConfigs/editConfig_merge_multiple_operations_4_remove-non-existing.xml"),
361                 RPC_REPLY_OK);
362         ex = assertThrows(DocumentedException.class,
363             () -> edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_4_delete-non-existing.xml"));
364         assertEquals(ErrorSeverity.ERROR, ex.getErrorSeverity());
365         assertEquals(ErrorTag.DATA_MISSING, ex.getErrorTag());
366         assertEquals(ErrorType.PROTOCOL, ex.getErrorType());
367
368         verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_5_choice_setup.xml"),
369                 RPC_REPLY_OK);
370         verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument(
371                 "messages/mapping/editConfigs/editConfig_merge_multiple_operations_5_choice_setup-control.xml"));
372
373         // Test files have been modified. RFC6020 requires that at most once case inside a choice is present at any time
374         verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_5_choice_setup2.xml"),
375                 RPC_REPLY_OK);
376         verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument(
377                 "messages/mapping/editConfigs/editConfig_merge_multiple_operations_5_choice_setup2-control.xml"));
378
379         verifyResponse(edit("messages/mapping/editConfigs/editConfig_merge_multiple_operations_5_choice_delete.xml"),
380                 RPC_REPLY_OK);
381         verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument(
382                 "messages/mapping/editConfigs"
383                         + "/editConfig_merge_multiple_operations_4_delete_children_operations_control.xml"));
384
385         deleteDatastore();
386     }
387
388     @Test
389     public void testEditConfigGetElementByTagName() throws Exception {
390         String stringWithoutPrefix =
391                 "<rpc xmlns:nc=\"urn:ietf:params:xml:ns:netconf:base:1.0\" message-id=\"0\">\n"
392                         + "  <edit-config>\n"
393                         + "    <target>\n"
394                         + "      <candidate/>\n"
395                         + "    </target>\n"
396                         + "  </edit-config>\n"
397                         + "</rpc>";
398         XmlElement xe = getXmlElement(stringWithoutPrefix);
399         NodeList nodeList = EditConfig.getElementsByTagName(xe, TARGET_KEY);
400         assertEquals(1, nodeList.getLength());
401
402         String stringWithPrefix =
403                 "<nc:rpc xmlns:nc=\"urn:ietf:params:xml:ns:netconf:base:1.0\" message-id=\"0\">\n"
404                         + "  <nc:edit-config>\n"
405                         + "    <nc:target>\n"
406                         + "      <nc:candidate/>\n"
407                         + "    </nc:target>\n"
408                         + "  </nc:edit-config>\n"
409                         + "</nc:rpc>";
410
411         xe = getXmlElement(stringWithPrefix);
412         nodeList = EditConfig.getElementsByTagName(xe, TARGET_KEY);
413         assertEquals(1, nodeList.getLength());
414
415         String stringWithoutTarget =
416                 "<nc:rpc xmlns:nc=\"urn:ietf:params:xml:ns:netconf:base:1.0\" message-id=\"0\">\n"
417                         + "  <nc:edit-config>\n"
418                         + "    <nc:target>\n"
419                         + "    </nc:target>\n"
420                         + "  </nc:edit-config>\n"
421                         + "</nc:rpc>";
422         xe = getXmlElement(stringWithoutTarget);
423
424         final NodeList targetKey = EditConfig.getElementsByTagName(xe, TARGET_KEY);
425         assertThrows(DocumentedException.class,
426             () -> XmlElement.fromDomElement((Element) targetKey.item(0)).getOnlyChildElement());
427     }
428
429     private static XmlElement getXmlElement(final String elementAsString) throws Exception {
430         Document document = XmlUtil.readXmlToDocument(elementAsString);
431         Element element = document.getDocumentElement();
432         return XmlElement.fromDomElement(element);
433     }
434
435     @Test
436     public void testReplaceMapEntry() throws Exception {
437         verifyResponse(edit("messages/mapping/editConfigs/edit-config-replace-map-entry.xml"), RPC_REPLY_OK);
438         verifyResponse(commit(), RPC_REPLY_OK);
439         verifyResponse(getConfigRunning(),
440                 XmlFileLoader.xmlFileToDocument("messages/mapping/get-config-map-entry.xml"));
441     }
442
443     @Test
444     public void testMergeMapEntry() throws Exception {
445         verifyResponse(edit("messages/mapping/editConfigs/edit-config-merge-map-entry.xml"), RPC_REPLY_OK);
446         verifyResponse(commit(), RPC_REPLY_OK);
447         verifyResponse(getConfigRunning(),
448                 XmlFileLoader.xmlFileToDocument("messages/mapping/get-config-map-entry.xml"));
449     }
450
451     @Test
452     public void testFiltering() throws Exception {
453         assertEmptyDatastore(getConfigCandidate());
454         assertEmptyDatastore(getConfigRunning());
455
456         verifyResponse(getConfigWithFilter("messages/mapping/filters/get-config-empty-filter.xml"),
457                 XmlFileLoader.xmlFileToDocument("messages/mapping/get-empty-response.xml"));
458         verifyResponse(getWithFilter("messages/mapping/filters/get-empty-filter.xml"),
459                 XmlFileLoader.xmlFileToDocument("messages/mapping/get-empty-response.xml"));
460
461         verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument("messages/mapping/get-empty-response"
462                 + ".xml"));
463         verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument("messages/mapping/get-empty-response.xml"));
464         verifyResponse(getConfigWithFilter("messages/mapping/filters/get-filter-users.xml"),
465                 XmlFileLoader.xmlFileToDocument("messages/mapping/get-empty-response.xml"));
466
467         verifyResponse(edit("messages/mapping/editConfigs/editConfig-filtering-setup.xml"), RPC_REPLY_OK);
468         verifyResponse(commit(), RPC_REPLY_OK);
469
470         verifyFilterIdentifier("messages/mapping/filters/get-filter-alluser.xml",
471                 YangInstanceIdentifier.builder().node(TOP).node(USERS).node(USER).build());
472         verifyFilterIdentifier("messages/mapping/filters/get-filter-company-info.xml",
473                 YangInstanceIdentifier.builder().node(TOP).node(USERS).node(USER).build());
474         verifyFilterIdentifier("messages/mapping/filters/get-filter-modules-and-admin.xml",
475                 YangInstanceIdentifier.builder().node(TOP).build());
476         verifyFilterIdentifier("messages/mapping/filters/get-filter-only-names-types.xml",
477                 YangInstanceIdentifier.builder().node(TOP).node(USERS).node(USER).build());
478         verifyFilterIdentifier("messages/mapping/filters/get-filter-specific-module-type-and-user.xml",
479                 YangInstanceIdentifier.builder().node(TOP).build());
480         verifyFilterIdentifier("messages/mapping/filters/get-filter-superuser.xml",
481                 YangInstanceIdentifier.builder().node(TOP).node(USERS).node(USER).build());
482         verifyFilterIdentifier("messages/mapping/filters/get-filter-users.xml",
483                 YangInstanceIdentifier.builder().node(TOP).node(USERS).build());
484
485         final YangInstanceIdentifier ident = YangInstanceIdentifier
486                 .builder(AUGMENTED_CONTAINER_IN_MODULES)
487                 .node(AUGMENTED_CONTAINER)
488                 .node(AUGMENTED_STRING_IN_CONT).build();
489
490         verifyFilterIdentifier("messages/mapping/filters/get-filter-augmented-string.xml", ident);
491         verifyFilterIdentifier("messages/mapping/filters/get-filter-augmented-case.xml",
492                 YangInstanceIdentifier.builder().node(TOP).node(CHOICE_NODE).node(AUGMENTED_CASE).build());
493
494         verifyResponse(getConfigWithFilter("messages/mapping/filters/get-filter-augmented-case.xml"),
495                 XmlFileLoader.xmlFileToDocument("messages/mapping/filters/response-augmented-case.xml"));
496
497         /*
498          *  RFC6020 requires that at most once case inside a choice is present at any time.
499          *  Therefore
500          *  <augmented-case>augmented case</augmented-case>
501          *  from
502          *  messages/mapping/editConfigs/editConfig-filtering-setup.xml
503          *  cannot exists together with
504          *  <text>augmented nested choice text1</text>
505          *  from
506          *  messages/mapping/editConfigs/editConfig-filtering-setup2.xml
507          */
508         //verifyResponse(edit("messages/mapping/editConfigs/editConfig-filtering-setup2.xml"), RPC_REPLY_OK);
509         //verifyResponse(commit(), RPC_REPLY_OK);
510
511         verifyFilterIdentifier("messages/mapping/filters/get-filter-augmented-case-inner-choice.xml",
512                 YangInstanceIdentifier.builder().node(TOP).node(CHOICE_NODE).node(CHOICE_WRAPPER).build());
513         verifyFilterIdentifier("messages/mapping/filters/get-filter-augmented-case-inner-case.xml",
514                 YangInstanceIdentifier.builder().node(TOP).node(CHOICE_NODE).node(CHOICE_WRAPPER).node(INNER_CHOICE)
515                         .node(INNER_CHOICE_TEXT).build());
516
517 //        verifyResponse(getConfigWithFilter("messages/mapping/filters/get-filter-augmented-string.xml"),
518 //                XmlFileLoader.xmlFileToDocument("messages/mapping/filters/response-augmented-string.xml"));
519 //        verifyResponse(getConfigWithFilter("messages/mapping/filters/get-filter-augmented-case-inner-choice.xml"),
520 //                XmlFileLoader.xmlFileToDocument("messages/mapping/filters/response-augmented-case-inner-choice.xml"));
521 //        verifyResponse(getConfigWithFilter("messages/mapping/filters/get-filter-augmented-case-inner-case.xml"),
522 //                XmlFileLoader.xmlFileToDocument("messages/mapping/filters/response-augmented-case-inner-choice.xml"));
523
524         verifyResponse(edit("messages/mapping/editConfigs/editConfig_delete-top.xml"), RPC_REPLY_OK);
525         verifyResponse(commit(), RPC_REPLY_OK);
526
527     }
528
529     private void verifyFilterIdentifier(final String resource, final YangInstanceIdentifier identifier)
530             throws Exception {
531         final TestingGetConfig getConfig = new TestingGetConfig(SESSION_ID_FOR_REPORTING, getCurrentSchemaContext(),
532                 getTransactionProvider());
533         final Document request = XmlFileLoader.xmlFileToDocument(resource);
534         final YangInstanceIdentifier iid = getConfig.getInstanceIdentifierFromDocument(request);
535         assertEquals(identifier, iid);
536     }
537
538     private class TestingGetConfig extends GetConfig {
539         TestingGetConfig(final String sessionId, final CurrentSchemaContext schemaContext,
540                          final TransactionProvider transactionProvider) {
541             super(sessionId, schemaContext, transactionProvider);
542         }
543
544         YangInstanceIdentifier getInstanceIdentifierFromDocument(final Document request) throws DocumentedException {
545             final XmlElement filterElement = XmlElement.fromDomDocument(request).getOnlyChildElement(GET_CONFIG)
546                     .getOnlyChildElement(FILTER_NODE);
547             return getInstanceIdentifierFromFilter(filterElement);
548         }
549     }
550
551     private void deleteDatastore() throws Exception {
552         verifyResponse(edit("messages/mapping/editConfigs/editConfig_delete-root.xml"), RPC_REPLY_OK);
553         assertEmptyDatastore(getConfigCandidate());
554
555         verifyResponse(commit(), RPC_REPLY_OK);
556         assertEmptyDatastore(getConfigRunning());
557     }
558
559     @Test
560     public void testEditUsingConfigFromFile() throws Exception {
561         // Ask class loader for URI of config file and use it as <url> in <edit-config> RPC:
562         final String template = XmlFileLoader.fileToString("messages/mapping/editConfigs/editConfig_from_file.xml");
563         final URI uri = getClass().getClassLoader().getResource("messages/mapping/editConfigs/config_file.xml").toURI();
564         final String copyConfig = template.replaceFirst("URL", uri.toString());
565         final Document request = XmlUtil.readXmlToDocument(copyConfig);
566
567         verifyResponse(edit(request), RPC_REPLY_OK);
568         verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument(
569             "messages/mapping/editConfigs/editConfig_from_file_control.xml"));
570     }
571 }