Add missing license headers
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / services / AbstractRequestCallbackTest.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.openflowplugin.impl.services;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertTrue;
12
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;
25
26 public class AbstractRequestCallbackTest {
27
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 = new EventIdentifier(DUMMY_EVENT_NAME, DUMMY_DEVICE_ID);
32     private static final String DUMMY_EXCEPTION_DESCRIPTION = "dummy exception description";
33     private static final Long DUMMY_XID = 100L;
34     private static final String DUMMY_MESSAGE_ILLEGAL_STATE_EXCEPTION = "dummy illegal state exception";
35
36     AbstractRequestContext dummyRequestContext;
37     AbstractRequestCallback abstractRequestCallback;
38
39     @Before
40     public void initialization() {
41         dummyRequestContext = new AbstractRequestContext(DUMMY_XID) {
42             @Override
43             public void close() {
44
45             }
46         };
47
48         abstractRequestCallback = new AbstractRequestCallback(dummyRequestContext,
49                 DUMMY_REQUEST_TYPE, new MessageIntelligenceAgencyImpl(),
50                 DUMMY_EVENT_IDENTIFIER) {
51             @Override
52             public void onSuccess(Object o) {
53
54             }
55
56         };
57
58     }
59
60     @Test
61     public void testOnFailureWithDeviceRequestFailedException() throws Exception {
62         ErrorMessage dummyErrorMessage = new ErrorMessageBuilder().build();
63         abstractRequestCallback.onFailure(new DeviceRequestFailedException(DUMMY_EXCEPTION_DESCRIPTION, dummyErrorMessage));
64         final ListenableFuture futureResult = dummyRequestContext.getFuture();
65
66         RpcError rpcError = provideRpcError(futureResult);
67         assertEquals("Device reported error type null code null", rpcError.getMessage());
68     }
69
70     @Test
71     public void testOnFailure() throws Exception {
72         ErrorMessage dummyErrorMessage = new ErrorMessageBuilder().build();
73         abstractRequestCallback.onFailure(new IllegalStateException(DUMMY_MESSAGE_ILLEGAL_STATE_EXCEPTION));
74         final ListenableFuture futureResult = dummyRequestContext.getFuture();
75
76         RpcError rpcError = provideRpcError(futureResult);
77         assertEquals(DUMMY_MESSAGE_ILLEGAL_STATE_EXCEPTION, rpcError.getMessage());
78
79     }
80
81     private RpcError provideRpcError(ListenableFuture futureResult) throws InterruptedException, java.util.concurrent.ExecutionException {
82         final Object result = futureResult.get();
83         assertTrue(result instanceof RpcResult);
84         RpcResult rpcResult = (RpcResult) result;
85         final Collection errors = rpcResult.getErrors();
86         assertEquals(1, errors.size());
87         final Object error = errors.iterator().next();
88         assertTrue(error instanceof RpcError);
89         return (RpcError) error;
90     }
91
92
93 }