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