Merge "Added feature for sal-mdsal-xsql"
[controller.git] / opendaylight / netconf / netconf-monitoring / src / test / java / org / opendaylight / controller / netconf / monitoring / GetTest.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.monitoring;
10
11 import static junit.framework.Assert.assertEquals;
12 import static junit.framework.Assert.assertTrue;
13 import static junit.framework.TestCase.fail;
14 import static org.junit.Assert.assertThat;
15 import static org.mockito.Mockito.doReturn;
16 import static org.mockito.Mockito.doThrow;
17
18 import java.util.Collections;
19 import org.hamcrest.CoreMatchers;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.mockito.Mock;
23 import org.mockito.MockitoAnnotations;
24 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
25 import org.opendaylight.controller.netconf.api.monitoring.NetconfMonitoringService;
26 import org.opendaylight.controller.netconf.mapping.api.HandlingPriority;
27 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationChainedExecution;
28 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.SchemasBuilder;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.SessionsBuilder;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.schemas.Schema;
32 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.sessions.Session;
33 import org.w3c.dom.Document;
34
35 public class GetTest {
36
37     @Mock
38     private NetconfMonitoringService monitor;
39     @Mock
40     private Document request;
41     @Mock
42     private NetconfOperationChainedExecution subsequentOperation;
43     private Document incorrectSubsequentResult;
44     private Document correctSubsequentResult;
45
46     private Get get;
47
48     @Before
49     public void setUp() throws Exception {
50         MockitoAnnotations.initMocks(this);
51
52         incorrectSubsequentResult = XmlUtil.readXmlToDocument("<rpc-reply xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"/>");
53         correctSubsequentResult = XmlUtil.readXmlToDocument("<rpc-reply xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"><data></data></rpc-reply>");
54
55         doReturn(new SessionsBuilder().setSession(Collections.<Session>emptyList()).build()).when(monitor).getSessions();
56         doReturn(new SchemasBuilder().setSchema(Collections.<Schema>emptyList()).build()).when(monitor).getSchemas();
57         doReturn(false).when(subsequentOperation).isExecutionTermination();
58
59         get = new Get(monitor);
60     }
61
62     @Test
63     public void testHandleNoSubsequent() throws Exception {
64         try {
65             get.handle(null, NetconfOperationChainedExecution.EXECUTION_TERMINATION_POINT);
66         } catch (final NetconfDocumentedException e) {
67             assertNetconfDocumentedEx(e, NetconfDocumentedException.ErrorSeverity.error, NetconfDocumentedException.ErrorTag.operation_failed, NetconfDocumentedException.ErrorType.application);
68             return;
69         }
70
71         fail("Get should fail without subsequent operation");
72     }
73
74     @Test
75     public void testHandleWrongPlaceholder() throws Exception {
76         doReturn(incorrectSubsequentResult).when(subsequentOperation).execute(request);
77         try {
78             get.handle(request, subsequentOperation);
79         } catch (final NetconfDocumentedException e) {
80             assertNetconfDocumentedEx(e, NetconfDocumentedException.ErrorSeverity.error, NetconfDocumentedException.ErrorTag.invalid_value, NetconfDocumentedException.ErrorType.application);
81             return;
82         }
83
84         fail("Get should fail with wrong xml");
85     }
86
87     @Test
88     public void testHandleRuntimeEx() throws Exception {
89         doThrow(RuntimeException.class).when(subsequentOperation).execute(request);
90         try {
91             get.handle(request, subsequentOperation);
92         } catch (final NetconfDocumentedException e) {
93             assertNetconfDocumentedEx(e, NetconfDocumentedException.ErrorSeverity.error, NetconfDocumentedException.ErrorTag.operation_failed, NetconfDocumentedException.ErrorType.application);
94             assertEquals(1, e.getErrorInfo().size());
95             return;
96         }
97
98         fail("Get should fail with wrong xml");
99     }
100
101     @Test
102     public void testSuccessHandle() throws Exception {
103         doReturn(correctSubsequentResult).when(subsequentOperation).execute(request);
104         assertTrue(get.getHandlingPriority().getPriority().get() > HandlingPriority.HANDLE_WITH_DEFAULT_PRIORITY.getPriority().get());
105         final Document result = get.handle(request, subsequentOperation);
106         assertThat(XmlUtil.toString(result), CoreMatchers.containsString("sessions"));
107         assertThat(XmlUtil.toString(result), CoreMatchers.containsString("schemas"));
108
109     }
110
111     @Test(expected = UnsupportedOperationException.class)
112     public void testHandle() throws Exception {
113         get.handle(null, null, null);
114
115     }
116
117     private void assertNetconfDocumentedEx(final NetconfDocumentedException e, final NetconfDocumentedException.ErrorSeverity severity, final NetconfDocumentedException.ErrorTag errorTag, final NetconfDocumentedException.ErrorType type) {
118         assertEquals(severity, e.getErrorSeverity());
119         assertEquals(errorTag, e.getErrorTag());
120         assertEquals(type, e.getErrorType());
121     }
122 }