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