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