Unit tests to improve coverage
- DefaultPacketRequest - PacketEventTest - PacketProcessor Change-Id: I86d1416f808ebc901ed84204f2e772dc25f9eb58
Showing
3 changed files
with
203 additions
and
0 deletions
1 | +/* | ||
2 | + * Copyright 2015 Open Networking Laboratory | ||
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 | +package org.onosproject.net.packet; | ||
17 | + | ||
18 | +import org.junit.Test; | ||
19 | +import org.onosproject.core.DefaultApplicationId; | ||
20 | +import org.onosproject.net.NetTestTools; | ||
21 | +import org.onosproject.net.flow.DefaultTrafficSelector; | ||
22 | +import org.onosproject.net.flow.TrafficSelector; | ||
23 | + | ||
24 | +import com.google.common.testing.EqualsTester; | ||
25 | + | ||
26 | +import static org.hamcrest.MatcherAssert.assertThat; | ||
27 | +import static org.hamcrest.Matchers.is; | ||
28 | +import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable; | ||
29 | + | ||
30 | +/** | ||
31 | + * Unit tests for the DefaultPacketRequest class. | ||
32 | + */ | ||
33 | +public class DefaultPacketRequestTest { | ||
34 | + | ||
35 | + private final TrafficSelector selector = DefaultTrafficSelector | ||
36 | + .builder() | ||
37 | + .matchIcmpCode((byte) 1) | ||
38 | + .build(); | ||
39 | + | ||
40 | + private final DefaultPacketRequest packetRequest1 = | ||
41 | + new DefaultPacketRequest(DefaultTrafficSelector.emptySelector(), | ||
42 | + PacketPriority.CONTROL, | ||
43 | + NetTestTools.APP_ID); | ||
44 | + private final DefaultPacketRequest sameAsacketRequest1 = | ||
45 | + new DefaultPacketRequest(DefaultTrafficSelector.emptySelector(), | ||
46 | + PacketPriority.CONTROL, | ||
47 | + NetTestTools.APP_ID); | ||
48 | + private final DefaultPacketRequest packetRequest2 = | ||
49 | + new DefaultPacketRequest(selector, | ||
50 | + PacketPriority.CONTROL, | ||
51 | + NetTestTools.APP_ID); | ||
52 | + private final DefaultPacketRequest packetRequest3 = | ||
53 | + new DefaultPacketRequest(DefaultTrafficSelector.emptySelector(), | ||
54 | + PacketPriority.REACTIVE, | ||
55 | + NetTestTools.APP_ID); | ||
56 | + private final DefaultPacketRequest packetRequest4 = | ||
57 | + new DefaultPacketRequest(DefaultTrafficSelector.emptySelector(), | ||
58 | + PacketPriority.CONTROL, | ||
59 | + new DefaultApplicationId(1, "foo")); | ||
60 | + | ||
61 | + /** | ||
62 | + * Tests the operation of the equals(), toAstring() and hashCode() methods. | ||
63 | + */ | ||
64 | + @Test | ||
65 | + public void testEquals() { | ||
66 | + new EqualsTester() | ||
67 | + .addEqualityGroup(packetRequest1, sameAsacketRequest1) | ||
68 | + .addEqualityGroup(packetRequest2) | ||
69 | + .addEqualityGroup(packetRequest3) | ||
70 | + .addEqualityGroup(packetRequest4) | ||
71 | + .testEquals(); | ||
72 | + } | ||
73 | + | ||
74 | + /** | ||
75 | + * Tests that building and fetching from a DefaultPacketRequest is correct. | ||
76 | + */ | ||
77 | + @Test | ||
78 | + public void testConstruction() { | ||
79 | + assertThat(packetRequest1.priority(), is(PacketPriority.CONTROL)); | ||
80 | + assertThat(packetRequest1.priority().priorityValue(), | ||
81 | + is(PacketPriority.CONTROL.priorityValue())); | ||
82 | + assertThat(packetRequest1.selector(), is(DefaultTrafficSelector.emptySelector())); | ||
83 | + assertThat(packetRequest1.appId(), is(NetTestTools.APP_ID)); | ||
84 | + } | ||
85 | + | ||
86 | + /** | ||
87 | + * Checks that the DefaultPacketRequest class is immutable. | ||
88 | + */ | ||
89 | + @Test | ||
90 | + public void testImmutability() { | ||
91 | + assertThatClassIsImmutable(DefaultPacketRequest.class); | ||
92 | + } | ||
93 | +} |
1 | +/* | ||
2 | + * Copyright 2015 Open Networking Laboratory | ||
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 | +package org.onosproject.net.packet; | ||
17 | + | ||
18 | +import org.junit.Test; | ||
19 | + | ||
20 | +import static org.hamcrest.MatcherAssert.assertThat; | ||
21 | +import static org.hamcrest.Matchers.greaterThanOrEqualTo; | ||
22 | +import static org.hamcrest.Matchers.is; | ||
23 | + | ||
24 | +/** | ||
25 | + * Unit tests for PacketEvent class. | ||
26 | + */ | ||
27 | +public class PacketEventTest { | ||
28 | + | ||
29 | + OutboundPacket packet; | ||
30 | + | ||
31 | + @Test | ||
32 | + public void testConstruction1() { | ||
33 | + long time = System.currentTimeMillis(); | ||
34 | + PacketEvent event = new PacketEvent(PacketEvent.Type.EMIT, packet); | ||
35 | + | ||
36 | + assertThat(event.type(), is(PacketEvent.Type.EMIT)); | ||
37 | + assertThat(event.subject(), is(packet)); | ||
38 | + assertThat(event.time(), greaterThanOrEqualTo(time)); | ||
39 | + } | ||
40 | + | ||
41 | + @Test | ||
42 | + public void testConstruction2() { | ||
43 | + long time = 12345678; | ||
44 | + PacketEvent event = new PacketEvent(PacketEvent.Type.EMIT, packet, time); | ||
45 | + | ||
46 | + assertThat(event.type(), is(PacketEvent.Type.EMIT)); | ||
47 | + assertThat(event.subject(), is(packet)); | ||
48 | + assertThat(event.time(), is(time)); | ||
49 | + } | ||
50 | + | ||
51 | +} |
1 | +/* | ||
2 | + * Copyright 2015 Open Networking Laboratory | ||
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 | +package org.onosproject.net.packet; | ||
17 | + | ||
18 | +import org.junit.Test; | ||
19 | + | ||
20 | +import static org.hamcrest.MatcherAssert.assertThat; | ||
21 | +import static org.hamcrest.Matchers.greaterThanOrEqualTo; | ||
22 | +import static org.hamcrest.Matchers.lessThan; | ||
23 | + | ||
24 | +/** | ||
25 | + * Unit tests for static APIs in the packet processor class. | ||
26 | + */ | ||
27 | +public class PacketProcessorTest { | ||
28 | + | ||
29 | + /** | ||
30 | + * Tests a priority in the advisor range. | ||
31 | + */ | ||
32 | + @Test | ||
33 | + public void testAdvisorPriorities() { | ||
34 | + int advisorPriority = PacketProcessor.advisor(3); | ||
35 | + assertThat(advisorPriority, lessThan(PacketProcessor.ADVISOR_MAX)); | ||
36 | + assertThat(advisorPriority, greaterThanOrEqualTo(0)); | ||
37 | + } | ||
38 | + | ||
39 | + /** | ||
40 | + * Tests a priority in the director range. | ||
41 | + */ | ||
42 | + @Test | ||
43 | + public void testDirectorPriorities() { | ||
44 | + int directorPriority = PacketProcessor.director(3); | ||
45 | + assertThat(directorPriority, lessThan(PacketProcessor.DIRECTOR_MAX)); | ||
46 | + assertThat(directorPriority, greaterThanOrEqualTo(PacketProcessor.ADVISOR_MAX)); | ||
47 | + } | ||
48 | + | ||
49 | + /** | ||
50 | + * Tests a priority in the observer range. | ||
51 | + */ | ||
52 | + @Test | ||
53 | + public void testObserverPriorities() { | ||
54 | + int observerPriority = PacketProcessor.observer(3); | ||
55 | + assertThat(observerPriority, lessThan(PacketProcessor.OBSERVER_MAX)); | ||
56 | + assertThat(observerPriority, greaterThanOrEqualTo(PacketProcessor.DIRECTOR_MAX)); | ||
57 | + } | ||
58 | + | ||
59 | +} |
-
Please register or login to post a comment