Bug 5543 - Bo: Update JUnit tests part_2
[openflowplugin.git] / extension / openflowjava-extension-nicira / src / test / java / org / opendaylight / openflowjava / nx / codec / action / ConntrackCodecTest.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 io.netty.buffer.ByteBuf;
12 import io.netty.buffer.ByteBufAllocator;
13 import org.junit.Assert;
14 import org.junit.Before;
15 import org.junit.Test;
16 import org.opendaylight.openflowjava.nx.api.NiciraConstants;
17 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.actions.grouping.Action;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.actions.grouping.ActionBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.ExperimenterId;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.action.rev140421.action.container.action.choice.ActionConntrack;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.action.rev140421.action.container.action.choice.ActionConntrackBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.action.rev140421.ofj.nx.action.conntrack.grouping.NxActionConntrackBuilder;
24
25 public class ConntrackCodecTest {
26
27     private ConntrackCodec conntrackCodec;
28
29     private ByteBuf buffer;
30     private Action action;
31
32     private final int LENGTH = 24;
33     private final byte NXAST_CONNTRACK_SUBTYPE = 35;
34
35     @Before
36     public void setUp() {
37         conntrackCodec = new ConntrackCodec();
38         buffer = ByteBufAllocator.DEFAULT.buffer();
39     }
40
41     @Test
42     public void serializeTest() {
43         action = createAction();
44         conntrackCodec.serialize(action, buffer);
45
46         Assert.assertEquals(24, buffer.readableBytes());
47         Assert.assertEquals(EncodeConstants.EXPERIMENTER_VALUE, buffer.readUnsignedShort());
48         Assert.assertEquals(LENGTH, buffer.readUnsignedShort());
49         Assert.assertEquals(NiciraConstants.NX_VENDOR_ID.intValue(), buffer.readUnsignedInt());
50         Assert.assertEquals(NXAST_CONNTRACK_SUBTYPE, buffer.readUnsignedShort());
51         Assert.assertEquals(1, buffer.readUnsignedShort());
52         Assert.assertEquals(2, buffer.readUnsignedInt());
53         Assert.assertEquals(3, buffer.readUnsignedShort());
54         Assert.assertEquals(4, buffer.readByte());
55         buffer.skipBytes(5);
56     }
57
58     @Test
59     public void deserializeTest() {
60         createBufer(buffer);
61         action = conntrackCodec.deserialize(buffer);
62
63         ActionConntrack result = ((ActionConntrack) action.getActionChoice());
64
65         Assert.assertEquals(1, result.getNxActionConntrack().getFlags().shortValue());
66         Assert.assertEquals(2, result.getNxActionConntrack().getZoneSrc().intValue());
67         Assert.assertEquals(3, result.getNxActionConntrack().getConntrackZone().shortValue());
68         Assert.assertEquals(4, result.getNxActionConntrack().getRecircTable().byteValue());
69         Assert.assertEquals(0, buffer.readableBytes());
70
71     }
72
73     private Action createAction() {
74         ExperimenterId experimenterId = new ExperimenterId(NiciraConstants.NX_VENDOR_ID);
75         ActionBuilder actionBuilder = new ActionBuilder();
76         actionBuilder.setExperimenterId(experimenterId);
77         ActionConntrackBuilder actionConntrackBuilder = new ActionConntrackBuilder();
78
79         NxActionConntrackBuilder nxActionConntrackBuilder = new NxActionConntrackBuilder();
80         nxActionConntrackBuilder.setFlags(1);
81         nxActionConntrackBuilder.setZoneSrc((long) 2);
82         nxActionConntrackBuilder.setConntrackZone(3);
83         nxActionConntrackBuilder.setRecircTable((short) 4);
84         actionConntrackBuilder.setNxActionConntrack(nxActionConntrackBuilder.build());
85         actionBuilder.setActionChoice(actionConntrackBuilder.build());
86
87         return actionBuilder.build();
88     }
89
90     private void createBufer(ByteBuf message) {
91         message.writeShort(EncodeConstants.EXPERIMENTER_VALUE);
92         message.writeShort(LENGTH);
93         message.writeInt(NiciraConstants.NX_VENDOR_ID.intValue());
94         message.writeShort(NXAST_CONNTRACK_SUBTYPE);
95         //FLAG = 1
96         message.writeShort(1);
97         //ZONE_SRC = 2
98         message.writeInt(2);
99         //CONNTRACK_ZONE = 3
100         message.writeShort(3);
101         //RECIRC_TABLE = 4
102         message.writeByte(4);
103         //ADDS 5 empty bytes
104         message.writeZero(5);
105     }
106 }