blob: 7deedda5d0c1d6f8b2a8a39b2fd0211473ef3e21 [file] [log] [blame]
Pier Luigi09220c22017-09-14 22:00:30 +02001/*
2 * Copyright 2017-present Open Networking Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.onosproject.net.behaviour.trafficcontrol;
18
19import org.junit.Rule;
20import org.junit.Test;
21import org.junit.rules.ExpectedException;
22
23import static org.hamcrest.Matchers.is;
24import static org.hamcrest.Matchers.notNullValue;
25import static org.junit.Assert.*;
26import static org.onosproject.net.behaviour.trafficcontrol.TokenBucket.Action.DROP;
27import static org.onosproject.net.behaviour.trafficcontrol.TokenBucket.Action.DSCP_CLASS;
28import static org.onosproject.net.behaviour.trafficcontrol.TokenBucket.Action.DSCP_PRECEDENCE;
29
30/**
31 * Test class for TokenBucket.
32 */
33public class TokenBucketTest {
34
35 // Test rate
36 private static final long RATE = 1;
37 // Test dscp drop precedence
38 private static final short DSCP_PREC = 2;
39 // Test dscp class
40 private static final short DSCP_CL = 250;
41 // Test wrong dscp
42 private static final short WRONG_DSCP = -1;
43
44 /**
45 * Test creation of a drop token bucket.
46 */
47 @Test
48 public void testDropCreation() {
49 // Create a drop token bucket
50 TokenBucket drop = DefaultTokenBucket.builder()
51 .withRate(RATE)
52 .withAction(DROP)
53 .build();
54 // Not null
55 assertThat(drop, notNullValue());
56 // Rate should be equal to RATE
57 assertThat(drop.rate(), is(RATE));
58 // Burst size should be equal to 2xMTU
59 assertThat(drop.burstSize(), is(2 * 1500L));
60 // Action should be drop
61 assertThat(drop.action(), is(DROP));
62 }
63
64 /**
65 * Test creation of a dscp precedence token bucket.
66 */
67 @Test
68 public void testDscpPrecCreation() {
69 // Create a dscp precedence token bucket
70 TokenBucket drop = DefaultTokenBucket.builder()
71 .withRate(RATE)
72 .withAction(DSCP_PRECEDENCE)
73 .withBurstSize(6 * 1500)
74 .withDscp(DSCP_PREC)
75 .build();
76 // Not null
77 assertThat(drop, notNullValue());
78 // Rate should be equal to RATE
79 assertThat(drop.rate(), is(RATE));
80 // Burst size should be equal to 6xMTU
81 assertThat(drop.burstSize(), is(6 * 1500L));
82 // Action should increase dscp drop precedence
83 assertThat(drop.action(), is(DSCP_PRECEDENCE));
84 // Dcsp drop precedence should be increased of 2
85 assertThat(drop.dscp(), is(DSCP_PREC));
86 }
87
88 /**
89 * Test creation of a dscp class token bucket.
90 */
91 @Test
92 public void testDscpClassCreation() {
93 // Create a dscp class token bucket
94 TokenBucket drop = DefaultTokenBucket.builder()
95 .withRate(RATE)
96 .withAction(DSCP_CLASS)
97 .withDscp(DSCP_CL)
98 .build();
99 // Not null
100 assertThat(drop, notNullValue());
101 // Rate should be equal to RATE
102 assertThat(drop.rate(), is(RATE));
103 // Burst size should be equal to 2xMTU
104 assertThat(drop.burstSize(), is(2 * 1500L));
105 // Action should be drop
106 assertThat(drop.action(), is(DSCP_CLASS));
107 // Dcsp drop precedence should be increased of 2
108 assertThat(drop.dscp(), is(DSCP_CL));
109 }
110
111 /**
112 * Exception expected to raise when creating a token bucket with null action.
113 */
114 @Rule
115 public ExpectedException exceptionNullAction = ExpectedException.none();
116
117 /**
118 * Test creation of a token bucket with null action.
119 */
120 @Test
121 public void testNullActionCreation() {
122 // Define expected exception
123 exceptionNullAction.expect(NullPointerException.class);
124 // Create a token bucket without action
125 TokenBucket drop = DefaultTokenBucket.builder()
126 .withRate(RATE)
127 .build();
128 }
129
130 /**
131 * Exception expected to raise when creating a token bucket with wrong dscp.
132 */
133 @Rule
134 public ExpectedException exceptionWrongDscp = ExpectedException.none();
135
136 /**
137 * Test creation of a token bucket with wrong dscp.
138 */
139 @Test
140 public void testWrongDscpCreation() {
141 // Define expected exception
142 exceptionWrongDscp.expect(IllegalArgumentException.class);
143 // Create a token bucket with wrong dscp
144 TokenBucket drop = DefaultTokenBucket.builder()
145 .withRate(RATE)
146 .withAction(DSCP_PRECEDENCE)
147 .withDscp(WRONG_DSCP)
148 .build();
149 }
150
151 /**
152 * Test equality between policer ids.
153 */
154 @Test
155 public void testEqualilty() {
156 // Create a drop token bucket
157 TokenBucket drop = DefaultTokenBucket.builder()
158 .withRate(RATE)
159 .withAction(DROP)
160 .build();
161 // Create a mark token bucket
162 TokenBucket mark = DefaultTokenBucket.builder()
163 .withRate(RATE)
164 .withAction(DSCP_PRECEDENCE)
165 .withDscp(DSCP_PREC)
166 .build();
167 // Create a copy of the drop token bucket
168 TokenBucket copyDrop = DefaultTokenBucket.builder()
169 .withRate(RATE)
170 .withAction(DROP)
171 .build();
172 // Verify equality
173 assertEquals(drop, copyDrop);
174 // Verify not equals
175 assertNotEquals(mark, drop);
176 assertNotEquals(mark, copyDrop);
177 }
178
179}