blob: 98ff5a71fb8511a34e089d9f493fd2f8d4e1b114 [file] [log] [blame]
Ray Milkeyd164c0e2015-06-16 11:37:39 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Ray Milkeyd164c0e2015-06-16 11:37:39 -07003 *
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 */
16package org.onosproject.net.device;
17
Ray Milkey419dcad2015-06-17 11:27:11 -070018import java.lang.reflect.Constructor;
19import java.util.Arrays;
20
21import org.junit.Assert;
Ray Milkeyd164c0e2015-06-16 11:37:39 -070022import org.junit.Test;
23import org.onosproject.net.NetTestTools;
24
25import com.google.common.testing.EqualsTester;
Ray Milkey5ec42082019-02-13 09:56:07 -080026import org.onosproject.net.PortNumber;
Ray Milkeyd164c0e2015-06-16 11:37:39 -070027
28import static org.hamcrest.MatcherAssert.assertThat;
29import static org.hamcrest.Matchers.is;
Ray Milkey419dcad2015-06-17 11:27:11 -070030import static org.hamcrest.Matchers.notNullValue;
Ray Milkeyd164c0e2015-06-16 11:37:39 -070031import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
32
33/**
34 * DefaultPortStatistics unit tests.
35 */
36public class DefaultPortStatisticsTest {
37
38 private final PortStatistics stats1 = DefaultPortStatistics.builder()
39 .setBytesReceived(1)
40 .setBytesSent(2)
41 .setDurationNano(3)
42 .setDurationSec(4)
43 .setPacketsReceived(5)
44 .setPacketsSent(6)
45 .setPacketsRxDropped(7)
46 .setPacketsRxErrors(8)
47 .setPacketsTxDropped(9)
48 .setPacketsTxErrors(10)
Ray Milkey5ec42082019-02-13 09:56:07 -080049 .setPort(PortNumber.portNumber(80))
Ray Milkeyd164c0e2015-06-16 11:37:39 -070050 .setDeviceId(NetTestTools.did("1"))
51 .build();
52
53 private final PortStatistics stats2 = DefaultPortStatistics.builder()
54 .setBytesReceived(1)
55 .setBytesSent(2)
56 .setDurationNano(3)
57 .setDurationSec(4)
58 .setPacketsReceived(5)
59 .setPacketsSent(6)
60 .setPacketsRxDropped(7)
61 .setPacketsRxErrors(8)
62 .setPacketsTxDropped(9)
63 .setPacketsTxErrors(11)
Ray Milkey5ec42082019-02-13 09:56:07 -080064 .setPort(PortNumber.portNumber(80))
Ray Milkeyd164c0e2015-06-16 11:37:39 -070065 .setDeviceId(NetTestTools.did("1"))
66 .build();
67
68 /**
69 * Checks that the GroupOperation class is immutable.
70 */
71 @Test
72 public void testImmutability() {
73 assertThatClassIsImmutable(DefaultPortStatistics.class);
74 }
75
76 @Test
77 public void testConstruction() {
78 assertThat(stats1.bytesReceived(), is(1L));
79 assertThat(stats1.bytesSent(), is(2L));
80 assertThat(stats1.durationNano(), is(3L));
81 assertThat(stats1.durationSec(), is(4L));
82 assertThat(stats1.packetsReceived(), is(5L));
83 assertThat(stats1.packetsSent(), is(6L));
84 assertThat(stats1.packetsRxDropped(), is(7L));
85 assertThat(stats1.packetsRxErrors(), is(8L));
86 assertThat(stats1.packetsTxDropped(), is(9L));
87 assertThat(stats1.packetsTxErrors(), is(10L));
Ray Milkey5ec42082019-02-13 09:56:07 -080088 assertThat(stats1.portNumber().toLong(), is(80L));
Ray Milkeyd164c0e2015-06-16 11:37:39 -070089 }
90
91 /**
92 * Tests the equals, hashCode and toString methods using Guava EqualsTester.
93 */
94 @Test
95 public void testEquals() {
96 new EqualsTester()
97 .addEqualityGroup(stats1, stats1)
98 .addEqualityGroup(stats2)
99 .testEquals();
100 }
Ray Milkey419dcad2015-06-17 11:27:11 -0700101
102 /**
103 * Tests that the empty argument list constructor for serialization
104 * is present and creates a proper object.
105 */
106 @Test
107 public void testSerializerConstructor() {
108 try {
109 Constructor[] constructors = DefaultPortStatistics.class.getDeclaredConstructors();
110 assertThat(constructors, notNullValue());
111 Arrays.stream(constructors).filter(ctor ->
112 ctor.getParameterTypes().length == 0)
113 .forEach(noParamsCtor -> {
114 try {
115 noParamsCtor.setAccessible(true);
116 DefaultPortStatistics stats =
117 (DefaultPortStatistics) noParamsCtor.newInstance();
118 assertThat(stats, notNullValue());
119 } catch (Exception e) {
120 Assert.fail("Exception instantiating no parameters constructor");
121 }
122 });
123 } catch (Exception e) {
124 Assert.fail("Exception looking up constructors");
125 }
126 }
Ray Milkeyd164c0e2015-06-16 11:37:39 -0700127}