Merge changes from topic 'ofj-models-to-ofp-models'
[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  * <p/>
38  * Test class for testing basic method functionality for {@link MultiMsgCollector}
39  *
40  * @author <a href="mailto:vdemcak@cisco.com">Vaclav Demcak</a>
41  * @author <a href="mailto:tkubas@cisco.com">Timotej Kubas</a>
42  *         <p/>
43  *         Created: Mar 23, 2015
44  */
45 @RunWith(MockitoJUnitRunner.class)
46 public class MultiMsgCollectorImplTest {
47
48     private MultiMsgCollectorImpl<MultipartReply> collector;
49     private Runnable cleanUpCheck;
50
51     @Mock
52     DeviceReplyProcessor deviceProcessor;
53     @Captor
54     ArgumentCaptor<DeviceRequestFailedException> ddeCaptor;
55     @Captor
56     ArgumentCaptor<Xid> xidCaptor;
57     @Captor
58     ArgumentCaptor<List<MultipartReply>> mmCaptor;
59     @Mock
60     RequestContext<List<MultipartReply>> requestContext;
61     final Long xid = 1L;
62
63
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";
68
69     @Before
70     public void setUp() {
71         collector = new MultiMsgCollectorImpl<>(deviceProcessor, requestContext);
72         cleanUpCheck = Runnables.doNothing();
73         Mockito.when(requestContext.getXid()).thenReturn(new Xid(xid));
74     }
75
76     @After
77     public void tearDown() throws InterruptedException {
78         Thread.sleep(1100L);
79
80         // flush cache action
81         cleanUpCheck.run();
82         Mockito.verifyNoMoreInteractions(deviceProcessor);
83     }
84
85     /**
86      * test of ${link MultiMsgCollector#addMultipartMsg} <br>
87      * success with message consisting of 1 part
88      */
89     @Test
90     public void testAddMultipartMsgOne() {
91         collector.addMultipartMsg(MsgGeneratorTestUtils.makeMultipartDescReply(xid, hwTestValue, false).build(), false, null);
92
93         Mockito.verify(deviceProcessor).processReply(xidCaptor.capture(), mmCaptor.capture());
94         Assert.assertEquals(xid, xidCaptor.getValue().getValue());
95
96         final List<MultipartReply> multipartReplyList = mmCaptor.getValue();
97         Assert.assertEquals(1, multipartReplyList.size());
98         Assert.assertEquals(MultipartType.OFPMPDESC, multipartReplyList.get(0).getType());
99     }
100
101     /**
102      * test of ${link MultiMsgCollector#addMultipartMsg} <br>
103      * success with message consisting of 2 parts
104      */
105     @Test
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);
109
110         Mockito.verify(deviceProcessor).processReply(xidCaptor.capture(), mmCaptor.capture());
111         Assert.assertEquals(xid, xidCaptor.getValue().getValue());
112
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());
117     }
118
119     /**
120      * test of ${link MultiMsgCollector#addMultipartMsg} <br>
121      * xid not registered before message
122      */
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);
128     }
129
130     /**
131      * test of ${link MultiMsgCollector#addMultipartMsg} <br>
132      * message types are inconsistent - second message is final and should be rejected
133      */
134     @Test
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);
139
140         Mockito.verify(deviceProcessor).processReply(xidCaptor.capture(), mmCaptor.capture());
141         Assert.assertEquals(xid, xidCaptor.getValue().getValue());
142
143         Mockito.reset(deviceProcessor);
144
145         cleanUpCheck = new Runnable() {
146             @Override
147             public void run() {
148                 Mockito.verify(deviceProcessor, VerificationModeFactory.noMoreInteractions())
149                     .processReply(xidCaptor.capture(), mmCaptor.capture());
150                 Assert.assertEquals(xid, xidCaptor.getValue().getValue());
151             }
152         };
153     }
154
155     /**
156      * test of ${link MultiMsgCollector#addMultipartMsg} <br>
157      * message types are inconsistent - second message is not final and should be rejected
158      */
159     @Test
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);
165
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());
169
170         Mockito.reset(deviceProcessor);
171
172         cleanUpCheck = new Runnable() {
173             @Override
174             public void run() {
175                 Mockito.verify(deviceProcessor, VerificationModeFactory.noMoreInteractions())
176                     .processReply(xidCaptor.capture(), mmCaptor.capture());
177                 Assert.assertEquals(xid, xidCaptor.getValue().getValue());
178             }
179         };
180     }
181
182     /**
183      * test of ${link MultiMsgCollector#addMultipartMsg} <br>
184      * message types are inconsistent - second message and third should be rejected
185      */
186     @Test
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);
192
193         Mockito.verify(deviceProcessor).processReply(xidCaptor.capture(), mmCaptor.capture());
194         Assert.assertEquals(xid, xidCaptor.getValue().getValue());
195
196         Mockito.verify(deviceProcessor).processReply(xidCaptor.capture(), mmCaptor.capture());
197         Assert.assertEquals(xid, xidCaptor.getValue().getValue());
198
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());
204     }
205 }