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