* terms of the Eclipse Public License v1.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/epl-v10.html
*/
-
package org.opendaylight.netconf.mdsal.connector.ops;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
-import static org.opendaylight.yangtools.yang.test.util.YangParserTestUtils.parseYangResources;
+import java.io.File;
+import java.io.FileInputStream;
+import java.net.MalformedURLException;
+import java.net.URI;
+import org.junit.Rule;
import org.junit.Test;
-import org.opendaylight.controller.config.util.xml.DocumentedException;
-import org.opendaylight.controller.config.util.xml.DocumentedException.ErrorSeverity;
-import org.opendaylight.controller.config.util.xml.DocumentedException.ErrorTag;
-import org.opendaylight.controller.config.util.xml.DocumentedException.ErrorType;
+import org.junit.rules.TemporaryFolder;
+import org.opendaylight.netconf.api.DocumentedException;
+import org.opendaylight.netconf.api.xml.XmlUtil;
import org.opendaylight.netconf.util.test.XmlFileLoader;
-import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+import org.opendaylight.yangtools.yang.common.ErrorSeverity;
+import org.opendaylight.yangtools.yang.common.ErrorTag;
+import org.opendaylight.yangtools.yang.common.ErrorType;
import org.w3c.dom.Document;
+import org.xml.sax.SAXException;
public class CopyConfigTest extends AbstractNetconfOperationTest {
-
- @Override
- protected SchemaContext getSchemaContext() {
- return parseYangResources(CopyConfigTest.class,
- "/yang/mdsal-netconf-mapping-test.yang");
- }
-
+ @Rule
+ public TemporaryFolder tmpDir = new TemporaryFolder();
@Test
public void testTargetMissing() throws Exception {
copyConfig("messages/mapping/copyConfigs/copyConfig_no_target.xml");
fail("Should have failed - <target> element is missing");
} catch (final DocumentedException e) {
+ // FIXME: use assertThrows() and assertEquals()
assertTrue(e.getErrorSeverity() == ErrorSeverity.ERROR);
assertTrue(e.getErrorTag() == ErrorTag.MISSING_ATTRIBUTE);
assertTrue(e.getErrorType() == ErrorType.PROTOCOL);
public void testConfigMissing() throws Exception {
try {
copyConfig("messages/mapping/copyConfigs/copyConfig_no_config.xml");
- fail("Should have failed - <config> element is missing");
+ fail("Should have failed - neither <config> nor <url> element is present");
} catch (final DocumentedException e) {
assertTrue(e.getErrorSeverity() == ErrorSeverity.ERROR);
assertTrue(e.getErrorTag() == ErrorTag.MISSING_ELEMENT);
"messages/mapping/copyConfigs/copyConfig_choices_control.xml"));
}
+ @Test
+ public void testConfigFromFile() throws Exception {
+ // Ask class loader for URI of config file and use it as <url> in <copy-config> RPC:
+ final String template = XmlFileLoader.fileToString("messages/mapping/copyConfigs/copyConfig_from_file.xml");
+ final URI uri = getClass().getClassLoader()
+ .getResource("messages/mapping/copyConfigs/config_file_valid.xml").toURI();
+ final String copyConfig = template.replaceFirst("URL", uri.toString());
+ final Document request = XmlUtil.readXmlToDocument(copyConfig);
+
+ verifyResponse(copyConfig(request), RPC_REPLY_OK);
+ verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument(
+ "messages/mapping/copyConfigs/copyConfig_from_file_control.xml"));
+ }
+
+ @Test
+ public void testConfigFromInvalidUrl() throws Exception {
+ try {
+ copyConfig("messages/mapping/copyConfigs/copyConfig_invalid_url.xml");
+ fail("Should have failed - provided <url> is not valid");
+ } catch (final DocumentedException e) {
+ assertTrue(e.getErrorSeverity() == ErrorSeverity.ERROR);
+ assertTrue(e.getErrorTag() == ErrorTag.INVALID_VALUE);
+ assertTrue(e.getErrorType() == ErrorType.APPLICATION);
+ assertTrue(e.getCause() instanceof MalformedURLException);
+ }
+ }
+
+ @Test
+ public void testExternalConfigInvalid() throws Exception {
+ try {
+ // Ask class loader for URI of config file and use it as <url> in <copy-config> RPC:
+ final String template = XmlFileLoader.fileToString("messages/mapping/copyConfigs/copyConfig_from_file.xml");
+ final URI uri = getClass().getClassLoader()
+ .getResource("messages/mapping/copyConfigs/config_file_invalid.xml").toURI();
+ final String copyConfig = template.replaceFirst("URL", uri.toString());
+ final Document request = XmlUtil.readXmlToDocument(copyConfig);
+ copyConfig(request);
+ fail("Should have failed - provided config is not valid XML");
+ } catch (final DocumentedException e) {
+ assertTrue(e.getErrorSeverity() == ErrorSeverity.ERROR);
+ assertTrue(e.getErrorTag() == ErrorTag.OPERATION_FAILED);
+ assertTrue(e.getErrorType() == ErrorType.APPLICATION);
+ assertTrue(e.getCause() instanceof SAXException);
+ }
+ }
+
+ @Test
+ public void testCopyToFile() throws Exception {
+ // Initialize config:
+ verifyResponse(copyConfig("messages/mapping/copyConfigs/copyConfig_top_modules.xml"), RPC_REPLY_OK);
+ verifyResponse(getConfigCandidate(), XmlFileLoader.xmlFileToDocument(
+ "messages/mapping/copyConfigs/copyConfig_top_modules_control.xml"));
+
+ // Load copy-config template and replace URL with the URI of target file:
+ final String template = XmlFileLoader.fileToString("messages/mapping/copyConfigs/copyConfig_to_file.xml");
+ final File outFile = new File(tmpDir.getRoot(),"test-copy-to-file.xml");
+ final String copyConfig = template.replaceFirst("URL", outFile.toURI().toString());
+ final Document request = XmlUtil.readXmlToDocument(copyConfig);
+
+ // Invoke copy-config RPC:
+ verifyResponse(copyConfig(request), RPC_REPLY_OK);
+
+ // Check if outFile was created with expected content:
+ verifyResponse(XmlUtil.readXmlToDocument(new FileInputStream(outFile)),
+ XmlFileLoader.xmlFileToDocument("messages/mapping/copyConfigs/copyConfig_to_file_control.xml"));
+ }
+
+ @Test
+ public void testUnsupportedTargetUrlProtocol() throws Exception {
+ try {
+ copyConfig("messages/mapping/copyConfigs/copyConfig_to_unsupported_url_protocol.xml");
+ fail("Should have failed - exporting config to http server is not supported");
+ } catch (final DocumentedException e) {
+ assertTrue(e.getErrorSeverity() == ErrorSeverity.ERROR);
+ assertTrue(e.getErrorTag() == ErrorTag.OPERATION_NOT_SUPPORTED);
+ assertTrue(e.getErrorType() == ErrorType.PROTOCOL);
+ }
+ }
+
+ @Test
+ public void testCopyToFileFromRunning() throws Exception {
+ // Load copy-config template and replace URL with the URI of target file:
+ final String template =
+ XmlFileLoader.fileToString("messages/mapping/copyConfigs/copyConfig_to_file_from_running.xml");
+ final File outFile = new File(tmpDir.getRoot(),"test-copy-to-file-from-running.xml");
+ final String copyConfig = template.replaceFirst("URL", outFile.toURI().toString());
+ final Document request = XmlUtil.readXmlToDocument(copyConfig);
+
+ // Invoke copy-config RPC:
+ verifyResponse(copyConfig(request), RPC_REPLY_OK);
+
+ // Check if outFile was created with expected content:
+ verifyResponse(XmlUtil.readXmlToDocument(new FileInputStream(outFile)),
+ XmlFileLoader.xmlFileToDocument(
+ "messages/mapping/copyConfigs/copyConfig_to_file_from_running_control.xml"));
+
+ }
+
+ @Test
+ public void testRemoteToRemoteOperationIsNotSupported() throws Exception {
+ try {
+ copyConfig("messages/mapping/copyConfigs/copyConfig_url_remote_to_remote.xml");
+ fail("Should have failed - remote to remote operations are not supported");
+ } catch (final DocumentedException e) {
+ assertTrue(e.getErrorSeverity() == ErrorSeverity.ERROR);
+ assertTrue(e.getErrorTag() == ErrorTag.OPERATION_NOT_SUPPORTED);
+ assertTrue(e.getErrorType() == ErrorType.PROTOCOL);
+ }
+ }
+
private Document copyConfig(final String resource) throws Exception {
final CopyConfig copyConfig = new CopyConfig(SESSION_ID_FOR_REPORTING, getCurrentSchemaContext(),
getTransactionProvider());
return executeOperation(copyConfig, resource);
}
-}
\ No newline at end of file
+
+ private Document copyConfig(final Document request) throws Exception {
+ final CopyConfig copyConfig = new CopyConfig(SESSION_ID_FOR_REPORTING, getCurrentSchemaContext(),
+ getTransactionProvider());
+ return executeOperation(copyConfig, request);
+ }
+}