Merge remote-tracking branch 'liblldp/master'
[openflowplugin.git] / extension / openflowjava-extension-nicira / src / test / java / org / opendaylight / openflowjava / nx / codec / action / ActionDeserializerTest.java
1 /**
2  * Copyright (c) 2016 Cisco 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.nx.codec.action;
10
11 import static org.junit.Assert.assertNull;
12
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.ByteBufAllocator;
15 import java.util.LinkedList;
16 import java.util.List;
17 import org.junit.Before;
18 import org.junit.Test;
19 import org.junit.runner.RunWith;
20 import org.mockito.Mock;
21 import org.mockito.Mockito;
22 import org.mockito.runners.MockitoJUnitRunner;
23 import org.opendaylight.openflowjava.nx.NiciraExtensionCodecRegistratorImpl;
24 import org.opendaylight.openflowjava.nx.api.NiciraActionDeserializerKey;
25 import org.opendaylight.openflowjava.nx.api.NiciraConstants;
26 import org.opendaylight.openflowjava.protocol.api.extensibility.OFDeserializer;
27 import org.opendaylight.openflowjava.protocol.spi.connection.SwitchConnectionProvider;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.actions.grouping.Action;
29
30 @RunWith(MockitoJUnitRunner.class)
31 public class ActionDeserializerTest {
32
33     ActionDeserializer actionDeserializer;
34     ByteBuf buffer;
35     NiciraExtensionCodecRegistratorImpl niciraExtensionCodecRegistrator;
36     List<SwitchConnectionProvider> providers;
37
38
39     @Mock
40     OFDeserializer<Action> deserializer;
41     @Mock
42     SwitchConnectionProvider provider;
43     @Mock
44     OFDeserializer<Action> ofDeserializer;
45
46
47
48
49     private static final short VERSION = 4;
50     private static final long EXPERIMENT_ID = NiciraConstants.NX_VENDOR_ID;
51     private static final byte SUBTYPE = 10;
52
53     @Before
54     public void setUp() {
55         actionDeserializer = new ActionDeserializer(VERSION);
56         buffer = ByteBufAllocator.DEFAULT.buffer();
57         providers = new LinkedList<>();
58
59
60     }
61
62
63     /**
64      * If NiciraExtensionCodecRegistratorImpl.getActionDeserializer(actionSerializerKey) returns NULL
65      * then NULL should be returned
66      */
67     @Test
68     public void deserializeTest() {
69         createBuffer(buffer);
70         assertNull(actionDeserializer.deserialize(buffer));
71     }
72
73     /**
74      * If experimentId is different from NiciraConstants.NX_VENDOR_ID
75      * then exception should be thrown
76      */
77     @Test(expected = IllegalStateException.class)
78     public void deserializeTest1() {
79         createBufferWithWrongExperimentId(buffer);
80         actionDeserializer.deserialize(buffer);
81     }
82
83     @Test
84     public void deserializeTest2() {
85         createBuffer(buffer);
86         addRecordToMap();
87
88         actionDeserializer.deserialize(buffer);
89
90         Mockito.verify(deserializer).deserialize(buffer);
91     }
92
93     private void createBuffer(ByteBuf message) {
94         //size of experiment type
95         message.writeShort(1);
96         //size of length
97         message.writeShort(13);
98         //experimentId
99         message.writeInt((int)EXPERIMENT_ID);
100         //subtype
101         message.writeShort(SUBTYPE);
102     }
103
104     private void createBufferWithWrongExperimentId(ByteBuf message) {
105         //size of experiment type
106         message.writeShort(1);
107         //size of length
108         message.writeShort(13);
109         //experimentId
110         message.writeInt(1);
111         //subtype
112         message.writeShort(SUBTYPE);
113     }
114
115     private void addRecordToMap() {
116         niciraExtensionCodecRegistrator = new NiciraExtensionCodecRegistratorImpl(providers);
117         NiciraActionDeserializerKey key = new NiciraActionDeserializerKey(VERSION, SUBTYPE);
118
119         niciraExtensionCodecRegistrator.registerActionDeserializer(key, deserializer);
120     }
121
122 }