Merge "Make methods static"
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / util / FlatBatchUtilTest.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 package org.opendaylight.openflowplugin.impl.util;
9
10 import com.google.common.collect.Lists;
11 import java.util.ArrayList;
12 import java.util.EnumSet;
13 import java.util.List;
14 import org.junit.Assert;
15 import org.junit.Test;
16 import org.opendaylight.openflowplugin.impl.services.batch.BatchPlanStep;
17 import org.opendaylight.openflowplugin.impl.services.batch.BatchStepType;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.ProcessFlatBatchOutput;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.ProcessFlatBatchOutputBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.Batch;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.BatchBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.BatchChoice;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.FlatBatchAddFlowCaseBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.FlatBatchAddGroupCaseBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.FlatBatchAddMeterCaseBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.FlatBatchRemoveFlowCaseBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.FlatBatchRemoveGroupCaseBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.FlatBatchRemoveMeterCaseBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.FlatBatchUpdateFlowCaseBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.FlatBatchUpdateGroupCaseBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.FlatBatchUpdateMeterCaseBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.flat.batch.add.flow._case.FlatBatchAddFlow;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.flat.batch.add.flow._case.FlatBatchAddFlowBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.flat.batch.add.flow._case.FlatBatchAddFlowKey;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.flat.batch.add.group._case.FlatBatchAddGroup;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.flat.batch.add.group._case.FlatBatchAddGroupBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.flat.batch.add.group._case.FlatBatchAddGroupKey;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.flat.batch.add.meter._case.FlatBatchAddMeter;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.flat.batch.add.meter._case.FlatBatchAddMeterBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.flat.batch.add.meter._case.FlatBatchAddMeterKey;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.flat.batch.remove.flow._case.FlatBatchRemoveFlow;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.flat.batch.remove.flow._case.FlatBatchRemoveFlowBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.flat.batch.remove.flow._case.FlatBatchRemoveFlowKey;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.flat.batch.remove.group._case.FlatBatchRemoveGroup;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.flat.batch.remove.group._case.FlatBatchRemoveGroupBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.flat.batch.remove.group._case.FlatBatchRemoveGroupKey;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.flat.batch.remove.meter._case.FlatBatchRemoveMeter;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.flat.batch.remove.meter._case.FlatBatchRemoveMeterBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.flat.batch.remove.meter._case.FlatBatchRemoveMeterKey;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.flat.batch.update.flow._case.FlatBatchUpdateFlow;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.flat.batch.update.flow._case.FlatBatchUpdateFlowBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.flat.batch.update.flow._case.FlatBatchUpdateFlowKey;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.flat.batch.update.group._case.FlatBatchUpdateGroup;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.flat.batch.update.group._case.FlatBatchUpdateGroupBuilder;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.flat.batch.update.group._case.FlatBatchUpdateGroupKey;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.flat.batch.update.meter._case.FlatBatchUpdateMeter;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.flat.batch.update.meter._case.FlatBatchUpdateMeterBuilder;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.input.batch.batch.choice.flat.batch.update.meter._case.FlatBatchUpdateMeterKey;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.output.BatchFailure;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.output.BatchFailureBuilder;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.output.BatchFailureKey;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.process.flat.batch.output.batch.failure.batch.item.id.choice.FlatBatchFailureFlowIdCaseBuilder;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
64 import org.opendaylight.yangtools.yang.common.RpcError;
65 import org.opendaylight.yangtools.yang.common.RpcResult;
66 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
67 import org.opendaylight.yangtools.yang.common.Uint16;
68 import org.slf4j.Logger;
69 import org.slf4j.LoggerFactory;
70
71 /**
72  * Test for {@link FlatBatchUtil}.
73  */
74 public class FlatBatchUtilTest {
75
76     private static final Logger LOG = LoggerFactory.getLogger(FlatBatchUtilTest.class);
77
78     @Test
79     public void testMarkBarriersWhereNeeded_noBarrier() {
80         final List<Batch> batches = Lists.newArrayList(
81                 //general part - no flush required
82                 createBatch(BatchStepType.GROUP_REMOVE),
83                 createBatch(BatchStepType.METER_REMOVE),
84                 createBatch(BatchStepType.FLOW_ADD),
85                 createBatch(BatchStepType.FLOW_REMOVE, 2),
86                 createBatch(BatchStepType.FLOW_ADD),
87                 createBatch(BatchStepType.FLOW_UPDATE),
88                 createBatch(BatchStepType.GROUP_ADD),
89                 createBatch(BatchStepType.GROUP_UPDATE),
90                 createBatch(BatchStepType.METER_ADD),
91                 createBatch(BatchStepType.METER_UPDATE)
92         );
93
94         final List<BatchPlanStep> batchPlan = FlatBatchUtil.assembleBatchPlan(batches);
95         FlatBatchUtil.markBarriersWhereNeeded(batchPlan);
96
97         Assert.assertEquals(10, batchPlan.size());
98         for (int i = 0; i < batchPlan.size(); i++) {
99             final BatchPlanStep planStep = batchPlan.get(i);
100             final boolean barrierBefore = planStep.isBarrierAfter();
101             LOG.debug("checking barrier mark @ {} {} -> {}",
102                     i, planStep.getStepType(), barrierBefore);
103
104             Assert.assertFalse(barrierBefore);
105         }
106     }
107
108     @Test
109     public void testMarkBarriersWhereNeeded_allBarriers() {
110         // need to flush G+/F+
111         checkBarriersBetween(BatchStepType.GROUP_ADD, BatchStepType.FLOW_ADD);
112         // need to flush G+/F*
113         checkBarriersBetween(BatchStepType.GROUP_ADD, BatchStepType.FLOW_UPDATE);
114         // need to flush F-/G-
115         checkBarriersBetween(BatchStepType.FLOW_REMOVE, BatchStepType.GROUP_REMOVE);
116         // need to flush F*/G-
117         checkBarriersBetween(BatchStepType.FLOW_UPDATE, BatchStepType.GROUP_REMOVE);
118
119         // need to flush G+/G+
120         checkBarriersBetween(BatchStepType.GROUP_ADD, BatchStepType.GROUP_ADD);
121         // need to flush G-/G-
122         checkBarriersBetween(BatchStepType.GROUP_REMOVE, BatchStepType.GROUP_REMOVE);
123         // need to flush G*/G+
124         checkBarriersBetween(BatchStepType.GROUP_UPDATE, BatchStepType.GROUP_ADD);
125         // need to flush G*/G-
126         checkBarriersBetween(BatchStepType.GROUP_UPDATE, BatchStepType.GROUP_REMOVE);
127
128         // need to flush M+/F+
129         checkBarriersBetween(BatchStepType.METER_ADD, BatchStepType.FLOW_ADD);
130         // need to flush M+/F*
131         checkBarriersBetween(BatchStepType.METER_ADD, BatchStepType.FLOW_UPDATE);
132         // need to flush F-/M-
133         checkBarriersBetween(BatchStepType.FLOW_REMOVE, BatchStepType.METER_REMOVE);
134         // need to flush F*/M-
135         checkBarriersBetween(BatchStepType.FLOW_UPDATE, BatchStepType.METER_REMOVE);
136     }
137
138     private void checkBarriersBetween(final BatchStepType typeOfFirst, final BatchStepType typeOfSecond) {
139         final List<Batch> batches = Lists.newArrayList(createBatch(typeOfFirst), createBatch(typeOfSecond));
140         final List<BatchPlanStep> batchPlan = FlatBatchUtil.assembleBatchPlan(batches);
141         FlatBatchUtil.markBarriersWhereNeeded(batchPlan);
142         LOG.debug("checking barrier between {} / {}", typeOfFirst, typeOfSecond);
143         Assert.assertEquals(2, batchPlan.size());
144         Assert.assertTrue("barrier expected between "
145                 + typeOfFirst + " / "
146                 + typeOfSecond, batchPlan.get(0).isBarrierAfter());
147         Assert.assertFalse(batchPlan.get(1).isBarrierAfter());
148     }
149
150     @Test
151     public void testMarkBarriersWhereNeeded_single() {
152         final List<Batch> batches = Lists.newArrayList(
153                 //general part - no flush required
154                 createBatch(BatchStepType.GROUP_REMOVE)
155         );
156
157         final List<BatchPlanStep> batchPlan = FlatBatchUtil.assembleBatchPlan(batches);
158         FlatBatchUtil.markBarriersWhereNeeded(batchPlan);
159
160         Assert.assertEquals(1, batchPlan.size());
161         Assert.assertFalse(batchPlan.get(0).isBarrierAfter());
162     }
163
164     @Test
165     public void testDecideBarrier() {
166         Assert.assertTrue(FlatBatchUtil.decideBarrier(EnumSet.of(BatchStepType.GROUP_ADD), BatchStepType.FLOW_ADD));
167         Assert.assertTrue(FlatBatchUtil.decideBarrier(EnumSet.of(BatchStepType.GROUP_ADD), BatchStepType.FLOW_UPDATE));
168
169         Assert.assertTrue(FlatBatchUtil
170                 .decideBarrier(EnumSet.of(BatchStepType.FLOW_REMOVE), BatchStepType.GROUP_REMOVE));
171         Assert.assertTrue(FlatBatchUtil
172                 .decideBarrier(EnumSet.of(BatchStepType.FLOW_UPDATE), BatchStepType.GROUP_REMOVE));
173
174         Assert.assertTrue(FlatBatchUtil.decideBarrier(EnumSet.of(BatchStepType.METER_ADD), BatchStepType.FLOW_ADD));
175         Assert.assertTrue(FlatBatchUtil.decideBarrier(EnumSet.of(BatchStepType.METER_ADD), BatchStepType.FLOW_UPDATE));
176
177         Assert.assertTrue(FlatBatchUtil
178                 .decideBarrier(EnumSet.of(BatchStepType.FLOW_REMOVE), BatchStepType.METER_REMOVE));
179         Assert.assertTrue(FlatBatchUtil
180                 .decideBarrier(EnumSet.of(BatchStepType.FLOW_UPDATE), BatchStepType.METER_REMOVE));
181     }
182
183     @Test
184     public void testAssembleBatchPlan() {
185         final List<Batch> batches = Lists.newArrayList(
186                 createBatch(BatchStepType.GROUP_ADD),
187                 createBatch(BatchStepType.GROUP_REMOVE, 2),
188                 createBatch(BatchStepType.GROUP_REMOVE),
189                 createBatch(BatchStepType.GROUP_ADD),
190                 createBatch(BatchStepType.GROUP_UPDATE, 3)
191         );
192
193         final List<BatchPlanStep> batchPlanSteps = FlatBatchUtil.assembleBatchPlan(batches);
194         Assert.assertEquals(5, batchPlanSteps.size());
195
196         int index = 0;
197         checkSegment(batchPlanSteps.get(index++), BatchStepType.GROUP_ADD, 1);
198         checkSegment(batchPlanSteps.get(index++), BatchStepType.GROUP_REMOVE, 2);
199         checkSegment(batchPlanSteps.get(index++), BatchStepType.GROUP_REMOVE, 1);
200         checkSegment(batchPlanSteps.get(index++), BatchStepType.GROUP_ADD, 1);
201         checkSegment(batchPlanSteps.get(index++), BatchStepType.GROUP_UPDATE, 3);
202     }
203
204     private static void checkSegment(final BatchPlanStep planStep, final BatchStepType stepType, final int expected) {
205         Assert.assertEquals(stepType, planStep.getStepType());
206         Assert.assertEquals(expected, planStep.getTaskBag().size());
207     }
208
209     @Test
210     public void testDetectBatchStepType() {
211         for (BatchStepType stepType : BatchStepType.values()) {
212             LOG.debug("checking detection of: {}", stepType);
213             final Batch batch = createBatch(stepType);
214             final BatchStepType actualType = FlatBatchUtil.detectBatchStepType(batch.getBatchChoice());
215             Assert.assertEquals(stepType, actualType);
216         }
217     }
218
219     private Batch createBatch(BatchStepType type) {
220         return createBatch(type, 1);
221     }
222
223     private Batch createBatch(BatchStepType type, final int size) {
224         final BatchChoice batchCase;
225         switch (type) {
226             case FLOW_ADD:
227                 batchCase = new FlatBatchAddFlowCaseBuilder()
228                         .setFlatBatchAddFlow(repeatFlatBatchAddFlowIntoList(size))
229                         .build();
230                 break;
231             case FLOW_REMOVE:
232                 batchCase = new FlatBatchRemoveFlowCaseBuilder()
233                         .setFlatBatchRemoveFlow(repeatFlatBatchRemoveFlowIntoList(size))
234                         .build();
235                 break;
236             case FLOW_UPDATE:
237                 batchCase = new FlatBatchUpdateFlowCaseBuilder()
238                         .setFlatBatchUpdateFlow(repeatFlatBatchUpdateFlowIntoList(size))
239                         .build();
240                 break;
241             case GROUP_ADD:
242                 batchCase = new FlatBatchAddGroupCaseBuilder()
243                         .setFlatBatchAddGroup(repeatFlatBatchAddGroupIntoList(size))
244                         .build();
245                 break;
246             case GROUP_REMOVE:
247                 batchCase = new FlatBatchRemoveGroupCaseBuilder()
248                         .setFlatBatchRemoveGroup(repeatFlatBatchRemoveGroupIntoList(size))
249                         .build();
250                 break;
251             case GROUP_UPDATE:
252                 batchCase = new FlatBatchUpdateGroupCaseBuilder()
253                         .setFlatBatchUpdateGroup(repeatFlatBatchUpdateGroupIntoList(size))
254                         .build();
255                 break;
256             case METER_ADD:
257                 batchCase = new FlatBatchAddMeterCaseBuilder()
258                         .setFlatBatchAddMeter(repeatFlatBatchAddMeterIntoList(size))
259                         .build();
260                 break;
261             case METER_REMOVE:
262                 batchCase = new FlatBatchRemoveMeterCaseBuilder()
263                         .setFlatBatchRemoveMeter(repeatFlatBatchRemoveMeterIntoList(size))
264                         .build();
265                 break;
266             case METER_UPDATE:
267                 batchCase = new FlatBatchUpdateMeterCaseBuilder()
268                         .setFlatBatchUpdateMeter(repeatFlatBatchUpdateMeterIntoList(size))
269                         .build();
270                 break;
271             default:
272                 LOG.warn("unsupported batch type: {}", type);
273                 throw new IllegalArgumentException("unsupported batch type: " + type);
274         }
275
276         return new BatchBuilder()
277                 .setBatchChoice(batchCase)
278                 .build();
279     }
280
281     private static List<FlatBatchAddGroup> repeatFlatBatchAddGroupIntoList(final int size) {
282         final List<FlatBatchAddGroup> list = new ArrayList<>();
283         for (int i = 0; i < size; i++) {
284             list.add(new FlatBatchAddGroupBuilder().withKey(new FlatBatchAddGroupKey(i)).build());
285         }
286         return list;
287     }
288
289     private static List<FlatBatchUpdateGroup> repeatFlatBatchUpdateGroupIntoList(final int size) {
290         final List<FlatBatchUpdateGroup> list = new ArrayList<>();
291         for (int i = 0; i < size; i++) {
292             list.add(new FlatBatchUpdateGroupBuilder().withKey(new FlatBatchUpdateGroupKey(i)).build());
293         }
294         return list;
295     }
296
297     private static List<FlatBatchRemoveGroup> repeatFlatBatchRemoveGroupIntoList(final int size) {
298         final List<FlatBatchRemoveGroup> list = new ArrayList<>();
299         for (int i = 0; i < size; i++) {
300             list.add(new FlatBatchRemoveGroupBuilder().withKey(new FlatBatchRemoveGroupKey(i)).build());
301         }
302         return list;
303     }
304
305     private static List<FlatBatchAddFlow> repeatFlatBatchAddFlowIntoList(final int size) {
306         final List<FlatBatchAddFlow> list = new ArrayList<>();
307         for (int i = 0; i < size; i++) {
308             list.add(new FlatBatchAddFlowBuilder().withKey(new FlatBatchAddFlowKey(i)).build());
309         }
310         return list;
311     }
312
313     private static List<FlatBatchUpdateFlow> repeatFlatBatchUpdateFlowIntoList(final int size) {
314         final List<FlatBatchUpdateFlow> list = new ArrayList<>();
315         for (int i = 0; i < size; i++) {
316             list.add(new FlatBatchUpdateFlowBuilder().withKey(new FlatBatchUpdateFlowKey(i)).build());
317         }
318         return list;
319     }
320
321     private static List<FlatBatchRemoveFlow> repeatFlatBatchRemoveFlowIntoList(final int size) {
322         final List<FlatBatchRemoveFlow> list = new ArrayList<>();
323         for (int i = 0; i < size; i++) {
324             list.add(new FlatBatchRemoveFlowBuilder().withKey(new FlatBatchRemoveFlowKey(i)).build());
325         }
326         return list;
327     }
328
329     private static List<FlatBatchAddMeter> repeatFlatBatchAddMeterIntoList(final int size) {
330         final List<FlatBatchAddMeter> list = new ArrayList<>();
331         for (int i = 0; i < size; i++) {
332             list.add(new FlatBatchAddMeterBuilder().withKey(new FlatBatchAddMeterKey(i)).build());
333         }
334         return list;
335     }
336
337     private static List<FlatBatchUpdateMeter> repeatFlatBatchUpdateMeterIntoList(final int size) {
338         final List<FlatBatchUpdateMeter> list = new ArrayList<>();
339         for (int i = 0; i < size; i++) {
340             list.add(new FlatBatchUpdateMeterBuilder().withKey(new FlatBatchUpdateMeterKey(i)).build());
341         }
342         return list;
343     }
344
345     private static List<FlatBatchRemoveMeter> repeatFlatBatchRemoveMeterIntoList(final int size) {
346         final List<FlatBatchRemoveMeter> list = new ArrayList<>();
347         for (int i = 0; i < size; i++) {
348             list.add(new FlatBatchRemoveMeterBuilder().withKey(new FlatBatchRemoveMeterKey(i)).build());
349         }
350         return list;
351     }
352
353     @Test
354     public void testMergeJobsResultsFutures() {
355         final BatchFailure batchFailure = new BatchFailureBuilder()
356                 .setBatchOrder(9)
357                 .setBatchItemIdChoice(new FlatBatchFailureFlowIdCaseBuilder()
358                         .setFlowId(new FlowId("11"))
359                         .build())
360                 .withKey(new BatchFailureKey(Uint16.ZERO))
361                 .build();
362         final BatchFailure batchFailure_1 = new BatchFailureBuilder()
363                 .setBatchOrder(9)
364                 .setBatchItemIdChoice(new FlatBatchFailureFlowIdCaseBuilder()
365                         .setFlowId(new FlowId("11"))
366                         .build())
367                 .withKey(new BatchFailureKey(Uint16.ONE))
368                 .build();
369
370         final ProcessFlatBatchOutput output
371                 = new ProcessFlatBatchOutputBuilder().setBatchFailure(Lists.newArrayList(batchFailure)).build();
372
373         final ProcessFlatBatchOutput output_1
374                 = new ProcessFlatBatchOutputBuilder().setBatchFailure(Lists.newArrayList(batchFailure_1)).build();
375
376         final RpcResult<ProcessFlatBatchOutput> rpcResultFailed = RpcResultBuilder.<ProcessFlatBatchOutput>failed()
377                 .withError(RpcError.ErrorType.APPLICATION, "ut-rpcError")
378                 .withResult(output).build();
379
380         final RpcResult<ProcessFlatBatchOutput> rpcResultFailed_1 = RpcResultBuilder.<ProcessFlatBatchOutput>failed()
381                 .withError(RpcError.ErrorType.APPLICATION, "ut-rpcError")
382                 .withResult(output_1).build();
383         final RpcResult<ProcessFlatBatchOutput> rpcResultSuccess = RpcResultBuilder.<ProcessFlatBatchOutput>success()
384                 .withResult(new ProcessFlatBatchOutputBuilder().setBatchFailure(new ArrayList<>())).build();
385
386         final RpcResult<ProcessFlatBatchOutput> rpcResult1
387                 = FlatBatchUtil.mergeRpcResults().apply(Lists.newArrayList(rpcResultFailed, rpcResultSuccess));
388         Assert.assertEquals(1, rpcResult1.getErrors().size());
389         Assert.assertFalse(rpcResult1.isSuccessful());
390
391         final RpcResult<ProcessFlatBatchOutput> rpcResult2
392                 = FlatBatchUtil.mergeRpcResults().apply(Lists.newArrayList(rpcResultFailed, rpcResultFailed_1));
393         Assert.assertEquals(2, rpcResult2.getErrors().size());
394         Assert.assertFalse(rpcResult2.isSuccessful());
395
396         final RpcResult<ProcessFlatBatchOutput> rpcResult3
397                 = FlatBatchUtil.mergeRpcResults().apply(Lists.newArrayList(rpcResultSuccess, rpcResultSuccess));
398         Assert.assertEquals(0, rpcResult3.getErrors().size());
399         Assert.assertTrue(rpcResult3.isSuccessful());
400     }
401 }