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.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;
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 xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"/>");
53 correctSubsequentResult = XmlUtil.readXmlToDocument("<rpc-reply xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"><data></data></rpc-reply>");
55 doReturn(new SessionsBuilder().setSession(Collections.<Session>emptyList()).build()).when(monitor).getSessions();
56 doReturn(new SchemasBuilder().setSchema(Collections.<Schema>emptyList()).build()).when(monitor).getSchemas();
57 doReturn(false).when(subsequentOperation).isExecutionTermination();
59 get = new Get(monitor);
63 public void testHandleNoSubsequent() throws Exception {
65 get.handle(null, NetconfOperationChainedExecution.EXECUTION_TERMINATION_POINT);
66 } catch (final DocumentedException e) {
67 assertNetconfDocumentedEx(e, DocumentedException.ErrorSeverity.error, DocumentedException.ErrorTag.operation_failed, DocumentedException.ErrorType.application);
71 fail("Get should fail without subsequent operation");
75 public void testHandleWrongPlaceholder() throws Exception {
76 doReturn(incorrectSubsequentResult).when(subsequentOperation).execute(request);
78 get.handle(request, subsequentOperation);
79 } catch (final DocumentedException e) {
80 assertNetconfDocumentedEx(e, DocumentedException.ErrorSeverity.error, DocumentedException.ErrorTag.invalid_value, DocumentedException.ErrorType.application);
84 fail("Get should fail with wrong xml");
88 public void testHandleRuntimeEx() throws Exception {
89 doThrow(RuntimeException.class).when(subsequentOperation).execute(request);
91 get.handle(request, subsequentOperation);
92 } catch (final DocumentedException e) {
93 assertNetconfDocumentedEx(e, DocumentedException.ErrorSeverity.error, DocumentedException.ErrorTag.operation_failed, DocumentedException.ErrorType.application);
94 assertEquals(1, e.getErrorInfo().size());
98 fail("Get should fail with wrong xml");
102 public void testSuccessHandle() throws Exception {
103 doReturn(correctSubsequentResult).when(subsequentOperation).execute(request);
104 assertTrue(get.getHandlingPriority().getPriority().get() > HandlingPriority.HANDLE_WITH_DEFAULT_PRIORITY.getPriority().get());
105 final Document result = get.handle(request, subsequentOperation);
106 assertThat(XmlUtil.toString(result), CoreMatchers.containsString("sessions"));
107 assertThat(XmlUtil.toString(result), CoreMatchers.containsString("schemas"));
111 @Test(expected = UnsupportedOperationException.class)
112 public void testHandle() throws Exception {
113 get.handle(null, null, null);
117 private void assertNetconfDocumentedEx(final DocumentedException e, final DocumentedException.ErrorSeverity severity, final DocumentedException.ErrorTag errorTag, final DocumentedException.ErrorType type) {
118 assertEquals(severity, e.getErrorSeverity());
119 assertEquals(errorTag, e.getErrorTag());
120 assertEquals(type, e.getErrorType());