2 * Copyright (c) 2014 Brocade Communications 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.openflowjava.protocol.impl.core;
11 import static org.mockito.ArgumentMatchers.any;
12 import static org.mockito.ArgumentMatchers.anyShort;
13 import static org.mockito.Mockito.doThrow;
14 import static org.mockito.Mockito.times;
15 import static org.mockito.Mockito.verify;
16 import static org.mockito.Mockito.when;
18 import io.netty.buffer.ByteBuf;
19 import io.netty.channel.ChannelHandlerContext;
20 import io.netty.util.concurrent.Future;
21 import io.netty.util.concurrent.GenericFutureListener;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.mockito.Mock;
25 import org.mockito.MockitoAnnotations;
26 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
27 import org.opendaylight.openflowjava.protocol.impl.core.connection.MessageListenerWrapper;
28 import org.opendaylight.openflowjava.protocol.impl.serialization.SerializationFactory;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
30 import org.opendaylight.yangtools.yang.binding.DataObject;
33 * Unit tests for OFEncoder.
37 public class OFEncoderTest {
39 @Mock ChannelHandlerContext mockChHndlrCtx ;
40 @Mock SerializationFactory mockSerializationFactory ;
41 @Mock MessageListenerWrapper wrapper;
42 @Mock OfHeader mockMsg ;
43 @Mock ByteBuf mockOut ;
44 @Mock Future<Void> future;
45 @Mock GenericFutureListener<Future<Void>> listener;
47 OFEncoder ofEncoder = new OFEncoder() ;
50 * Sets up test environment.
54 MockitoAnnotations.initMocks(this);
55 ofEncoder = new OFEncoder() ;
56 ofEncoder.setSerializationFactory(mockSerializationFactory);
60 * Test successful write (no clear).
63 public void testEncodeSuccess() throws Exception {
64 when(mockOut.readableBytes()).thenReturn(1);
65 when(wrapper.getMsg()).thenReturn(mockMsg);
66 when(wrapper.getMsg().getVersion()).thenReturn((short) EncodeConstants.OF13_VERSION_ID);
68 ofEncoder.encode(mockChHndlrCtx, wrapper, mockOut);
70 // Verify that the channel was flushed after the ByteBuf was retained.
71 verify(mockOut, times(0)).clear();
75 * Test Bytebuf clearing after serialization failure.
78 public void testEncodeSerializationException() throws Exception {
79 when(wrapper.getMsg()).thenReturn(mockMsg);
80 when(wrapper.getListener()).thenReturn(listener);
81 when(wrapper.getMsg().getVersion()).thenReturn((short) EncodeConstants.OF13_VERSION_ID);
82 doThrow(new IllegalArgumentException()).when(mockSerializationFactory).messageToBuffer(anyShort(),
83 any(ByteBuf.class), any(DataObject.class));
85 ofEncoder.encode(mockChHndlrCtx, wrapper, mockOut);
87 // Verify that the output message buf was cleared...
88 verify(mockOut, times(1)).clear();
92 * Test no action on empty bytebuf.
95 public void testEncodeSerializesNoBytes() throws Exception {
96 when(mockOut.readableBytes()).thenReturn(0);
97 when(wrapper.getMsg()).thenReturn(mockMsg);
98 when(wrapper.getMsg().getVersion()).thenReturn((short) EncodeConstants.OF13_VERSION_ID);
100 ofEncoder.encode(mockChHndlrCtx, wrapper, mockOut);
102 // Verify that the output message buf was cleared...
103 verify(mockOut, times(0)).clear();
104 verify(mockChHndlrCtx, times(0)).writeAndFlush(mockOut);
105 verify(mockOut, times(0)).retain();