Ray Milkey | d164c0e | 2015-06-16 11:37:39 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Ray Milkey | d164c0e | 2015-06-16 11:37:39 -0700 | [diff] [blame] | 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.device; |
| 17 | |
Ray Milkey | 419dcad | 2015-06-17 11:27:11 -0700 | [diff] [blame] | 18 | import java.lang.reflect.Constructor; |
| 19 | import java.util.Arrays; |
| 20 | |
| 21 | import org.junit.Assert; |
Ray Milkey | d164c0e | 2015-06-16 11:37:39 -0700 | [diff] [blame] | 22 | import org.junit.Test; |
| 23 | import org.onosproject.net.NetTestTools; |
| 24 | |
| 25 | import com.google.common.testing.EqualsTester; |
Ray Milkey | 5ec4208 | 2019-02-13 09:56:07 -0800 | [diff] [blame] | 26 | import org.onosproject.net.PortNumber; |
Ray Milkey | d164c0e | 2015-06-16 11:37:39 -0700 | [diff] [blame] | 27 | |
| 28 | import static org.hamcrest.MatcherAssert.assertThat; |
| 29 | import static org.hamcrest.Matchers.is; |
Ray Milkey | 419dcad | 2015-06-17 11:27:11 -0700 | [diff] [blame] | 30 | import static org.hamcrest.Matchers.notNullValue; |
Ray Milkey | d164c0e | 2015-06-16 11:37:39 -0700 | [diff] [blame] | 31 | import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable; |
| 32 | |
| 33 | /** |
| 34 | * DefaultPortStatistics unit tests. |
| 35 | */ |
| 36 | public 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 Milkey | 5ec4208 | 2019-02-13 09:56:07 -0800 | [diff] [blame] | 49 | .setPort(PortNumber.portNumber(80)) |
Ray Milkey | d164c0e | 2015-06-16 11:37:39 -0700 | [diff] [blame] | 50 | .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 Milkey | 5ec4208 | 2019-02-13 09:56:07 -0800 | [diff] [blame] | 64 | .setPort(PortNumber.portNumber(80)) |
Ray Milkey | d164c0e | 2015-06-16 11:37:39 -0700 | [diff] [blame] | 65 | .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 Milkey | 5ec4208 | 2019-02-13 09:56:07 -0800 | [diff] [blame] | 88 | assertThat(stats1.portNumber().toLong(), is(80L)); |
Ray Milkey | d164c0e | 2015-06-16 11:37:39 -0700 | [diff] [blame] | 89 | } |
| 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 Milkey | 419dcad | 2015-06-17 11:27:11 -0700 | [diff] [blame] | 101 | |
| 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 Milkey | d164c0e | 2015-06-16 11:37:39 -0700 | [diff] [blame] | 127 | } |