Bug 8153: Enforce check-style rules for netconf - netconf-monitoring
[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.controller.config.util.xml.DocumentedException;
25 import org.opendaylight.controller.config.util.xml.XmlUtil;
26 import org.opendaylight.netconf.api.monitoring.NetconfMonitoringService;
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.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 "
53                 + "xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"/>");
54         correctSubsequentResult = XmlUtil.readXmlToDocument("<rpc-reply xmlns=\"urn:ietf:params:xml:ns:netconf:base:"
55                 + "1.0\"><data></data></rpc-reply>");
56
57         doReturn(new SessionsBuilder().setSession(Collections.<Session>emptyList()).build()).when(monitor)
58                 .getSessions();
59         doReturn(new SchemasBuilder().setSchema(Collections.<Schema>emptyList()).build()).when(monitor).getSchemas();
60         doReturn(false).when(subsequentOperation).isExecutionTermination();
61
62         get = new Get(monitor);
63     }
64
65     @Test
66     public void testHandleNoSubsequent() throws Exception {
67         try {
68             get.handle(null, NetconfOperationChainedExecution.EXECUTION_TERMINATION_POINT);
69         } catch (final DocumentedException e) {
70             assertNetconfDocumentedEx(e, DocumentedException.ErrorSeverity.ERROR,
71                     DocumentedException.ErrorTag.OPERATION_FAILED, DocumentedException.ErrorType.APPLICATION);
72             return;
73         }
74
75         fail("Get should fail without subsequent operation");
76     }
77
78     @Test
79     public void testHandleWrongPlaceholder() throws Exception {
80         doReturn(incorrectSubsequentResult).when(subsequentOperation).execute(request);
81         try {
82             get.handle(request, subsequentOperation);
83         } catch (final DocumentedException e) {
84             assertNetconfDocumentedEx(e, DocumentedException.ErrorSeverity.ERROR,
85                     DocumentedException.ErrorTag.INVALID_VALUE, DocumentedException.ErrorType.APPLICATION);
86             return;
87         }
88
89         fail("Get should fail with wrong xml");
90     }
91
92     @Test
93     public void testHandleRuntimeEx() throws Exception {
94         doThrow(RuntimeException.class).when(subsequentOperation).execute(request);
95         try {
96             get.handle(request, subsequentOperation);
97         } catch (final DocumentedException e) {
98             assertNetconfDocumentedEx(e, DocumentedException.ErrorSeverity.ERROR,
99                     DocumentedException.ErrorTag.OPERATION_FAILED, DocumentedException.ErrorType.APPLICATION);
100             assertEquals(1, e.getErrorInfo().size());
101             return;
102         }
103
104         fail("Get should fail with wrong xml");
105     }
106
107     @Test
108     public void testSuccessHandle() throws Exception {
109         doReturn(correctSubsequentResult).when(subsequentOperation).execute(request);
110         assertTrue(get.getHandlingPriority().getPriority().get()
111                 > HandlingPriority.HANDLE_WITH_DEFAULT_PRIORITY.getPriority().get());
112         final Document result = get.handle(request, subsequentOperation);
113         assertThat(XmlUtil.toString(result), CoreMatchers.containsString("sessions"));
114         assertThat(XmlUtil.toString(result), CoreMatchers.containsString("schemas"));
115
116     }
117
118     @Test(expected = UnsupportedOperationException.class)
119     public void testHandle() throws Exception {
120         get.handle(null, null, null);
121
122     }
123
124     private void assertNetconfDocumentedEx(final DocumentedException exception, final DocumentedException.ErrorSeverity
125             severity, final DocumentedException.ErrorTag errorTag, final DocumentedException.ErrorType type) {
126         assertEquals(severity, exception.getErrorSeverity());
127         assertEquals(errorTag, exception.getErrorTag());
128         assertEquals(type, exception.getErrorType());
129     }
130 }