2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.openflowplugin.impl.device.listener;
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;
33 * org.opendaylight.openflowplugin.impl.openflow.device
35 * Test class for testing basic method functionality for {@link MultiMsgCollector}
37 * @author <a href="mailto:vdemcak@cisco.com">Vaclav Demcak</a>
38 * @author <a href="mailto:tkubas@cisco.com">Timotej Kubas</a>
40 * Created: Mar 23, 2015
42 @RunWith(MockitoJUnitRunner.class)
43 public class MultiMsgCollectorImplTest {
45 private MultiMsgCollectorImpl collector;
46 private Runnable cleanUpCheck;
49 DeviceReplyProcessor deviceProcessor;
51 ArgumentCaptor<DeviceDataException> ddeCaptor;
53 ArgumentCaptor<Xid> xidCaptor;
55 ArgumentCaptor<List<MultipartReply>> mmCaptor;
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";
64 collector = new MultiMsgCollectorImpl(1);
65 collector.setDeviceReplyProcessor(deviceProcessor);
66 cleanUpCheck = Runnables.doNothing();
70 public void tearDown() throws InterruptedException {
74 collector.registerMultipartXid(0L);
76 Mockito.verifyNoMoreInteractions(deviceProcessor);
80 * test of ${link MultiMsgCollector#addMultipartMsg} <br>
81 * success with message consisting of 1 part
84 public void testAddMultipartMsgOne() {
86 collector.registerMultipartXid(xid);
87 collector.addMultipartMsg(MsgGeneratorTestUtils.makeMultipartDescReply(xid, hwTestValue, false).build());
89 Mockito.verify(deviceProcessor).processReply(xidCaptor.capture(), mmCaptor.capture());
90 Assert.assertEquals(xid, xidCaptor.getValue().getValue());
92 List<MultipartReply> multipartReplyList = mmCaptor.getValue();
93 Assert.assertEquals(1, multipartReplyList.size());
94 Assert.assertEquals(MultipartType.OFPMPDESC, multipartReplyList.get(0).getType());
98 * test of ${link MultiMsgCollector#addMultipartMsg} <br>
99 * success with message consisting of 2 parts
102 public void testAddMultipartMsgTwo() {
104 collector.registerMultipartXid(xid);
105 collector.addMultipartMsg(MsgGeneratorTestUtils.makeMultipartDescReply(xid, hwTestValue, true).build());
106 collector.addMultipartMsg(MsgGeneratorTestUtils.makeMultipartDescReply(xid, hwTestValue, false).build());
108 Mockito.verify(deviceProcessor).processReply(xidCaptor.capture(), mmCaptor.capture());
109 Assert.assertEquals(xid, xidCaptor.getValue().getValue());
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());
118 * test of ${link MultiMsgCollector#addMultipartMsg} <br>
119 * xid not registered before message
122 public void testAddMultipartMsgNotExpectedXid() {
124 collector.addMultipartMsg(MsgGeneratorTestUtils.makeMultipartDescReply(xid, hwTestValue, true).build());
126 Mockito.verify(deviceProcessor).processException(xidCaptor.capture(), ddeCaptor.capture());
127 Assert.assertEquals(xid, xidCaptor.getValue().getValue());
128 Assert.assertEquals(expectedUnknownXidMsg, ddeCaptor.getValue().getMessage());
132 * test of ${link MultiMsgCollector#addMultipartMsg} <br>
133 * message types are inconsistent - second message is final and should be rejected
136 public void testAddMultipartMsgWrongType1() {
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());
144 Mockito.verify(deviceProcessor).processException(xidCaptor.capture(), ddeCaptor.capture());
145 Assert.assertEquals(xid, xidCaptor.getValue().getValue());
146 Assert.assertEquals(expectedTypMismatchMsg, ddeCaptor.getValue().getMessage());
148 Mockito.reset(deviceProcessor);
150 cleanUpCheck = new Runnable() {
153 Mockito.verify(deviceProcessor).processException(xidCaptor.capture(), ddeCaptor.capture());
154 Assert.assertEquals(xid, xidCaptor.getValue().getValue());
155 Assert.assertEquals(expectedExpirationMsg, ddeCaptor.getValue().getMessage());
161 * test of ${link MultiMsgCollector#addMultipartMsg} <br>
162 * message types are inconsistent - second message is not final and should be rejected
165 public void testAddMultipartMsgWrongType2() {
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());
172 Mockito.verify(deviceProcessor).processException(xidCaptor.capture(), ddeCaptor.capture());
173 Assert.assertEquals(xid, xidCaptor.getValue().getValue());
174 Assert.assertEquals(expectedTypMismatchMsg, ddeCaptor.getValue().getMessage());
176 Mockito.reset(deviceProcessor);
178 cleanUpCheck = new Runnable() {
181 Mockito.verify(deviceProcessor).processException(xidCaptor.capture(), ddeCaptor.capture());
182 Assert.assertEquals(xid, xidCaptor.getValue().getValue());
183 Assert.assertEquals(expectedExpirationMsg, ddeCaptor.getValue().getMessage());
189 * test of ${link MultiMsgCollector#addMultipartMsg} <br>
190 * message types are inconsistent - second message and third should be rejected
193 public void testAddMultipartMsgWrongType3() {
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());
201 Mockito.verify(deviceProcessor).processException(xidCaptor.capture(), ddeCaptor.capture());
202 Assert.assertEquals(xid, xidCaptor.getValue().getValue());
203 Assert.assertEquals(expectedTypMismatchMsg, ddeCaptor.getValue().getMessage());
205 Mockito.verify(deviceProcessor).processReply(xidCaptor.capture(), mmCaptor.capture());
206 Assert.assertEquals(xid, xidCaptor.getValue().getValue());
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());
215 * test of ${link MultiMsgCollector#addMultipartMsg} <br>
216 * no second message arrived within expiration limit - first message should expire
219 public void testAddMultipartMsgExpiration() throws InterruptedException {
221 collector.registerMultipartXid(xid);
222 collector.addMultipartMsg(MsgGeneratorTestUtils.makeMultipartDescReply(xid, hwTestValue, true).build());
224 cleanUpCheck = new Runnable() {
227 Mockito.verify(deviceProcessor).processException(xidCaptor.capture(), ddeCaptor.capture());
228 Assert.assertEquals(xid, xidCaptor.getValue().getValue());
229 Assert.assertEquals(expectedExpirationMsg, ddeCaptor.getValue().getMessage());