Jian Li | 6bd3510 | 2018-06-09 00:18:47 +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.codec; |
| 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 | import org.onosproject.openstacktelemetry.impl.DefaultStatsInfo; |
| 23 | |
| 24 | import java.nio.ByteBuffer; |
| 25 | |
| 26 | /** |
| 27 | * Unit tests for TinaStatsInfoByteBufferCodef. |
| 28 | */ |
Jian Li | 3db9085 | 2018-06-10 22:29:16 +0900 | [diff] [blame] | 29 | public class TinaStatsInfoByteBufferCodecTest { |
Jian Li | 6bd3510 | 2018-06-09 00:18:47 +0900 | [diff] [blame] | 30 | |
| 31 | private static final int STARTUP_TIME = 1000; |
| 32 | private static final int CURRENT_ACCUMULATED_PACKETS = 8000; |
| 33 | private static final long CURRENT_ACCUMULATED_BYTES = 9000; |
| 34 | private static final int PREVIOUS_ACCUMULATED_PACKETS = 8000; |
| 35 | private static final long PREVIOUS_ACCUMULATED_BYTES = 9000; |
| 36 | private static final long FIRST_PACKET_ARRIVAL_TIME = 10000; |
| 37 | private static final int LAST_PACKET_OFFSET = 20000; |
| 38 | private static final short ERROR_PACKETS = 30000; |
| 39 | private static final short DROP_PACKETS = 30000; |
| 40 | |
| 41 | private StatsInfo info; |
| 42 | private final TinaStatsInfoByteBufferCodec codec = |
| 43 | new TinaStatsInfoByteBufferCodec(); |
| 44 | |
Jian Li | ae3fcff | 2018-07-30 11:55:44 +0900 | [diff] [blame^] | 45 | /** |
| 46 | * Initial setup for this unit test. |
| 47 | */ |
Jian Li | 6bd3510 | 2018-06-09 00:18:47 +0900 | [diff] [blame] | 48 | @Before |
| 49 | public void setup() { |
| 50 | StatsInfo.Builder builder = new DefaultStatsInfo.DefaultBuilder(); |
| 51 | |
| 52 | info = builder |
| 53 | .withStartupTime(STARTUP_TIME) |
| 54 | .withCurrAccPkts(CURRENT_ACCUMULATED_PACKETS) |
| 55 | .withCurrAccBytes(CURRENT_ACCUMULATED_BYTES) |
| 56 | .withPrevAccPkts(PREVIOUS_ACCUMULATED_PACKETS) |
| 57 | .withPrevAccBytes(PREVIOUS_ACCUMULATED_BYTES) |
| 58 | .withFstPktArrTime(FIRST_PACKET_ARRIVAL_TIME) |
| 59 | .withLstPktOffset(LAST_PACKET_OFFSET) |
| 60 | .withErrorPkts(ERROR_PACKETS) |
| 61 | .withDropPkts(DROP_PACKETS) |
| 62 | .build(); |
| 63 | } |
| 64 | |
Jian Li | ae3fcff | 2018-07-30 11:55:44 +0900 | [diff] [blame^] | 65 | /** |
| 66 | * Tests codec encode and decode. |
| 67 | */ |
Jian Li | 6bd3510 | 2018-06-09 00:18:47 +0900 | [diff] [blame] | 68 | @Test |
| 69 | public void testEncodeDecode() { |
| 70 | ByteBuffer buffer = codec.encode(info); |
| 71 | StatsInfo decoded = codec.decode(ByteBuffer.wrap(buffer.array())); |
| 72 | new EqualsTester().addEqualityGroup(info, decoded).testEquals(); |
| 73 | } |
| 74 | } |