Make sure RequestContext has a constant XID
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / device / listener / MultiMsgCollectorImplTest.java
1 /**
2  * Copyright (c) 2015 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.impl.device.listener;
10
11 import com.google.common.util.concurrent.Runnables;
12 import java.util.List;
13 import org.junit.After;
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.Mock;
21 import org.mockito.Mockito;
22 import org.mockito.runners.MockitoJUnitRunner;
23 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
24 import org.opendaylight.openflowplugin.api.openflow.device.exception.DeviceDataException;
25 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceReplyProcessor;
26 import org.opendaylight.openflowplugin.api.openflow.device.handlers.MultiMsgCollector;
27 import org.opendaylight.openflowplugin.impl.connection.testutil.MsgGeneratorTestUtils;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReply;
30
31 /**
32  * openflowplugin-api
33  * org.opendaylight.openflowplugin.impl.openflow.device
34  *
35  * Test class for testing basic method functionality for {@link MultiMsgCollector}
36  *
37  * @author <a href="mailto:vdemcak@cisco.com">Vaclav Demcak</a>
38  * @author <a href="mailto:tkubas@cisco.com">Timotej Kubas</a>
39  *
40  * Created: Mar 23, 2015
41  */
42 @RunWith(MockitoJUnitRunner.class)
43 public class MultiMsgCollectorImplTest {
44
45     private MultiMsgCollectorImpl collector;
46     private Runnable cleanUpCheck;
47
48     @Mock
49     DeviceReplyProcessor deviceProcessor;
50     @Captor
51     ArgumentCaptor<DeviceDataException> ddeCaptor;
52     @Captor
53     ArgumentCaptor<Xid> xidCaptor;
54     @Captor
55     ArgumentCaptor<List<MultipartReply>> mmCaptor;
56
57     private final String hwTestValue = "test-value";
58     private final String expectedExpirationMsg = "MultiMsgCollector can not wait for last multipart any more";
59     private final String expectedTypMismatchMsg = "multipart message type mismatch";
60     private final String expectedUnknownXidMsg = "unknown xid received for multipart of type OFPMPDESC";
61
62     @Before
63     public void setUp() {
64         collector = new MultiMsgCollectorImpl(1);
65         collector.setDeviceReplyProcessor(deviceProcessor);
66         cleanUpCheck = Runnables.doNothing();
67     }
68
69     @After
70     public void tearDown() throws InterruptedException {
71         Thread.sleep(1100L);
72
73         // flush cache action
74         collector.registerMultipartXid(0L);
75         cleanUpCheck.run();
76         Mockito.verifyNoMoreInteractions(deviceProcessor);
77     }
78
79     /**
80      * test of ${link MultiMsgCollector#addMultipartMsg} <br>
81      *     success with message consisting of 1 part
82      */
83     @Test
84     public void testAddMultipartMsgOne() {
85         final Long xid = 1L;
86         collector.registerMultipartXid(xid);
87         collector.addMultipartMsg(MsgGeneratorTestUtils.makeMultipartDescReply(xid, hwTestValue, false).build());
88
89         Mockito.verify(deviceProcessor).processReply(xidCaptor.capture(), mmCaptor.capture());
90         Assert.assertEquals(xid, xidCaptor.getValue().getValue());
91
92         List<MultipartReply> multipartReplyList = mmCaptor.getValue();
93         Assert.assertEquals(1, multipartReplyList.size());
94         Assert.assertEquals(MultipartType.OFPMPDESC, multipartReplyList.get(0).getType());
95     }
96
97     /**
98      *  test of ${link MultiMsgCollector#addMultipartMsg} <br>
99      *     success with message consisting of 2 parts
100      */
101     @Test
102     public void testAddMultipartMsgTwo() {
103         final Long xid = 1L;
104         collector.registerMultipartXid(xid);
105         collector.addMultipartMsg(MsgGeneratorTestUtils.makeMultipartDescReply(xid, hwTestValue, true).build());
106         collector.addMultipartMsg(MsgGeneratorTestUtils.makeMultipartDescReply(xid, hwTestValue, false).build());
107
108         Mockito.verify(deviceProcessor).processReply(xidCaptor.capture(), mmCaptor.capture());
109         Assert.assertEquals(xid, xidCaptor.getValue().getValue());
110
111         List<MultipartReply> multipartReplyList = mmCaptor.getValue();
112         Assert.assertEquals(2, multipartReplyList.size());
113         Assert.assertEquals(MultipartType.OFPMPDESC, multipartReplyList.get(0).getType());
114         Assert.assertEquals(MultipartType.OFPMPDESC, multipartReplyList.get(1).getType());
115     }
116
117     /**
118      * test of ${link MultiMsgCollector#addMultipartMsg} <br>
119      *     xid not registered before message
120      */
121     @Test
122     public void testAddMultipartMsgNotExpectedXid() {
123         final Long xid = 1L;
124         collector.addMultipartMsg(MsgGeneratorTestUtils.makeMultipartDescReply(xid, hwTestValue, true).build());
125
126         Mockito.verify(deviceProcessor).processException(xidCaptor.capture(), ddeCaptor.capture());
127         Assert.assertEquals(xid, xidCaptor.getValue().getValue());
128         Assert.assertEquals(expectedUnknownXidMsg, ddeCaptor.getValue().getMessage());
129     }
130
131     /**
132      * test of ${link MultiMsgCollector#addMultipartMsg} <br>
133      *     message types are inconsistent - second message is final and should be rejected
134      */
135     @Test
136     public void testAddMultipartMsgWrongType1() {
137         final Long xid = 1L;
138         collector.registerMultipartXid(xid);
139         collector.addMultipartMsg(MsgGeneratorTestUtils.makeMultipartDescReply(xid, hwTestValue, true).build());
140         collector.addMultipartMsg(MsgGeneratorTestUtils.makeMultipartDescReply(xid, hwTestValue, false)
141                 .setType(MultipartType.OFPMPPORTDESC).build());
142
143
144         Mockito.verify(deviceProcessor).processException(xidCaptor.capture(), ddeCaptor.capture());
145         Assert.assertEquals(xid, xidCaptor.getValue().getValue());
146         Assert.assertEquals(expectedTypMismatchMsg, ddeCaptor.getValue().getMessage());
147
148         Mockito.reset(deviceProcessor);
149
150         cleanUpCheck = new Runnable() {
151             @Override
152             public void run() {
153                 Mockito.verify(deviceProcessor).processException(xidCaptor.capture(), ddeCaptor.capture());
154                 Assert.assertEquals(xid, xidCaptor.getValue().getValue());
155                 Assert.assertEquals(expectedExpirationMsg, ddeCaptor.getValue().getMessage());
156             }
157         };
158     }
159
160     /**
161      * test of ${link MultiMsgCollector#addMultipartMsg} <br>
162      *     message types are inconsistent - second message is not final and should be rejected
163      */
164     @Test
165     public void testAddMultipartMsgWrongType2() {
166         final Long xid = 1L;
167         collector.registerMultipartXid(xid);
168         collector.addMultipartMsg(MsgGeneratorTestUtils.makeMultipartDescReply(xid, hwTestValue, true).build());
169         collector.addMultipartMsg(MsgGeneratorTestUtils.makeMultipartDescReply(xid, hwTestValue, true)
170                 .setType(MultipartType.OFPMPPORTDESC).build());
171
172         Mockito.verify(deviceProcessor).processException(xidCaptor.capture(), ddeCaptor.capture());
173         Assert.assertEquals(xid, xidCaptor.getValue().getValue());
174         Assert.assertEquals(expectedTypMismatchMsg, ddeCaptor.getValue().getMessage());
175
176         Mockito.reset(deviceProcessor);
177
178         cleanUpCheck = new Runnable() {
179             @Override
180             public void run() {
181                 Mockito.verify(deviceProcessor).processException(xidCaptor.capture(), ddeCaptor.capture());
182                 Assert.assertEquals(xid, xidCaptor.getValue().getValue());
183                 Assert.assertEquals(expectedExpirationMsg, ddeCaptor.getValue().getMessage());
184             }
185         };
186     }
187
188     /**
189      * test of ${link MultiMsgCollector#addMultipartMsg} <br>
190      *     message types are inconsistent - second message and third should be rejected
191      */
192     @Test
193     public void testAddMultipartMsgWrongType3() {
194         final Long xid = 1L;
195         collector.registerMultipartXid(xid);
196         collector.addMultipartMsg(MsgGeneratorTestUtils.makeMultipartDescReply(xid, hwTestValue, true).build());
197         collector.addMultipartMsg(MsgGeneratorTestUtils.makeMultipartDescReply(xid, hwTestValue, true)
198                 .setType(MultipartType.OFPMPPORTDESC).build());
199         collector.addMultipartMsg(MsgGeneratorTestUtils.makeMultipartDescReply(xid, hwTestValue, false).build());
200
201         Mockito.verify(deviceProcessor).processException(xidCaptor.capture(), ddeCaptor.capture());
202         Assert.assertEquals(xid, xidCaptor.getValue().getValue());
203         Assert.assertEquals(expectedTypMismatchMsg, ddeCaptor.getValue().getMessage());
204
205         Mockito.verify(deviceProcessor).processReply(xidCaptor.capture(), mmCaptor.capture());
206         Assert.assertEquals(xid, xidCaptor.getValue().getValue());
207
208         List<MultipartReply> multipartReplyList = mmCaptor.getValue();
209         Assert.assertEquals(2, multipartReplyList.size());
210         Assert.assertEquals(MultipartType.OFPMPDESC, multipartReplyList.get(0).getType());
211         Assert.assertEquals(MultipartType.OFPMPDESC, multipartReplyList.get(1).getType());
212     }
213
214     /**
215      * test of ${link MultiMsgCollector#addMultipartMsg} <br>
216      *     no second message arrived within expiration limit - first message should expire
217      */
218     @Test
219     public void testAddMultipartMsgExpiration() throws InterruptedException {
220         final Long xid = 1L;
221         collector.registerMultipartXid(xid);
222         collector.addMultipartMsg(MsgGeneratorTestUtils.makeMultipartDescReply(xid, hwTestValue, true).build());
223
224         cleanUpCheck = new Runnable() {
225             @Override
226             public void run() {
227                 Mockito.verify(deviceProcessor).processException(xidCaptor.capture(), ddeCaptor.capture());
228                 Assert.assertEquals(xid, xidCaptor.getValue().getValue());
229                 Assert.assertEquals(expectedExpirationMsg, ddeCaptor.getValue().getMessage());
230             }
231         };
232     }
233
234 }