Cleaning and preparation before bug 6170
[openflowplugin.git] / applications / forwardingrules-sync / src / test / java / org / opendaylight / openflowplugin / applications / frsync / impl / GroupForwarderTest.java
1 /**
2  * Copyright (c) 2016 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
9 package org.opendaylight.openflowplugin.applications.frsync.impl;
10
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.Captor;
20 import org.mockito.Matchers;
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;
49
50 /**
51  * Test for {@link GroupForwarder}.
52  */
53 @RunWith(MockitoJUnitRunner.class)
54 public class GroupForwarderTest {
55
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()
60             .setGroupId(groupId)
61             .setGroupName("test-group")
62             .setBuckets(new BucketsBuilder().build())
63             .build();
64
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);
70
71     @Mock
72     private SalGroupService salGroupService;
73     @Captor
74     private ArgumentCaptor<AddGroupInput> addGroupInputCpt;
75     @Captor
76     private ArgumentCaptor<RemoveGroupInput> removeGroupInputCpt;
77     @Captor
78     private ArgumentCaptor<UpdateGroupInput> updateGroupInputCpt;
79
80     private TransactionId txId;
81
82     private GroupForwarder groupForwarder;
83
84     @Before
85     public void setUp() throws Exception {
86         groupForwarder = new GroupForwarder(salGroupService);
87         txId = new TransactionId(BigInteger.ONE);
88     }
89
90     @Test
91     public void testRemove() throws Exception {
92         Mockito.when(salGroupService.removeGroup(removeGroupInputCpt.capture())).thenReturn(
93                 RpcResultBuilder.success(
94                         new RemoveGroupOutputBuilder()
95                                 .setTransactionId(txId)
96                                 .build())
97                         .buildFuture()
98         );
99
100         final Future<RpcResult<RemoveGroupOutput>> addResult = groupForwarder.remove(groupPath, group, flowCapableNodePath);
101
102         Mockito.verify(salGroupService).removeGroup(Matchers.<RemoveGroupInput>any());
103
104         Assert.assertTrue(addResult.isDone());
105         final RpcResult<RemoveGroupOutput> result = addResult.get(2, TimeUnit.SECONDS);
106         Assert.assertTrue(result.isSuccessful());
107
108         Assert.assertEquals(1, result.getResult().getTransactionId().getValue().intValue());
109
110         final RemoveGroupInput removeGroupInput = removeGroupInputCpt.getValue();
111         Assert.assertEquals(groupPath, removeGroupInput.getGroupRef().getValue());
112         Assert.assertNull(removeGroupInput.getBuckets());
113         Assert.assertEquals(nodePath, removeGroupInput.getNode().getValue());
114         Assert.assertEquals("test-group", removeGroupInput.getGroupName());
115     }
116
117     @Test
118     public void testUpdate() throws Exception {
119         Mockito.when(salGroupService.updateGroup(updateGroupInputCpt.capture())).thenReturn(
120                 RpcResultBuilder.success(
121                         new UpdateGroupOutputBuilder()
122                                 .setTransactionId(txId)
123                                 .build())
124                         .buildFuture()
125         );
126
127         Group groupOriginal = new GroupBuilder(group).build();
128         Group groupUpdate = new GroupBuilder(group)
129                 .setGroupName("another-test")
130                 .build();
131
132         final Future<RpcResult<UpdateGroupOutput>> addResult = groupForwarder.update(groupPath, groupOriginal, groupUpdate,
133                 flowCapableNodePath);
134
135         Mockito.verify(salGroupService).updateGroup(Matchers.<UpdateGroupInput>any());
136
137         Assert.assertTrue(addResult.isDone());
138         final RpcResult<UpdateGroupOutput> result = addResult.get(2, TimeUnit.SECONDS);
139         Assert.assertTrue(result.isSuccessful());
140
141         Assert.assertEquals(1, result.getResult().getTransactionId().getValue().intValue());
142
143         final UpdateGroupInput updateGroupInput = updateGroupInputCpt.getValue();
144         Assert.assertEquals(groupPath, updateGroupInput.getGroupRef().getValue());
145         Assert.assertEquals(nodePath, updateGroupInput.getNode().getValue());
146         Assert.assertNotNull(updateGroupInput.getOriginalGroup().getBuckets());
147         Assert.assertNotNull(updateGroupInput.getUpdatedGroup().getBuckets());
148
149         Assert.assertEquals("test-group", updateGroupInput.getOriginalGroup().getGroupName());
150         Assert.assertEquals("another-test", updateGroupInput.getUpdatedGroup().getGroupName());
151     }
152
153     @Test
154     public void testAdd() throws Exception {
155         Mockito.when(salGroupService.addGroup(addGroupInputCpt.capture())).thenReturn(
156                 RpcResultBuilder.success(
157                         new AddGroupOutputBuilder()
158                                 .setTransactionId(txId)
159                                 .build())
160                         .buildFuture()
161         );
162
163         final Future<RpcResult<AddGroupOutput>> addResult = groupForwarder.add(groupPath, group, flowCapableNodePath);
164
165         Mockito.verify(salGroupService).addGroup(Matchers.<AddGroupInput>any());
166
167         Assert.assertTrue(addResult.isDone());
168         final RpcResult<AddGroupOutput> result = addResult.get(2, TimeUnit.SECONDS);
169         Assert.assertTrue(result.isSuccessful());
170
171         Assert.assertEquals(1, result.getResult().getTransactionId().getValue().intValue());
172
173         final AddGroupInput addGroupInput = addGroupInputCpt.getValue();
174         Assert.assertEquals(groupPath, addGroupInput.getGroupRef().getValue());
175         Assert.assertEquals(nodePath, addGroupInput.getNode().getValue());
176         Assert.assertNotNull(addGroupInput.getBuckets());
177         Assert.assertEquals("test-group", addGroupInput.getGroupName());
178     }
179 }