Merge "Bug 5543 - Bo: Update JUnit tests part_4"
[openflowplugin.git] / extension / openflowjava-extension-nicira / src / test / java / org / opendaylight / openflowjava / nx / codec / action / SetNsiCodecTest.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.assertEquals;
12
13 import io.netty.buffer.ByteBuf;
14 import io.netty.buffer.ByteBufAllocator;
15 import org.junit.Before;
16 import org.junit.Test;
17 import org.opendaylight.openflowjava.nx.api.NiciraConstants;
18 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.actions.grouping.Action;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.actions.grouping.ActionBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.ExperimenterId;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.action.rev140421.action.container.action.choice.ActionSetNsi;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.action.rev140421.action.container.action.choice.ActionSetNsiBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.action.rev140421.ofj.nx.action.set.nsi.grouping.NxActionSetNsiBuilder;
25
26 public class SetNsiCodecTest {
27
28     SetNsiCodec setNsiCodec;
29     ByteBuf buffer;
30     Action action;
31
32     private final int LENGTH = 16;
33     private final byte NXAST_SET_NSC_SUBTYPE = 33;
34     private final int padding = 5;
35
36     @Before
37     public void setUp() {
38         setNsiCodec = new SetNsiCodec();
39         buffer = ByteBufAllocator.DEFAULT.buffer();
40     }
41
42     @Test
43     public void serializeTest() {
44         action = createAction();
45         setNsiCodec.serialize(action, buffer);
46
47         assertEquals(LENGTH, buffer.readableBytes());
48
49         //SerializeHeader part
50         assertEquals(EncodeConstants.EXPERIMENTER_VALUE, buffer.readUnsignedShort());
51         assertEquals(LENGTH, buffer.readUnsignedShort());
52         assertEquals(NiciraConstants.NX_VENDOR_ID.intValue(), buffer.readUnsignedInt());
53         assertEquals(NXAST_SET_NSC_SUBTYPE, buffer.readUnsignedShort());
54         //Serialize
55         assertEquals(1, buffer.readUnsignedByte());
56     }
57
58     @Test
59     public void deserializeTest() {
60         createBuffer(buffer);
61
62         action = setNsiCodec.deserialize(buffer);
63
64         ActionSetNsi result = (ActionSetNsi) action.getActionChoice();
65
66         assertEquals(1, result.getNxActionSetNsi().getNsi().intValue());
67         assertEquals(0, buffer.readableBytes());
68     }
69
70     private Action createAction() {
71         ExperimenterId experimenterId = new ExperimenterId(NiciraConstants.NX_VENDOR_ID);
72         ActionBuilder actionBuilder = new ActionBuilder();
73         actionBuilder.setExperimenterId(experimenterId);
74         ActionSetNsiBuilder actionSetNsiBuilder = new ActionSetNsiBuilder();
75         NxActionSetNsiBuilder nxActionSetNsiBuilder = new NxActionSetNsiBuilder();
76
77         nxActionSetNsiBuilder.setNsi((short)1);
78
79         actionSetNsiBuilder.setNxActionSetNsi(nxActionSetNsiBuilder.build());
80         actionBuilder.setActionChoice(actionSetNsiBuilder.build());
81
82         return actionBuilder.build();
83     }
84
85     private void createBuffer(ByteBuf message) {
86         message.writeShort(EncodeConstants.EXPERIMENTER_VALUE);
87         message.writeShort(LENGTH);
88         message.writeInt(NiciraConstants.NX_VENDOR_ID.intValue());
89         message.writeShort(NXAST_SET_NSC_SUBTYPE);
90
91         message.writeByte(1);
92         message.writeZero(padding);
93     }
94
95 }