2 * Copyright (c) 2016 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
9 package org.opendaylight.openflowplugin.applications.frsync.impl.strategy;
11 import java.math.BigInteger;
12 import java.util.concurrent.Future;
13 import java.util.concurrent.TimeUnit;
14 import org.junit.Assert;
15 import org.junit.Before;
16 import org.junit.Test;
17 import org.junit.runner.RunWith;
18 import org.mockito.ArgumentCaptor;
19 import org.mockito.ArgumentMatchers;
20 import org.mockito.Captor;
21 import org.mockito.Mock;
22 import org.mockito.Mockito;
23 import org.mockito.runners.MockitoJUnitRunner;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev150304.TransactionId;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.AddGroupInput;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.AddGroupOutput;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.AddGroupOutputBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.RemoveGroupInput;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.RemoveGroupOutput;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.RemoveGroupOutputBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.SalGroupService;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.UpdateGroupInput;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.UpdateGroupOutput;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.UpdateGroupOutputBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupId;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.BucketsBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.GroupBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.GroupKey;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
45 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
46 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
47 import org.opendaylight.yangtools.yang.common.RpcResult;
48 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
51 * Test for {@link GroupForwarder}.
53 @RunWith(MockitoJUnitRunner.class)
54 public class GroupForwarderTest {
56 private final NodeKey s1Key = new NodeKey(new NodeId("S1"));
57 private final GroupId groupId = new GroupId(42L);
58 private final GroupKey groupKey = new GroupKey(groupId);
59 private final Group group = new GroupBuilder()
61 .setGroupName("test-group")
62 .setBuckets(new BucketsBuilder().build())
65 private final KeyedInstanceIdentifier<Node, NodeKey> nodePath = InstanceIdentifier.create(Nodes.class)
66 .child(Node.class, s1Key);
67 private final InstanceIdentifier<FlowCapableNode> flowCapableNodePath = nodePath
68 .augmentation(FlowCapableNode.class);
69 private final InstanceIdentifier<Group> groupPath = flowCapableNodePath.child(Group.class, groupKey);
72 private SalGroupService salGroupService;
74 private ArgumentCaptor<AddGroupInput> addGroupInputCpt;
76 private ArgumentCaptor<RemoveGroupInput> removeGroupInputCpt;
78 private ArgumentCaptor<UpdateGroupInput> updateGroupInputCpt;
80 private TransactionId txId;
82 private GroupForwarder groupForwarder;
85 public void setUp() throws Exception {
86 groupForwarder = new GroupForwarder(salGroupService);
87 txId = new TransactionId(BigInteger.ONE);
91 public void testRemove() throws Exception {
92 Mockito.when(salGroupService.removeGroup(removeGroupInputCpt.capture())).thenReturn(
93 RpcResultBuilder.success(
94 new RemoveGroupOutputBuilder()
95 .setTransactionId(txId)
100 final Future<RpcResult<RemoveGroupOutput>> addResult =
101 groupForwarder.remove(groupPath, group, flowCapableNodePath);
103 Mockito.verify(salGroupService).removeGroup(ArgumentMatchers.<RemoveGroupInput>any());
105 Assert.assertTrue(addResult.isDone());
106 final RpcResult<RemoveGroupOutput> result = addResult.get(2, TimeUnit.SECONDS);
107 Assert.assertTrue(result.isSuccessful());
109 Assert.assertEquals(1, result.getResult().getTransactionId().getValue().intValue());
111 final RemoveGroupInput removeGroupInput = removeGroupInputCpt.getValue();
112 Assert.assertEquals(groupPath, removeGroupInput.getGroupRef().getValue());
113 Assert.assertNull(removeGroupInput.getBuckets());
114 Assert.assertEquals(nodePath, removeGroupInput.getNode().getValue());
115 Assert.assertEquals("test-group", removeGroupInput.getGroupName());
119 public void testUpdate() throws Exception {
120 Mockito.when(salGroupService.updateGroup(updateGroupInputCpt.capture())).thenReturn(
121 RpcResultBuilder.success(
122 new UpdateGroupOutputBuilder()
123 .setTransactionId(txId)
128 Group groupOriginal = new GroupBuilder(group).build();
129 Group groupUpdate = new GroupBuilder(group)
130 .setGroupName("another-test")
133 final Future<RpcResult<UpdateGroupOutput>> addResult =
134 groupForwarder.update(groupPath, groupOriginal, groupUpdate, flowCapableNodePath);
136 Mockito.verify(salGroupService).updateGroup(ArgumentMatchers.<UpdateGroupInput>any());
138 Assert.assertTrue(addResult.isDone());
139 final RpcResult<UpdateGroupOutput> result = addResult.get(2, TimeUnit.SECONDS);
140 Assert.assertTrue(result.isSuccessful());
142 Assert.assertEquals(1, result.getResult().getTransactionId().getValue().intValue());
144 final UpdateGroupInput updateGroupInput = updateGroupInputCpt.getValue();
145 Assert.assertEquals(groupPath, updateGroupInput.getGroupRef().getValue());
146 Assert.assertEquals(nodePath, updateGroupInput.getNode().getValue());
147 Assert.assertNotNull(updateGroupInput.getOriginalGroup().getBuckets());
148 Assert.assertNotNull(updateGroupInput.getUpdatedGroup().getBuckets());
150 Assert.assertEquals("test-group", updateGroupInput.getOriginalGroup().getGroupName());
151 Assert.assertEquals("another-test", updateGroupInput.getUpdatedGroup().getGroupName());
155 public void testAdd() throws Exception {
156 Mockito.when(salGroupService.addGroup(addGroupInputCpt.capture())).thenReturn(
157 RpcResultBuilder.success(
158 new AddGroupOutputBuilder()
159 .setTransactionId(txId)
164 final Future<RpcResult<AddGroupOutput>> addResult = groupForwarder.add(groupPath, group, flowCapableNodePath);
166 Mockito.verify(salGroupService).addGroup(ArgumentMatchers.<AddGroupInput>any());
168 Assert.assertTrue(addResult.isDone());
169 final RpcResult<AddGroupOutput> result = addResult.get(2, TimeUnit.SECONDS);
170 Assert.assertTrue(result.isSuccessful());
172 Assert.assertEquals(1, result.getResult().getTransactionId().getValue().intValue());
174 final AddGroupInput addGroupInput = addGroupInputCpt.getValue();
175 Assert.assertEquals(groupPath, addGroupInput.getGroupRef().getValue());
176 Assert.assertEquals(nodePath, addGroupInput.getNode().getValue());
177 Assert.assertNotNull(addGroupInput.getBuckets());
178 Assert.assertEquals("test-group", addGroupInput.getGroupName());