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.internal.verification.VerificationModeFactory;
23 import org.mockito.runners.MockitoJUnitRunner;
24 import org.opendaylight.openflowjava.protocol.api.connection.DeviceRequestFailedException;
25 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
26 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
27 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceReplyProcessor;
28 import org.opendaylight.openflowplugin.api.openflow.device.handlers.MultiMsgCollector;
29 import org.opendaylight.openflowplugin.impl.connection.testutil.MsgGeneratorTestUtils;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReply;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReplyMessage;
36 * org.opendaylight.openflowplugin.impl.openflow.device
38 * Test class for testing basic method functionality for {@link MultiMsgCollector}
40 * @author <a href="mailto:vdemcak@cisco.com">Vaclav Demcak</a>
41 * @author <a href="mailto:tkubas@cisco.com">Timotej Kubas</a>
43 * Created: Mar 23, 2015
45 @RunWith(MockitoJUnitRunner.class)
46 public class MultiMsgCollectorImplTest {
48 private MultiMsgCollectorImpl<MultipartReply> collector;
49 private Runnable cleanUpCheck;
52 DeviceReplyProcessor deviceProcessor;
54 ArgumentCaptor<DeviceRequestFailedException> ddeCaptor;
56 ArgumentCaptor<Xid> xidCaptor;
58 ArgumentCaptor<List<MultipartReply>> mmCaptor;
60 RequestContext<List<MultipartReply>> requestContext;
64 private final String hwTestValue = "test-value";
65 private final String expectedExpirationMsg = "MultiMsgCollector can not wait for last multipart any more";
66 private final String expectedTypMismatchMsg = "multipart message type mismatch";
67 private final String expectedUnknownXidMsg = "unknown xid received for multipart of type OFPMPDESC";
71 collector = new MultiMsgCollectorImpl<>(deviceProcessor, requestContext);
72 cleanUpCheck = Runnables.doNothing();
73 Mockito.when(requestContext.getXid()).thenReturn(new Xid(xid));
77 public void tearDown() throws InterruptedException {
82 Mockito.verifyNoMoreInteractions(deviceProcessor);
86 * test of ${link MultiMsgCollector#addMultipartMsg} <br>
87 * success with message consisting of 1 part
90 public void testAddMultipartMsgOne() {
91 collector.addMultipartMsg(MsgGeneratorTestUtils.makeMultipartDescReply(xid, hwTestValue, false).build(), false, null);
93 Mockito.verify(deviceProcessor).processReply(xidCaptor.capture(), mmCaptor.capture());
94 Assert.assertEquals(xid, xidCaptor.getValue().getValue());
96 final List<MultipartReply> multipartReplyList = mmCaptor.getValue();
97 Assert.assertEquals(1, multipartReplyList.size());
98 Assert.assertEquals(MultipartType.OFPMPDESC, multipartReplyList.get(0).getType());
102 * test of ${link MultiMsgCollector#addMultipartMsg} <br>
103 * success with message consisting of 2 parts
106 public void testAddMultipartMsgTwo() {
107 collector.addMultipartMsg(MsgGeneratorTestUtils.makeMultipartDescReply(xid, hwTestValue, true).build(), true, null);
108 collector.addMultipartMsg(MsgGeneratorTestUtils.makeMultipartDescReply(xid, hwTestValue, false).build(), false, null);
110 Mockito.verify(deviceProcessor).processReply(xidCaptor.capture(), mmCaptor.capture());
111 Assert.assertEquals(xid, xidCaptor.getValue().getValue());
113 final List<MultipartReply> multipartReplyList = mmCaptor.getValue();
114 Assert.assertEquals(2, multipartReplyList.size());
115 Assert.assertEquals(MultipartType.OFPMPDESC, multipartReplyList.get(0).getType());
116 Assert.assertEquals(MultipartType.OFPMPDESC, multipartReplyList.get(1).getType());
120 * test of ${link MultiMsgCollector#addMultipartMsg} <br>
121 * xid not registered before message
123 @Test(expected=IllegalArgumentException.class)
124 public void testAddMultipartMsgNotExpectedXid() {
125 final Long dif_xid = 5L;
126 final MultipartReplyMessage mrMsg = MsgGeneratorTestUtils.makeMultipartDescReply(dif_xid, hwTestValue, true).build();
127 collector.addMultipartMsg(mrMsg, true, null);
131 * test of ${link MultiMsgCollector#addMultipartMsg} <br>
132 * message types are inconsistent - second message is final and should be rejected
135 public void testAddMultipartMsgWrongType1() {
136 collector.addMultipartMsg(MsgGeneratorTestUtils.makeMultipartDescReply(xid, hwTestValue, true).build(), true, null);
137 collector.addMultipartMsg(MsgGeneratorTestUtils.makeMultipartDescReply(xid, hwTestValue, false)
138 .setType(MultipartType.OFPMPPORTDESC).build(), false, null);
140 Mockito.verify(deviceProcessor).processReply(xidCaptor.capture(), mmCaptor.capture());
141 Assert.assertEquals(xid, xidCaptor.getValue().getValue());
143 Mockito.reset(deviceProcessor);
145 cleanUpCheck = new Runnable() {
148 Mockito.verify(deviceProcessor, VerificationModeFactory.noMoreInteractions())
149 .processReply(xidCaptor.capture(), mmCaptor.capture());
150 Assert.assertEquals(xid, xidCaptor.getValue().getValue());
156 * test of ${link MultiMsgCollector#addMultipartMsg} <br>
157 * message types are inconsistent - second message is not final and should be rejected
160 public void testAddMultipartMsgWrongType2() {
161 collector.addMultipartMsg(MsgGeneratorTestUtils.makeMultipartDescReply(xid, hwTestValue, true).build(), true, null);
162 collector.addMultipartMsg(MsgGeneratorTestUtils.makeMultipartDescReply(xid, hwTestValue, true)
163 .setType(MultipartType.OFPMPPORTDESC).build(), true, null);
164 collector.addMultipartMsg(MsgGeneratorTestUtils.makeMultipartDescReply(xid, hwTestValue, false).build(), false, null);
166 Mockito.verify(deviceProcessor).processReply(xidCaptor.capture(), mmCaptor.capture());
167 Mockito.verify(deviceProcessor).processReply(xidCaptor.capture(), mmCaptor.capture());
168 Assert.assertEquals(xid, xidCaptor.getValue().getValue());
170 Mockito.reset(deviceProcessor);
172 cleanUpCheck = new Runnable() {
175 Mockito.verify(deviceProcessor, VerificationModeFactory.noMoreInteractions())
176 .processReply(xidCaptor.capture(), mmCaptor.capture());
177 Assert.assertEquals(xid, xidCaptor.getValue().getValue());
183 * test of ${link MultiMsgCollector#addMultipartMsg} <br>
184 * message types are inconsistent - second message and third should be rejected
187 public void testAddMultipartMsgWrongType3() {
188 collector.addMultipartMsg(MsgGeneratorTestUtils.makeMultipartDescReply(xid, hwTestValue, true).build(), true, null);
189 collector.addMultipartMsg(MsgGeneratorTestUtils.makeMultipartDescReply(xid, hwTestValue, true)
190 .setType(MultipartType.OFPMPPORTDESC).build(), true, null);
191 collector.addMultipartMsg(MsgGeneratorTestUtils.makeMultipartDescReply(xid, hwTestValue, false).build(), false, null);
193 Mockito.verify(deviceProcessor).processReply(xidCaptor.capture(), mmCaptor.capture());
194 Assert.assertEquals(xid, xidCaptor.getValue().getValue());
196 Mockito.verify(deviceProcessor).processReply(xidCaptor.capture(), mmCaptor.capture());
197 Assert.assertEquals(xid, xidCaptor.getValue().getValue());
199 final List<MultipartReply> multipartReplyList = mmCaptor.getValue();
200 Assert.assertEquals(3, multipartReplyList.size());
201 Assert.assertEquals(MultipartType.OFPMPDESC, multipartReplyList.get(0).getType());
202 Assert.assertEquals(MultipartType.OFPMPPORTDESC, multipartReplyList.get(1).getType());
203 Assert.assertEquals(MultipartType.OFPMPDESC, multipartReplyList.get(2).getType());