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