Improve NETCONF session ID handling
[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.test.util.XmlFileLoader;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.base._1._0.rev110601.SessionIdType;
23 import org.opendaylight.yangtools.yang.common.Uint32;
24 import org.w3c.dom.Document;
25 import org.w3c.dom.Element;
26 import org.xml.sax.SAXException;
27
28 public class AbstractNetconfOperationTest {
29     private static class NetconfOperationImpl extends AbstractNetconfOperation {
30         public boolean handleRun;
31
32         NetconfOperationImpl(final SessionIdType sessionId) {
33             super(sessionId);
34         }
35
36         @Override
37         protected String getOperationName() {
38             return null;
39         }
40
41         @Override
42         protected Element handle(final Document document, final XmlElement message,
43                 final NetconfOperationChainedExecution subsequentOperation) throws DocumentedException {
44             handleRun = true;
45             try {
46                 return XmlUtil.readXmlToElement("<element/>");
47             } catch (SAXException | IOException e) {
48                 throw DocumentedException.wrap(e);
49             }
50         }
51     }
52
53     private final NetconfOperationImpl netconfOperation = new NetconfOperationImpl(new SessionIdType(Uint32.ONE));
54     private NetconfOperationChainedExecution operation;
55
56     @Before
57     public void setUp() throws Exception {
58         operation = mock(NetconfOperationChainedExecution.class);
59     }
60
61     @Test
62     public void testAbstractNetconfOperation() throws Exception {
63         Document helloMessage = XmlFileLoader.xmlFileToDocument("netconfMessages/edit_config.xml");
64         assertEquals(new SessionIdType(Uint32.ONE), netconfOperation.sessionId());
65         assertNotNull(netconfOperation.canHandle(helloMessage));
66         assertEquals(HandlingPriority.HANDLE_WITH_DEFAULT_PRIORITY, netconfOperation.getHandlingPriority());
67
68         netconfOperation.handle(helloMessage, operation);
69         assertTrue(netconfOperation.handleRun);
70     }
71 }