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 org.junit.Before;
16 import org.junit.Test;
17 import org.opendaylight.openflowjava.protocol.api.connection.DeviceRequestFailedException;
18 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.EventIdentifier;
19 import org.opendaylight.openflowplugin.impl.rpc.AbstractRequestContext;
20 import org.opendaylight.openflowplugin.impl.statistics.ofpspecific.MessageIntelligenceAgencyImpl;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ErrorMessage;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ErrorMessageBuilder;
23 import org.opendaylight.yangtools.yang.common.RpcError;
24 import org.opendaylight.yangtools.yang.common.RpcResult;
26 public class AbstractRequestCallbackTest {
28 private static final Class<?> DUMMY_REQUEST_TYPE = String.class;
29 private static final String DUMMY_DEVICE_ID = "DEVICE ID";
30 private static final String DUMMY_EVENT_NAME = "EVENT NAME";
31 private static final EventIdentifier DUMMY_EVENT_IDENTIFIER =
32 new EventIdentifier(DUMMY_EVENT_NAME, DUMMY_DEVICE_ID);
33 private static final String DUMMY_EXCEPTION_DESCRIPTION = "dummy exception description";
34 private static final Long DUMMY_XID = 100L;
35 private static final String DUMMY_MESSAGE_ILLEGAL_STATE_EXCEPTION = "dummy illegal state exception";
37 AbstractRequestContext dummyRequestContext;
38 AbstractRequestCallback abstractRequestCallback;
41 public void initialization() {
42 dummyRequestContext = new AbstractRequestContext(DUMMY_XID) {
49 abstractRequestCallback = new AbstractRequestCallback(dummyRequestContext,
50 DUMMY_REQUEST_TYPE, new MessageIntelligenceAgencyImpl(),
51 DUMMY_EVENT_IDENTIFIER) {
53 public void onSuccess(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 RpcError provideRpcError(ListenableFuture futureResult) throws InterruptedException,
82 java.util.concurrent.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;