Remove DocumentedException.ErrorSeverity
[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.ErrorTag;
22 import org.opendaylight.netconf.api.DocumentedException.ErrorType;
23 import org.opendaylight.netconf.api.xml.XmlUtil;
24 import org.opendaylight.netconf.util.test.XmlFileLoader;
25 import org.opendaylight.yangtools.yang.common.ErrorSeverity;
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             // FIXME: use assertThrows() and assertEquals()
40             assertTrue(e.getErrorSeverity() == ErrorSeverity.ERROR);
41             assertTrue(e.getErrorTag() == ErrorTag.MISSING_ATTRIBUTE);
42             assertTrue(e.getErrorType() == ErrorType.PROTOCOL);
43         }
44     }
45
46     @Test
47     public void testSourceMissing() throws Exception {
48         try {
49             copyConfig("messages/mapping/copyConfigs/copyConfig_no_source.xml");
50             fail("Should have fanode1iled - <source> element is missing");
51         } catch (final DocumentedException e) {
52             assertTrue(e.getErrorSeverity() == ErrorSeverity.ERROR);
53             assertTrue(e.getErrorTag() == ErrorTag.MISSING_ELEMENT);
54             assertTrue(e.getErrorType() == ErrorType.PROTOCOL);
55         }
56     }
57
58     @Test
59     public void testConfigMissing() throws Exception {
60         try {
61             copyConfig("messages/mapping/copyConfigs/copyConfig_no_config.xml");
62             fail("Should have failed - neither <config> nor <url> element is present");
63         } catch (final DocumentedException e) {
64             assertTrue(e.getErrorSeverity() == ErrorSeverity.ERROR);
65             assertTrue(e.getErrorTag() == ErrorTag.MISSING_ELEMENT);
66             assertTrue(e.getErrorType() == ErrorType.PROTOCOL);
67         }
68     }
69
70     @Test
71     public void testRunning() throws Exception {
72         try {
73             copyConfig("messages/mapping/copyConfigs/copyConfig_running.xml");
74             fail("Should have failed - copy config on running datastore is not supported");
75         } catch (final DocumentedException e) {
76             assertTrue(e.getErrorSeverity() == ErrorSeverity.ERROR);
77             assertTrue(e.getErrorTag() == ErrorTag.OPERATION_NOT_SUPPORTED);
78             assertTrue(e.getErrorType() == ErrorType.PROTOCOL);
79         }
80     }
81
82     @Test
83     public void testCandidateTransaction() throws Exception {
84         verifyResponse(copyConfig("messages/mapping/copyConfigs/copyConfig_top_modules.xml"), RPC_REPLY_OK);
85         verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument(
86             "messages/mapping/copyConfigs/copyConfig_top_modules_control.xml"));
87         assertEmptyDatastore(getConfigRunning());
88
89         verifyResponse(discardChanges(), RPC_REPLY_OK);
90         assertEmptyDatastore(getConfigCandidate());
91     }
92
93     @Test
94     public void testWithCommit() throws Exception {
95         verifyResponse(copyConfig("messages/mapping/copyConfigs/copyConfig_top_modules.xml"), RPC_REPLY_OK);
96         final Document expectedConfig = XmlFileLoader.xmlFileToDocument(
97             "messages/mapping/copyConfigs/copyConfig_top_modules_control.xml");
98         verifyResponse(getConfigCandidate(), expectedConfig);
99
100         verifyResponse(commit(), RPC_REPLY_OK);
101         verifyResponse(getConfigRunning(), expectedConfig);
102     }
103
104     @Test
105     public void testDeleteSubtree() throws Exception {
106         // Initialize datastore
107         verifyResponse(copyConfig("messages/mapping/copyConfigs/copyConfig_delete_setup.xml"), RPC_REPLY_OK);
108         verifyResponse(commit(), RPC_REPLY_OK);
109         verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument(
110             "messages/mapping/copyConfigs/copyConfig_delete_setup_control.xml"));
111
112         // Issue second copy-config, this time without top container
113         verifyResponse(copyConfig("messages/mapping/copyConfigs/copyConfig_delete.xml"), RPC_REPLY_OK);
114         verifyResponse(commit(), RPC_REPLY_OK);
115         verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument(
116             "messages/mapping/copyConfigs/copyConfig_delete_control.xml"));
117     }
118
119     @Test
120     public void testList() throws Exception {
121         verifyResponse(copyConfig("messages/mapping/copyConfigs/copyConfig_list_setup.xml"), RPC_REPLY_OK);
122         verifyResponse(commit(), RPC_REPLY_OK);
123         verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument(
124             "messages/mapping/copyConfigs/copyConfig_list_setup_control.xml"));
125
126         verifyResponse(copyConfig("messages/mapping/copyConfigs/copyConfig_list_update.xml"), RPC_REPLY_OK);
127         verifyResponse(commit(), RPC_REPLY_OK);
128         verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument(
129             "messages/mapping/copyConfigs/copyConfig_list_update_control.xml"));
130     }
131
132     @Test
133     public void testOrderedList() throws Exception {
134         verifyResponse(copyConfig("messages/mapping/copyConfigs/copyConfig_ordered_list_setup.xml"),
135             RPC_REPLY_OK);
136         verifyResponse(commit(), RPC_REPLY_OK);
137         verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument(
138             "messages/mapping/copyConfigs/copyConfig_ordered_list_setup_control.xml"));
139
140         verifyResponse(copyConfig("messages/mapping/copyConfigs/copyConfig_ordered_list_update.xml"),
141             RPC_REPLY_OK);
142         verifyResponse(commit(), RPC_REPLY_OK);
143         verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument(
144             "messages/mapping/copyConfigs/copyConfig_ordered_list_update_control.xml"));
145     }
146
147     @Test
148     public void testToplevelList() throws Exception {
149         verifyResponse(copyConfig("messages/mapping/copyConfigs/copyConfig_toplevel_list_setup.xml"),
150             RPC_REPLY_OK);
151         verifyResponse(commit(), RPC_REPLY_OK);
152         verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument(
153             "messages/mapping/copyConfigs/copyConfig_toplevel_list_setup_control.xml"));
154
155         verifyResponse(copyConfig("messages/mapping/copyConfigs/copyConfig_toplevel_list_update.xml"),
156             RPC_REPLY_OK);
157         verifyResponse(commit(), RPC_REPLY_OK);
158         verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument(
159             "messages/mapping/copyConfigs/copyConfig_toplevel_list_update_control.xml"));
160     }
161
162     @Test
163     public void testEmptyContainer() throws Exception {
164         // Check that empty non-presence container is removed.
165         verifyResponse(copyConfig("messages/mapping/copyConfigs/copyConfig_empty_container.xml"),
166             RPC_REPLY_OK);
167         verifyResponse(commit(), RPC_REPLY_OK);
168         verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument(
169             "messages/mapping/copyConfigs/copyConfig_empty_container_control.xml"));
170     }
171
172     @Test
173     public void testEmptyPresenceContainer() throws Exception {
174         // Check that empty presence container is not removed.
175         verifyResponse(copyConfig("messages/mapping/copyConfigs/copyConfig_empty_presence_container.xml"),
176             RPC_REPLY_OK);
177         verifyResponse(commit(), RPC_REPLY_OK);
178         verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument(
179             "messages/mapping/copyConfigs/copyConfig_empty_presence_container_control.xml"));
180     }
181
182     @Test
183     public void testAugmentations() throws Exception {
184         verifyResponse(copyConfig("messages/mapping/copyConfigs/copyConfig_top_augmentation.xml"),
185             RPC_REPLY_OK);
186         verifyResponse(commit(), RPC_REPLY_OK);
187         verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument(
188             "messages/mapping/copyConfigs/copyConfig_top_augmentation_control.xml"));
189     }
190
191     @Test
192     public void testChoices() throws Exception {
193         verifyResponse(copyConfig("messages/mapping/copyConfigs/copyConfig_choices1.xml"), RPC_REPLY_OK);
194         verifyResponse(commit(), RPC_REPLY_OK);
195         verifyResponse(copyConfig("messages/mapping/copyConfigs/copyConfig_choices2.xml"), RPC_REPLY_OK);
196         verifyResponse(commit(), RPC_REPLY_OK);
197         verifyResponse(copyConfig("messages/mapping/copyConfigs/copyConfig_choices3.xml"), RPC_REPLY_OK);
198         verifyResponse(commit(), RPC_REPLY_OK);
199         verifyResponse(copyConfig("messages/mapping/copyConfigs/copyConfig_choices4.xml"), RPC_REPLY_OK);
200         verifyResponse(commit(), RPC_REPLY_OK);
201         verifyResponse(getConfigRunning(), XmlFileLoader.xmlFileToDocument(
202             "messages/mapping/copyConfigs/copyConfig_choices_control.xml"));
203     }
204
205     @Test
206     public void testConfigFromFile() throws Exception {
207         // Ask class loader for URI of config file and use it as <url> in <copy-config> RPC:
208         final String template = XmlFileLoader.fileToString("messages/mapping/copyConfigs/copyConfig_from_file.xml");
209         final URI uri = getClass().getClassLoader()
210             .getResource("messages/mapping/copyConfigs/config_file_valid.xml").toURI();
211         final String copyConfig = template.replaceFirst("URL", uri.toString());
212         final Document request = XmlUtil.readXmlToDocument(copyConfig);
213
214         verifyResponse(copyConfig(request), RPC_REPLY_OK);
215         verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument(
216             "messages/mapping/copyConfigs/copyConfig_from_file_control.xml"));
217     }
218
219     @Test
220     public void testConfigFromInvalidUrl() throws Exception {
221         try {
222             copyConfig("messages/mapping/copyConfigs/copyConfig_invalid_url.xml");
223             fail("Should have failed - provided <url> is not valid");
224         } catch (final DocumentedException e) {
225             assertTrue(e.getErrorSeverity() == ErrorSeverity.ERROR);
226             assertTrue(e.getErrorTag() == ErrorTag.INVALID_VALUE);
227             assertTrue(e.getErrorType() == ErrorType.APPLICATION);
228             assertTrue(e.getCause() instanceof MalformedURLException);
229         }
230     }
231
232     @Test
233     public void testExternalConfigInvalid() throws Exception {
234         try {
235             // Ask class loader for URI of config file and use it as <url> in <copy-config> RPC:
236             final String template = XmlFileLoader.fileToString("messages/mapping/copyConfigs/copyConfig_from_file.xml");
237             final URI uri = getClass().getClassLoader()
238                 .getResource("messages/mapping/copyConfigs/config_file_invalid.xml").toURI();
239             final String copyConfig = template.replaceFirst("URL", uri.toString());
240             final Document request = XmlUtil.readXmlToDocument(copyConfig);
241             copyConfig(request);
242             fail("Should have failed - provided config is not valid XML");
243         } catch (final DocumentedException e) {
244             assertTrue(e.getErrorSeverity() == ErrorSeverity.ERROR);
245             assertTrue(e.getErrorTag() == ErrorTag.OPERATION_FAILED);
246             assertTrue(e.getErrorType() == ErrorType.APPLICATION);
247             assertTrue(e.getCause() instanceof SAXException);
248         }
249     }
250
251     @Test
252     public void testCopyToFile() throws Exception {
253         // Initialize config:
254         verifyResponse(copyConfig("messages/mapping/copyConfigs/copyConfig_top_modules.xml"), RPC_REPLY_OK);
255         verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument(
256             "messages/mapping/copyConfigs/copyConfig_top_modules_control.xml"));
257
258         // Load copy-config template and replace URL with the URI of target file:
259         final String template = XmlFileLoader.fileToString("messages/mapping/copyConfigs/copyConfig_to_file.xml");
260         final File outFile = new File(tmpDir.getRoot(),"test-copy-to-file.xml");
261         final String copyConfig = template.replaceFirst("URL", outFile.toURI().toString());
262         final Document request = XmlUtil.readXmlToDocument(copyConfig);
263
264         // Invoke copy-config RPC:
265         verifyResponse(copyConfig(request), RPC_REPLY_OK);
266
267         // Check if outFile was created with expected content:
268         verifyResponse(XmlUtil.readXmlToDocument(new FileInputStream(outFile)),
269             XmlFileLoader.xmlFileToDocument("messages/mapping/copyConfigs/copyConfig_to_file_control.xml"));
270     }
271
272     @Test
273     public void testUnsupportedTargetUrlProtocol() throws Exception {
274         try {
275             copyConfig("messages/mapping/copyConfigs/copyConfig_to_unsupported_url_protocol.xml");
276             fail("Should have failed - exporting config to http server is not supported");
277         } catch (final DocumentedException e) {
278             assertTrue(e.getErrorSeverity() == ErrorSeverity.ERROR);
279             assertTrue(e.getErrorTag() == ErrorTag.OPERATION_NOT_SUPPORTED);
280             assertTrue(e.getErrorType() == ErrorType.PROTOCOL);
281         }
282     }
283
284     @Test
285     public void testCopyToFileFromRunning() throws Exception {
286         // Load copy-config template and replace URL with the URI of target file:
287         final String template =
288             XmlFileLoader.fileToString("messages/mapping/copyConfigs/copyConfig_to_file_from_running.xml");
289         final File outFile = new File(tmpDir.getRoot(),"test-copy-to-file-from-running.xml");
290         final String copyConfig = template.replaceFirst("URL", outFile.toURI().toString());
291         final Document request = XmlUtil.readXmlToDocument(copyConfig);
292
293         // Invoke copy-config RPC:
294         verifyResponse(copyConfig(request), RPC_REPLY_OK);
295
296         // Check if outFile was created with expected content:
297         verifyResponse(XmlUtil.readXmlToDocument(new FileInputStream(outFile)),
298             XmlFileLoader.xmlFileToDocument(
299                 "messages/mapping/copyConfigs/copyConfig_to_file_from_running_control.xml"));
300
301     }
302
303     @Test
304     public void testRemoteToRemoteOperationIsNotSupported() throws Exception {
305         try {
306             copyConfig("messages/mapping/copyConfigs/copyConfig_url_remote_to_remote.xml");
307             fail("Should have failed - remote to remote operations are not supported");
308         } catch (final DocumentedException e) {
309             assertTrue(e.getErrorSeverity() == ErrorSeverity.ERROR);
310             assertTrue(e.getErrorTag() == ErrorTag.OPERATION_NOT_SUPPORTED);
311             assertTrue(e.getErrorType() == ErrorType.PROTOCOL);
312         }
313     }
314
315     private Document copyConfig(final String resource) throws Exception {
316         final CopyConfig copyConfig = new CopyConfig(SESSION_ID_FOR_REPORTING, getCurrentSchemaContext(),
317             getTransactionProvider());
318         return executeOperation(copyConfig, resource);
319     }
320
321     private Document copyConfig(final Document request) throws Exception {
322         final CopyConfig copyConfig = new CopyConfig(SESSION_ID_FOR_REPORTING, getCurrentSchemaContext(),
323             getTransactionProvider());
324         return executeOperation(copyConfig, request);
325     }
326 }