2 * Copyright (c) 2013 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.controller.netconf.confignetconfconnector.operations;
11 import org.junit.Test;
12 import org.opendaylight.controller.config.api.ValidationException;
13 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
14 import org.opendaylight.controller.netconf.confignetconfconnector.transactions.TransactionProvider;
15 import org.opendaylight.controller.netconf.util.xml.XmlElement;
16 import org.opendaylight.controller.netconf.util.xml.XmlNetconfConstants;
17 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
18 import org.w3c.dom.Element;
20 import static org.junit.Assert.assertEquals;
21 import static org.mockito.Mockito.doNothing;
22 import static org.mockito.Mockito.doThrow;
23 import static org.mockito.Mockito.mock;
25 public class ValidateTest {
27 public static final String NETCONF_SESSION_ID_FOR_REPORTING = "foo";
29 @Test(expected = NetconfDocumentedException.class)
30 public void test() throws Exception {
31 final XmlElement xml = XmlElement.fromString("<abc></abc>");
32 final Validate validate = new Validate(null, null, NETCONF_SESSION_ID_FOR_REPORTING);
33 validate.handle(null, xml);
36 @Test(expected = NetconfDocumentedException.class)
37 public void testNoSource() throws Exception {
38 final XmlElement xml = XmlElement.fromString("<validate xmlns=\""
39 + XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0 + "\"/>");
40 final Validate validate = new Validate(null, null, NETCONF_SESSION_ID_FOR_REPORTING);
41 validate.handle(null, xml);
44 @Test(expected = NetconfDocumentedException.class)
45 public void testNoNamespace() throws Exception {
46 final XmlElement xml = XmlElement.fromString("<validate/>");
47 final Validate validate = new Validate(null, null, NETCONF_SESSION_ID_FOR_REPORTING);
48 validate.handle(null, xml);
51 @Test(expected = NetconfDocumentedException.class)
52 public void testRunningSource() throws Exception {
54 final XmlElement xml = XmlElement.fromString("<validate xmlns=\""
55 + XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0
56 + "\"><source><running></running></source></validate>");
57 final Validate validate = new Validate(null, null, NETCONF_SESSION_ID_FOR_REPORTING);
58 validate.handle(null, xml);
61 @Test(expected = NetconfDocumentedException.class)
62 public void testNoTransaction() throws Exception {
63 final XmlElement xml = XmlElement.fromString("<validate xmlns=\""
64 + XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0
65 + "\"><source><candidate/></source></validate>");
66 final TransactionProvider transactionProvider = mock(TransactionProvider.class);
67 doThrow(IllegalStateException.class).when(transactionProvider).validateTransaction();
68 final Validate validate = new Validate(transactionProvider, null, NETCONF_SESSION_ID_FOR_REPORTING);
69 validate.handle(null, xml);
72 @Test(expected = NetconfDocumentedException.class)
73 public void testValidationException() throws Exception {
74 final XmlElement xml = XmlElement.fromString("<validate xmlns=\""
75 + XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0
76 + "\">><source><candidate/></source></validate>");
77 final TransactionProvider transactionProvider = mock(TransactionProvider.class);
78 doThrow(ValidationException.class).when(transactionProvider).validateTransaction();
79 final Validate validate = new Validate(transactionProvider, null, NETCONF_SESSION_ID_FOR_REPORTING);
80 validate.handle(null, xml);
84 public void testValidation() throws Exception {
85 final XmlElement xml = XmlElement.fromString("<validate xmlns=\""
86 + XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0
87 + "\"><source><candidate/></source></validate>");
88 final TransactionProvider transactionProvider = mock(TransactionProvider.class);
89 final Element okElement = XmlUtil.readXmlToElement("<ok/>");
90 doNothing().when(transactionProvider).validateTransaction();
91 final Validate validate = new Validate(transactionProvider, null, NETCONF_SESSION_ID_FOR_REPORTING);
92 Element ok = validate.handle(XmlUtil.newDocument(), xml);
93 assertEquals(XmlUtil.toString(okElement), XmlUtil.toString(ok));