Added openflow-codec and openflowj_netty from openflowplugin
[openflowjava.git] / third-party / openflow-codec / src / test / java / org / openflow / codec / protocol / instruction / OFPInstructionActionsTest.java
1 package org.openflow.codec.protocol.instruction;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import junit.framework.TestCase;
7
8 import org.openflow.codec.io.DataBuffers;
9 import org.openflow.codec.io.IDataBuffer;
10 import org.openflow.codec.protocol.action.OFPAction;
11 import org.openflow.codec.protocol.action.OFPActionCopyTimeToLiveIn;
12 import org.openflow.codec.protocol.action.OFPActionOutput;
13 import org.openflow.codec.protocol.factory.OFPBasicFactoryImpl;
14 import org.openflow.codec.protocol.factory.OFPActionFactory;
15 import org.openflow.codec.protocol.instruction.OFPInstructionApplyActions;
16 import org.openflow.codec.protocol.instruction.OFPInstructionClearActions;
17 import org.openflow.codec.protocol.instruction.OFPInstructionType;
18 import org.openflow.codec.protocol.instruction.OFPInstructionWriteActions;
19
20 /**
21  * test class to verify instruction structure
22  *
23  * @author AnilGujele
24  *
25  */
26 public class OFPInstructionActionsTest extends TestCase {
27
28     private OFPActionFactory factory = new OFPBasicFactoryImpl();
29
30     private IDataBuffer buffer = DataBuffers.allocate(1024);
31
32     protected void tearDown() throws Exception {
33         buffer.clear();
34     }
35
36     public void testOFInstructionApplyActions() {
37         OFPInstructionApplyActions instruction = new OFPInstructionApplyActions();
38         assertTrue(instruction.type.equals(OFPInstructionType.APPLY_ACTIONS));
39
40     }
41
42     public void testOFInstructionWriteActions() {
43         OFPInstructionWriteActions instruction = new OFPInstructionWriteActions();
44         assertTrue(instruction.type.equals(OFPInstructionType.WRITE_ACTIONS));
45
46     }
47
48     public void testOFInstructionClearActions() {
49         OFPInstructionClearActions instruction = new OFPInstructionClearActions();
50         assertTrue(instruction.type.equals(OFPInstructionType.CLEAR_ACTIONS));
51
52     }
53
54     public void testClone() throws CloneNotSupportedException {
55         OFPInstructionApplyActions instruction = getOFInstructionApplyActions();
56         OFPInstructionApplyActions instructionCloned = (OFPInstructionApplyActions) instruction.clone();
57         TestCase.assertEquals(instruction, instructionCloned);
58
59         OFPAction action = new OFPActionCopyTimeToLiveIn();
60         List<OFPAction> actionList = new ArrayList<OFPAction>();
61         actionList.add(action);
62         instructionCloned.setActions(actionList);
63         instructionCloned.setActionFactory(factory);
64         TestCase.assertNotSame(instruction, instructionCloned);
65
66         instruction = (OFPInstructionApplyActions) instructionCloned.clone();
67         TestCase.assertEquals(instruction, instructionCloned);
68     }
69
70     public void testReadWriteSuccess() {
71         OFPInstructionApplyActions instruction = getOFInstructionApplyActions();
72
73         instruction.writeTo(buffer);
74         buffer.flip();
75
76         OFPInstructionApplyActions instructionTemp = new OFPInstructionApplyActions();
77         instructionTemp.setActionFactory(factory);
78         instructionTemp.readFrom(buffer);
79         TestCase.assertEquals(instruction, instructionTemp);
80
81     }
82
83     public void testReadWriteFailed() {
84         OFPInstructionApplyActions instruction = getOFInstructionApplyActions();
85
86         instruction.writeTo(buffer);
87         buffer.flip();
88
89         OFPInstructionApplyActions instructionTemp = new OFPInstructionApplyActions();
90         boolean result = false;
91         try {
92             instructionTemp.readFrom(buffer);
93         } catch (RuntimeException ex) {
94             result = true;
95         }
96         TestCase.assertTrue(result);
97
98     }
99
100     public void testToString() {
101         OFPInstructionApplyActions instruction = getOFInstructionApplyActions();
102
103         instruction.writeTo(buffer);
104         buffer.flip();
105
106         OFPInstructionApplyActions instructionTemp = new OFPInstructionApplyActions();
107         instructionTemp.setActionFactory(factory);
108         instructionTemp.readFrom(buffer);
109         TestCase.assertEquals(instruction.toString(), instructionTemp.toString());
110     }
111
112     public void testEqualHashcode() {
113
114         OFPInstructionApplyActions instruction = getOFInstructionApplyActions();
115
116         instruction.writeTo(buffer);
117         buffer.flip();
118
119         OFPInstructionApplyActions instructionTemp = new OFPInstructionApplyActions();
120         instructionTemp.setActionFactory(factory);
121         instructionTemp.readFrom(buffer);
122
123         TestCase.assertTrue(instruction.equals(instructionTemp));
124         TestCase.assertEquals(instruction.hashCode(), instructionTemp.hashCode());
125     }
126
127     private OFPInstructionApplyActions getOFInstructionApplyActions() {
128         OFPInstructionApplyActions instruction = new OFPInstructionApplyActions();
129         OFPAction action = new OFPActionOutput();
130         List<OFPAction> actionList = new ArrayList<OFPAction>();
131         actionList.add(action);
132         instruction.setActions(actionList);
133         instruction.setActionFactory(factory);
134         return instruction;
135     }
136
137 }