2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.netconf.monitoring;
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;
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.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;
35 public class GetTest {
38 private NetconfMonitoringService monitor;
40 private Document request;
42 private NetconfOperationChainedExecution subsequentOperation;
43 private Document incorrectSubsequentResult;
44 private Document correctSubsequentResult;
49 public void setUp() throws Exception {
50 MockitoAnnotations.initMocks(this);
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>");
57 doReturn(new SessionsBuilder().setSession(Collections.<Session>emptyList()).build()).when(monitor)
59 doReturn(new SchemasBuilder().setSchema(Collections.<Schema>emptyList()).build()).when(monitor).getSchemas();
60 doReturn(false).when(subsequentOperation).isExecutionTermination();
62 get = new Get(monitor);
66 public void testHandleNoSubsequent() throws Exception {
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);
75 fail("Get should fail without subsequent operation");
79 public void testHandleWrongPlaceholder() throws Exception {
80 doReturn(incorrectSubsequentResult).when(subsequentOperation).execute(request);
82 get.handle(request, subsequentOperation);
83 } catch (final DocumentedException e) {
84 assertNetconfDocumentedEx(e, DocumentedException.ErrorSeverity.ERROR,
85 DocumentedException.ErrorTag.INVALID_VALUE, DocumentedException.ErrorType.APPLICATION);
89 fail("Get should fail with wrong xml");
93 public void testHandleRuntimeEx() throws Exception {
94 doThrow(RuntimeException.class).when(subsequentOperation).execute(request);
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());
104 fail("Get should fail with wrong xml");
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"));
118 @Test(expected = UnsupportedOperationException.class)
119 public void testHandle() throws Exception {
120 get.handle(null, null, null);
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());