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.controller.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;
17 import java.util.Collections;
18 import org.hamcrest.CoreMatchers;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.mockito.Mock;
22 import org.mockito.MockitoAnnotations;
23 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
24 import org.opendaylight.controller.netconf.api.monitoring.NetconfMonitoringService;
25 import org.opendaylight.controller.netconf.mapping.api.HandlingPriority;
26 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationChainedExecution;
27 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.SchemasBuilder;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.SessionsBuilder;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.schemas.Schema;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.sessions.Session;
32 import org.w3c.dom.Document;
34 public class GetTest {
37 private NetconfMonitoringService monitor;
39 private Document request;
41 private NetconfOperationChainedExecution subsequentOperation;
42 private Document incorrectSubsequentResult;
43 private Document correctSubsequentResult;
48 public void setUp() throws Exception {
49 MockitoAnnotations.initMocks(this);
51 incorrectSubsequentResult = XmlUtil.readXmlToDocument("<rpc-reply xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"/>");
52 correctSubsequentResult = XmlUtil.readXmlToDocument("<rpc-reply xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"><data></data></rpc-reply>");
54 doReturn(new SessionsBuilder().setSession(Collections.<Session>emptyList()).build()).when(monitor).getSessions();
55 doReturn(new SchemasBuilder().setSchema(Collections.<Schema>emptyList()).build()).when(monitor).getSchemas();
56 doReturn(false).when(subsequentOperation).isExecutionTermination();
58 get = new Get(monitor);
62 public void testHandleNoSubsequent() throws Exception {
64 get.handle(null, NetconfOperationChainedExecution.EXECUTION_TERMINATION_POINT);
65 } catch (final NetconfDocumentedException e) {
66 assertNetconfDocumentedEx(e, NetconfDocumentedException.ErrorSeverity.error, NetconfDocumentedException.ErrorTag.operation_failed, NetconfDocumentedException.ErrorType.application);
70 fail("Get should fail without subsequent operation");
74 public void testHandleWrongPlaceholder() throws Exception {
75 doReturn(incorrectSubsequentResult).when(subsequentOperation).execute(request);
77 get.handle(request, subsequentOperation);
78 } catch (final NetconfDocumentedException e) {
79 assertNetconfDocumentedEx(e, NetconfDocumentedException.ErrorSeverity.error, NetconfDocumentedException.ErrorTag.invalid_value, NetconfDocumentedException.ErrorType.application);
83 fail("Get should fail with wrong xml");
87 public void testHandleRuntimeEx() throws Exception {
88 doThrow(RuntimeException.class).when(subsequentOperation).execute(request);
90 get.handle(request, subsequentOperation);
91 } catch (final NetconfDocumentedException e) {
92 assertNetconfDocumentedEx(e, NetconfDocumentedException.ErrorSeverity.error, NetconfDocumentedException.ErrorTag.operation_failed, NetconfDocumentedException.ErrorType.application);
93 assertEquals(1, e.getErrorInfo().size());
97 fail("Get should fail with wrong xml");
101 public void testSuccessHandle() throws Exception {
102 doReturn(correctSubsequentResult).when(subsequentOperation).execute(request);
103 assertTrue(get.getHandlingPriority().getPriority().get() > HandlingPriority.HANDLE_WITH_DEFAULT_PRIORITY.getPriority().get());
104 final Document result = get.handle(request, subsequentOperation);
105 assertThat(XmlUtil.toString(result), CoreMatchers.containsString("sessions"));
106 assertThat(XmlUtil.toString(result), CoreMatchers.containsString("schemas"));
110 @Test(expected = UnsupportedOperationException.class)
111 public void testHandle() throws Exception {
112 get.handle(null, null, null);
116 private void assertNetconfDocumentedEx(final NetconfDocumentedException e, final NetconfDocumentedException.ErrorSeverity severity, final NetconfDocumentedException.ErrorTag errorTag, final NetconfDocumentedException.ErrorType type) {
117 assertEquals(severity, e.getErrorSeverity());
118 assertEquals(errorTag, e.getErrorTag());
119 assertEquals(type, e.getErrorType());