package org.opendaylight.openflowjava.protocol.impl.core;
-import static org.mockito.Matchers.any;
-import static org.mockito.Matchers.anyShort;
+import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
+
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.GenericFutureListener;
-
-import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
+import org.junit.runner.RunWith;
import org.mockito.Mock;
-import org.mockito.MockitoAnnotations;
+import org.mockito.junit.MockitoJUnitRunner;
import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
import org.opendaylight.openflowjava.protocol.impl.core.connection.MessageListenerWrapper;
import org.opendaylight.openflowjava.protocol.impl.serialization.SerializationFactory;
import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
import org.opendaylight.yangtools.yang.binding.DataObject;
+import org.opendaylight.yangtools.yang.common.Uint8;
/**
+ * Unit tests for OFEncoder.
*
* @author jameshall
*/
+@RunWith(MockitoJUnitRunner.class)
public class OFEncoderTest {
@Mock ChannelHandlerContext mockChHndlrCtx ;
OFEncoder ofEncoder = new OFEncoder() ;
/**
- * Sets up test environment
+ * Sets up test environment.
*/
@Before
public void setUp() {
- MockitoAnnotations.initMocks(this);
ofEncoder = new OFEncoder() ;
- ofEncoder.setSerializationFactory( mockSerializationFactory ) ;
+ ofEncoder.setSerializationFactory(mockSerializationFactory);
}
/**
- * Test successful write (no clear)
+ * Test successful write (no clear).
*/
@Test
- public void testEncodeSuccess() {
- when(mockOut.readableBytes()).thenReturn(1);
+ public void testEncodeSuccess() throws Exception {
when(wrapper.getMsg()).thenReturn(mockMsg);
- when(wrapper.getMsg().getVersion()).thenReturn((short) EncodeConstants.OF13_VERSION_ID);
- try {
- ofEncoder.encode(mockChHndlrCtx, wrapper, mockOut);
- } catch (Exception e) {
- Assert.fail();
- }
+ when(wrapper.getMsg().getVersion()).thenReturn(Uint8.valueOf(EncodeConstants.OF13_VERSION_ID));
+
+ ofEncoder.encode(mockChHndlrCtx, wrapper, mockOut);
// Verify that the channel was flushed after the ByteBuf was retained.
verify(mockOut, times(0)).clear();
}
/**
- * Test Bytebuf clearing after serialization failure
+ * Test Bytebuf clearing after serialization failure.
*/
@Test
- public void testEncodeSerializationException() {
+ public void testEncodeSerializationException() throws Exception {
when(wrapper.getMsg()).thenReturn(mockMsg);
when(wrapper.getListener()).thenReturn(listener);
- when(wrapper.getMsg().getVersion()).thenReturn((short) EncodeConstants.OF13_VERSION_ID);
- doThrow(new IllegalArgumentException()).when(mockSerializationFactory).messageToBuffer(anyShort(),any(ByteBuf.class), any(DataObject.class));
- try {
- ofEncoder.encode(mockChHndlrCtx, wrapper, mockOut);
- } catch (Exception e) {
- Assert.fail();
- }
+ when(wrapper.getMsg().getVersion()).thenReturn(Uint8.valueOf(EncodeConstants.OF13_VERSION_ID));
+ doThrow(new IllegalArgumentException()).when(mockSerializationFactory).messageToBuffer(any(Uint8.class),
+ any(ByteBuf.class), any(DataObject.class));
+
+ ofEncoder.encode(mockChHndlrCtx, wrapper, mockOut);
// Verify that the output message buf was cleared...
verify(mockOut, times(1)).clear();
}
/**
- * Test no action on empty bytebuf
+ * Test no action on empty bytebuf.
*/
@Test
- public void testEncodeSerializesNoBytes() {
- when(mockOut.readableBytes()).thenReturn(0);
+ public void testEncodeSerializesNoBytes() throws Exception {
when(wrapper.getMsg()).thenReturn(mockMsg);
- when(wrapper.getMsg().getVersion()).thenReturn((short) EncodeConstants.OF13_VERSION_ID);
- try {
- ofEncoder.encode(mockChHndlrCtx, wrapper, mockOut);
- } catch (Exception e) {
- Assert.fail();
- }
+ when(wrapper.getMsg().getVersion()).thenReturn(Uint8.valueOf(EncodeConstants.OF13_VERSION_ID));
+
+ ofEncoder.encode(mockChHndlrCtx, wrapper, mockOut);
// Verify that the output message buf was cleared...
verify(mockOut, times(0)).clear();
verify(mockChHndlrCtx, times(0)).writeAndFlush(mockOut);
verify(mockOut, times(0)).retain();
}
-}
\ No newline at end of file
+}