Fix codestyle
[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 = () -> {
149             Mockito.verify(deviceProcessor, VerificationModeFactory.noMoreInteractions())
150                 .processReply(xidCaptor.capture(), mmCaptor.capture());
151             Assert.assertEquals(xid, xidCaptor.getValue().getValue());
152         };
153     }
154
155     /**
156      * Test of ${link MultiMsgCollector#addMultipartMsg}
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
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);
167
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());
171
172         Mockito.reset(deviceProcessor);
173
174         cleanUpCheck = () -> {
175             Mockito.verify(deviceProcessor, VerificationModeFactory.noMoreInteractions())
176                 .processReply(xidCaptor.capture(), mmCaptor.capture());
177             Assert.assertEquals(xid, xidCaptor.getValue().getValue());
178         };
179     }
180
181     /**
182      * Test of ${link MultiMsgCollector#addMultipartMsg}
183      * message types are inconsistent - second message and third should be rejected.
184      */
185     @Test
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);
193
194         Mockito.verify(deviceProcessor).processReply(xidCaptor.capture(), mmCaptor.capture());
195         Assert.assertEquals(xid, xidCaptor.getValue().getValue());
196
197         Mockito.verify(deviceProcessor).processReply(xidCaptor.capture(), mmCaptor.capture());
198         Assert.assertEquals(xid, xidCaptor.getValue().getValue());
199
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());
205     }
206 }