af8a0102e8fe4a2d0d28b7ff03e3a9753ddbf19b
[openflowjava.git] / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / core / OFDecoderTest.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.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;
17
18 import java.util.ArrayList;
19 import java.util.List;
20
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;
29
30 /**
31  *
32  * @author jameshall
33  */
34 public class OFDecoderTest {
35
36     @Mock ChannelHandlerContext mockChHndlrCtx ;
37     @Mock DeserializationFactory mockDeserializationFactory ;
38     @Mock DataObject mockDataObject ;
39
40     OFDecoder ofDecoder ;
41     private ByteBuf writeObj;
42     private VersionMessageWrapper inMsg;
43     private List<Object> outList;
44
45     /**
46      * Sets up test environment
47      * 
48      */
49     @Before
50     public void setUp() {
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<>();
57     }
58
59     /**
60      * 
61      */
62     @Test
63     public void testDecode() {
64         when(mockDeserializationFactory.deserialize( any(ByteBuf.class), anyShort() )).thenReturn(mockDataObject);
65         try {
66             ofDecoder.decode(mockChHndlrCtx, inMsg, outList);
67         } catch (Exception e) {
68             Assert.fail();
69         }
70
71         // Verify that the message buf was released...
72         assertEquals( mockDataObject, outList.get(0) ) ;
73         assertEquals( 0, writeObj.refCnt() ) ;
74     }
75
76     /**
77      * 
78      */
79     @Test
80     public void testDecodeDeserializeException() {
81         when(mockDeserializationFactory.deserialize( any(ByteBuf.class), anyShort() ))
82         .thenThrow(new IllegalArgumentException()) ;
83
84         try {
85             ofDecoder.decode(mockChHndlrCtx, inMsg, outList);
86         } catch (Exception e) {
87             Assert.fail();
88         }
89
90         // Verify that the message buf was released...
91         assertEquals( 0, outList.size() ) ;
92         assertEquals( 0, writeObj.refCnt() ) ;
93     }
94
95     /**
96      * 
97      */
98     @Test
99     public void testDecodeDeserializeNull() {
100         when(mockDeserializationFactory.deserialize( any(ByteBuf.class), anyShort() ))
101         .thenReturn(null) ;
102
103         try {
104             ofDecoder.decode(mockChHndlrCtx, inMsg, outList);
105         } catch (Exception e) {
106             Assert.fail();
107         }
108
109         // Verify that the message buf was released...
110         assertEquals( 0, outList.size() ) ;
111         assertEquals( 0, writeObj.refCnt() ) ;
112     }
113 }