Sho SHIMIZU
Committed by Jonathan Hart

Fix unit tests and bugs of EncodableDiscreteResources

Change-Id: Iea3b39dbdcfabaf23fa7e7ef6cb8a98bfe432081
......@@ -66,6 +66,17 @@ final class EncodableDiscreteResources implements DiscreteResources {
return new EncodableDiscreteResources(parent, values);
}
private static DiscreteResources of(DiscreteResource parent, Map<Class<?>, EncodedDiscreteResources> map) {
if (isEmpty(map)) {
return DiscreteResources.empty();
}
return new EncodableDiscreteResources(parent, map);
}
private static boolean isEmpty(Map<Class<?>, EncodedDiscreteResources> map) {
return map.values().stream().allMatch(EncodedDiscreteResources::isEmpty);
}
EncodableDiscreteResources(DiscreteResource parent, Map<Class<?>, EncodedDiscreteResources> map) {
this.parent = parent;
this.map = map;
......@@ -73,6 +84,9 @@ final class EncodableDiscreteResources implements DiscreteResources {
@Override
public Optional<DiscreteResource> lookup(DiscreteResourceId id) {
if (!id.parent().filter(parent.id()::equals).isPresent()) {
return Optional.empty();
}
DiscreteResource resource = Resources.discrete(id).resource();
Class<?> cls = getClass(resource);
return Optional.ofNullable(map.get(cls))
......@@ -92,7 +106,7 @@ final class EncodableDiscreteResources implements DiscreteResources {
Map.Entry::getValue,
EncodedDiscreteResources::difference,
LinkedHashMap::new));
return new EncodableDiscreteResources(parent, newMap);
return of(parent, newMap);
} else if (other instanceof EmptyDiscreteResources) {
return this;
}
......@@ -102,8 +116,7 @@ final class EncodableDiscreteResources implements DiscreteResources {
@Override
public boolean isEmpty() {
return map.values().stream()
.allMatch(x -> x.isEmpty());
return isEmpty(map);
}
@Override
......@@ -125,7 +138,7 @@ final class EncodableDiscreteResources implements DiscreteResources {
EncodedDiscreteResources::add,
LinkedHashMap::new
));
return new EncodableDiscreteResources(parent, newMap);
return of(parent, newMap);
} else if (other instanceof EmptyDiscreteResources) {
return this;
}
......
......@@ -62,9 +62,9 @@ public class EncodableDiscreteResourcesTest {
@Test
public void testIfResourceIsNotFound() {
DiscreteResource res1 = Resources.discrete(DeviceId.deviceId("a"), PortNumber.portNumber(1)).resource();
DiscreteResource res2 = Resources.discrete(DeviceId.deviceId("b"), PortNumber.portNumber(1)).resource();
DiscreteResource res2 = Resources.discrete(DeviceId.deviceId("a"), PortNumber.portNumber(2)).resource();
DiscreteResources sut = GenericDiscreteResources.of(ImmutableSet.of(res1));
DiscreteResources sut = EncodableDiscreteResources.of(ImmutableSet.of(res1));
assertThat(sut.lookup(res2.id()), is(Optional.empty()));
}
......@@ -72,39 +72,39 @@ public class EncodableDiscreteResourcesTest {
@Test
public void testIfDifferenceIsEmpty() {
DiscreteResource res1 = Resources.discrete(DeviceId.deviceId("a"), PortNumber.portNumber(1)).resource();
DiscreteResource res2 = Resources.discrete(DeviceId.deviceId("b"), PortNumber.portNumber(1)).resource();
DiscreteResource res2 = Resources.discrete(DeviceId.deviceId("a"), PortNumber.portNumber(2)).resource();
DiscreteResources sut = GenericDiscreteResources.of(ImmutableSet.of(res1, res2));
DiscreteResources sut = EncodableDiscreteResources.of(ImmutableSet.of(res1, res2));
DiscreteResources other = GenericDiscreteResources.of(ImmutableSet.of(res1, res2));
DiscreteResources other = EncodableDiscreteResources.of(ImmutableSet.of(res1, res2));
DiscreteResources expected = GenericDiscreteResources.of(ImmutableSet.of());
DiscreteResources expected = DiscreteResources.empty();
assertThat(sut.difference(other), is(expected));
}
@Test
public void testIfDifferenceIsNotEmpty() {
DiscreteResource res1 = Resources.discrete(DeviceId.deviceId("a"), PortNumber.portNumber(1)).resource();
DiscreteResource res2 = Resources.discrete(DeviceId.deviceId("b"), PortNumber.portNumber(1)).resource();
DiscreteResource res2 = Resources.discrete(DeviceId.deviceId("a"), PortNumber.portNumber(2)).resource();
DiscreteResources sut = GenericDiscreteResources.of(ImmutableSet.of(res1, res2));
DiscreteResources sut = EncodableDiscreteResources.of(ImmutableSet.of(res1, res2));
DiscreteResources other = GenericDiscreteResources.of(ImmutableSet.of(res1));
DiscreteResources other = EncodableDiscreteResources.of(ImmutableSet.of(res1));
DiscreteResources expected = GenericDiscreteResources.of(ImmutableSet.of(res2));
DiscreteResources expected = EncodableDiscreteResources.of(ImmutableSet.of(res2));
assertThat(sut.difference(other), is(expected));
}
@Test
public void testIfDifferenceIsNotChanged() {
DiscreteResource res1 = Resources.discrete(DeviceId.deviceId("a"), PortNumber.portNumber(1)).resource();
DiscreteResource res2 = Resources.discrete(DeviceId.deviceId("b"), PortNumber.portNumber(1)).resource();
DiscreteResource res2 = Resources.discrete(DeviceId.deviceId("a"), PortNumber.portNumber(2)).resource();
DiscreteResources sut = GenericDiscreteResources.of(ImmutableSet.of(res1));
DiscreteResources sut = EncodableDiscreteResources.of(ImmutableSet.of(res1));
DiscreteResources other = GenericDiscreteResources.of(ImmutableSet.of(res2));
DiscreteResources other = EncodableDiscreteResources.of(ImmutableSet.of(res2));
DiscreteResources expected = GenericDiscreteResources.of(ImmutableSet.of(res1));
DiscreteResources expected = EncodableDiscreteResources.of(ImmutableSet.of(res1));
assertThat(sut.difference(other), is(expected));
}
......@@ -112,17 +112,17 @@ public class EncodableDiscreteResourcesTest {
public void testDifferenceFromEmpty() {
DiscreteResource res1 = Resources.discrete(DeviceId.deviceId("a"), PortNumber.portNumber(1)).resource();
DiscreteResources sut = GenericDiscreteResources.of(ImmutableSet.of(res1));
DiscreteResources sut = EncodableDiscreteResources.of(ImmutableSet.of(res1));
DiscreteResources other = GenericDiscreteResources.of(ImmutableSet.of());
DiscreteResources other = DiscreteResources.empty();
DiscreteResources expected = GenericDiscreteResources.of(ImmutableSet.of(res1));
DiscreteResources expected = EncodableDiscreteResources.of(ImmutableSet.of(res1));
assertThat(sut.difference(other), is(expected));
}
@Test
public void testEmpty() {
DiscreteResources sut = GenericDiscreteResources.of(ImmutableSet.of());
DiscreteResources sut = EncodableDiscreteResources.of(ImmutableSet.of());
assertThat(sut.isEmpty(), is(true));
}
......@@ -131,7 +131,7 @@ public class EncodableDiscreteResourcesTest {
public void testNotEmpty() {
DiscreteResource res1 = Resources.discrete(DeviceId.deviceId("a"), PortNumber.portNumber(1)).resource();
DiscreteResources sut = GenericDiscreteResources.of(ImmutableSet.of(res1));
DiscreteResources sut = EncodableDiscreteResources.of(ImmutableSet.of(res1));
assertThat(sut.isEmpty(), is(false));
}
......@@ -140,7 +140,7 @@ public class EncodableDiscreteResourcesTest {
public void testIfResourceIsContained() {
DiscreteResource res1 = Resources.discrete(DeviceId.deviceId("a"), PortNumber.portNumber(1)).resource();
DiscreteResources sut = GenericDiscreteResources.of(ImmutableSet.of(res1));
DiscreteResources sut = EncodableDiscreteResources.of(ImmutableSet.of(res1));
assertThat(sut.containsAny(ImmutableSet.of(res1)), is(true));
}
......@@ -148,9 +148,9 @@ public class EncodableDiscreteResourcesTest {
@Test
public void testIfResourceIsNotContained() {
DiscreteResource res1 = Resources.discrete(DeviceId.deviceId("a"), PortNumber.portNumber(1)).resource();
DiscreteResource res2 = Resources.discrete(DeviceId.deviceId("b"), PortNumber.portNumber(1)).resource();
DiscreteResource res2 = Resources.discrete(DeviceId.deviceId("a"), PortNumber.portNumber(2)).resource();
DiscreteResources sut = GenericDiscreteResources.of(ImmutableSet.of(res1));
DiscreteResources sut = EncodableDiscreteResources.of(ImmutableSet.of(res1));
assertThat(sut.containsAny(ImmutableSet.of(res2)), is(false));
}
......@@ -159,7 +159,7 @@ public class EncodableDiscreteResourcesTest {
public void testContainsWithEmpty() {
DiscreteResource res1 = Resources.discrete(DeviceId.deviceId("a"), PortNumber.portNumber(1)).resource();
DiscreteResources sut = GenericDiscreteResources.of(ImmutableSet.of(res1));
DiscreteResources sut = EncodableDiscreteResources.of(ImmutableSet.of(res1));
assertThat(sut.containsAny(ImmutableSet.of()), is(false));
}
......@@ -167,22 +167,22 @@ public class EncodableDiscreteResourcesTest {
@Test
public void testAdd() {
DiscreteResource res1 = Resources.discrete(DeviceId.deviceId("a"), PortNumber.portNumber(1)).resource();
DiscreteResource res2 = Resources.discrete(DeviceId.deviceId("b"), PortNumber.portNumber(1)).resource();
DiscreteResource res2 = Resources.discrete(DeviceId.deviceId("a"), PortNumber.portNumber(2)).resource();
DiscreteResources sut = GenericDiscreteResources.of(ImmutableSet.of(res1));
DiscreteResources sut = EncodableDiscreteResources.of(ImmutableSet.of(res1));
DiscreteResources other = GenericDiscreteResources.of(ImmutableSet.of(res2));
DiscreteResources other = EncodableDiscreteResources.of(ImmutableSet.of(res2));
DiscreteResources expected = GenericDiscreteResources.of(ImmutableSet.of(res1, res2));
DiscreteResources expected = EncodableDiscreteResources.of(ImmutableSet.of(res1, res2));
assertThat(sut.add(other), is(expected));
}
@Test
public void testValues() {
DiscreteResource res1 = Resources.discrete(DeviceId.deviceId("a"), PortNumber.portNumber(1)).resource();
DiscreteResource res2 = Resources.discrete(DeviceId.deviceId("b"), PortNumber.portNumber(1)).resource();
DiscreteResource res2 = Resources.discrete(DeviceId.deviceId("a"), PortNumber.portNumber(2)).resource();
DiscreteResources sut = GenericDiscreteResources.of(ImmutableSet.of(res1, res2));
DiscreteResources sut = EncodableDiscreteResources.of(ImmutableSet.of(res1, res2));
assertThat(sut.values(), is(ImmutableSet.of(res1, res2)));
}
......