Improve core API coverage with new unit tests
- DefaultMirroringDescription
- MirroringName
- MirroringStatistics
- QosId
- made classes immutable
Change-Id: I1d1bf983b181ca87afc35565c5e614f4c54fe0fc
diff --git a/core/api/src/main/java/org/onosproject/net/behaviour/DefaultMirroringDescription.java b/core/api/src/main/java/org/onosproject/net/behaviour/DefaultMirroringDescription.java
index 47a19a3..0c82e48 100644
--- a/core/api/src/main/java/org/onosproject/net/behaviour/DefaultMirroringDescription.java
+++ b/core/api/src/main/java/org/onosproject/net/behaviour/DefaultMirroringDescription.java
@@ -29,7 +29,7 @@
* Default implementation of mirroring description entity.
*/
@Beta
-public class DefaultMirroringDescription extends AbstractDescription
+public final class DefaultMirroringDescription extends AbstractDescription
implements MirroringDescription {
private final MirroringName mirroringName;
diff --git a/core/api/src/main/java/org/onosproject/net/behaviour/MirroringStatistics.java b/core/api/src/main/java/org/onosproject/net/behaviour/MirroringStatistics.java
index 6713633..633995f 100644
--- a/core/api/src/main/java/org/onosproject/net/behaviour/MirroringStatistics.java
+++ b/core/api/src/main/java/org/onosproject/net/behaviour/MirroringStatistics.java
@@ -16,19 +16,27 @@
package org.onosproject.net.behaviour;
-import com.google.common.base.MoreObjects;
-
import java.util.Map;
import java.util.Objects;
+import com.google.common.base.MoreObjects;
+
/**
* Represents statistics associated to a mirroring.
*/
public final class MirroringStatistics {
- private MirroringName mirroringName;
- private int txBytes;
- private int txPackets;
+ private final MirroringName mirroringName;
+ private final long txBytes;
+ private final long txPackets;
+
+ /**
+ * Hide private constructor to prevent calls to new().
+ */
+ private MirroringStatistics() {
+ // This should never happen
+ throw new UnsupportedOperationException();
+ }
/**
* Statistics associated to a named mirroring.
@@ -37,7 +45,7 @@
* @param bytes transmitted bytes
* @param packets transmitted packets
*/
- private MirroringStatistics(String name, int bytes, int packets) {
+ private MirroringStatistics(String name, long bytes, long packets) {
this.mirroringName = MirroringName.mirroringName(name);
this.txBytes = bytes;
this.txPackets = packets;
@@ -78,7 +86,7 @@
*
* @return the packets
*/
- public long packtes() {
+ public long packets() {
return txPackets;
}
@@ -107,7 +115,7 @@
return MoreObjects.toStringHelper(getClass())
.add("name", name())
.add("tx_bytes", bytes())
- .add("tx_packets", packtes())
+ .add("tx_packets", packets())
.toString();
}
diff --git a/core/api/src/main/java/org/onosproject/net/behaviour/TunnelEndPoint.java b/core/api/src/main/java/org/onosproject/net/behaviour/TunnelEndPoint.java
index 2b96452..ab9eee5 100644
--- a/core/api/src/main/java/org/onosproject/net/behaviour/TunnelEndPoint.java
+++ b/core/api/src/main/java/org/onosproject/net/behaviour/TunnelEndPoint.java
@@ -22,9 +22,9 @@
* Represents for source end point or destination end point of a tunnel. Maybe a tunnel
* based on ConnectPoint, IpAddress, MacAddress and so on is built.
*/
-public class TunnelEndPoint<T> {
+public final class TunnelEndPoint<T> {
- protected final T value;
+ private final T value;
/**
* Default constructor.
diff --git a/core/api/src/test/java/org/onosproject/net/behaviour/DefaultMirroringDescriptionTest.java b/core/api/src/test/java/org/onosproject/net/behaviour/DefaultMirroringDescriptionTest.java
new file mode 100644
index 0000000..2596f7b
--- /dev/null
+++ b/core/api/src/test/java/org/onosproject/net/behaviour/DefaultMirroringDescriptionTest.java
@@ -0,0 +1,75 @@
+/*
+ * Copyright 2017-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.net.behaviour;
+
+import java.util.List;
+import java.util.Optional;
+
+import org.junit.Test;
+import org.onlab.packet.VlanId;
+
+import com.google.common.collect.ImmutableList;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.containsString;
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.notNullValue;
+import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
+
+public class DefaultMirroringDescriptionTest {
+
+ private static final MirroringName NAME_1 = MirroringName.mirroringName("mirror1");
+ private static final List<String> MONITOR_SRC_PORTS_1 =
+ ImmutableList.of("s1", "s2", "s3");
+ private static final List<String> MONITOR_DST_PORTS_1 =
+ ImmutableList.of("d1", "d2");
+ private static final List<VlanId> MONITOR_VLANS_1 = ImmutableList.of(VlanId.ANY);
+ private static final Optional<String> MIRROR_PORT_1 = Optional.of("port1");
+ private static final Optional<VlanId> MIRROR_VLAN_1 = Optional.of(VlanId.ANY);
+ private MirroringDescription md1 =
+ new DefaultMirroringDescription(NAME_1, MONITOR_SRC_PORTS_1,
+ MONITOR_DST_PORTS_1, MONITOR_VLANS_1,
+ MIRROR_PORT_1, MIRROR_VLAN_1);
+
+
+ @Test
+ public void testImmutability() {
+ assertThatClassIsImmutable(DefaultMirroringDescription.class);
+ }
+
+ @Test
+ public void testConstruction() {
+ assertThat(md1.name(), is(NAME_1));
+ assertThat(md1.monitorSrcPorts(), is(MONITOR_SRC_PORTS_1));
+ assertThat(md1.monitorDstPorts(), is(MONITOR_DST_PORTS_1));
+ assertThat(md1.monitorVlans(), is(MONITOR_VLANS_1));
+ assertThat(md1.mirrorPort(), is(MIRROR_PORT_1));
+ assertThat(md1.mirrorVlan(), is(MIRROR_VLAN_1));
+ }
+
+ @Test
+ public void testToString() {
+ String result = md1.toString();
+ assertThat(result, notNullValue());
+ assertThat(result, containsString("name=" + NAME_1.toString()));
+ assertThat(result, containsString("monitorsrcports=" + MONITOR_SRC_PORTS_1.toString()));
+ assertThat(result, containsString("monitordstports=" + MONITOR_DST_PORTS_1.toString()));
+ assertThat(result, containsString("monitorvlans=" + MONITOR_VLANS_1.toString()));
+ assertThat(result, containsString("mirrorport=" + MIRROR_PORT_1.toString()));
+ assertThat(result, containsString("mirrorvlan=" + MIRROR_VLAN_1.toString()));
+ }
+}
diff --git a/core/api/src/test/java/org/onosproject/net/behaviour/MirroringNameTest.java b/core/api/src/test/java/org/onosproject/net/behaviour/MirroringNameTest.java
new file mode 100644
index 0000000..bce584f
--- /dev/null
+++ b/core/api/src/test/java/org/onosproject/net/behaviour/MirroringNameTest.java
@@ -0,0 +1,52 @@
+/*
+ * Copyright 2017-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.net.behaviour;
+
+import org.junit.Test;
+
+import com.google.common.testing.EqualsTester;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
+
+public class MirroringNameTest {
+
+ private static final String NAME1 = "name1";
+ private MirroringName name1 = MirroringName.mirroringName(NAME1);
+ private MirroringName sameAsName1 = MirroringName.mirroringName(NAME1);
+ private static final String NAME2 = "name2";
+ private MirroringName name2 = MirroringName.mirroringName(NAME2);
+
+ @Test
+ public void testImmutability() {
+ assertThatClassIsImmutable(MirroringName.class);
+ }
+
+ @Test
+ public void testConstruction() {
+ assertThat(name1.name(), is(NAME1));
+ }
+
+ @Test
+ public void testEquals() {
+ new EqualsTester()
+ .addEqualityGroup(name1, sameAsName1)
+ .addEqualityGroup(name2)
+ .testEquals();
+ }
+}
diff --git a/core/api/src/test/java/org/onosproject/net/behaviour/MirroringStatisticsTest.java b/core/api/src/test/java/org/onosproject/net/behaviour/MirroringStatisticsTest.java
new file mode 100644
index 0000000..b79da88
--- /dev/null
+++ b/core/api/src/test/java/org/onosproject/net/behaviour/MirroringStatisticsTest.java
@@ -0,0 +1,78 @@
+/*
+ * Copyright 2017-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.net.behaviour;
+
+import java.util.Map;
+
+import org.junit.Test;
+
+import com.google.common.collect.ImmutableMap;
+import com.google.common.testing.EqualsTester;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
+import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
+
+public class MirroringStatisticsTest {
+
+ private static final long BYTES_1 = 100L;
+ private static final long PACKETS_1 = 2L;
+ private static final String NAME_1 = "mirror1";
+ private Map<String, Integer> statistics1 =
+ ImmutableMap.of("tx_bytes", (int) BYTES_1, "tx_packets", (int) PACKETS_1);
+ private MirroringStatistics mirrorStatisticStats1 = MirroringStatistics.mirroringStatistics(NAME_1, statistics1);
+
+ private Map<String, Integer> sameAsStatistics1 =
+ ImmutableMap.of("tx_bytes", (int) BYTES_1, "tx_packets", (int) PACKETS_1);
+ private MirroringStatistics sameAsMirrorStatisticStats1 =
+ MirroringStatistics.mirroringStatistics(NAME_1, sameAsStatistics1);
+
+ private static final long BYTES_2 = 100L;
+ private static final long PACKETS_2 = 2L;
+ private static final String NAME_2 = "mirror2";
+ private Map<String, Integer> statistics2 =
+ ImmutableMap.of("tx_bytes", (int) BYTES_2, "tx_packets", (int) PACKETS_2);
+ private MirroringStatistics mirrorStatisticStats2 = MirroringStatistics.mirroringStatistics(NAME_2, statistics2);
+
+ private static final long BYTES_3 = 100L;
+ private static final long PACKETS_3 = 2L;
+ private static final String NAME_3 = "mirror3";
+ private Map<String, Integer> statistics3 =
+ ImmutableMap.of("tx_bytes", (int) BYTES_3, "tx_packets", (int) PACKETS_3);
+ private MirroringStatistics mirrorStatisticStats3 = MirroringStatistics.mirroringStatistics(NAME_3, statistics3);
+
+ @Test
+ public void testImmutability() {
+ assertThatClassIsImmutable(MirroringStatistics.class);
+ }
+
+ @Test
+ public void testConstruction() {
+ assertThat(mirrorStatisticStats1.bytes(), is(BYTES_1));
+ assertThat(mirrorStatisticStats1.name().name(), is(NAME_1));
+ assertThat(mirrorStatisticStats1.packets(), is(PACKETS_1));
+ }
+
+ @Test
+ public void testEquals() {
+ new EqualsTester()
+ .addEqualityGroup(mirrorStatisticStats1, sameAsMirrorStatisticStats1)
+ .addEqualityGroup(mirrorStatisticStats2)
+ .addEqualityGroup(mirrorStatisticStats3)
+ .testEquals();
+ }
+}
diff --git a/core/api/src/test/java/org/onosproject/net/behaviour/QosIdTest.java b/core/api/src/test/java/org/onosproject/net/behaviour/QosIdTest.java
new file mode 100644
index 0000000..11f5c40
--- /dev/null
+++ b/core/api/src/test/java/org/onosproject/net/behaviour/QosIdTest.java
@@ -0,0 +1,21 @@
+/*
+ * Copyright 2017-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.net.behaviour;
+
+public class QosIdTest {
+
+}
diff --git a/core/api/src/test/java/org/onosproject/net/behaviour/TunnelEndPointTest.java b/core/api/src/test/java/org/onosproject/net/behaviour/TunnelEndPointTest.java
new file mode 100644
index 0000000..af76773
--- /dev/null
+++ b/core/api/src/test/java/org/onosproject/net/behaviour/TunnelEndPointTest.java
@@ -0,0 +1,64 @@
+/*
+ * Copyright 2017-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.net.behaviour;
+
+import org.junit.Test;
+import org.onlab.packet.MacAddress;
+import org.onosproject.net.ConnectPoint;
+import org.onosproject.net.NetTestTools;
+
+import com.google.common.testing.EqualsTester;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
+import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
+
+public class TunnelEndPointTest {
+
+ private ConnectPoint cp1 = NetTestTools.connectPoint("cp1", 1);
+ private TunnelEndPoint<ConnectPoint> endPoint1 =
+ new TunnelEndPoint<>(cp1);
+ private TunnelEndPoint<ConnectPoint> sameAsEndPoint1 =
+ new TunnelEndPoint<>(cp1);
+
+ private ConnectPoint cp2 = NetTestTools.connectPoint("cp2", 2);
+ private TunnelEndPoint<ConnectPoint> endPoint2 =
+ new TunnelEndPoint<>(cp2);
+
+ private TunnelEndPoint<MacAddress> endPoint3 =
+ new TunnelEndPoint<>(MacAddress.BROADCAST);
+
+ @Test
+ public void testImmutability() {
+ assertThatClassIsImmutable(TunnelEndPoint.class);
+ }
+
+ @Test
+ public void testConstruction() {
+ assertThat(endPoint1.value(), is(cp1));
+ assertThat(endPoint1.strValue(), is(cp1.toString()));
+ }
+
+ @Test
+ public void testEquals() {
+ new EqualsTester()
+ .addEqualityGroup(endPoint1, sameAsEndPoint1)
+ .addEqualityGroup(endPoint2)
+ .addEqualityGroup(endPoint3)
+ .testEquals();
+ }
+}