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