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.junit.Assert.assertEquals;
12 import static org.mockito.ArgumentMatchers.any;
13 import static org.mockito.ArgumentMatchers.anyShort;
14 import static org.mockito.Mockito.when;
16 import io.netty.buffer.ByteBuf;
17 import io.netty.channel.ChannelHandlerContext;
18 import java.util.ArrayList;
19 import java.util.List;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.mockito.Mock;
23 import org.mockito.MockitoAnnotations;
24 import org.opendaylight.openflowjava.protocol.impl.deserialization.DeserializationFactory;
25 import org.opendaylight.openflowjava.util.ByteBufUtils;
26 import org.opendaylight.yangtools.yang.binding.DataObject;
29 * Unit tests for OFDecoder.
33 public class OFDecoderTest {
35 @Mock ChannelHandlerContext mockChHndlrCtx ;
36 @Mock DeserializationFactory mockDeserializationFactory ;
37 @Mock DataObject mockDataObject ;
40 private ByteBuf writeObj;
41 private VersionMessageWrapper inMsg;
42 private List<Object> outList;
45 * Sets up test environment.
49 MockitoAnnotations.initMocks(this);
50 ofDecoder = new OFDecoder();
51 ofDecoder.setDeserializationFactory(mockDeserializationFactory);
52 writeObj = ByteBufUtils.hexStringToByteBuf("16 03 01 00");
53 inMsg = new VersionMessageWrapper((short) 8, writeObj);
54 outList = new ArrayList<>();
58 public void testDecode() throws Exception {
59 when(mockDeserializationFactory.deserialize(any(ByteBuf.class), anyShort())).thenReturn(mockDataObject);
61 ofDecoder.decode(mockChHndlrCtx, inMsg, outList);
63 // Verify that the message buf was released...
64 assertEquals(mockDataObject, outList.get(0));
65 assertEquals(0, writeObj.refCnt());
69 public void testDecodeDeserializeException() throws Exception {
70 when(mockDeserializationFactory.deserialize(any(ByteBuf.class), anyShort()))
71 .thenThrow(new IllegalArgumentException());
73 ofDecoder.decode(mockChHndlrCtx, inMsg, outList);
75 // Verify that the message buf was released...
76 assertEquals(0, outList.size());
77 assertEquals(0, writeObj.refCnt());
81 public void testDecodeDeserializeNull() throws Exception {
82 when(mockDeserializationFactory.deserialize(any(ByteBuf.class), anyShort())).thenReturn(null);
84 ofDecoder.decode(mockChHndlrCtx, inMsg, outList);
86 // Verify that the message buf was released...
87 assertEquals(0, outList.size());
88 assertEquals(0, writeObj.refCnt());