Initial code drop of netconf protocol implementation
[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.*;
22
23 public class ValidateTest {
24
25     public static final String NETCONF_SESSION_ID_FOR_REPORTING = "foo";
26
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);
32     }
33
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);
40     }
41
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);
47     }
48
49     @Test(expected = NetconfDocumentedException.class)
50     public void testRunningSource() throws Exception {
51
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);
57     }
58
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);
68     }
69
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);
79     }
80
81     @Test
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));
92     }
93
94 }