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.Matchers.any;
13 import static org.mockito.Matchers.anyShort;
14 import static org.mockito.Mockito.when;
15 import io.netty.buffer.ByteBuf;
16 import io.netty.channel.ChannelHandlerContext;
18 import java.util.ArrayList;
19 import java.util.List;
21 import org.junit.Assert;
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.impl.deserialization.DeserializationFactory;
27 import org.opendaylight.openflowjava.util.ByteBufUtils;
28 import org.opendaylight.yangtools.yang.binding.DataObject;
34 public class OFDecoderTest {
36 @Mock ChannelHandlerContext mockChHndlrCtx ;
37 @Mock DeserializationFactory mockDeserializationFactory ;
38 @Mock DataObject mockDataObject ;
41 private ByteBuf writeObj;
42 private VersionMessageWrapper inMsg;
43 private List<Object> outList;
46 * Sets up test environment
51 MockitoAnnotations.initMocks(this);
52 ofDecoder = new OFDecoder() ;
53 ofDecoder.setDeserializationFactory( mockDeserializationFactory ) ;
54 writeObj = ByteBufUtils.hexStringToByteBuf("16 03 01 00");
55 inMsg = new VersionMessageWrapper( (short)8, writeObj );
56 outList = new ArrayList<>();
63 public void testDecode() {
64 when(mockDeserializationFactory.deserialize( any(ByteBuf.class), anyShort() )).thenReturn(mockDataObject);
66 ofDecoder.decode(mockChHndlrCtx, inMsg, outList);
67 } catch (Exception e) {
71 // Verify that the message buf was released...
72 assertEquals( mockDataObject, outList.get(0) ) ;
73 assertEquals( 0, writeObj.refCnt() ) ;
80 public void testDecodeDeserializeException() {
81 when(mockDeserializationFactory.deserialize( any(ByteBuf.class), anyShort() ))
82 .thenThrow(new IllegalArgumentException()) ;
85 ofDecoder.decode(mockChHndlrCtx, inMsg, outList);
86 } catch (Exception e) {
87 System.out.println("a");
91 // Verify that the message buf was released...
92 assertEquals( 0, outList.size() ) ;
93 assertEquals( 1, writeObj.refCnt() ) ;
100 public void testDecodeDeserializeNull() {
101 when(mockDeserializationFactory.deserialize( any(ByteBuf.class), anyShort() ))
105 ofDecoder.decode(mockChHndlrCtx, inMsg, outList);
106 } catch (Exception e) {
110 // Verify that the message buf was released...
111 assertEquals( 0, outList.size() ) ;
112 assertEquals( 1, writeObj.refCnt() ) ;