Add unit tests for mapping management app
Change-Id: Ie8be52be8c65ba39e8acf541af991865248b019b
diff --git a/apps/mappingmanagement/api/src/test/java/org/onosproject/mapping/DefaultMappingEntryTest.java b/apps/mappingmanagement/api/src/test/java/org/onosproject/mapping/DefaultMappingEntryTest.java
new file mode 100644
index 0000000..d9b1ff8
--- /dev/null
+++ b/apps/mappingmanagement/api/src/test/java/org/onosproject/mapping/DefaultMappingEntryTest.java
@@ -0,0 +1,78 @@
+/*
+ * Copyright 2017-present Open Networking Laboratory
+ *
+ * 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.mapping;
+
+import com.google.common.testing.EqualsTester;
+import org.junit.Test;
+import org.onosproject.net.DeviceId;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
+
+/**
+ * Unit tests for the default mapping entry class.
+ */
+public class DefaultMappingEntryTest {
+
+ private static final MappingKey MAPPING_KEY = new DefaultMappingKey();
+ private static final MappingValue MAPPING_VALUE = new DefaultMappingValue();
+
+ /**
+ * Creates a new mapping entry from an unique value.
+ *
+ * @param value unique value
+ * @return a new mapping entry
+ */
+ private static DefaultMappingEntry makeMappingEntry(int value) {
+ Mapping mapping = new DefaultMapping.Builder()
+ .forDevice(DeviceId.deviceId("lisp:10.1.1." +
+ Integer.toString(value)))
+ .withKey(MAPPING_KEY)
+ .withValue(MAPPING_VALUE)
+ .withId(value)
+ .build();
+
+ return new DefaultMappingEntry(mapping, MappingEntry.MappingEntryState.ADDED);
+ }
+
+ final MappingEntry entry1 = makeMappingEntry(1);
+ final MappingEntry sameAsEntry1 = makeMappingEntry(1);
+ final MappingEntry entry2 = makeMappingEntry(2);
+
+ /**
+ * Tests the equals, hashCode and toString methods using Guava EqualsTester.
+ */
+ @Test
+ public void testEquals() {
+ new EqualsTester()
+ .addEqualityGroup(entry1, sameAsEntry1)
+ .addEqualityGroup(entry2)
+ .testEquals();
+ }
+
+ /**
+ * Tests creation of a DefaultMappingEntry using a mapping builder.
+ */
+ @Test
+ public void testConstruction() {
+ DefaultMappingEntry entry = (DefaultMappingEntry) entry1;
+
+ assertThat(entry.deviceId(), is(DeviceId.deviceId("lisp:10.1.1.1")));
+ assertThat(entry.id(), is(MappingId.valueOf(1)));
+ assertThat(entry.key(), is(MAPPING_KEY));
+ assertThat(entry.value(), is(MAPPING_VALUE));
+ }
+}
diff --git a/apps/mappingmanagement/api/src/test/java/org/onosproject/mapping/DefaultMappingTest.java b/apps/mappingmanagement/api/src/test/java/org/onosproject/mapping/DefaultMappingTest.java
new file mode 100644
index 0000000..6a87d8b
--- /dev/null
+++ b/apps/mappingmanagement/api/src/test/java/org/onosproject/mapping/DefaultMappingTest.java
@@ -0,0 +1,99 @@
+/*
+ * Copyright 2017-present Open Networking Laboratory
+ *
+ * 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.mapping;
+
+import com.google.common.testing.EqualsTester;
+import org.junit.Before;
+import org.junit.Test;
+import org.onosproject.net.DeviceId;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
+import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutableBaseClass;
+
+/**
+ * Unit tests for the default mapping class.
+ */
+public class DefaultMappingTest {
+
+ private Mapping mapping1;
+ private Mapping sameAsMapping1;
+ private Mapping mapping2;
+
+ @Before
+ public void setup() {
+
+ Mapping.Builder builder1 = new DefaultMapping.Builder();
+ DeviceId deviceId1 = DeviceId.deviceId("lisp:10.1.1.1");
+ MappingKey mappingKey1 = new DefaultMappingKey();
+ MappingValue mappingValue1 = new DefaultMappingValue();
+
+ mapping1 = builder1
+ .withId(1)
+ .withKey(mappingKey1)
+ .withValue(mappingValue1)
+ .forDevice(deviceId1)
+ .build();
+
+ Mapping.Builder builder2 = new DefaultMapping.Builder();
+
+ sameAsMapping1 = builder2
+ .withId(1)
+ .withKey(mappingKey1)
+ .withValue(mappingValue1)
+ .forDevice(deviceId1)
+ .build();
+
+ Mapping.Builder builder3 = new DefaultMapping.Builder();
+ DeviceId deviceId2 = DeviceId.deviceId("lisp:10.1.1.2");
+
+ mapping2 = builder3
+ .withId(2)
+ .withKey(new DefaultMappingKey())
+ .withValue(new DefaultMappingValue())
+ .forDevice(deviceId2)
+ .build();
+ }
+
+ /**
+ * Checks that the DefaultMapping class is immutable but can be inherited from.
+ */
+ @Test
+ public void testImmutability() {
+ assertThatClassIsImmutableBaseClass(DefaultMapping.class);
+ }
+
+ /**
+ * Tests the equals, hashCode and toString methods using Guava EqualsTester.
+ */
+ @Test
+ public void testEquality() {
+ new EqualsTester()
+ .addEqualityGroup(mapping1, sameAsMapping1)
+ .addEqualityGroup(mapping2).testEquals();
+ }
+
+ /**
+ * Tests creation of a DefaultMapping using a mapping builder.
+ */
+ @Test
+ public void testConstruction() {
+ DefaultMapping mapping = (DefaultMapping) mapping1;
+
+ assertThat(mapping.deviceId(), is(DeviceId.deviceId("lisp:10.1.1.1")));
+ assertThat(mapping.id(), is(MappingId.valueOf(1)));
+ }
+}
diff --git a/apps/mappingmanagement/api/src/test/java/org/onosproject/mapping/MappingEventTest.java b/apps/mappingmanagement/api/src/test/java/org/onosproject/mapping/MappingEventTest.java
new file mode 100644
index 0000000..35a33db
--- /dev/null
+++ b/apps/mappingmanagement/api/src/test/java/org/onosproject/mapping/MappingEventTest.java
@@ -0,0 +1,87 @@
+/*
+ * Copyright 2017-present Open Networking Laboratory
+ *
+ * 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.mapping;
+
+import com.google.common.testing.EqualsTester;
+import org.junit.Before;
+import org.junit.Test;
+import org.onosproject.event.AbstractEventTest;
+import org.onosproject.net.DeviceId;
+
+import java.util.concurrent.TimeUnit;
+
+/**
+ * Unit tests for the MappingEvent class.
+ */
+public class MappingEventTest extends AbstractEventTest {
+
+ private MappingEvent event1;
+ private MappingEvent sameAsEvent1;
+ private MappingEvent event2;
+
+ /**
+ * Creates a mock of mapping object from mappingId and deviceId.
+ *
+ * @param mid mapping identifier
+ * @param did device identifier
+ * @return mock of mapping object
+ */
+ private Mapping mockMapping(long mid, String did) {
+ Mapping.Builder builder = new DefaultMapping.Builder();
+ DeviceId deviceId = DeviceId.deviceId(did);
+
+ return builder
+ .withId(mid)
+ .withKey(new DefaultMappingKey())
+ .withValue(new DefaultMappingValue())
+ .forDevice(deviceId)
+ .build();
+ }
+
+ @Before
+ public void setup() {
+ final Mapping mapping1 = mockMapping(1, "lisp:10.1.1.1");
+ final Mapping mapping2 = mockMapping(2, "lisp:10.1.1.2");
+
+ event1 = new MappingEvent(MappingEvent.Type.MAPPING_ADDED, mapping1);
+ sameAsEvent1 = new MappingEvent(MappingEvent.Type.MAPPING_ADDED, mapping1);
+ event2 = new MappingEvent(MappingEvent.Type.MAPPING_ADD_REQUESTED, mapping2);
+ }
+
+ /**
+ * Tests the equals, hashCode and toString methods using Guava EqualsTester.
+ */
+ @Test
+ public void testEquals() {
+ new EqualsTester()
+ .addEqualityGroup(event1)
+ .addEqualityGroup(sameAsEvent1)
+ .addEqualityGroup(event2)
+ .testEquals();
+ }
+
+ /**
+ * Tests creation of a MappingEvent.
+ */
+ @Test
+ public void testConstructor() {
+ final long time = System.currentTimeMillis();
+ final Mapping mapping = mockMapping(1, "lisp:10.1.1.1");
+ final MappingEvent event = new MappingEvent(MappingEvent.Type.MAPPING_UPDATED, mapping);
+ validateEvent(event, MappingEvent.Type.MAPPING_UPDATED, mapping, time,
+ time + TimeUnit.SECONDS.toMillis(30));
+ }
+}
diff --git a/apps/mappingmanagement/api/src/test/java/org/onosproject/mapping/MappingIdTest.java b/apps/mappingmanagement/api/src/test/java/org/onosproject/mapping/MappingIdTest.java
new file mode 100644
index 0000000..0acdcaa
--- /dev/null
+++ b/apps/mappingmanagement/api/src/test/java/org/onosproject/mapping/MappingIdTest.java
@@ -0,0 +1,64 @@
+/*
+ * Copyright 2017-present Open Networking Laboratory
+ *
+ * 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.mapping;
+
+import com.google.common.testing.EqualsTester;
+import org.junit.Test;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.notNullValue;
+import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
+
+/**
+ * Unit tests for mapping id class.
+ */
+public class MappingIdTest {
+
+ final MappingId mappingId1 = MappingId.valueOf(1);
+ final MappingId sameAsMappingId1 = MappingId.valueOf(1);
+ final MappingId mappingId2 = MappingId.valueOf(2);
+
+ /**
+ * Checks that the MappingId class is immutable.
+ */
+ @Test
+ public void testImmutability() {
+ assertThatClassIsImmutable(MappingId.class);
+ }
+
+ /**
+ * Checks the operation of equals(), hashCode() and toString() methods.
+ */
+ @Test
+ public void testEquals() {
+ new EqualsTester()
+ .addEqualityGroup(mappingId1, sameAsMappingId1)
+ .addEqualityGroup(mappingId2)
+ .testEquals();
+ }
+
+ /**
+ * Checks the construction of a MappingId object.
+ */
+ @Test
+ public void testConstruction() {
+ final long mappingIdValue = 8888L;
+ final MappingId mappingId = MappingId.valueOf(mappingIdValue);
+ assertThat(mappingId, is(notNullValue()));
+ assertThat(mappingId.value(), is(mappingIdValue));
+ }
+}