Merge "Take advantage of MultipartTransactionAware"
[controller.git] / opendaylight / netconf / config-netconf-connector / src / test / java / org / opendaylight / controller / netconf / confignetconfconnector / operations / ValidateTest.java
1 /*
2  * Copyright (c) 2013 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
9 package org.opendaylight.controller.netconf.confignetconfconnector.operations;
10
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;
19
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;
24
25 public class ValidateTest {
26
27     public static final String NETCONF_SESSION_ID_FOR_REPORTING = "foo";
28
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.handleWithNoSubsequentOperations(null, xml);
34     }
35
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.handleWithNoSubsequentOperations(null, xml);
42     }
43
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.handleWithNoSubsequentOperations(null, xml);
49     }
50
51     @Test(expected = NetconfDocumentedException.class)
52     public void testRunningSource() throws Exception {
53
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.handleWithNoSubsequentOperations(null, xml);
59     }
60
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.handleWithNoSubsequentOperations(null, xml);
70     }
71
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.handleWithNoSubsequentOperations(null, xml);
81     }
82
83     @Test
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.handleWithNoSubsequentOperations(XmlUtil.newDocument(), xml);
93         assertEquals(XmlUtil.toString(okElement), XmlUtil.toString(ok));
94     }
95
96 }