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