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
9 package org.opendaylight.openflowplugin.impl.device;
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;
34 * org.opendaylight.openflowplugin.impl.openflow.device
36 * Test class for testing basic method functionality for {@link org.opendaylight.openflowplugin.api.openflow.device.MultiMsgCollector}
38 * @author <a href="mailto:vdemcak@cisco.com">Vaclav Demcak</a>
39 * @author <a href="mailto:tkubas@cisco.com">Timotej Kubas</a>
41 * Created: Mar 23, 2015
43 public class MultiMsgCollectorImplTest {
45 private MultiMsgCollectorImpl collector;
48 public void initialization() {
49 collector = new MultiMsgCollectorImpl(1);
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
59 public void testRegisterMultipartMsg() throws InterruptedException, ExecutionException, TimeoutException{
61 final String hwTestValue = "test-value";
62 final ListenableFuture<Collection<MultipartReply>> response = collector.registerMultipartMsg(xid);
63 collector.addMultipartMsg(makeMultipartDescReply(xid, hwTestValue, false));
65 validateDescReply(response, xid, Collections.singletonList(hwTestValue));
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
75 public void testAddMultipartMsg() throws InterruptedException, ExecutionException, TimeoutException{
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));
83 validateDescReply(response, xid, Arrays.asList(hwTestValue1, hwTestValue2));
87 * Test could return NullPointerException if the body of addMultipartMsg not
90 public void testAddMultipartMsgNotExpectedXid() {
92 final String hwTestValue = "test-value";
93 collector.addMultipartMsg(makeMultipartDescReply(xid, hwTestValue, true));
97 * Test could return NullPointerException if the body of addMultipartMsg not
98 * @throws InterruptedException
101 public void testCheckExistMultipartMsgInCacheAfterTimeout() throws InterruptedException, ExecutionException {
102 final long xid = 24L;
103 final ListenableFuture<Collection<MultipartReply>> response = collector.registerMultipartMsg(xid);
104 assertNotNull(response);
106 collector.addMultipartMsg(makeMultipartDescReply(xid, "hw-text-value", false));
108 response.get(1L, TimeUnit.SECONDS);
109 fail("We expected timeout exception");
111 catch (final TimeoutException e) {
112 // expected exception
116 private void validateDescReply(final ListenableFuture<Collection<MultipartReply>> response, final long xid,
117 final Collection<String> hwTestValues) throws InterruptedException, ExecutionException, TimeoutException {
118 assertNotNull(response);
120 assertNotNull(hwTestValues);
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));
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();