Move SendErrorExceptionUtil
[netconf.git] / netconf / netconf-util / src / test / java / org / opendaylight / netconf / util / mapping / AbstractNetconfOperationTest.java
1 /*
2  * Copyright (c) 2014 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.netconf.util.mapping;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertTrue;
14 import static org.mockito.Mockito.mock;
15
16 import java.io.IOException;
17 import org.junit.Before;
18 import org.junit.Test;
19 import org.opendaylight.netconf.api.DocumentedException;
20 import org.opendaylight.netconf.api.xml.XmlElement;
21 import org.opendaylight.netconf.api.xml.XmlUtil;
22 import org.opendaylight.netconf.mapping.api.HandlingPriority;
23 import org.opendaylight.netconf.mapping.api.NetconfOperationChainedExecution;
24 import org.opendaylight.netconf.util.test.XmlFileLoader;
25 import org.w3c.dom.Document;
26 import org.w3c.dom.Element;
27 import org.xml.sax.SAXException;
28
29 public class AbstractNetconfOperationTest {
30     static class NetconfOperationImpl extends AbstractNetconfOperation {
31         public boolean handleRun;
32
33         protected NetconfOperationImpl(final String netconfSessionIdForReporting) {
34             super(netconfSessionIdForReporting);
35             this.handleRun = false;
36         }
37
38         @Override
39         protected String getOperationName() {
40             return null;
41         }
42
43         @Override
44         protected Element handle(final Document document, final XmlElement message,
45                 final NetconfOperationChainedExecution subsequentOperation) throws DocumentedException {
46             this.handleRun = true;
47             try {
48                 return XmlUtil.readXmlToElement("<element/>");
49             } catch (SAXException | IOException e) {
50                 throw DocumentedException.wrap(e);
51             }
52         }
53     }
54
55     private final NetconfOperationImpl netconfOperation = new NetconfOperationImpl("str");
56     private NetconfOperationChainedExecution operation;
57
58     @Before
59     public void setUp() throws Exception {
60         operation = mock(NetconfOperationChainedExecution.class);
61     }
62
63     @Test
64     public void testAbstractNetconfOperation() throws Exception {
65         Document helloMessage = XmlFileLoader.xmlFileToDocument("netconfMessages/edit_config.xml");
66         assertEquals(netconfOperation.getNetconfSessionIdForReporting(), "str");
67         assertNotNull(netconfOperation.canHandle(helloMessage));
68         assertEquals(netconfOperation.getHandlingPriority(), HandlingPriority.HANDLE_WITH_DEFAULT_PRIORITY);
69
70         netconfOperation.handle(helloMessage, operation);
71         assertTrue(netconfOperation.handleRun);
72     }
73 }