Skip to content

Commit 33cb700

Browse files
Merge pull request #80 from cisco-system-traffic-generator/unit-test
add unit tests
2 parents 67954fb + 5a74eda commit 33cb700

File tree

2 files changed

+201
-9
lines changed

2 files changed

+201
-9
lines changed

src/main/java/com/cisco/trex/stateless/TRexClient.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ public class TRexClient extends ClientBase {
6969
TRexClient(TRexTransport transport, Set<String> supportedCommands) {
7070
// For unit testing
7171
this.transport = transport;
72+
this.host = "testHost";
73+
this.port = "testPort";
74+
this.userName = "testUser";
7275
supportedCmds.addAll(supportedCommands);
7376
}
7477

src/test/java/com/cisco/trex/stateless/TRexClientTest.java

Lines changed: 198 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,231 @@
22

33
import static org.junit.Assert.assertTrue;
44

5+
import com.cisco.trex.stateless.exception.TRexConnectionException;
6+
import com.cisco.trex.stateless.model.RPCResponse;
7+
import com.fasterxml.jackson.core.JsonParseException;
8+
import com.fasterxml.jackson.databind.JsonMappingException;
9+
import com.fasterxml.jackson.databind.ObjectMapper;
10+
import java.io.IOException;
511
import java.util.Arrays;
12+
import java.util.HashMap;
613
import java.util.HashSet;
714
import java.util.List;
15+
import java.util.Map;
816
import java.util.Set;
917
import org.junit.Before;
18+
import org.junit.BeforeClass;
1019
import org.junit.Test;
1120
import org.mockito.ArgumentMatchers;
1221
import org.mockito.Mockito;
1322

1423
@SuppressWarnings("javadoc")
1524
public class TRexClientTest {
1625

17-
TRexTransport transport = Mockito.mock(TRexTransport.class);
18-
TRexClient client;
26+
private static TRexTransport transport = Mockito.mock(TRexTransport.class);
27+
private static TRexClient client;
28+
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
1929

20-
private static final String[] SET_VALUES = new String[] {"api_sync_v2", "get_supported_cmds"};
30+
private static final String PORT_STATUS =
31+
"[{\"id\":1113911190,\"jsonrpc\":\"2.0\",\"result\":{\"attr\":{\"fc\":{\"mode\":0},\"layer_cfg\":{\"ether\":{\"dst\":\"00:00:00:00:00:00\",\"src\":\"08:00:27:1a:58:90\",\"state\":\"unconfigured\"},\"ipv4\":{\"state\":\"none\"},\"ipv6\":{\"enabled\":false}},\"link\":{\"up\":true},\"multicast\":{\"enabled\":false},\"promiscuous\":{\"enabled\":false},\"rx_filter_mode\":\"hw\",\"speed\":1.0,\"vlan\":{\"tags\":[]},\"vxlan_fs\":[]},\"max_stream_id\":0,\"owner\":\"\",\"profile_count\":1,\"rx_info\":{\"capture_port\":{\"is_active\":false},\"capwap_proxy\":{\"is_active\":false},\"grat_arp\":{\"is_active\":false},\"latency\":{\"is_active\":false},\"queue\":{\"is_active\":false},\"stack\":{\"is_active\":true}},\"service\":false,\"state\":\"IDLE\"}}]";
32+
private static final String[] SET_VALUES =
33+
new String[] {
34+
"api_sync_v2",
35+
"get_supported_cmds",
36+
"acquire",
37+
"get_port_status",
38+
"get_profile_list",
39+
"get_all_streams",
40+
"start_traffic",
41+
"stop_traffic",
42+
"remove_all_streams",
43+
"release"
44+
};
2145
private static final Set<String> SUPPORTED_COMMANDS = new HashSet<>(Arrays.asList(SET_VALUES));
2246

23-
@Before
24-
public void setup() {
47+
@BeforeClass
48+
public static void setup() {
2549
client = new TRexClient(transport, SUPPORTED_COMMANDS);
2650
}
2751

52+
@Before
53+
public void beforeTest() {
54+
Mockito.reset(transport);
55+
}
56+
57+
/** Test that TRex client can handle single and array json answer from server */
2858
@Test
2959
public void jsonResponsTest() {
30-
String SUPPORTED_CMDS =
60+
String supportedCmds =
3161
"{\"id\":\"aggogxls\",\"jsonrpc\":\"2.0\",\"result\":[\"push_remote\",\"remove_rx_filters\",\"update_streams\",\"pause_streams\",\"pause_traffic\",\"stop_traffic\",\"service\",\"start_traffic\",\"remove_all_streams\",\"remove_stream\",\"add_stream\",\"resume_traffic\",\"start_capture_port\",\"shutdown\",\"conf_ipv6\",\"get_supported_cmds\",\"get_all_streams\",\"stop_capture_port\",\"get_global_stats\",\"api_sync_v2\",\"get_version\",\"validate\",\"update_traffic\",\"get_stream\",\"get_pgid_stats\",\"set_l2\",\"get_system_info\",\"set_capture_port_bpf\",\"get_profile_list\",\"get_port_status\",\"get_utilization\",\"publish_now\",\"ping\",\"set_vlan\",\"set_port_attr\",\"set_l3\",\"get_port_xstats_names\",\"get_port_xstats_values\",\"api_sync\",\"get_async_results\",\"get_port_stats\",\"get_owner\",\"push_pkts\",\"cancel_async_task\",\"capture\",\"get_rx_queue_pkts\",\"set_rx_feature\",\"acquire\",\"conf_ns_batch\",\"get_active_pgids\",\"release\",\"resume_streams\",\"get_stream_list\"]}";
32-
String SUPPORTED_CMDS2 =
62+
String supportedCmds2 =
3363
"[{\"id\":\"aggogxls\",\"jsonrpc\":\"2.0\",\"result\":[\"push_remote\",\"remove_rx_filters\",\"update_streams\",\"pause_streams\",\"pause_traffic\",\"stop_traffic\",\"service\",\"start_traffic\",\"remove_all_streams\",\"remove_stream\",\"add_stream\",\"resume_traffic\",\"start_capture_port\",\"shutdown\",\"conf_ipv6\",\"get_supported_cmds\",\"get_all_streams\",\"stop_capture_port\",\"get_global_stats\",\"api_sync_v2\",\"get_version\",\"validate\",\"update_traffic\",\"get_stream\",\"get_pgid_stats\",\"set_l2\",\"get_system_info\",\"set_capture_port_bpf\",\"get_profile_list\",\"get_port_status\",\"get_utilization\",\"publish_now\",\"ping\",\"set_vlan\",\"set_port_attr\",\"set_l3\",\"get_port_xstats_names\",\"get_port_xstats_values\",\"api_sync\",\"get_async_results\",\"get_port_stats\",\"get_owner\",\"push_pkts\",\"cancel_async_task\",\"capture\",\"get_rx_queue_pkts\",\"set_rx_feature\",\"acquire\",\"conf_ns_batch\",\"get_active_pgids\",\"release\",\"resume_streams\",\"get_stream_list\"]}]";
34-
Mockito.when(transport.sendJson(ArgumentMatchers.anyString())).thenReturn(SUPPORTED_CMDS);
64+
Mockito.when(transport.sendJson(ArgumentMatchers.anyString())).thenReturn(supportedCmds);
3565

3666
List<String> commands = client.getSupportedCommands();
3767
assertTrue("faulty respons", commands.contains("update_streams"));
3868

39-
Mockito.when(transport.sendJson(ArgumentMatchers.anyString())).thenReturn(SUPPORTED_CMDS2);
69+
Mockito.when(transport.sendJson(ArgumentMatchers.anyString())).thenReturn(supportedCmds2);
4070
List<String> commands2 = client.getSupportedCommands();
4171
assertTrue("faulty respons", commands2.contains("update_streams"));
4272
}
73+
74+
@Test
75+
public void serverAPISyncTest()
76+
throws TRexConnectionException, JsonParseException, JsonMappingException, IOException {
77+
ObjectMapper objectMapper = new ObjectMapper();
78+
RPCResponse[] response =
79+
objectMapper.readValue(
80+
"[{\"id\":1417504019,\"jsonrpc\":\"2.0\",\"result\":{\"api_h\":\"8jNorAXh\"}}]",
81+
RPCResponse[].class);
82+
Mockito.when(transport.sendCommand(ArgumentMatchers.any(TRexCommand.class)))
83+
.thenReturn(response[0]);
84+
85+
client.serverAPISync();
86+
}
87+
88+
@Test(expected = TRexConnectionException.class)
89+
public void serverAPISyncExceptionTest()
90+
throws TRexConnectionException, JsonParseException, JsonMappingException, IOException {
91+
RPCResponse[] response =
92+
OBJECT_MAPPER.readValue(
93+
"[{\"id\":1417504019,\"jsonrpc\":\"2.0\",\"result\":null}]", RPCResponse[].class);
94+
Mockito.when(transport.sendCommand(ArgumentMatchers.any(TRexCommand.class)))
95+
.thenReturn(response[0]);
96+
97+
client.serverAPISync();
98+
}
99+
100+
/**
101+
* JSON Req:
102+
* {"method":"acquire","id":"aggogxls","jsonrpc":"2.0","params":{"api_h":"Zp76xIYz","port_id":0,"session_id":123456789,"force":true,"user":"trex"}}
103+
* JSON Resp: [{"id":"aggogxls","jsonrpc":"2.0","result":"UKoNP1hY"}]
104+
*
105+
* @throws IOException
106+
* @throws JsonMappingException
107+
* @throws JsonParseException
108+
*/
109+
@Test
110+
public void acquirePortTest() throws JsonParseException, JsonMappingException, IOException {
111+
RPCResponse[] response = OBJECT_MAPPER.readValue(PORT_STATUS, RPCResponse[].class);
112+
Mockito.when(transport.sendCommand(ArgumentMatchers.any(TRexCommand.class)))
113+
.thenReturn(response[0]);
114+
Mockito.when(transport.sendJson(ArgumentMatchers.contains("\"acquire\"")))
115+
.thenReturn("[{\"id\":\"aggogxls\",\"jsonrpc\":\"2.0\",\"result\":\"UKoNP1hY\"}]");
116+
117+
client.acquirePort(0, true);
118+
}
119+
120+
/**
121+
* JSON Req:
122+
* {"method":"get_port_status","id":1113911190,"jsonrpc":"2.0","params":{"api_h":"Zp76xIYz","port_id":0,"block":false}}
123+
* JSON Resp:
124+
* [{"id":1113911190,"jsonrpc":"2.0","result":{"attr":{"fc":{"mode":0},"layer_cfg":{"ether":{"dst":"00:00:00:00:00:00","src":"08:00:27:1a:58:90","state":"unconfigured"},"ipv4":{"state":"none"},"ipv6":{"enabled":false}},"link":{"up":true},"multicast":{"enabled":false},"promiscuous":{"enabled":false},"rx_filter_mode":"hw","speed":1.0,"vlan":{"tags":[]},"vxlan_fs":[]},"max_stream_id":0,"owner":"","profile_count":1,"rx_info":{"capture_port":{"is_active":false},"capwap_proxy":{"is_active":false},"grat_arp":{"is_active":false},"latency":{"is_active":false},"queue":{"is_active":false},"stack":{"is_active":true}},"service":false,"state":"IDLE"}}]
125+
*
126+
* @throws IOException
127+
* @throws JsonMappingException
128+
* @throws JsonParseException
129+
*/
130+
@Test
131+
public void getPortStatusTest() throws JsonParseException, JsonMappingException, IOException {
132+
RPCResponse[] response = OBJECT_MAPPER.readValue(PORT_STATUS, RPCResponse[].class);
133+
Mockito.when(transport.sendCommand(ArgumentMatchers.any(TRexCommand.class)))
134+
.thenReturn(response[0]);
135+
136+
client.getPortStatus(0);
137+
}
138+
139+
/**
140+
* JSON Req:
141+
* {"method":"get_profile_list","id":"aggogxls","jsonrpc":"2.0","params":{"handler":"THSLqmTH","api_h":"Zp76xIYz","port_id":1}}
142+
* JSON Resp: [{"id":"aggogxls","jsonrpc":"2.0","result":["_"]}]
143+
*/
144+
@Test
145+
public void getProfileIdsTest() {
146+
Mockito.when(transport.sendJson(ArgumentMatchers.contains("\"get_profile_list\"")))
147+
.thenReturn("[{\"id\":\"aggogxls\",\"jsonrpc\":\"2.0\",\"result\":[\"_\"]}]");
148+
149+
client.getProfileIds(0);
150+
}
151+
152+
/**
153+
* JSON Req:
154+
* {"method":"get_all_streams","id":"aggogxls","jsonrpc":"2.0","params":{"handler":"THSLqmTH","profile_id":"_","api_h":"Zp76xIYz","port_id":1}}
155+
* JSON Resp:
156+
* [{"id":"aggogxls","jsonrpc":"2.0","result":{"streams":{"131040989":{"action_count":0,"core_id":-1,"enabled":true,"flags":3,"flow_stats":{"enabled":false,"rule_type":"stats","stream_id":131040989},"id":131040989,"isg":0.0,"mode":{"count":0,"ibg":0.0,"pkts_per_burst":0,"rate":{"type":"pps","value":1.0},"total_pkts":0,"type":"continuous"},"next_stream_id":-1,"packet":{"binary":"////////AFBWlCHfCAYAAQgABgQAAQBQVpQh38CoCRv////////AqAkcAAAAAAAAAAAAAAAAAAAAAAAA","meta":""},"random_seed":0,"rx_stats":{"enabled":true,"latency_enabled":true,"seq_enabled":true,"stream_id":131040989},"self_start":true,"vm":{"instructions":[],"split_by_var":""}},"1360153259":{"action_count":0,"core_id":-1,"enabled":true,"flags":3,"flow_stats":{"enabled":false,"rule_type":"stats","stream_id":1360153259},"id":1360153259,"isg":0.0,"mode":{"count":0,"ibg":0.0,"pkts_per_burst":0,"rate":{"type":"pps","value":1.0},"total_pkts":0,"type":"continuous"},"next_stream_id":-1,"packet":{"binary":"////////AFBWlCHfCAYAAQgABgQAAQBQVpQh38CoCRv////////AqAkcAAAAAAAAAAAAAAAAAAAAAAAA","meta":""},"random_seed":0,"rx_stats":{"enabled":true,"latency_enabled":true,"seq_enabled":true,"stream_id":1360153259},"self_start":true,"vm":{"instructions":[],"split_by_var":""}}}}}]
157+
*/
158+
@Test
159+
public void getAllStreamsTest() {
160+
Mockito.when(transport.sendJson(ArgumentMatchers.contains("\"get_all_streams\"")))
161+
.thenReturn(
162+
"[{\"id\":\"aggogxls\",\"jsonrpc\":\"2.0\",\"result\":{\"streams\":{\"131040989\":{\"action_count\":0,\"core_id\":-1,\"enabled\":true,\"flags\":3,\"flow_stats\":{\"enabled\":false,\"rule_type\":\"stats\",\"stream_id\":131040989},\"id\":131040989,\"isg\":0.0,\"mode\":{\"count\":0,\"ibg\":0.0,\"pkts_per_burst\":0,\"rate\":{\"type\":\"pps\",\"value\":1.0},\"total_pkts\":0,\"type\":\"continuous\"},\"next_stream_id\":-1,\"packet\":{\"binary\":\"////////AFBWlCHfCAYAAQgABgQAAQBQVpQh38CoCRv////////AqAkcAAAAAAAAAAAAAAAAAAAAAAAA\",\"meta\":\"\"},\"random_seed\":0,\"rx_stats\":{\"enabled\":true,\"latency_enabled\":true,\"seq_enabled\":true,\"stream_id\":131040989},\"self_start\":true,\"vm\":{\"instructions\":[],\"split_by_var\":\"\"}},\"1360153259\":{\"action_count\":0,\"core_id\":-1,\"enabled\":true,\"flags\":3,\"flow_stats\":{\"enabled\":false,\"rule_type\":\"stats\",\"stream_id\":1360153259},\"id\":1360153259,\"isg\":0.0,\"mode\":{\"count\":0,\"ibg\":0.0,\"pkts_per_burst\":0,\"rate\":{\"type\":\"pps\",\"value\":1.0},\"total_pkts\":0,\"type\":\"continuous\"},\"next_stream_id\":-1,\"packet\":{\"binary\":\"////////AFBWlCHfCAYAAQgABgQAAQBQVpQh38CoCRv////////AqAkcAAAAAAAAAAAAAAAAAAAAAAAA\",\"meta\":\"\"},\"random_seed\":0,\"rx_stats\":{\"enabled\":true,\"latency_enabled\":true,\"seq_enabled\":true,\"stream_id\":1360153259},\"self_start\":true,\"vm\":{\"instructions\":[],\"split_by_var\":\"\"}}}}}]");
163+
164+
client.getAllStreams(0);
165+
}
166+
167+
/**
168+
* JSON Req:
169+
* {"method":"start_traffic","id":"aggogxls","jsonrpc":"2.0","params":{"duration":-1.0,"handler":"THSLqmTH","mul":{"op":"abs","type":"raw","value":1.0},"profile_id":"_","api_h":"Zp76xIYz","port_id":1,"core_mask":1,"force":true}}
170+
* JSON Resp:
171+
* [{"id":"aggogxls","jsonrpc":"2.0","result":{"multiplier":1.0,"ts":1924.383710379147}}]
172+
*/
173+
@Test
174+
public void startAllTraffic() {
175+
Map<String, Object> mul = new HashMap<>();
176+
mul.put("op", "abs");
177+
mul.put("type", "pps");
178+
mul.put("value", 1.0);
179+
Mockito.when(transport.sendJson(ArgumentMatchers.contains("\"start_traffic\"")))
180+
.thenReturn(
181+
"[{\"id\":\"aggogxls\",\"jsonrpc\":\"2.0\",\"result\":{\"multiplier\":1.0,\"ts\":1924.383710379147}}]");
182+
183+
client.startTraffic(0, -1, true, mul, 0);
184+
}
185+
186+
/**
187+
* JSON Req:
188+
* {"method":"stop_traffic","id":"aggogxls","jsonrpc":"2.0","params":{"handler":"THSLqmTH","profile_id":"_","api_h":"Zp76xIYz","port_id":1}}
189+
* JSON Resp: [{"id":"aggogxls","jsonrpc":"2.0","result":{}}]
190+
*/
191+
@Test
192+
public void stopAllTraffic() {
193+
Mockito.when(transport.sendJson(ArgumentMatchers.contains("\"get_profile_list\"")))
194+
.thenReturn("[{\"id\":\"aggogxls\",\"jsonrpc\":\"2.0\",\"result\":[\"_\"]}]");
195+
Mockito.when(transport.sendJson(ArgumentMatchers.contains("\"stop_traffic\"")))
196+
.thenReturn("[{\"id\":\"aggogxls\",\"jsonrpc\":\"2.0\",\"result\":{}}]");
197+
198+
client.stopAllTraffic(0);
199+
}
200+
201+
/**
202+
* JSON Req:
203+
* {"method":"remove_all_streams","id":"aggogxls","jsonrpc":"2.0","params":{"handler":"6rHxHisC","profile_id":"_","api_h":"Zp76xIYz","port_id":1}}
204+
* JSON Resp: [{"id":"aggogxls","jsonrpc":"2.0","result":{}}]
205+
*/
206+
@Test
207+
public void removeAllStreams() {
208+
Mockito.when(transport.sendJson(ArgumentMatchers.contains("\"remove_all_streams\"")))
209+
.thenReturn(
210+
"[{\"id\":\"aggogxls\",\"jsonrpc\":\"2.0\",\"result\":{\"streams\":{\"131040989\":{\"action_count\":0,\"core_id\":-1,\"enabled\":true,\"flags\":3,\"flow_stats\":{\"enabled\":false,\"rule_type\":\"stats\",\"stream_id\":131040989},\"id\":131040989,\"isg\":0.0,\"mode\":{\"count\":0,\"ibg\":0.0,\"pkts_per_burst\":0,\"rate\":{\"type\":\"pps\",\"value\":1.0},\"total_pkts\":0,\"type\":\"continuous\"},\"next_stream_id\":-1,\"packet\":{\"binary\":\"////////AFBWlCHfCAYAAQgABgQAAQBQVpQh38CoCRv////////AqAkcAAAAAAAAAAAAAAAAAAAAAAAA\",\"meta\":\"\"},\"random_seed\":0,\"rx_stats\":{\"enabled\":true,\"latency_enabled\":true,\"seq_enabled\":true,\"stream_id\":131040989},\"self_start\":true,\"vm\":{\"instructions\":[],\"split_by_var\":\"\"}},\"1360153259\":{\"action_count\":0,\"core_id\":-1,\"enabled\":true,\"flags\":3,\"flow_stats\":{\"enabled\":false,\"rule_type\":\"stats\",\"stream_id\":1360153259},\"id\":1360153259,\"isg\":0.0,\"mode\":{\"count\":0,\"ibg\":0.0,\"pkts_per_burst\":0,\"rate\":{\"type\":\"pps\",\"value\":1.0},\"total_pkts\":0,\"type\":\"continuous\"},\"next_stream_id\":-1,\"packet\":{\"binary\":\"////////AFBWlCHfCAYAAQgABgQAAQBQVpQh38CoCRv////////AqAkcAAAAAAAAAAAAAAAAAAAAAAAA\",\"meta\":\"\"},\"random_seed\":0,\"rx_stats\":{\"enabled\":true,\"latency_enabled\":true,\"seq_enabled\":true,\"stream_id\":1360153259},\"self_start\":true,\"vm\":{\"instructions\":[],\"split_by_var\":\"\"}}}}}]");
211+
212+
client.removeAllStreams(0);
213+
}
214+
215+
/**
216+
* JSON Req:
217+
* {"method":"release","id":"aggogxls","jsonrpc":"2.0","params":{"handler":"6rHxHisC","api_h":"Zp76xIYz","port_id":1,"user":"trex"}}
218+
* JSON Resp: [{"id":"aggogxls","jsonrpc":"2.0","result":{}}]
219+
*
220+
* @throws IOException
221+
*/
222+
@Test
223+
public void releasePort() throws IOException {
224+
RPCResponse[] response = OBJECT_MAPPER.readValue(PORT_STATUS, RPCResponse[].class);
225+
Mockito.when(transport.sendCommand(ArgumentMatchers.any(TRexCommand.class)))
226+
.thenReturn(response[0]);
227+
Mockito.when(transport.sendJson(ArgumentMatchers.contains("\"release\"")))
228+
.thenReturn("[{\"id\":\"aggogxls\",\"jsonrpc\":\"2.0\",\"result\":{}}]");
229+
230+
client.releasePort(0);
231+
}
43232
}

0 commit comments

Comments
 (0)