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
8 package org.opendaylight.openflowplugin.impl.device.listener;
10 import com.google.common.util.concurrent.Runnables;
11 import java.util.List;
12 import org.junit.After;
13 import org.junit.Assert;
14 import org.junit.Before;
15 import org.junit.Test;
16 import org.junit.runner.RunWith;
17 import org.mockito.ArgumentCaptor;
18 import org.mockito.Captor;
19 import org.mockito.Mock;
20 import org.mockito.Mockito;
21 import org.mockito.internal.verification.VerificationModeFactory;
22 import org.mockito.junit.MockitoJUnitRunner;
23 import org.opendaylight.openflowjava.protocol.api.connection.DeviceRequestFailedException;
24 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
25 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
26 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceReplyProcessor;
27 import org.opendaylight.openflowplugin.api.openflow.device.handlers.MultiMsgCollector;
28 import org.opendaylight.openflowplugin.impl.connection.testutil.MsgGeneratorTestUtils;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReply;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReplyMessage;
32 import org.opendaylight.yangtools.yang.common.Uint32;
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(Uint32.valueOf(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().toJava());
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().toJava());
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().toJava());
146 Mockito.reset(deviceProcessor);
148 cleanUpCheck = () -> {
149 Mockito.verify(deviceProcessor, VerificationModeFactory.noMoreInteractions())
150 .processReply(xidCaptor.capture(), mmCaptor.capture());
151 Assert.assertEquals(xid, xidCaptor.getValue().getValue().toJava());
156 * Test of ${link MultiMsgCollector#addMultipartMsg}
157 * message types are inconsistent - second message is not final and should be rejected.
160 public void testAddMultipartMsgWrongType2() {
161 collector.addMultipartMsg(MsgGeneratorTestUtils
162 .makeMultipartDescReply(xid, hwTestValue, true).build(), true, null);
163 collector.addMultipartMsg(MsgGeneratorTestUtils.makeMultipartDescReply(xid, hwTestValue, true)
164 .setType(MultipartType.OFPMPPORTDESC).build(), true, null);
165 collector.addMultipartMsg(MsgGeneratorTestUtils
166 .makeMultipartDescReply(xid, hwTestValue, false).build(), false, null);
168 Mockito.verify(deviceProcessor).processReply(xidCaptor.capture(), mmCaptor.capture());
169 Mockito.verify(deviceProcessor).processReply(xidCaptor.capture(), mmCaptor.capture());
170 Assert.assertEquals(xid, xidCaptor.getValue().getValue().toJava());
172 Mockito.reset(deviceProcessor);
174 cleanUpCheck = () -> {
175 Mockito.verify(deviceProcessor, VerificationModeFactory.noMoreInteractions())
176 .processReply(xidCaptor.capture(), mmCaptor.capture());
177 Assert.assertEquals(xid, xidCaptor.getValue().getValue().toJava());
182 * Test of ${link MultiMsgCollector#addMultipartMsg}
183 * message types are inconsistent - second message and third should be rejected.
186 public void testAddMultipartMsgWrongType3() {
187 collector.addMultipartMsg(MsgGeneratorTestUtils
188 .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
192 .makeMultipartDescReply(xid, hwTestValue, false).build(), false, null);
194 Mockito.verify(deviceProcessor).processReply(xidCaptor.capture(), mmCaptor.capture());
195 Assert.assertEquals(xid, xidCaptor.getValue().getValue().toJava());
197 Mockito.verify(deviceProcessor).processReply(xidCaptor.capture(), mmCaptor.capture());
198 Assert.assertEquals(xid, xidCaptor.getValue().getValue().toJava());
200 final List<MultipartReply> multipartReplyList = mmCaptor.getValue();
201 Assert.assertEquals(3, multipartReplyList.size());
202 Assert.assertEquals(MultipartType.OFPMPDESC, multipartReplyList.get(0).getType());
203 Assert.assertEquals(MultipartType.OFPMPPORTDESC, multipartReplyList.get(1).getType());
204 Assert.assertEquals(MultipartType.OFPMPDESC, multipartReplyList.get(2).getType());