Bug 1277 - Move ByteBuffUtils to separate bundle
[openflowjava.git] / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / core / OFEncoderTest.java
1 /*
2  * Copyright (c) 2014 Brocade Communications 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.openflowjava.protocol.impl.core;
10
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
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;
28
29 /**
30  *
31  * @author jameshall
32  */
33 public class OFEncoderTest {
34
35     @Mock ChannelHandlerContext mockChHndlrCtx ;
36     @Mock SerializationFactory mockSerializationFactory ;
37     @Mock OfHeader mockMsg ;
38     @Mock ByteBuf mockOut ;
39
40     OFEncoder ofEncoder = new OFEncoder() ;
41
42     /**
43      * Sets up test environment
44      */
45     @Before
46     public void setUp() {
47         MockitoAnnotations.initMocks(this);
48         ofEncoder = new OFEncoder() ;
49         ofEncoder.setSerializationFactory( mockSerializationFactory ) ;
50     }
51
52     /**
53      * Test successful write (no clear)
54      */
55     @Test
56     public void testEncodeSuccess() {
57         when(mockOut.readableBytes()).thenReturn(1);
58         try {
59             ofEncoder.encode(mockChHndlrCtx, mockMsg, mockOut);
60         } catch (Exception e) {
61             Assert.fail();
62         }
63
64         // Verify that the channel was flushed after the ByteBuf was retained.
65         verify(mockOut, times(0)).clear();
66     }
67
68     /**
69      * Test Bytebuf clearing after serialization failure
70      */
71     @Test
72     public void testEncodeSerializationException() {
73         doThrow(new IllegalArgumentException()).when(mockSerializationFactory).messageToBuffer(anyShort(),any(ByteBuf.class), any(DataObject.class));
74         try {
75             ofEncoder.encode(mockChHndlrCtx, mockMsg, mockOut);
76         } catch (Exception e) {
77             Assert.fail();
78         }
79
80         // Verify that the output message buf was cleared...
81         verify(mockOut, times(1)).clear();
82     }
83
84     /**
85      * Test no action on empty bytebuf
86      */
87     @Test
88     public void testEncodeSerializesNoBytes() {
89         when(mockOut.readableBytes()).thenReturn(0);
90         try {
91             ofEncoder.encode(mockChHndlrCtx, mockMsg, mockOut);
92         } catch (Exception e) {
93             Assert.fail();
94         }
95
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();
100     }
101 }