Jian Li | 0a5d4d2 | 2018-06-08 14:06:56 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 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.openstacktelemetry.impl; |
| 17 | |
| 18 | import com.google.common.testing.EqualsTester; |
| 19 | import org.junit.Before; |
| 20 | import org.junit.Test; |
| 21 | import org.onosproject.openstacktelemetry.api.StatsInfo; |
| 22 | |
| 23 | import static org.hamcrest.MatcherAssert.assertThat; |
| 24 | import static org.hamcrest.Matchers.is; |
Jian Li | ae3fcff | 2018-07-30 11:55:44 +0900 | [diff] [blame^] | 25 | import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable; |
Jian Li | 0a5d4d2 | 2018-06-08 14:06:56 +0900 | [diff] [blame] | 26 | |
| 27 | /** |
| 28 | * Unit tests for DefaultStatsInfo class. |
| 29 | */ |
Jian Li | 3db9085 | 2018-06-10 22:29:16 +0900 | [diff] [blame] | 30 | public final class DefaultStatsInfoTest { |
Jian Li | 0a5d4d2 | 2018-06-08 14:06:56 +0900 | [diff] [blame] | 31 | |
| 32 | private static final int STATIC_INTEGER_1 = 1; |
| 33 | private static final int STATIC_INTEGER_2 = 2; |
| 34 | |
| 35 | private StatsInfo info1; |
| 36 | private StatsInfo sameAsInfo1; |
| 37 | private StatsInfo info2; |
| 38 | |
Jian Li | ae3fcff | 2018-07-30 11:55:44 +0900 | [diff] [blame^] | 39 | /** |
| 40 | * Initial setup for this unit test. |
| 41 | */ |
Jian Li | 0a5d4d2 | 2018-06-08 14:06:56 +0900 | [diff] [blame] | 42 | @Before |
| 43 | public void setup() { |
| 44 | StatsInfo.Builder builder1 = new DefaultStatsInfo.DefaultBuilder(); |
| 45 | StatsInfo.Builder builder2 = new DefaultStatsInfo.DefaultBuilder(); |
| 46 | StatsInfo.Builder builder3 = new DefaultStatsInfo.DefaultBuilder(); |
| 47 | |
| 48 | info1 = builder1 |
| 49 | .withStartupTime(STATIC_INTEGER_1) |
| 50 | .withCurrAccPkts(STATIC_INTEGER_1) |
| 51 | .withCurrAccBytes(STATIC_INTEGER_1) |
| 52 | .withPrevAccPkts(STATIC_INTEGER_1) |
| 53 | .withPrevAccBytes(STATIC_INTEGER_1) |
| 54 | .withFstPktArrTime(STATIC_INTEGER_1) |
| 55 | .withLstPktOffset(STATIC_INTEGER_1) |
| 56 | .withErrorPkts((short) STATIC_INTEGER_1) |
| 57 | .withDropPkts((short) STATIC_INTEGER_1) |
| 58 | .build(); |
| 59 | |
| 60 | sameAsInfo1 = builder2 |
| 61 | .withStartupTime(STATIC_INTEGER_1) |
| 62 | .withCurrAccPkts(STATIC_INTEGER_1) |
| 63 | .withCurrAccBytes(STATIC_INTEGER_1) |
| 64 | .withPrevAccPkts(STATIC_INTEGER_1) |
| 65 | .withPrevAccBytes(STATIC_INTEGER_1) |
| 66 | .withFstPktArrTime(STATIC_INTEGER_1) |
| 67 | .withLstPktOffset(STATIC_INTEGER_1) |
| 68 | .withErrorPkts((short) STATIC_INTEGER_1) |
| 69 | .withDropPkts((short) STATIC_INTEGER_1) |
| 70 | .build(); |
| 71 | |
| 72 | info2 = builder3 |
| 73 | .withStartupTime(STATIC_INTEGER_2) |
| 74 | .withCurrAccPkts(STATIC_INTEGER_2) |
| 75 | .withCurrAccBytes(STATIC_INTEGER_2) |
| 76 | .withPrevAccPkts(STATIC_INTEGER_2) |
| 77 | .withPrevAccBytes(STATIC_INTEGER_2) |
| 78 | .withFstPktArrTime(STATIC_INTEGER_2) |
| 79 | .withLstPktOffset(STATIC_INTEGER_2) |
| 80 | .withErrorPkts((short) STATIC_INTEGER_2) |
| 81 | .withDropPkts((short) STATIC_INTEGER_2) |
| 82 | .build(); |
| 83 | } |
| 84 | |
Jian Li | ae3fcff | 2018-07-30 11:55:44 +0900 | [diff] [blame^] | 85 | /** |
| 86 | * Tests class immutability. |
| 87 | */ |
| 88 | @Test |
| 89 | public void testImmutability() { |
| 90 | assertThatClassIsImmutable(DefaultStatsInfo.class); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Tests object equality. |
| 95 | */ |
Jian Li | 0a5d4d2 | 2018-06-08 14:06:56 +0900 | [diff] [blame] | 96 | @Test |
| 97 | public void testEquality() { |
| 98 | new EqualsTester() |
| 99 | .addEqualityGroup(info1, sameAsInfo1) |
| 100 | .addEqualityGroup(info2).testEquals(); |
| 101 | } |
| 102 | |
Jian Li | ae3fcff | 2018-07-30 11:55:44 +0900 | [diff] [blame^] | 103 | /** |
| 104 | * Tests object construction. |
| 105 | */ |
Jian Li | 0a5d4d2 | 2018-06-08 14:06:56 +0900 | [diff] [blame] | 106 | @Test |
| 107 | public void testConstruction() { |
| 108 | StatsInfo info = info1; |
| 109 | |
| 110 | assertThat(info.startupTime(), is((long) STATIC_INTEGER_1)); |
| 111 | assertThat(info.currAccPkts(), is(STATIC_INTEGER_1)); |
| 112 | assertThat(info.currAccBytes(), is((long) STATIC_INTEGER_1)); |
| 113 | assertThat(info.prevAccPkts(), is(STATIC_INTEGER_1)); |
| 114 | assertThat(info.prevAccBytes(), is((long) STATIC_INTEGER_1)); |
| 115 | assertThat(info.fstPktArrTime(), is((long) STATIC_INTEGER_1)); |
| 116 | assertThat(info.lstPktOffset(), is(STATIC_INTEGER_1)); |
| 117 | assertThat(info.errorPkts(), is((short) STATIC_INTEGER_1)); |
| 118 | assertThat(info.dropPkts(), is((short) STATIC_INTEGER_1)); |
| 119 | } |
| 120 | } |