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.w3c.dom.Document;
33 public class GetTest {
36 private NetconfMonitoringService monitor;
38 private Document request;
40 private NetconfOperationChainedExecution subsequentOperation;
41 private Document incorrectSubsequentResult;
42 private Document correctSubsequentResult;
47 public void setUp() throws Exception {
48 MockitoAnnotations.initMocks(this);
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>");
55 doReturn(new SessionsBuilder().setSession(Collections.emptyList()).build()).when(monitor)
57 doReturn(new SchemasBuilder().setSchema(Collections.emptyList()).build()).when(monitor).getSchemas();
58 doReturn(false).when(subsequentOperation).isExecutionTermination();
60 get = new Get(monitor);
64 public void testHandleNoSubsequent() throws Exception {
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);
73 fail("Get should fail without subsequent operation");
77 public void testHandleWrongPlaceholder() throws Exception {
78 doReturn(incorrectSubsequentResult).when(subsequentOperation).execute(request);
80 get.handle(request, subsequentOperation);
81 } catch (final DocumentedException e) {
82 assertNetconfDocumentedEx(e, DocumentedException.ErrorSeverity.ERROR,
83 DocumentedException.ErrorTag.INVALID_VALUE, DocumentedException.ErrorType.APPLICATION);
87 fail("Get should fail with wrong xml");
91 public void testHandleRuntimeEx() throws Exception {
92 doThrow(RuntimeException.class).when(subsequentOperation).execute(request);
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());
102 fail("Get should fail with wrong xml");
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"));
116 @Test(expected = UnsupportedOperationException.class)
117 public void testHandle() throws Exception {
118 get.handle(null, null, null);
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());