1 package org.opendaylight.openflowplugin.impl.services;
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertTrue;
5 import com.google.common.util.concurrent.ListenableFuture;
6 import java.util.Collection;
7 import org.junit.Before;
9 import org.opendaylight.openflowjava.protocol.api.connection.DeviceRequestFailedException;
10 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.EventIdentifier;
11 import org.opendaylight.openflowplugin.impl.rpc.AbstractRequestContext;
12 import org.opendaylight.openflowplugin.impl.statistics.ofpspecific.MessageIntelligenceAgencyImpl;
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ErrorMessage;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ErrorMessageBuilder;
15 import org.opendaylight.yangtools.yang.common.RpcError;
16 import org.opendaylight.yangtools.yang.common.RpcResult;
18 public class AbstractRequestCallbackTest {
20 private static final Class<?> DUMMY_REQUEST_TYPE = String.class;
21 private static final String DUMMY_DEVICE_ID = "DEVICE ID";
22 private static final String DUMMY_EVENT_NAME = "EVENT NAME";
23 private static final EventIdentifier DUMMY_EVENT_IDENTIFIER = new EventIdentifier(DUMMY_EVENT_NAME, DUMMY_DEVICE_ID);
24 private static final String DUMMY_EXCEPTION_DESCRIPTION = "dummy exception description";
25 private static final Long DUMMY_XID = 100L;
26 private static final String DUMMY_MESSAGE_ILLEGAL_STATE_EXCEPTION = "dummy illegal state exception";
28 AbstractRequestContext dummyRequestContext;
29 AbstractRequestCallback abstractRequestCallback;
32 public void initialization() {
33 dummyRequestContext = new AbstractRequestContext(DUMMY_XID) {
40 abstractRequestCallback = new AbstractRequestCallback(dummyRequestContext,
41 DUMMY_REQUEST_TYPE, new MessageIntelligenceAgencyImpl(),
42 DUMMY_EVENT_IDENTIFIER) {
44 public void onSuccess(Object o) {
53 public void testOnFailureWithDeviceRequestFailedException() throws Exception {
54 ErrorMessage dummyErrorMessage = new ErrorMessageBuilder().build();
55 abstractRequestCallback.onFailure(new DeviceRequestFailedException(DUMMY_EXCEPTION_DESCRIPTION, dummyErrorMessage));
56 final ListenableFuture futureResult = dummyRequestContext.getFuture();
58 RpcError rpcError = provideRpcError(futureResult);
59 assertEquals("Device reported error type null code null", rpcError.getMessage());
63 public void testOnFailure() throws Exception {
64 ErrorMessage dummyErrorMessage = new ErrorMessageBuilder().build();
65 abstractRequestCallback.onFailure(new IllegalStateException(DUMMY_MESSAGE_ILLEGAL_STATE_EXCEPTION));
66 final ListenableFuture futureResult = dummyRequestContext.getFuture();
68 RpcError rpcError = provideRpcError(futureResult);
69 assertEquals(DUMMY_MESSAGE_ILLEGAL_STATE_EXCEPTION, rpcError.getMessage());
73 private RpcError provideRpcError(ListenableFuture futureResult) throws InterruptedException, java.util.concurrent.ExecutionException {
74 final Object result = futureResult.get();
75 assertTrue(result instanceof RpcResult);
76 RpcResult rpcResult = (RpcResult) result;
77 final Collection errors = rpcResult.getErrors();
78 assertEquals(1, errors.size());
79 final Object error = errors.iterator().next();
80 assertTrue(error instanceof RpcError);
81 return (RpcError) error;