Merge "BUG-2613: Migrating Openflow Specific NSF from controller project to openflowp...
[openflowplugin.git] / openflowplugin / src / test / java / org / opendaylight / openflowplugin / openflow / md / util / PollableQueuesPriorityZipperTest.java
1 /**
2  * Copyright (c) 2014 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.openflow.md.util;
9
10 import java.util.ArrayList;
11 import java.util.List;
12 import java.util.Queue;
13 import java.util.concurrent.LinkedBlockingQueue;
14
15 import org.junit.Assert;
16 import org.junit.Test;
17 import org.opendaylight.openflowplugin.api.openflow.md.util.PollableQueuesPriorityZipper;
18
19 import com.google.common.collect.Lists;
20
21 /**
22  * test for {@link PollableQueuesPriorityZipper}
23  */
24 public class PollableQueuesPriorityZipperTest {
25
26     /**
27      * Test method for
28      * {@link org.opendaylight.openflowplugin.api.openflow.md.util.PollableQueuesPriorityZipper#poll()}
29      * .
30      */
31     @Test
32     public void testPoll3() {
33         Queue<String> l1 = new LinkedBlockingQueue<String>(Lists.newArrayList(
34                 "1", "2", "3"));
35         Queue<String> l2 = new LinkedBlockingQueue<String>(Lists.newArrayList(
36                 "a", "b", "c", "d"));
37         Queue<String> l3 = new LinkedBlockingQueue<String>(Lists.newArrayList(
38                 "A", "B"));
39
40         PollableQueuesPriorityZipper<String> zipper = new PollableQueuesPriorityZipper<>();
41         zipper.setPrioritizedSource(l1);
42         zipper.addSource(l2);
43         zipper.addSource(l3);
44
45         String[] expected = new String[] { "1", "2", "3", "a", "A", "b", "B",
46                 "c", "d", null, "XXX" };
47         List<String> result = new ArrayList<>();
48         while (true) {
49             String data = zipper.poll();
50             result.add(data);
51             if (data == null) {
52                 break;
53             }
54         }
55         l1.offer("XXX");
56         result.add(zipper.poll());
57         Assert.assertArrayEquals(expected, result.toArray());
58     }
59
60     /**
61      * Test method for
62      * {@link org.opendaylight.openflowplugin.api.openflow.md.util.PollableQueuesPriorityZipper#poll()}
63      * .
64      */
65     @Test
66     public void testPoll2() {
67         Queue<String> l1 = new LinkedBlockingQueue<String>(Lists.newArrayList(
68                 "1", "2", "3"));
69         Queue<String> l3 = new LinkedBlockingQueue<String>(Lists.newArrayList(
70                 "A", "B"));
71
72         PollableQueuesPriorityZipper<String> zipper = new PollableQueuesPriorityZipper<>();
73         zipper.setPrioritizedSource(l1);
74         zipper.addSource(l3);
75
76         String[] expected = new String[] { "1", "2", "3", "A", "B", null, "XXX" };
77         List<String> result = new ArrayList<>();
78         while (true) {
79             String data = zipper.poll();
80             result.add(data);
81             if (data == null) {
82                 break;
83             }
84         }
85         l1.offer("XXX");
86         result.add(zipper.poll());
87         Assert.assertArrayEquals(expected, result.toArray());
88     }
89
90 }