2 * Copyright (c) 2018 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.netconf.mdsal.connector.ops;
11 import static org.junit.Assert.assertTrue;
12 import static org.junit.Assert.fail;
13 import static org.opendaylight.yangtools.yang.test.util.YangParserTestUtils.parseYangResources;
15 import org.junit.Test;
16 import org.opendaylight.netconf.api.DocumentedException;
17 import org.opendaylight.netconf.api.DocumentedException.ErrorSeverity;
18 import org.opendaylight.netconf.api.DocumentedException.ErrorTag;
19 import org.opendaylight.netconf.api.DocumentedException.ErrorType;
20 import org.opendaylight.netconf.util.test.XmlFileLoader;
21 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
22 import org.w3c.dom.Document;
24 public class CopyConfigTest extends AbstractNetconfOperationTest {
27 protected SchemaContext getSchemaContext() {
28 return parseYangResources(CopyConfigTest.class,
29 "/yang/mdsal-netconf-mapping-test.yang");
34 public void testTargetMissing() throws Exception {
36 copyConfig("messages/mapping/copyConfigs/copyConfig_no_target.xml");
37 fail("Should have failed - <target> element is missing");
38 } catch (final DocumentedException e) {
39 assertTrue(e.getErrorSeverity() == ErrorSeverity.ERROR);
40 assertTrue(e.getErrorTag() == ErrorTag.MISSING_ATTRIBUTE);
41 assertTrue(e.getErrorType() == ErrorType.PROTOCOL);
46 public void testSourceMissing() throws Exception {
48 copyConfig("messages/mapping/copyConfigs/copyConfig_no_source.xml");
49 fail("Should have fanode1iled - <source> element is missing");
50 } catch (final DocumentedException e) {
51 assertTrue(e.getErrorSeverity() == ErrorSeverity.ERROR);
52 assertTrue(e.getErrorTag() == ErrorTag.MISSING_ELEMENT);
53 assertTrue(e.getErrorType() == ErrorType.PROTOCOL);
58 public void testConfigMissing() throws Exception {
60 copyConfig("messages/mapping/copyConfigs/copyConfig_no_config.xml");
61 fail("Should have failed - <config> element is missing");
62 } catch (final DocumentedException e) {
63 assertTrue(e.getErrorSeverity() == ErrorSeverity.ERROR);
64 assertTrue(e.getErrorTag() == ErrorTag.MISSING_ELEMENT);
65 assertTrue(e.getErrorType() == ErrorType.PROTOCOL);
70 public void testRunning() throws Exception {
72 copyConfig("messages/mapping/copyConfigs/copyConfig_running.xml");
73 fail("Should have failed - copy config on running datastore is not supported");
74 } catch (final DocumentedException e) {
75 assertTrue(e.getErrorSeverity() == ErrorSeverity.ERROR);
76 assertTrue(e.getErrorTag() == ErrorTag.OPERATION_NOT_SUPPORTED);
77 assertTrue(e.getErrorType() == ErrorType.PROTOCOL);
82 public void testCandidateTransaction() throws Exception {
83 verifyResponse(copyConfig("messages/mapping/copyConfigs/copyConfig_top_modules.xml"), RPC_REPLY_OK);
84 verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument(
85 "messages/mapping/copyConfigs/copyConfig_top_modules_control.xml"));
86 assertEmptyDatastore(getConfigRunning());
88 verifyResponse(discardChanges(), RPC_REPLY_OK);
89 assertEmptyDatastore(getConfigCandidate());
93 public void testWithCommit() throws Exception {
94 verifyResponse(copyConfig("messages/mapping/copyConfigs/copyConfig_top_modules.xml"), RPC_REPLY_OK);
95 final Document expectedConfig = XmlFileLoader.xmlFileToDocument(
96 "messages/mapping/copyConfigs/copyConfig_top_modules_control.xml");
97 verifyResponse(getConfigCandidate(), expectedConfig);
99 verifyResponse(commit(), RPC_REPLY_OK);
100 verifyResponse(getConfigRunning(), expectedConfig);
104 public void testDeleteSubtree() throws Exception {
105 // Initialize datastore
106 verifyResponse(copyConfig("messages/mapping/copyConfigs/copyConfig_delete_setup.xml"), RPC_REPLY_OK);
107 verifyResponse(commit(), RPC_REPLY_OK);
108 verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument(
109 "messages/mapping/copyConfigs/copyConfig_delete_setup_control.xml"));
111 // Issue second copy-config, this time without top container
112 verifyResponse(copyConfig("messages/mapping/copyConfigs/copyConfig_delete.xml"), RPC_REPLY_OK);
113 verifyResponse(commit(), RPC_REPLY_OK);
114 verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument(
115 "messages/mapping/copyConfigs/copyConfig_delete_control.xml"));
119 public void testList() throws Exception {
120 verifyResponse(copyConfig("messages/mapping/copyConfigs/copyConfig_list_setup.xml"), RPC_REPLY_OK);
121 verifyResponse(commit(), RPC_REPLY_OK);
122 verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument(
123 "messages/mapping/copyConfigs/copyConfig_list_setup_control.xml"));
125 verifyResponse(copyConfig("messages/mapping/copyConfigs/copyConfig_list_update.xml"), RPC_REPLY_OK);
126 verifyResponse(commit(), RPC_REPLY_OK);
127 verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument(
128 "messages/mapping/copyConfigs/copyConfig_list_update_control.xml"));
132 public void testOrderedList() throws Exception {
133 verifyResponse(copyConfig("messages/mapping/copyConfigs/copyConfig_ordered_list_setup.xml"),
135 verifyResponse(commit(), RPC_REPLY_OK);
136 verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument(
137 "messages/mapping/copyConfigs/copyConfig_ordered_list_setup_control.xml"));
139 verifyResponse(copyConfig("messages/mapping/copyConfigs/copyConfig_ordered_list_update.xml"),
141 verifyResponse(commit(), RPC_REPLY_OK);
142 verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument(
143 "messages/mapping/copyConfigs/copyConfig_ordered_list_update_control.xml"));
147 public void testToplevelList() throws Exception {
148 verifyResponse(copyConfig("messages/mapping/copyConfigs/copyConfig_toplevel_list_setup.xml"),
150 verifyResponse(commit(), RPC_REPLY_OK);
151 verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument(
152 "messages/mapping/copyConfigs/copyConfig_toplevel_list_setup_control.xml"));
154 verifyResponse(copyConfig("messages/mapping/copyConfigs/copyConfig_toplevel_list_update.xml"),
156 verifyResponse(commit(), RPC_REPLY_OK);
157 verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument(
158 "messages/mapping/copyConfigs/copyConfig_toplevel_list_update_control.xml"));
162 public void testEmptyContainer() throws Exception {
163 // Check that empty non-presence container is removed.
164 verifyResponse(copyConfig("messages/mapping/copyConfigs/copyConfig_empty_container.xml"),
166 verifyResponse(commit(), RPC_REPLY_OK);
167 verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument(
168 "messages/mapping/copyConfigs/copyConfig_empty_container_control.xml"));
172 public void testEmptyPresenceContainer() throws Exception {
173 // Check that empty presence container is not removed.
174 verifyResponse(copyConfig("messages/mapping/copyConfigs/copyConfig_empty_presence_container.xml"),
176 verifyResponse(commit(), RPC_REPLY_OK);
177 verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument(
178 "messages/mapping/copyConfigs/copyConfig_empty_presence_container_control.xml"));
182 public void testAugmentations() throws Exception {
183 verifyResponse(copyConfig("messages/mapping/copyConfigs/copyConfig_top_augmentation.xml"),
185 verifyResponse(commit(), RPC_REPLY_OK);
186 verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument(
187 "messages/mapping/copyConfigs/copyConfig_top_augmentation_control.xml"));
191 public void testChoices() throws Exception {
192 verifyResponse(copyConfig("messages/mapping/copyConfigs/copyConfig_choices1.xml"), RPC_REPLY_OK);
193 verifyResponse(commit(), RPC_REPLY_OK);
194 verifyResponse(copyConfig("messages/mapping/copyConfigs/copyConfig_choices2.xml"), RPC_REPLY_OK);
195 verifyResponse(commit(), RPC_REPLY_OK);
196 verifyResponse(copyConfig("messages/mapping/copyConfigs/copyConfig_choices3.xml"), RPC_REPLY_OK);
197 verifyResponse(commit(), RPC_REPLY_OK);
198 verifyResponse(copyConfig("messages/mapping/copyConfigs/copyConfig_choices4.xml"), RPC_REPLY_OK);
199 verifyResponse(commit(), RPC_REPLY_OK);
200 verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument(
201 "messages/mapping/copyConfigs/copyConfig_choices_control.xml"));
204 private Document copyConfig(final String resource) throws Exception {
205 final CopyConfig copyConfig = new CopyConfig(SESSION_ID_FOR_REPORTING, getCurrentSchemaContext(),
206 getTransactionProvider());
207 return executeOperation(copyConfig, resource);