252a80d9d0324f9637990d68b892910fd70be0a2
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / api / openflow / device / RpcContextImplTest.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.api.openflow.device;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertTrue;
14
15 import java.util.Iterator;
16 import java.util.concurrent.Future;
17 import org.junit.Before;
18 import org.junit.Test;
19 import org.junit.runner.RunWith;
20 import org.mockito.Mock;
21 import org.mockito.runners.MockitoJUnitRunner;
22 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
23 import org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext;
24 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
25 import org.opendaylight.openflowplugin.impl.rpc.RpcContextImpl;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.UpdateFlowOutput;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
31 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
32 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
33 import org.opendaylight.yangtools.yang.common.RpcError;
34 import org.opendaylight.yangtools.yang.common.RpcResult;
35
36 /**
37  * @author joe
38  */
39 @RunWith(MockitoJUnitRunner.class)
40 public class RpcContextImplTest {
41
42     @Mock
43     private BindingAwareBroker.ProviderContext mockedRpcProviderRegistry;
44
45     @Mock
46     private DeviceContext deviceContext;
47     @Mock
48     private MessageSpy messageSpy;
49
50     private RpcContext rpcContext;
51
52     private static final String QUEUE_IS_FULL = "Device's request queue is full.";
53
54     @Before
55     public void setup() {
56         NodeId nodeId = new NodeId("openflow:1");
57         KeyedInstanceIdentifier<Node, NodeKey> nodeInstanceIdentifier = InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(nodeId));
58
59         rpcContext = new RpcContextImpl(messageSpy, mockedRpcProviderRegistry, nodeInstanceIdentifier);
60     }
61
62     @Test
63     public void invokeRpcTest() {
64
65     }
66
67     @Test
68     public void testStoreOrFail() throws Exception {
69         RequestContext requestContext = rpcContext.createRequestContext();
70         rpcContext.setRequestContextQuota(100);
71         Future<RpcResult<UpdateFlowOutput>> resultFuture = rpcContext.storeOrFail(requestContext);
72         assertNotNull(resultFuture);
73         assertFalse(resultFuture.isDone());
74     }
75
76     @Test
77     public void testStoreOrFailThatFails() throws Exception {
78         RequestContext requestContext = rpcContext.createRequestContext();
79         rpcContext.setRequestContextQuota(0);
80         Future<RpcResult<UpdateFlowOutput>> resultFuture = rpcContext.storeOrFail(requestContext);
81         assertNotNull(resultFuture);
82         assertTrue(resultFuture.isDone());
83         RpcResult<UpdateFlowOutput> updateFlowOutputRpcResult = resultFuture.get();
84         assertNotNull(updateFlowOutputRpcResult);
85         assertEquals(1, updateFlowOutputRpcResult.getErrors().size());
86         Iterator<RpcError> iterator = updateFlowOutputRpcResult.getErrors().iterator();
87         assertEquals(QUEUE_IS_FULL, iterator.next().getMessage());
88     }
89
90 }