Sho SHIMIZU
Committed by Gerrit Code Review

Make sure to supply Kryo serializable set to constructor

Change-Id: Ida776fc3e7fbcacb604ae578865f2a280970101c
......@@ -16,7 +16,6 @@
package org.onosproject.store.resource.impl;
import com.google.common.base.MoreObjects;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Sets;
import org.onosproject.net.resource.DiscreteResource;
......@@ -41,7 +40,7 @@ final class GenericDiscreteResources implements DiscreteResources {
}
private GenericDiscreteResources(Set<DiscreteResource> values) {
this.values = ImmutableSet.copyOf(values);
this.values = values;
}
// for serializer
......@@ -62,7 +61,8 @@ final class GenericDiscreteResources implements DiscreteResources {
@Override
public DiscreteResources difference(DiscreteResources other) {
if (other instanceof GenericDiscreteResources) {
return of(Sets.difference(this.values(), other.values()));
// make sure that the set is serializable
return of(new LinkedHashSet<>(Sets.difference(this.values(), other.values())));
} else if (other instanceof EmptyDiscreteResources) {
return this;
}
......@@ -84,7 +84,8 @@ final class GenericDiscreteResources implements DiscreteResources {
@Override
public DiscreteResources add(DiscreteResources other) {
if (other instanceof GenericDiscreteResources) {
return new GenericDiscreteResources(Sets.union(this.values(), other.values()));
// make sure that the set is serializable
return of(new LinkedHashSet<>(Sets.union(this.values(), other.values())));
} else if (other instanceof EmptyDiscreteResources) {
return this;
}
......
......@@ -21,6 +21,7 @@ import org.junit.Test;
import org.onosproject.net.DeviceId;
import org.onosproject.net.resource.DiscreteResource;
import org.onosproject.net.resource.Resources;
import org.onosproject.store.service.Serializer;
import java.util.Optional;
......@@ -28,6 +29,8 @@ import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
public class GenericDiscreteResourcesTest {
private final Serializer serializer = ConsistentResourceStore.SERIALIZER;
@Test
public void testIfResourceIsFound() {
DiscreteResource res1 = Resources.discrete(DeviceId.deviceId("a")).resource();
......@@ -164,4 +167,28 @@ public class GenericDiscreteResourcesTest {
assertThat(sut.values(), is(ImmutableSet.of(res1, res2)));
}
@Test
public void testDifferenceSerializable() {
DiscreteResource res1 = Resources.discrete(DeviceId.deviceId("a")).resource();
DiscreteResource res2 = Resources.discrete(DeviceId.deviceId("b")).resource();
DiscreteResources set1 = GenericDiscreteResources.of(ImmutableSet.of(res1, res2));
DiscreteResources set2 = GenericDiscreteResources.of(ImmutableSet.of(res1));
DiscreteResources difference = set1.difference(set2);
assertThat(serializer.decode(serializer.encode(difference)), is(difference));
}
@Test
public void testAddSerializable() {
DiscreteResource res1 = Resources.discrete(DeviceId.deviceId("a")).resource();
DiscreteResource res2 = Resources.discrete(DeviceId.deviceId("b")).resource();
DiscreteResources set1 = GenericDiscreteResources.of(ImmutableSet.of(res1));
DiscreteResources set2 = GenericDiscreteResources.of(ImmutableSet.of(res2));
DiscreteResources add = set1.add(set2);
assertThat(serializer.decode(serializer.encode(add)), is(add));
}
}
......