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.Matchers.any;
12 import static org.mockito.Matchers.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;
17 import io.netty.buffer.ByteBuf;
18 import io.netty.channel.ChannelHandlerContext;
19 import io.netty.util.concurrent.Future;
20 import io.netty.util.concurrent.GenericFutureListener;
22 import org.junit.Assert;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.mockito.Mock;
26 import org.mockito.MockitoAnnotations;
27 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
28 import org.opendaylight.openflowjava.protocol.impl.core.connection.MessageListenerWrapper;
29 import org.opendaylight.openflowjava.protocol.impl.serialization.SerializationFactory;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
31 import org.opendaylight.yangtools.yang.binding.DataObject;
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() {
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);
69 } catch (Exception e) {
73 // Verify that the channel was flushed after the ByteBuf was retained.
74 verify(mockOut, times(0)).clear();
78 * Test Bytebuf clearing after serialization failure
81 public void testEncodeSerializationException() {
82 when(wrapper.getMsg()).thenReturn(mockMsg);
83 when(wrapper.getListener()).thenReturn(listener);
84 when(wrapper.getMsg().getVersion()).thenReturn((short) EncodeConstants.OF13_VERSION_ID);
85 doThrow(new IllegalArgumentException()).when(mockSerializationFactory).messageToBuffer(anyShort(),any(ByteBuf.class), any(DataObject.class));
87 ofEncoder.encode(mockChHndlrCtx, wrapper, mockOut);
88 } catch (Exception e) {
92 // Verify that the output message buf was cleared...
93 verify(mockOut, times(1)).clear();
97 * Test no action on empty bytebuf
100 public void testEncodeSerializesNoBytes() {
101 when(mockOut.readableBytes()).thenReturn(0);
102 when(wrapper.getMsg()).thenReturn(mockMsg);
103 when(wrapper.getMsg().getVersion()).thenReturn((short) EncodeConstants.OF13_VERSION_ID);
105 ofEncoder.encode(mockChHndlrCtx, wrapper, mockOut);
106 } catch (Exception e) {
110 // Verify that the output message buf was cleared...
111 verify(mockOut, times(0)).clear();
112 verify(mockChHndlrCtx, times(0)).writeAndFlush(mockOut);
113 verify(mockOut, times(0)).retain();