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.*;
23 public class ValidateTest {
25 public static final String NETCONF_SESSION_ID_FOR_REPORTING = "foo";
27 @Test(expected = NetconfDocumentedException.class)
28 public void test() throws Exception {
29 final XmlElement xml = XmlElement.fromString("<abc></abc>");
30 final Validate validate = new Validate(null, null, NETCONF_SESSION_ID_FOR_REPORTING);
31 validate.handle(null, xml);
34 @Test(expected = NetconfDocumentedException.class)
35 public void testNoSource() throws Exception {
36 final XmlElement xml = XmlElement.fromString("<validate xmlns=\""
37 + XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0 + "\"/>");
38 final Validate validate = new Validate(null, null, NETCONF_SESSION_ID_FOR_REPORTING);
39 validate.handle(null, xml);
42 @Test(expected = NetconfDocumentedException.class)
43 public void testNoNamespace() throws Exception {
44 final XmlElement xml = XmlElement.fromString("<validate/>");
45 final Validate validate = new Validate(null, null, NETCONF_SESSION_ID_FOR_REPORTING);
46 validate.handle(null, xml);
49 @Test(expected = NetconfDocumentedException.class)
50 public void testRunningSource() throws Exception {
52 final XmlElement xml = XmlElement.fromString("<validate xmlns=\""
53 + XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0
54 + "\"><source><running></running></source></validate>");
55 final Validate validate = new Validate(null, null, NETCONF_SESSION_ID_FOR_REPORTING);
56 validate.handle(null, xml);
59 @Test(expected = NetconfDocumentedException.class)
60 public void testNoTransaction() throws Exception {
61 final XmlElement xml = XmlElement.fromString("<validate xmlns=\""
62 + XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0
63 + "\"><source><candidate/></source></validate>");
64 final TransactionProvider transactionProvider = mock(TransactionProvider.class);
65 doThrow(IllegalStateException.class).when(transactionProvider).validateTransaction();
66 final Validate validate = new Validate(transactionProvider, null, NETCONF_SESSION_ID_FOR_REPORTING);
67 validate.handle(null, xml);
70 @Test(expected = NetconfDocumentedException.class)
71 public void testValidationException() throws Exception {
72 final XmlElement xml = XmlElement.fromString("<validate xmlns=\""
73 + XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0
74 + "\">><source><candidate/></source></validate>");
75 final TransactionProvider transactionProvider = mock(TransactionProvider.class);
76 doThrow(ValidationException.class).when(transactionProvider).validateTransaction();
77 final Validate validate = new Validate(transactionProvider, null, NETCONF_SESSION_ID_FOR_REPORTING);
78 validate.handle(null, xml);
82 public void testValidation() throws Exception {
83 final XmlElement xml = XmlElement.fromString("<validate xmlns=\""
84 + XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0
85 + "\"><source><candidate/></source></validate>");
86 final TransactionProvider transactionProvider = mock(TransactionProvider.class);
87 final Element okElement = XmlUtil.readXmlToElement("<ok/>");
88 doNothing().when(transactionProvider).validateTransaction();
89 final Validate validate = new Validate(transactionProvider, null, NETCONF_SESSION_ID_FOR_REPORTING);
90 Element ok = validate.handle(XmlUtil.newDocument(), xml);
91 assertEquals(XmlUtil.toString(okElement), XmlUtil.toString(ok));