2 * Copyright (c) 2016 Cisco 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
8 package org.opendaylight.openflowjava.nx.codec.action;
10 import static org.junit.Assert.assertNull;
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;
30 @RunWith(MockitoJUnitRunner.class)
31 public class ActionDeserializerTest {
33 ActionDeserializer actionDeserializer;
35 NiciraExtensionCodecRegistratorImpl niciraExtensionCodecRegistrator;
36 List<SwitchConnectionProvider> providers;
40 OFDeserializer<Action> deserializer;
42 SwitchConnectionProvider provider;
44 OFDeserializer<Action> ofDeserializer;
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;
55 actionDeserializer = new ActionDeserializer(VERSION);
56 buffer = ByteBufAllocator.DEFAULT.buffer();
57 providers = new LinkedList<>();
61 * If NiciraExtensionCodecRegistratorImpl.getActionDeserializer(actionSerializerKey) returns NULL
62 * then NULL should be returned.
65 public void deserializeTest() {
67 assertNull(actionDeserializer.deserialize(buffer));
71 * If experimentId is different from NiciraConstants.NX_VENDOR_ID then exception should be thrown.
73 @Test(expected = IllegalStateException.class)
74 public void deserializeTest1() {
75 createBufferWithWrongExperimentId(buffer);
76 actionDeserializer.deserialize(buffer);
80 public void deserializeTest2() {
84 actionDeserializer.deserialize(buffer);
86 Mockito.verify(deserializer).deserialize(buffer);
89 private static void createBuffer(ByteBuf message) {
90 //size of experiment type
91 message.writeShort(1);
93 message.writeShort(13);
95 message.writeInt(EXPERIMENT_ID.intValue());
97 message.writeShort(SUBTYPE);
100 private static void createBufferWithWrongExperimentId(ByteBuf message) {
101 //size of experiment type
102 message.writeShort(1);
104 message.writeShort(13);
108 message.writeShort(SUBTYPE);
111 private void addRecordToMap() {
112 niciraExtensionCodecRegistrator = new NiciraExtensionCodecRegistratorImpl(providers);
113 NiciraActionDeserializerKey key = new NiciraActionDeserializerKey(VERSION, SUBTYPE);
115 niciraExtensionCodecRegistrator.registerActionDeserializer(key, deserializer);