Merge "make ConfigurationServiceFactoryImpl independent of OSGi"
[openflowplugin.git] / openflowjava / 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
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;
27
28 /**
29  * Unit tests for OFDecoder.
30  *
31  * @author jameshall
32  */
33 public class OFDecoderTest {
34
35     @Mock ChannelHandlerContext mockChHndlrCtx ;
36     @Mock DeserializationFactory mockDeserializationFactory ;
37     @Mock DataObject mockDataObject ;
38
39     OFDecoder ofDecoder ;
40     private ByteBuf writeObj;
41     private VersionMessageWrapper inMsg;
42     private List<Object> outList;
43
44     /**
45      * Sets up test environment.
46      */
47     @Before
48     public void setUp() {
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<>();
55     }
56
57     @Test
58     public void testDecode() throws Exception {
59         when(mockDeserializationFactory.deserialize(any(ByteBuf.class), anyShort())).thenReturn(mockDataObject);
60
61         ofDecoder.decode(mockChHndlrCtx, inMsg, outList);
62
63         // Verify that the message buf was released...
64         assertEquals(mockDataObject, outList.get(0));
65         assertEquals(0, writeObj.refCnt());
66     }
67
68     @Test
69     public void testDecodeDeserializeException() throws Exception {
70         when(mockDeserializationFactory.deserialize(any(ByteBuf.class), anyShort()))
71                 .thenThrow(new IllegalArgumentException());
72
73         ofDecoder.decode(mockChHndlrCtx, inMsg, outList);
74
75         // Verify that the message buf was released...
76         assertEquals(0, outList.size());
77         assertEquals(0, writeObj.refCnt());
78     }
79
80     @Test
81     public void testDecodeDeserializeNull() throws Exception {
82         when(mockDeserializationFactory.deserialize(any(ByteBuf.class), anyShort())).thenReturn(null);
83
84         ofDecoder.decode(mockChHndlrCtx, inMsg, outList);
85
86         // Verify that the message buf was released...
87         assertEquals(0, outList.size());
88         assertEquals(0, writeObj.refCnt());
89     }
90 }