379326545925ba5c4a963c68085f8e3710c58d01
[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.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;
33
34 /**
35  * openflowplugin-api
36  * org.opendaylight.openflowplugin.impl.openflow.device
37  * Test class for testing basic method functionality for {@link MultiMsgCollector}.
38  *
39  * @author <a href="mailto:vdemcak@cisco.com">Vaclav Demcak</a>
40  * @author <a href="mailto:tkubas@cisco.com">Timotej Kubas</a>
41  *
42  */
43 @RunWith(MockitoJUnitRunner.class)
44 public class MultiMsgCollectorImplTest {
45
46     private MultiMsgCollectorImpl<MultipartReply> collector;
47     private Runnable cleanUpCheck;
48
49     @Mock
50     DeviceReplyProcessor deviceProcessor;
51     @Captor
52     ArgumentCaptor<DeviceRequestFailedException> ddeCaptor;
53     @Captor
54     ArgumentCaptor<Xid> xidCaptor;
55     @Captor
56     ArgumentCaptor<List<MultipartReply>> mmCaptor;
57     @Mock
58     RequestContext<List<MultipartReply>> requestContext;
59     final Long xid = 1L;
60
61
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";
66
67     @Before
68     public void setUp() {
69         collector = new MultiMsgCollectorImpl<>(deviceProcessor, requestContext);
70         cleanUpCheck = Runnables.doNothing();
71         Mockito.when(requestContext.getXid()).thenReturn(new Xid(xid));
72     }
73
74     @After
75     public void tearDown() throws InterruptedException {
76         Thread.sleep(1100L);
77
78         // flush cache action
79         cleanUpCheck.run();
80         Mockito.verifyNoMoreInteractions(deviceProcessor);
81     }
82
83     /**
84      * Test of ${link MultiMsgCollector#addMultipartMsg}
85      * success with message consisting of 1 part.
86      */
87     @Test
88     public void testAddMultipartMsgOne() {
89         collector.addMultipartMsg(MsgGeneratorTestUtils
90                 .makeMultipartDescReply(xid, hwTestValue, false).build(), false, null);
91
92         Mockito.verify(deviceProcessor).processReply(xidCaptor.capture(), mmCaptor.capture());
93         Assert.assertEquals(xid, xidCaptor.getValue().getValue());
94
95         final List<MultipartReply> multipartReplyList = mmCaptor.getValue();
96         Assert.assertEquals(1, multipartReplyList.size());
97         Assert.assertEquals(MultipartType.OFPMPDESC, multipartReplyList.get(0).getType());
98     }
99
100     /**
101      * Test of ${link MultiMsgCollector#addMultipartMsg}
102      * success with message consisting of 2 parts.
103      */
104     @Test
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);
110
111         Mockito.verify(deviceProcessor).processReply(xidCaptor.capture(), mmCaptor.capture());
112         Assert.assertEquals(xid, xidCaptor.getValue().getValue());
113
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());
118     }
119
120     /**
121      * Test of ${link MultiMsgCollector#addMultipartMsg}
122      * xid not registered before message.
123      */
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);
130     }
131
132     /**
133      * Test of ${link MultiMsgCollector#addMultipartMsg}
134      * message types are inconsistent - second message is final and should be rejected.
135      */
136     @Test
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);
142
143         Mockito.verify(deviceProcessor).processReply(xidCaptor.capture(), mmCaptor.capture());
144         Assert.assertEquals(xid, xidCaptor.getValue().getValue());
145
146         Mockito.reset(deviceProcessor);
147
148         cleanUpCheck = new Runnable() {
149             @Override
150             public void run() {
151                 Mockito.verify(deviceProcessor, VerificationModeFactory.noMoreInteractions())
152                     .processReply(xidCaptor.capture(), mmCaptor.capture());
153                 Assert.assertEquals(xid, xidCaptor.getValue().getValue());
154             }
155         };
156     }
157
158     /**
159      * Test of ${link MultiMsgCollector#addMultipartMsg}
160      * message types are inconsistent - second message is not final and should be rejected.
161      */
162     @Test
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);
170
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());
174
175         Mockito.reset(deviceProcessor);
176
177         cleanUpCheck = new Runnable() {
178             @Override
179             public void run() {
180                 Mockito.verify(deviceProcessor, VerificationModeFactory.noMoreInteractions())
181                     .processReply(xidCaptor.capture(), mmCaptor.capture());
182                 Assert.assertEquals(xid, xidCaptor.getValue().getValue());
183             }
184         };
185     }
186
187     /**
188      * Test of ${link MultiMsgCollector#addMultipartMsg}
189      * message types are inconsistent - second message and third should be rejected.
190      */
191     @Test
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);
199
200         Mockito.verify(deviceProcessor).processReply(xidCaptor.capture(), mmCaptor.capture());
201         Assert.assertEquals(xid, xidCaptor.getValue().getValue());
202
203         Mockito.verify(deviceProcessor).processReply(xidCaptor.capture(), mmCaptor.capture());
204         Assert.assertEquals(xid, xidCaptor.getValue().getValue());
205
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());
211     }
212 }