blob: da693803f48080e90b7c8f9e466f6cbabcefd91e [file] [log] [blame]
Jian Liae3fcff2018-07-30 11:55:44 +09001/*
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 */
16package org.onosproject.openstacktelemetry.impl;
17
18import com.google.common.collect.ImmutableSet;
19import com.google.common.testing.EqualsTester;
20import org.junit.Before;
21import org.junit.Test;
22import org.onlab.packet.IpAddress;
23import org.onlab.packet.IpPrefix;
24import org.onlab.packet.MacAddress;
25import org.onlab.packet.TpPort;
26import org.onlab.packet.VlanId;
27import org.onosproject.net.DeviceId;
Jian Li7fe7eaf2018-12-31 17:00:33 +090028import org.onosproject.openstacktelemetry.api.DefaultFlowInfo;
29import org.onosproject.openstacktelemetry.api.DefaultStatsInfo;
Jian Liae3fcff2018-07-30 11:55:44 +090030import org.onosproject.openstacktelemetry.api.FlowInfo;
31import org.onosproject.openstacktelemetry.api.InfluxRecord;
32import org.onosproject.openstacktelemetry.api.StatsInfo;
33
34import java.util.Set;
35
36import static org.hamcrest.MatcherAssert.assertThat;
37import static org.hamcrest.Matchers.is;
38import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
39
40/**
41 * Unit tests for DefaultInfluxRecord class.
42 */
43public final class DefaultInfluxRecordTest {
44
45 private static final String MEASUREMENT_1 = "sonaflow-1";
46 private static final String MEASUREMENT_2 = "sonaflow-2";
47
48 private static final String IP_ADDRESS_1 = "10.10.10.1";
49 private static final String IP_ADDRESS_2 = "20.20.20.1";
50
51 private static final String MAC_ADDRESS_1 = "AA:BB:CC:DD:EE:FF";
52 private static final String MAC_ADDRESS_2 = "FF:EE:DD:CC:BB:AA";
53
54 private static final int IP_PREFIX_LENGTH_1 = 10;
55 private static final int IP_PREFIX_LENGTH_2 = 20;
56
57 private static final int PORT_1 = 1000;
58 private static final int PORT_2 = 2000;
59
60 private static final int STATIC_INTEGER_1 = 1;
61 private static final int STATIC_INTEGER_2 = 2;
62
63 private static final String STATIC_STRING_1 = "1";
64 private static final String STATIC_STRING_2 = "2";
65
66 private InfluxRecord record1;
67 private InfluxRecord sameAsRecord1;
68 private InfluxRecord record2;
69
70 private Set<FlowInfo> flowInfos1;
71 private Set<FlowInfo> flowInfos2;
72
73 /**
74 * Initial setup for this unit test.
75 */
76 @Before
77 public void setUp() {
78
79 FlowInfo.Builder builder1 = new DefaultFlowInfo.DefaultBuilder();
80 FlowInfo.Builder builder2 = new DefaultFlowInfo.DefaultBuilder();
81
82 StatsInfo statsInfo = new DefaultStatsInfo.DefaultBuilder().build();
83
84 FlowInfo info1 = builder1
85 .withFlowType((byte) STATIC_INTEGER_1)
86 .withInputInterfaceId(STATIC_INTEGER_1)
87 .withOutputInterfaceId(STATIC_INTEGER_1)
88 .withDeviceId(DeviceId.deviceId(STATIC_STRING_1))
89 .withSrcIp(IpPrefix.valueOf(
90 IpAddress.valueOf(IP_ADDRESS_1), IP_PREFIX_LENGTH_1))
91 .withDstIp(IpPrefix.valueOf(
92 IpAddress.valueOf(IP_ADDRESS_1), IP_PREFIX_LENGTH_1))
93 .withSrcPort(TpPort.tpPort(PORT_1))
94 .withDstPort(TpPort.tpPort(PORT_1))
95 .withProtocol((byte) STATIC_INTEGER_1)
96 .withVlanId(VlanId.vlanId(STATIC_STRING_1))
97 .withSrcMac(MacAddress.valueOf(MAC_ADDRESS_1))
98 .withDstMac(MacAddress.valueOf(MAC_ADDRESS_1))
99 .withStatsInfo(statsInfo)
100 .build();
101
102 FlowInfo info2 = builder2
103 .withFlowType((byte) STATIC_INTEGER_2)
104 .withInputInterfaceId(STATIC_INTEGER_2)
105 .withOutputInterfaceId(STATIC_INTEGER_2)
106 .withDeviceId(DeviceId.deviceId(STATIC_STRING_2))
107 .withSrcIp(IpPrefix.valueOf(
108 IpAddress.valueOf(IP_ADDRESS_2), IP_PREFIX_LENGTH_2))
109 .withDstIp(IpPrefix.valueOf(
110 IpAddress.valueOf(IP_ADDRESS_2), IP_PREFIX_LENGTH_2))
111 .withSrcPort(TpPort.tpPort(PORT_2))
112 .withDstPort(TpPort.tpPort(PORT_2))
113 .withProtocol((byte) STATIC_INTEGER_2)
114 .withVlanId(VlanId.vlanId(STATIC_STRING_2))
115 .withSrcMac(MacAddress.valueOf(MAC_ADDRESS_2))
116 .withDstMac(MacAddress.valueOf(MAC_ADDRESS_2))
117 .withStatsInfo(statsInfo)
118 .build();
119 flowInfos1 = ImmutableSet.of(info1);
120 flowInfos2 = ImmutableSet.of(info2);
121
122 record1 = new DefaultInfluxRecord(MEASUREMENT_1, flowInfos1);
123 sameAsRecord1 = new DefaultInfluxRecord(MEASUREMENT_1, flowInfos1);
124 record2 = new DefaultInfluxRecord(MEASUREMENT_2, flowInfos2);
125 }
126
127 /**
128 * Tests class immutability.
129 */
130 @Test
131 public void testImmutability() {
132 assertThatClassIsImmutable(DefaultInfluxRecord.class);
133 }
134
135 /**
136 * Tests object equality.
137 */
138 @Test
139 public void testEquality() {
140 new EqualsTester()
141 .addEqualityGroup(record1, sameAsRecord1)
142 .addEqualityGroup(record2).testEquals();
143 }
144
145 /**
146 * Tests object construction.
147 */
148 @Test
149 public void testConstruction() {
150 InfluxRecord record = record1;
151
152 assertThat(record.measurement(), is(MEASUREMENT_1));
153 assertThat(record.flowInfos(), is(flowInfos1));
154 }
155}