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;
20 import org.junit.Assert;
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.mockito.Mock;
24 import org.mockito.MockitoAnnotations;
25 import org.opendaylight.openflowjava.protocol.impl.serialization.SerializationFactory;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
27 import org.opendaylight.yangtools.yang.binding.DataObject;
33 public class OFEncoderTest {
35 @Mock ChannelHandlerContext mockChHndlrCtx ;
36 @Mock SerializationFactory mockSerializationFactory ;
37 @Mock OfHeader mockMsg ;
38 @Mock ByteBuf mockOut ;
40 OFEncoder ofEncoder = new OFEncoder() ;
43 * Sets up test environment
47 MockitoAnnotations.initMocks(this);
48 ofEncoder = new OFEncoder() ;
49 ofEncoder.setSerializationFactory( mockSerializationFactory ) ;
53 * Test successful write (no clear)
56 public void testEncodeSuccess() {
57 when(mockOut.readableBytes()).thenReturn(1);
59 ofEncoder.encode(mockChHndlrCtx, mockMsg, mockOut);
60 } catch (Exception e) {
64 // Verify that the channel was flushed after the ByteBuf was retained.
65 verify(mockOut, times(0)).clear();
69 * Test Bytebuf clearing after serialization failure
72 public void testEncodeSerializationException() {
73 doThrow(new IllegalArgumentException()).when(mockSerializationFactory).messageToBuffer(anyShort(),any(ByteBuf.class), any(DataObject.class));
75 ofEncoder.encode(mockChHndlrCtx, mockMsg, mockOut);
76 } catch (Exception e) {
80 // Verify that the output message buf was cleared...
81 verify(mockOut, times(1)).clear();
85 * Test no action on empty bytebuf
88 public void testEncodeSerializesNoBytes() {
89 when(mockOut.readableBytes()).thenReturn(0);
91 ofEncoder.encode(mockChHndlrCtx, mockMsg, mockOut);
92 } catch (Exception e) {
96 // Verify that the output message buf was cleared...
97 verify(mockOut, times(0)).clear();
98 verify(mockChHndlrCtx, times(0)).writeAndFlush(mockOut);
99 verify(mockOut, times(0)).retain();