Bug-5523 RoleManager changes
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / services / AbstractRequestCallbackTest.java
1 package org.opendaylight.openflowplugin.impl.services;
2
3 import com.google.common.util.concurrent.ListenableFuture;
4 import org.junit.Before;
5 import org.junit.Test;
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;
14
15 import java.util.Collection;
16
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;
21
22 public class AbstractRequestCallbackTest {
23
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";
31
32     AbstractRequestContext dummyRequestContext;
33     AbstractRequestCallback abstractRequestCallback;
34
35     @Before
36     public void initialization() {
37         dummyRequestContext = new AbstractRequestContext(DUMMY_XID) {
38             @Override
39             public void close() {
40
41             }
42         };
43
44         abstractRequestCallback = new AbstractRequestCallback(dummyRequestContext,
45                 DUMMY_REQUEST_TYPE, new MessageIntelligenceAgencyImpl(),
46                 DUMMY_EVENT_IDENTIFIER) {
47             @Override
48             public void onSuccess(Object o) {
49
50             }
51
52         };
53
54     }
55
56     @Test
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();
61
62         RpcError rpcError = provideRpcError(futureResult);
63         assertEquals("Device reported error type null code null", rpcError.getMessage());
64     }
65
66     @Test
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();
71
72         RpcError rpcError = provideRpcError(futureResult);
73         assertEquals(DUMMY_MESSAGE_ILLEGAL_STATE_EXCEPTION, rpcError.getMessage());
74
75     }
76
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;
86     }
87
88
89 }