Reuse schemaContext in mdsal-netconf-connector tests
[netconf.git] / netconf / mdsal-netconf-connector / src / test / java / org / opendaylight / netconf / mdsal / connector / ops / CopyConfigTest.java
1 /*
2  * Copyright (c) 2018 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.assertTrue;
11 import static org.junit.Assert.fail;
12
13 import java.io.File;
14 import java.io.FileInputStream;
15 import java.net.MalformedURLException;
16 import java.net.URI;
17 import org.junit.Rule;
18 import org.junit.Test;
19 import org.junit.rules.TemporaryFolder;
20 import org.opendaylight.netconf.api.DocumentedException;
21 import org.opendaylight.netconf.api.DocumentedException.ErrorSeverity;
22 import org.opendaylight.netconf.api.DocumentedException.ErrorTag;
23 import org.opendaylight.netconf.api.DocumentedException.ErrorType;
24 import org.opendaylight.netconf.api.xml.XmlUtil;
25 import org.opendaylight.netconf.util.test.XmlFileLoader;
26 import org.w3c.dom.Document;
27 import org.xml.sax.SAXException;
28
29 public class CopyConfigTest extends AbstractNetconfOperationTest {
30     @Rule
31     public TemporaryFolder tmpDir = new TemporaryFolder();
32
33     @Test
34     public void testTargetMissing() throws Exception {
35         try {
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);
42         }
43     }
44
45     @Test
46     public void testSourceMissing() throws Exception {
47         try {
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);
54         }
55     }
56
57     @Test
58     public void testConfigMissing() throws Exception {
59         try {
60             copyConfig("messages/mapping/copyConfigs/copyConfig_no_config.xml");
61             fail("Should have failed - neither <config> nor <url> element is present");
62         } catch (final DocumentedException e) {
63             assertTrue(e.getErrorSeverity() == ErrorSeverity.ERROR);
64             assertTrue(e.getErrorTag() == ErrorTag.MISSING_ELEMENT);
65             assertTrue(e.getErrorType() == ErrorType.PROTOCOL);
66         }
67     }
68
69     @Test
70     public void testRunning() throws Exception {
71         try {
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);
78         }
79     }
80
81     @Test
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());
87
88         verifyResponse(discardChanges(), RPC_REPLY_OK);
89         assertEmptyDatastore(getConfigCandidate());
90     }
91
92     @Test
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);
98
99         verifyResponse(commit(), RPC_REPLY_OK);
100         verifyResponse(getConfigRunning(), expectedConfig);
101     }
102
103     @Test
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"));
110
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"));
116     }
117
118     @Test
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"));
124
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"));
129     }
130
131     @Test
132     public void testOrderedList() throws Exception {
133         verifyResponse(copyConfig("messages/mapping/copyConfigs/copyConfig_ordered_list_setup.xml"),
134             RPC_REPLY_OK);
135         verifyResponse(commit(), RPC_REPLY_OK);
136         verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument(
137             "messages/mapping/copyConfigs/copyConfig_ordered_list_setup_control.xml"));
138
139         verifyResponse(copyConfig("messages/mapping/copyConfigs/copyConfig_ordered_list_update.xml"),
140             RPC_REPLY_OK);
141         verifyResponse(commit(), RPC_REPLY_OK);
142         verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument(
143             "messages/mapping/copyConfigs/copyConfig_ordered_list_update_control.xml"));
144     }
145
146     @Test
147     public void testToplevelList() throws Exception {
148         verifyResponse(copyConfig("messages/mapping/copyConfigs/copyConfig_toplevel_list_setup.xml"),
149             RPC_REPLY_OK);
150         verifyResponse(commit(), RPC_REPLY_OK);
151         verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument(
152             "messages/mapping/copyConfigs/copyConfig_toplevel_list_setup_control.xml"));
153
154         verifyResponse(copyConfig("messages/mapping/copyConfigs/copyConfig_toplevel_list_update.xml"),
155             RPC_REPLY_OK);
156         verifyResponse(commit(), RPC_REPLY_OK);
157         verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument(
158             "messages/mapping/copyConfigs/copyConfig_toplevel_list_update_control.xml"));
159     }
160
161     @Test
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"),
165             RPC_REPLY_OK);
166         verifyResponse(commit(), RPC_REPLY_OK);
167         verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument(
168             "messages/mapping/copyConfigs/copyConfig_empty_container_control.xml"));
169     }
170
171     @Test
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"),
175             RPC_REPLY_OK);
176         verifyResponse(commit(), RPC_REPLY_OK);
177         verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument(
178             "messages/mapping/copyConfigs/copyConfig_empty_presence_container_control.xml"));
179     }
180
181     @Test
182     public void testAugmentations() throws Exception {
183         verifyResponse(copyConfig("messages/mapping/copyConfigs/copyConfig_top_augmentation.xml"),
184             RPC_REPLY_OK);
185         verifyResponse(commit(), RPC_REPLY_OK);
186         verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument(
187             "messages/mapping/copyConfigs/copyConfig_top_augmentation_control.xml"));
188     }
189
190     @Test
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"));
202     }
203
204     @Test
205     public void testConfigFromFile() throws Exception {
206         // Ask class loader for URI of config file and use it as <url> in <copy-config> RPC:
207         final String template = XmlFileLoader.fileToString("messages/mapping/copyConfigs/copyConfig_from_file.xml");
208         final URI uri = getClass().getClassLoader()
209             .getResource("messages/mapping/copyConfigs/config_file_valid.xml").toURI();
210         final String copyConfig = template.replaceFirst("URL", uri.toString());
211         final Document request = XmlUtil.readXmlToDocument(copyConfig);
212
213         verifyResponse(copyConfig(request), RPC_REPLY_OK);
214         verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument(
215             "messages/mapping/copyConfigs/copyConfig_from_file_control.xml"));
216     }
217
218     @Test
219     public void testConfigFromInvalidUrl() throws Exception {
220         try {
221             copyConfig("messages/mapping/copyConfigs/copyConfig_invalid_url.xml");
222             fail("Should have failed - provided <url> is not valid");
223         } catch (final DocumentedException e) {
224             assertTrue(e.getErrorSeverity() == ErrorSeverity.ERROR);
225             assertTrue(e.getErrorTag() == ErrorTag.INVALID_VALUE);
226             assertTrue(e.getErrorType() == ErrorType.APPLICATION);
227             assertTrue(e.getCause() instanceof MalformedURLException);
228         }
229     }
230
231     @Test
232     public void testExternalConfigInvalid() throws Exception {
233         try {
234             // Ask class loader for URI of config file and use it as <url> in <copy-config> RPC:
235             final String template = XmlFileLoader.fileToString("messages/mapping/copyConfigs/copyConfig_from_file.xml");
236             final URI uri = getClass().getClassLoader()
237                 .getResource("messages/mapping/copyConfigs/config_file_invalid.xml").toURI();
238             final String copyConfig = template.replaceFirst("URL", uri.toString());
239             final Document request = XmlUtil.readXmlToDocument(copyConfig);
240             copyConfig(request);
241             fail("Should have failed - provided config is not valid XML");
242         } catch (final DocumentedException e) {
243             assertTrue(e.getErrorSeverity() == ErrorSeverity.ERROR);
244             assertTrue(e.getErrorTag() == ErrorTag.OPERATION_FAILED);
245             assertTrue(e.getErrorType() == ErrorType.APPLICATION);
246             assertTrue(e.getCause() instanceof SAXException);
247         }
248     }
249
250     @Test
251     public void testCopyToFile() throws Exception {
252         // Initialize config:
253         verifyResponse(copyConfig("messages/mapping/copyConfigs/copyConfig_top_modules.xml"), RPC_REPLY_OK);
254         verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument(
255             "messages/mapping/copyConfigs/copyConfig_top_modules_control.xml"));
256
257         // Load copy-config template and replace URL with the URI of target file:
258         final String template = XmlFileLoader.fileToString("messages/mapping/copyConfigs/copyConfig_to_file.xml");
259         final File outFile = new File(tmpDir.getRoot(),"test-copy-to-file.xml");
260         final String copyConfig = template.replaceFirst("URL", outFile.toURI().toString());
261         final Document request = XmlUtil.readXmlToDocument(copyConfig);
262
263         // Invoke copy-config RPC:
264         verifyResponse(copyConfig(request), RPC_REPLY_OK);
265
266         // Check if outFile was created with expected content:
267         verifyResponse(XmlUtil.readXmlToDocument(new FileInputStream(outFile)),
268             XmlFileLoader.xmlFileToDocument("messages/mapping/copyConfigs/copyConfig_to_file_control.xml"));
269     }
270
271     @Test
272     public void testUnsupportedTargetUrlProtocol() throws Exception {
273         try {
274             copyConfig("messages/mapping/copyConfigs/copyConfig_to_unsupported_url_protocol.xml");
275             fail("Should have failed - exporting config to http server is not supported");
276         } catch (final DocumentedException e) {
277             assertTrue(e.getErrorSeverity() == ErrorSeverity.ERROR);
278             assertTrue(e.getErrorTag() == ErrorTag.OPERATION_NOT_SUPPORTED);
279             assertTrue(e.getErrorType() == ErrorType.PROTOCOL);
280         }
281     }
282
283     @Test
284     public void testCopyToFileFromRunning() throws Exception {
285         // Load copy-config template and replace URL with the URI of target file:
286         final String template =
287             XmlFileLoader.fileToString("messages/mapping/copyConfigs/copyConfig_to_file_from_running.xml");
288         final File outFile = new File(tmpDir.getRoot(),"test-copy-to-file-from-running.xml");
289         final String copyConfig = template.replaceFirst("URL", outFile.toURI().toString());
290         final Document request = XmlUtil.readXmlToDocument(copyConfig);
291
292         // Invoke copy-config RPC:
293         verifyResponse(copyConfig(request), RPC_REPLY_OK);
294
295         // Check if outFile was created with expected content:
296         verifyResponse(XmlUtil.readXmlToDocument(new FileInputStream(outFile)),
297             XmlFileLoader.xmlFileToDocument(
298                 "messages/mapping/copyConfigs/copyConfig_to_file_from_running_control.xml"));
299
300     }
301
302     @Test
303     public void testRemoteToRemoteOperationIsNotSupported() throws Exception {
304         try {
305             copyConfig("messages/mapping/copyConfigs/copyConfig_url_remote_to_remote.xml");
306             fail("Should have failed - remote to remote operations are not supported");
307         } catch (final DocumentedException e) {
308             assertTrue(e.getErrorSeverity() == ErrorSeverity.ERROR);
309             assertTrue(e.getErrorTag() == ErrorTag.OPERATION_NOT_SUPPORTED);
310             assertTrue(e.getErrorType() == ErrorType.PROTOCOL);
311         }
312     }
313
314     private Document copyConfig(final String resource) throws Exception {
315         final CopyConfig copyConfig = new CopyConfig(SESSION_ID_FOR_REPORTING, getCurrentSchemaContext(),
316             getTransactionProvider());
317         return executeOperation(copyConfig, resource);
318     }
319
320     private Document copyConfig(final Document request) throws Exception {
321         final CopyConfig copyConfig = new CopyConfig(SESSION_ID_FOR_REPORTING, getCurrentSchemaContext(),
322             getTransactionProvider());
323         return executeOperation(copyConfig, request);
324     }
325 }