OPNFLWPLUG-1032: Neon-MRI: Bump odlparent, yangtools, mdsal
[openflowplugin.git] / openflowjava / 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.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;
17
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;
31
32 /**
33  * Unit tests for OFEncoder.
34  *
35  * @author jameshall
36  */
37 public class OFEncoderTest {
38
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;
46
47     OFEncoder ofEncoder = new OFEncoder() ;
48
49     /**
50      * Sets up test environment.
51      */
52     @Before
53     public void setUp() {
54         MockitoAnnotations.initMocks(this);
55         ofEncoder = new OFEncoder() ;
56         ofEncoder.setSerializationFactory(mockSerializationFactory);
57     }
58
59     /**
60      * Test successful write (no clear).
61      */
62     @Test
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);
67
68         ofEncoder.encode(mockChHndlrCtx, wrapper, mockOut);
69
70         // Verify that the channel was flushed after the ByteBuf was retained.
71         verify(mockOut, times(0)).clear();
72     }
73
74     /**
75      * Test Bytebuf clearing after serialization failure.
76      */
77     @Test
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));
84
85         ofEncoder.encode(mockChHndlrCtx, wrapper, mockOut);
86
87         // Verify that the output message buf was cleared...
88         verify(mockOut, times(1)).clear();
89     }
90
91     /**
92      * Test no action on empty bytebuf.
93      */
94     @Test
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);
99
100         ofEncoder.encode(mockChHndlrCtx, wrapper, mockOut);
101
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();
106     }
107 }