blob: d76cd043ad88b0815f4e487b05866a0ef3379be7 [file] [log] [blame]
Ray Milkeyd164c0e2015-06-16 11:37:39 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
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;
26
27import static org.hamcrest.MatcherAssert.assertThat;
28import static org.hamcrest.Matchers.is;
Ray Milkey419dcad2015-06-17 11:27:11 -070029import static org.hamcrest.Matchers.notNullValue;
Ray Milkeyd164c0e2015-06-16 11:37:39 -070030import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
31
32/**
33 * DefaultPortStatistics unit tests.
34 */
35public class DefaultPortStatisticsTest {
36
37 private final PortStatistics stats1 = DefaultPortStatistics.builder()
38 .setBytesReceived(1)
39 .setBytesSent(2)
40 .setDurationNano(3)
41 .setDurationSec(4)
42 .setPacketsReceived(5)
43 .setPacketsSent(6)
44 .setPacketsRxDropped(7)
45 .setPacketsRxErrors(8)
46 .setPacketsTxDropped(9)
47 .setPacketsTxErrors(10)
48 .setPort(80)
49 .setDeviceId(NetTestTools.did("1"))
50 .build();
51
52 private final PortStatistics stats2 = DefaultPortStatistics.builder()
53 .setBytesReceived(1)
54 .setBytesSent(2)
55 .setDurationNano(3)
56 .setDurationSec(4)
57 .setPacketsReceived(5)
58 .setPacketsSent(6)
59 .setPacketsRxDropped(7)
60 .setPacketsRxErrors(8)
61 .setPacketsTxDropped(9)
62 .setPacketsTxErrors(11)
63 .setPort(80)
64 .setDeviceId(NetTestTools.did("1"))
65 .build();
66
67 /**
68 * Checks that the GroupOperation class is immutable.
69 */
70 @Test
71 public void testImmutability() {
72 assertThatClassIsImmutable(DefaultPortStatistics.class);
73 }
74
75 @Test
76 public void testConstruction() {
77 assertThat(stats1.bytesReceived(), is(1L));
78 assertThat(stats1.bytesSent(), is(2L));
79 assertThat(stats1.durationNano(), is(3L));
80 assertThat(stats1.durationSec(), is(4L));
81 assertThat(stats1.packetsReceived(), is(5L));
82 assertThat(stats1.packetsSent(), is(6L));
83 assertThat(stats1.packetsRxDropped(), is(7L));
84 assertThat(stats1.packetsRxErrors(), is(8L));
85 assertThat(stats1.packetsTxDropped(), is(9L));
86 assertThat(stats1.packetsTxErrors(), is(10L));
87 assertThat(stats1.port(), is(80));
88 }
89
90 /**
91 * Tests the equals, hashCode and toString methods using Guava EqualsTester.
92 */
93 @Test
94 public void testEquals() {
95 new EqualsTester()
96 .addEqualityGroup(stats1, stats1)
97 .addEqualityGroup(stats2)
98 .testEquals();
99 }
Ray Milkey419dcad2015-06-17 11:27:11 -0700100
101 /**
102 * Tests that the empty argument list constructor for serialization
103 * is present and creates a proper object.
104 */
105 @Test
106 public void testSerializerConstructor() {
107 try {
108 Constructor[] constructors = DefaultPortStatistics.class.getDeclaredConstructors();
109 assertThat(constructors, notNullValue());
110 Arrays.stream(constructors).filter(ctor ->
111 ctor.getParameterTypes().length == 0)
112 .forEach(noParamsCtor -> {
113 try {
114 noParamsCtor.setAccessible(true);
115 DefaultPortStatistics stats =
116 (DefaultPortStatistics) noParamsCtor.newInstance();
117 assertThat(stats, notNullValue());
118 } catch (Exception e) {
119 Assert.fail("Exception instantiating no parameters constructor");
120 }
121 });
122 } catch (Exception e) {
123 Assert.fail("Exception looking up constructors");
124 }
125 }
Ray Milkeyd164c0e2015-06-16 11:37:39 -0700126}