Remove redundant code constructs
[netconf.git] / netconf / netconf-monitoring / src / test / java / org / opendaylight / 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.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
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.netconf.api.DocumentedException;
25 import org.opendaylight.netconf.api.monitoring.NetconfMonitoringService;
26 import org.opendaylight.netconf.api.xml.XmlUtil;
27 import org.opendaylight.netconf.mapping.api.HandlingPriority;
28 import org.opendaylight.netconf.mapping.api.NetconfOperationChainedExecution;
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.w3c.dom.Document;
32
33 public class GetTest {
34
35     @Mock
36     private NetconfMonitoringService monitor;
37     @Mock
38     private Document request;
39     @Mock
40     private NetconfOperationChainedExecution subsequentOperation;
41     private Document incorrectSubsequentResult;
42     private Document correctSubsequentResult;
43
44     private Get get;
45
46     @Before
47     public void setUp() throws Exception {
48         MockitoAnnotations.initMocks(this);
49
50         incorrectSubsequentResult = XmlUtil.readXmlToDocument("<rpc-reply "
51                 + "xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"/>");
52         correctSubsequentResult = XmlUtil.readXmlToDocument("<rpc-reply xmlns=\"urn:ietf:params:xml:ns:netconf:base:"
53                 + "1.0\"><data></data></rpc-reply>");
54
55         doReturn(new SessionsBuilder().setSession(Collections.emptyList()).build()).when(monitor)
56                 .getSessions();
57         doReturn(new SchemasBuilder().setSchema(Collections.emptyList()).build()).when(monitor).getSchemas();
58         doReturn(false).when(subsequentOperation).isExecutionTermination();
59
60         get = new Get(monitor);
61     }
62
63     @Test
64     public void testHandleNoSubsequent() throws Exception {
65         try {
66             get.handle(null, NetconfOperationChainedExecution.EXECUTION_TERMINATION_POINT);
67         } catch (final DocumentedException e) {
68             assertNetconfDocumentedEx(e, DocumentedException.ErrorSeverity.ERROR,
69                     DocumentedException.ErrorTag.OPERATION_FAILED, DocumentedException.ErrorType.APPLICATION);
70             return;
71         }
72
73         fail("Get should fail without subsequent operation");
74     }
75
76     @Test
77     public void testHandleWrongPlaceholder() throws Exception {
78         doReturn(incorrectSubsequentResult).when(subsequentOperation).execute(request);
79         try {
80             get.handle(request, subsequentOperation);
81         } catch (final DocumentedException e) {
82             assertNetconfDocumentedEx(e, DocumentedException.ErrorSeverity.ERROR,
83                     DocumentedException.ErrorTag.INVALID_VALUE, DocumentedException.ErrorType.APPLICATION);
84             return;
85         }
86
87         fail("Get should fail with wrong xml");
88     }
89
90     @Test
91     public void testHandleRuntimeEx() throws Exception {
92         doThrow(RuntimeException.class).when(subsequentOperation).execute(request);
93         try {
94             get.handle(request, subsequentOperation);
95         } catch (final DocumentedException e) {
96             assertNetconfDocumentedEx(e, DocumentedException.ErrorSeverity.ERROR,
97                     DocumentedException.ErrorTag.OPERATION_FAILED, DocumentedException.ErrorType.APPLICATION);
98             assertEquals(1, e.getErrorInfo().size());
99             return;
100         }
101
102         fail("Get should fail with wrong xml");
103     }
104
105     @Test
106     public void testSuccessHandle() throws Exception {
107         doReturn(correctSubsequentResult).when(subsequentOperation).execute(request);
108         assertTrue(get.getHandlingPriority().getPriority().get()
109                 > HandlingPriority.HANDLE_WITH_DEFAULT_PRIORITY.getPriority().get());
110         final Document result = get.handle(request, subsequentOperation);
111         assertThat(XmlUtil.toString(result), CoreMatchers.containsString("sessions"));
112         assertThat(XmlUtil.toString(result), CoreMatchers.containsString("schemas"));
113
114     }
115
116     @Test(expected = UnsupportedOperationException.class)
117     public void testHandle() throws Exception {
118         get.handle(null, null, null);
119
120     }
121
122     private void assertNetconfDocumentedEx(final DocumentedException exception, final DocumentedException.ErrorSeverity
123             severity, final DocumentedException.ErrorTag errorTag, final DocumentedException.ErrorType type) {
124         assertEquals(severity, exception.getErrorSeverity());
125         assertEquals(errorTag, exception.getErrorTag());
126         assertEquals(type, exception.getErrorType());
127     }
128 }