Merge "Add INFO.yaml for openflowplugin"
[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 package org.opendaylight.openflowjava.nx.codec.action;
9
10 import static org.junit.Assert.assertNull;
11
12 import io.netty.buffer.ByteBuf;
13 import io.netty.buffer.ByteBufAllocator;
14 import java.util.LinkedList;
15 import java.util.List;
16 import org.junit.Before;
17 import org.junit.Test;
18 import org.junit.runner.RunWith;
19 import org.mockito.Mock;
20 import org.mockito.Mockito;
21 import org.mockito.junit.MockitoJUnitRunner;
22 import org.opendaylight.openflowjava.nx.NiciraExtensionCodecRegistratorImpl;
23 import org.opendaylight.openflowjava.nx.api.NiciraActionDeserializerKey;
24 import org.opendaylight.openflowjava.nx.api.NiciraConstants;
25 import org.opendaylight.openflowjava.protocol.api.extensibility.OFDeserializer;
26 import org.opendaylight.openflowjava.protocol.spi.connection.SwitchConnectionProvider;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.actions.grouping.Action;
28 import org.opendaylight.yangtools.yang.common.Uint32;
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 Uint32 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      * If NiciraExtensionCodecRegistratorImpl.getActionDeserializer(actionSerializerKey) returns NULL
62      * then NULL should be returned.
63      */
64     @Test
65     public void deserializeTest() {
66         createBuffer(buffer);
67         assertNull(actionDeserializer.deserialize(buffer));
68     }
69
70     /**
71      * If experimentId is different from NiciraConstants.NX_VENDOR_ID then exception should be thrown.
72      */
73     @Test(expected = IllegalStateException.class)
74     public void deserializeTest1() {
75         createBufferWithWrongExperimentId(buffer);
76         actionDeserializer.deserialize(buffer);
77     }
78
79     @Test
80     public void deserializeTest2() {
81         createBuffer(buffer);
82         addRecordToMap();
83
84         actionDeserializer.deserialize(buffer);
85
86         Mockito.verify(deserializer).deserialize(buffer);
87     }
88
89     private static void createBuffer(ByteBuf message) {
90         //size of experiment type
91         message.writeShort(1);
92         //size of length
93         message.writeShort(13);
94         //experimentId
95         message.writeInt(EXPERIMENT_ID.intValue());
96         //subtype
97         message.writeShort(SUBTYPE);
98     }
99
100     private static void createBufferWithWrongExperimentId(ByteBuf message) {
101         //size of experiment type
102         message.writeShort(1);
103         //size of length
104         message.writeShort(13);
105         //experimentId
106         message.writeInt(1);
107         //subtype
108         message.writeShort(SUBTYPE);
109     }
110
111     private void addRecordToMap() {
112         niciraExtensionCodecRegistrator = new NiciraExtensionCodecRegistratorImpl(providers);
113         NiciraActionDeserializerKey key = new NiciraActionDeserializerKey(VERSION, SUBTYPE);
114
115         niciraExtensionCodecRegistrator.registerActionDeserializer(key, deserializer);
116     }
117
118 }