OPNFLWPLUG-1032: Neon-MRI: Bump odlparent, yangtools, mdsal
[openflowplugin.git] / applications / forwardingrules-sync / src / test / java / org / opendaylight / openflowplugin / applications / frsync / impl / strategy / 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.strategy;
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.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;
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 =
101                 groupForwarder.remove(groupPath, group, flowCapableNodePath);
102
103         Mockito.verify(salGroupService).removeGroup(ArgumentMatchers.<RemoveGroupInput>any());
104
105         Assert.assertTrue(addResult.isDone());
106         final RpcResult<RemoveGroupOutput> result = addResult.get(2, TimeUnit.SECONDS);
107         Assert.assertTrue(result.isSuccessful());
108
109         Assert.assertEquals(1, result.getResult().getTransactionId().getValue().intValue());
110
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());
116     }
117
118     @Test
119     public void testUpdate() throws Exception {
120         Mockito.when(salGroupService.updateGroup(updateGroupInputCpt.capture())).thenReturn(
121                 RpcResultBuilder.success(
122                         new UpdateGroupOutputBuilder()
123                                 .setTransactionId(txId)
124                                 .build())
125                         .buildFuture()
126         );
127
128         Group groupOriginal = new GroupBuilder(group).build();
129         Group groupUpdate = new GroupBuilder(group)
130                 .setGroupName("another-test")
131                 .build();
132
133         final Future<RpcResult<UpdateGroupOutput>> addResult =
134                 groupForwarder.update(groupPath, groupOriginal, groupUpdate, flowCapableNodePath);
135
136         Mockito.verify(salGroupService).updateGroup(ArgumentMatchers.<UpdateGroupInput>any());
137
138         Assert.assertTrue(addResult.isDone());
139         final RpcResult<UpdateGroupOutput> result = addResult.get(2, TimeUnit.SECONDS);
140         Assert.assertTrue(result.isSuccessful());
141
142         Assert.assertEquals(1, result.getResult().getTransactionId().getValue().intValue());
143
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());
149
150         Assert.assertEquals("test-group", updateGroupInput.getOriginalGroup().getGroupName());
151         Assert.assertEquals("another-test", updateGroupInput.getUpdatedGroup().getGroupName());
152     }
153
154     @Test
155     public void testAdd() throws Exception {
156         Mockito.when(salGroupService.addGroup(addGroupInputCpt.capture())).thenReturn(
157                 RpcResultBuilder.success(
158                         new AddGroupOutputBuilder()
159                                 .setTransactionId(txId)
160                                 .build())
161                         .buildFuture()
162         );
163
164         final Future<RpcResult<AddGroupOutput>> addResult = groupForwarder.add(groupPath, group, flowCapableNodePath);
165
166         Mockito.verify(salGroupService).addGroup(ArgumentMatchers.<AddGroupInput>any());
167
168         Assert.assertTrue(addResult.isDone());
169         final RpcResult<AddGroupOutput> result = addResult.get(2, TimeUnit.SECONDS);
170         Assert.assertTrue(result.isSuccessful());
171
172         Assert.assertEquals(1, result.getResult().getTransactionId().getValue().intValue());
173
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());
179     }
180 }