Ray Milkey | d164c0e | 2015-06-16 11:37:39 -0700 | [diff] [blame] | 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.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; |
| 26 | |
| 27 | import static org.hamcrest.MatcherAssert.assertThat; |
| 28 | import static org.hamcrest.Matchers.is; |
Ray Milkey | 419dcad | 2015-06-17 11:27:11 -0700 | [diff] [blame] | 29 | import static org.hamcrest.Matchers.notNullValue; |
Ray Milkey | d164c0e | 2015-06-16 11:37:39 -0700 | [diff] [blame] | 30 | import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable; |
| 31 | |
| 32 | /** |
| 33 | * DefaultPortStatistics unit tests. |
| 34 | */ |
| 35 | public 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 Milkey | 419dcad | 2015-06-17 11:27:11 -0700 | [diff] [blame] | 100 | |
| 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 Milkey | d164c0e | 2015-06-16 11:37:39 -0700 | [diff] [blame] | 126 | } |