2 * Copyright (c) 2015 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
8 package org.opendaylight.openflowplugin.impl.services;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertTrue;
13 import com.google.common.util.concurrent.ListenableFuture;
14 import java.util.Collection;
15 import java.util.concurrent.ExecutionException;
16 import org.junit.Before;
17 import org.junit.Test;
18 import org.opendaylight.openflowjava.protocol.api.connection.DeviceRequestFailedException;
19 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.EventIdentifier;
20 import org.opendaylight.openflowplugin.impl.rpc.AbstractRequestContext;
21 import org.opendaylight.openflowplugin.impl.statistics.ofpspecific.MessageIntelligenceAgencyImpl;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ErrorMessage;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ErrorMessageBuilder;
24 import org.opendaylight.yangtools.yang.common.RpcError;
25 import org.opendaylight.yangtools.yang.common.RpcResult;
26 import org.opendaylight.yangtools.yang.common.Uint32;
28 public class AbstractRequestCallbackTest {
30 private static final Class<?> DUMMY_REQUEST_TYPE = String.class;
31 private static final String DUMMY_DEVICE_ID = "DEVICE ID";
32 private static final String DUMMY_EVENT_NAME = "EVENT NAME";
33 private static final EventIdentifier DUMMY_EVENT_IDENTIFIER =
34 new EventIdentifier(DUMMY_EVENT_NAME, DUMMY_DEVICE_ID);
35 private static final String DUMMY_EXCEPTION_DESCRIPTION = "dummy exception description";
36 private static final Uint32 DUMMY_XID = Uint32.valueOf(100L);
37 private static final String DUMMY_MESSAGE_ILLEGAL_STATE_EXCEPTION = "dummy illegal state exception";
39 AbstractRequestContext dummyRequestContext;
40 AbstractRequestCallback abstractRequestCallback;
43 public void initialization() {
44 dummyRequestContext = new AbstractRequestContext(DUMMY_XID) {
51 abstractRequestCallback = new AbstractRequestCallback(dummyRequestContext,
52 DUMMY_REQUEST_TYPE, new MessageIntelligenceAgencyImpl(),
53 DUMMY_EVENT_IDENTIFIER) {
55 public void onSuccess(final Object object) {
62 public void testOnFailureWithDeviceRequestFailedException() throws Exception {
63 ErrorMessage dummyErrorMessage = new ErrorMessageBuilder().build();
64 abstractRequestCallback
65 .onFailure(new DeviceRequestFailedException(DUMMY_EXCEPTION_DESCRIPTION, dummyErrorMessage));
66 final ListenableFuture futureResult = dummyRequestContext.getFuture();
68 RpcError rpcError = provideRpcError(futureResult);
69 assertEquals("Device reported error type null code null", rpcError.getMessage());
73 public void testOnFailure() throws Exception {
74 abstractRequestCallback.onFailure(new IllegalStateException(DUMMY_MESSAGE_ILLEGAL_STATE_EXCEPTION));
75 final ListenableFuture futureResult = dummyRequestContext.getFuture();
77 RpcError rpcError = provideRpcError(futureResult);
78 assertEquals(DUMMY_MESSAGE_ILLEGAL_STATE_EXCEPTION, rpcError.getMessage());
81 private static RpcError provideRpcError(final ListenableFuture futureResult)
82 throws InterruptedException, ExecutionException {
83 final Object result = futureResult.get();
84 assertTrue(result instanceof RpcResult);
85 RpcResult rpcResult = (RpcResult) result;
86 final Collection errors = rpcResult.getErrors();
87 assertEquals(1, errors.size());
88 final Object error = errors.iterator().next();
89 assertTrue(error instanceof RpcError);
90 return (RpcError) error;