1 package org.opendaylight.openflowplugin.impl.services;
3 import com.google.common.util.concurrent.ListenableFuture;
4 import org.junit.Before;
6 import org.opendaylight.openflowjava.protocol.api.connection.DeviceRequestFailedException;
7 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.EventIdentifier;
8 import org.opendaylight.openflowplugin.impl.rpc.AbstractRequestContext;
9 import org.opendaylight.openflowplugin.impl.statistics.ofpspecific.MessageIntelligenceAgencyImpl;
10 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ErrorMessage;
11 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ErrorMessageBuilder;
12 import org.opendaylight.yangtools.yang.common.RpcError;
13 import org.opendaylight.yangtools.yang.common.RpcResult;
15 import java.util.Collection;
17 import static org.junit.Assert.assertEquals;
18 import static org.junit.Assert.assertTrue;
19 import static org.mockito.Mockito.mock;
20 import static org.mockito.Mockito.verify;
22 public class AbstractRequestCallbackTest {
24 private static final Class<?> DUMMY_REQUEST_TYPE = String.class;
25 private static final String DUMMY_DEVICE_ID = "DEVICE ID";
26 private static final String DUMMY_EVENT_NAME = "EVENT NAME";
27 private static final EventIdentifier DUMMY_EVENT_IDENTIFIER = new EventIdentifier(DUMMY_EVENT_NAME, DUMMY_DEVICE_ID);
28 private static final String DUMMY_EXCEPTION_DESCRIPTION = "dummy exception description";
29 private static final Long DUMMY_XID = 100L;
30 private static final String DUMMY_MESSAGE_ILLEGAL_STATE_EXCEPTION = "dummy illegal state exception";
32 AbstractRequestContext dummyRequestContext;
33 AbstractRequestCallback abstractRequestCallback;
36 public void initialization() {
37 dummyRequestContext = new AbstractRequestContext(DUMMY_XID) {
44 abstractRequestCallback = new AbstractRequestCallback(dummyRequestContext,
45 DUMMY_REQUEST_TYPE, new MessageIntelligenceAgencyImpl(),
46 DUMMY_EVENT_IDENTIFIER) {
48 public void onSuccess(Object o) {
57 public void testOnFailureWithDeviceRequestFailedException() throws Exception {
58 ErrorMessage dummyErrorMessage = new ErrorMessageBuilder().build();
59 abstractRequestCallback.onFailure(new DeviceRequestFailedException(DUMMY_EXCEPTION_DESCRIPTION, dummyErrorMessage));
60 final ListenableFuture futureResult = dummyRequestContext.getFuture();
62 RpcError rpcError = provideRpcError(futureResult);
63 assertEquals("Device reported error type null code null", rpcError.getMessage());
67 public void testOnFailure() throws Exception {
68 ErrorMessage dummyErrorMessage = new ErrorMessageBuilder().build();
69 abstractRequestCallback.onFailure(new IllegalStateException(DUMMY_MESSAGE_ILLEGAL_STATE_EXCEPTION));
70 final ListenableFuture futureResult = dummyRequestContext.getFuture();
72 RpcError rpcError = provideRpcError(futureResult);
73 assertEquals(DUMMY_MESSAGE_ILLEGAL_STATE_EXCEPTION, rpcError.getMessage());
77 private RpcError provideRpcError(ListenableFuture futureResult) throws InterruptedException, java.util.concurrent.ExecutionException {
78 final Object result = futureResult.get();
79 assertTrue(result instanceof RpcResult);
80 RpcResult rpcResult = (RpcResult) result;
81 final Collection errors = rpcResult.getErrors();
82 assertEquals(1, errors.size());
83 final Object error = errors.iterator().next();
84 assertTrue(error instanceof RpcError);
85 return (RpcError) error;