MultiMsgCollector for Multipart response messaging
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / device / 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;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertTrue;
14 import static org.junit.Assert.fail;
15 import com.google.common.util.concurrent.ListenableFuture;
16 import java.util.Arrays;
17 import java.util.Collection;
18 import java.util.Collections;
19 import java.util.concurrent.ExecutionException;
20 import java.util.concurrent.TimeUnit;
21 import java.util.concurrent.TimeoutException;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartRequestFlags;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReply;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReplyMessageBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyDescCase;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyDescCaseBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.desc._case.MultipartReplyDesc;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.desc._case.MultipartReplyDescBuilder;
31
32 /**
33  * openflowplugin-api
34  * org.opendaylight.openflowplugin.impl.openflow.device
35  *
36  * Test class for testing basic method functionality for {@link org.opendaylight.openflowplugin.api.openflow.device.MultiMsgCollector}
37  *
38  * @author <a href="mailto:vdemcak@cisco.com">Vaclav Demcak</a>
39  * @author <a href="mailto:tkubas@cisco.com">Timotej Kubas</a>
40  *
41  * Created: Mar 23, 2015
42  */
43 public class MultiMsgCollectorImplTest {
44
45     private MultiMsgCollectorImpl collector;
46
47     @Before
48     public void initialization() {
49         collector = new MultiMsgCollectorImpl(1);
50     }
51
52     /**
53      * Test method for {@link org.opendaylight.openflowplugin.impl.openflow.device.MultiMsgCollectorImpl#registerMultipartMsg(org.opendaylight.openflowplugin.api.openflow.device.Xid)}.
54      * @throws ExecutionException
55      * @throws InterruptedException
56      * @throws TimeoutException
57      */
58     @Test
59     public void testRegisterMultipartMsg() throws InterruptedException, ExecutionException, TimeoutException{
60         final long xid = 45L;
61         final String hwTestValue = "test-value";
62         final ListenableFuture<Collection<MultipartReply>> response = collector.registerMultipartMsg(xid);
63         collector.addMultipartMsg(makeMultipartDescReply(xid, hwTestValue, false));
64
65         validateDescReply(response, xid, Collections.singletonList(hwTestValue));
66     }
67
68     /**
69      * Test method for {@link org.opendaylight.openflowplugin.impl.openflow.device.MultiMsgCollectorImpl#addMultipartMsg(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReply)}.
70      * @throws TimeoutException
71      * @throws ExecutionException
72      * @throws InterruptedException
73      */
74     @Test
75     public void testAddMultipartMsg() throws InterruptedException, ExecutionException, TimeoutException{
76         final long xid = 22L;
77         final String hwTestValue1 = "test-value1";
78         final String hwTestValue2 = "test-value2";
79         final ListenableFuture<Collection<MultipartReply>> response = collector.registerMultipartMsg(xid);
80         collector.addMultipartMsg(makeMultipartDescReply(xid, hwTestValue1, true));
81         collector.addMultipartMsg(makeMultipartDescReply(xid, hwTestValue2, false));
82
83         validateDescReply(response, xid, Arrays.asList(hwTestValue1, hwTestValue2));
84     }
85
86     /**
87      * Test could return NullPointerException if the body of addMultipartMsg not
88      */
89     @Test
90     public void testAddMultipartMsgNotExpectedXid() {
91         final long xid = 23L;
92         final String hwTestValue = "test-value";
93         collector.addMultipartMsg(makeMultipartDescReply(xid, hwTestValue, true));
94     }
95
96     /**
97      * Test could return NullPointerException if the body of addMultipartMsg not
98      * @throws InterruptedException
99      */
100     @Test(timeout=20000)
101     public void testCheckExistMultipartMsgInCacheAfterTimeout() throws InterruptedException, ExecutionException {
102         final long xid = 24L;
103         final ListenableFuture<Collection<MultipartReply>> response = collector.registerMultipartMsg(xid);
104         assertNotNull(response);
105         Thread.sleep(2000);
106         collector.addMultipartMsg(makeMultipartDescReply(xid, "hw-text-value", false));
107         try {
108             response.get(1L, TimeUnit.SECONDS);
109             fail("We expected timeout exception");
110         }
111         catch (final TimeoutException e) {
112             // expected exception
113         }
114     }
115
116     private void validateDescReply(final ListenableFuture<Collection<MultipartReply>> response, final long xid,
117             final Collection<String> hwTestValues) throws InterruptedException, ExecutionException, TimeoutException {
118         assertNotNull(response);
119         assertNotNull(xid);
120         assertNotNull(hwTestValues);
121
122         final Collection<MultipartReply> multipartReplyColl = response.get(1L, TimeUnit.SECONDS);
123         assertNotNull(multipartReplyColl);
124         assertTrue(multipartReplyColl.size() > 0);
125         for (final MultipartReply reply : multipartReplyColl) {
126             assertEquals(xid, reply.getXid().longValue());
127             assertTrue(reply.getMultipartReplyBody() instanceof MultipartReplyDescCase);
128             final String replayHwTestString = ((MultipartReplyDescCase) reply.getMultipartReplyBody())
129                     .getMultipartReplyDesc().getHwDesc();
130             assertTrue(hwTestValues.contains(replayHwTestString));
131         }
132     }
133
134     private MultipartReply makeMultipartDescReply(final long xid, final String value, final boolean isLast) {
135         final MultipartReplyDesc descValue = new MultipartReplyDescBuilder().setHwDesc(value).build();
136         final MultipartReplyDescCase replyBody = new MultipartReplyDescCaseBuilder()
137                                                         .setMultipartReplyDesc(descValue).build();
138         return new MultipartReplyMessageBuilder().setMultipartReplyBody(replyBody)
139                 .setXid(xid).setFlags(new MultipartRequestFlags(isLast)).build();
140     }
141 }