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