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
37 * Test class for testing basic method functionality for {@link MultiMsgCollector}.
39 * @author <a href="mailto:vdemcak@cisco.com">Vaclav Demcak</a>
40 * @author <a href="mailto:tkubas@cisco.com">Timotej Kubas</a>
43 @RunWith(MockitoJUnitRunner.class)
44 public class MultiMsgCollectorImplTest {
46 private MultiMsgCollectorImpl<MultipartReply> collector;
47 private Runnable cleanUpCheck;
50 DeviceReplyProcessor deviceProcessor;
52 ArgumentCaptor<DeviceRequestFailedException> ddeCaptor;
54 ArgumentCaptor<Xid> xidCaptor;
56 ArgumentCaptor<List<MultipartReply>> mmCaptor;
58 RequestContext<List<MultipartReply>> requestContext;
62 private final String hwTestValue = "test-value";
63 private final String expectedExpirationMsg = "MultiMsgCollector can not wait for last multipart any more";
64 private final String expectedTypMismatchMsg = "multipart message type mismatch";
65 private final String expectedUnknownXidMsg = "unknown xid received for multipart of type OFPMPDESC";
69 collector = new MultiMsgCollectorImpl<>(deviceProcessor, requestContext);
70 cleanUpCheck = Runnables.doNothing();
71 Mockito.when(requestContext.getXid()).thenReturn(new Xid(xid));
75 public void tearDown() throws InterruptedException {
80 Mockito.verifyNoMoreInteractions(deviceProcessor);
84 * Test of ${link MultiMsgCollector#addMultipartMsg}
85 * success with message consisting of 1 part.
88 public void testAddMultipartMsgOne() {
89 collector.addMultipartMsg(MsgGeneratorTestUtils
90 .makeMultipartDescReply(xid, hwTestValue, false).build(), false, null);
92 Mockito.verify(deviceProcessor).processReply(xidCaptor.capture(), mmCaptor.capture());
93 Assert.assertEquals(xid, xidCaptor.getValue().getValue());
95 final List<MultipartReply> multipartReplyList = mmCaptor.getValue();
96 Assert.assertEquals(1, multipartReplyList.size());
97 Assert.assertEquals(MultipartType.OFPMPDESC, multipartReplyList.get(0).getType());
101 * Test of ${link MultiMsgCollector#addMultipartMsg}
102 * success with message consisting of 2 parts.
105 public void testAddMultipartMsgTwo() {
106 collector.addMultipartMsg(MsgGeneratorTestUtils
107 .makeMultipartDescReply(xid, hwTestValue, true).build(), true, null);
108 collector.addMultipartMsg(MsgGeneratorTestUtils
109 .makeMultipartDescReply(xid, hwTestValue, false).build(), false, null);
111 Mockito.verify(deviceProcessor).processReply(xidCaptor.capture(), mmCaptor.capture());
112 Assert.assertEquals(xid, xidCaptor.getValue().getValue());
114 final List<MultipartReply> multipartReplyList = mmCaptor.getValue();
115 Assert.assertEquals(2, multipartReplyList.size());
116 Assert.assertEquals(MultipartType.OFPMPDESC, multipartReplyList.get(0).getType());
117 Assert.assertEquals(MultipartType.OFPMPDESC, multipartReplyList.get(1).getType());
121 * Test of ${link MultiMsgCollector#addMultipartMsg}
122 * xid not registered before message.
124 @Test(expected = IllegalArgumentException.class)
125 public void testAddMultipartMsgNotExpectedXid() {
126 final Long dif_xid = 5L;
127 final MultipartReplyMessage mrMsg = MsgGeneratorTestUtils
128 .makeMultipartDescReply(dif_xid, hwTestValue, true).build();
129 collector.addMultipartMsg(mrMsg, true, null);
133 * Test of ${link MultiMsgCollector#addMultipartMsg}
134 * message types are inconsistent - second message is final and should be rejected.
137 public void testAddMultipartMsgWrongType1() {
138 collector.addMultipartMsg(MsgGeneratorTestUtils
139 .makeMultipartDescReply(xid, hwTestValue, true).build(), true, null);
140 collector.addMultipartMsg(MsgGeneratorTestUtils.makeMultipartDescReply(xid, hwTestValue, false)
141 .setType(MultipartType.OFPMPPORTDESC).build(), false, null);
143 Mockito.verify(deviceProcessor).processReply(xidCaptor.capture(), mmCaptor.capture());
144 Assert.assertEquals(xid, xidCaptor.getValue().getValue());
146 Mockito.reset(deviceProcessor);
148 cleanUpCheck = new Runnable() {
151 Mockito.verify(deviceProcessor, VerificationModeFactory.noMoreInteractions())
152 .processReply(xidCaptor.capture(), mmCaptor.capture());
153 Assert.assertEquals(xid, xidCaptor.getValue().getValue());
159 * Test of ${link MultiMsgCollector#addMultipartMsg}
160 * message types are inconsistent - second message is not final and should be rejected.
163 public void testAddMultipartMsgWrongType2() {
164 collector.addMultipartMsg(MsgGeneratorTestUtils
165 .makeMultipartDescReply(xid, hwTestValue, true).build(), true, null);
166 collector.addMultipartMsg(MsgGeneratorTestUtils.makeMultipartDescReply(xid, hwTestValue, true)
167 .setType(MultipartType.OFPMPPORTDESC).build(), true, null);
168 collector.addMultipartMsg(MsgGeneratorTestUtils
169 .makeMultipartDescReply(xid, hwTestValue, false).build(), false, null);
171 Mockito.verify(deviceProcessor).processReply(xidCaptor.capture(), mmCaptor.capture());
172 Mockito.verify(deviceProcessor).processReply(xidCaptor.capture(), mmCaptor.capture());
173 Assert.assertEquals(xid, xidCaptor.getValue().getValue());
175 Mockito.reset(deviceProcessor);
177 cleanUpCheck = new Runnable() {
180 Mockito.verify(deviceProcessor, VerificationModeFactory.noMoreInteractions())
181 .processReply(xidCaptor.capture(), mmCaptor.capture());
182 Assert.assertEquals(xid, xidCaptor.getValue().getValue());
188 * Test of ${link MultiMsgCollector#addMultipartMsg}
189 * message types are inconsistent - second message and third should be rejected.
192 public void testAddMultipartMsgWrongType3() {
193 collector.addMultipartMsg(MsgGeneratorTestUtils
194 .makeMultipartDescReply(xid, hwTestValue, true).build(), true, null);
195 collector.addMultipartMsg(MsgGeneratorTestUtils.makeMultipartDescReply(xid, hwTestValue, true)
196 .setType(MultipartType.OFPMPPORTDESC).build(), true, null);
197 collector.addMultipartMsg(MsgGeneratorTestUtils
198 .makeMultipartDescReply(xid, hwTestValue, false).build(), false, null);
200 Mockito.verify(deviceProcessor).processReply(xidCaptor.capture(), mmCaptor.capture());
201 Assert.assertEquals(xid, xidCaptor.getValue().getValue());
203 Mockito.verify(deviceProcessor).processReply(xidCaptor.capture(), mmCaptor.capture());
204 Assert.assertEquals(xid, xidCaptor.getValue().getValue());
206 final List<MultipartReply> multipartReplyList = mmCaptor.getValue();
207 Assert.assertEquals(3, multipartReplyList.size());
208 Assert.assertEquals(MultipartType.OFPMPDESC, multipartReplyList.get(0).getType());
209 Assert.assertEquals(MultipartType.OFPMPPORTDESC, multipartReplyList.get(1).getType());
210 Assert.assertEquals(MultipartType.OFPMPDESC, multipartReplyList.get(2).getType());