Sho SHIMIZU
Committed by Gerrit Code Review

Fix bug of Resource#isSubTypeOf() and improve unit test coverage

Change-Id: I5488c1065a8f83abb110ad67276c30eb719ccd01
......@@ -77,8 +77,7 @@ public final class ContinuousResource implements Resource {
String typeName = (String) id.components().get(id.components().size() - 1);
boolean foundInLeaf = typeName.equals(ancestor.getCanonicalName());
boolean foundInAncestor = id.components().subList(0, id.components().size()).stream()
.map(Object::getClass)
.filter(x -> x.equals(ancestor))
.filter(x -> ancestor.isAssignableFrom(x.getClass()))
.findAny()
.isPresent();
return foundInAncestor || foundInLeaf;
......
......@@ -66,8 +66,7 @@ public final class DiscreteResource implements Resource {
checkNotNull(ancestor);
return id.components().stream()
.map(Object::getClass)
.filter(x -> x.equals(ancestor))
.filter(x -> ancestor.isAssignableFrom(x.getClass()))
.findAny()
.isPresent();
}
......
......@@ -52,6 +52,7 @@ public class ContinuousResourceTest {
public void testTypeOf() {
ContinuousResource continuous = Resources.continuous(D1, P1, Bandwidth.class)
.resource(BW1.bps());
assertThat(continuous.isTypeOf(DeviceId.class), is(false));
assertThat(continuous.isTypeOf(PortNumber.class), is(false));
assertThat(continuous.isTypeOf(Bandwidth.class), is(true));
......@@ -61,6 +62,7 @@ public class ContinuousResourceTest {
public void testSubTypeOf() {
ContinuousResource continuous = Resources.continuous(D1, P1, Bandwidth.class)
.resource(BW1.bps());
assertThat(continuous.isSubTypeOf(DeviceId.class), is(true));
assertThat(continuous.isSubTypeOf(PortNumber.class), is(true));
assertThat(continuous.isSubTypeOf(Bandwidth.class), is(true));
......@@ -68,11 +70,46 @@ public class ContinuousResourceTest {
}
@Test
public void testValueAs() {
public void testSubTypeOfObject() {
ContinuousResource continuous = Resources.continuous(D1, P1, Bandwidth.class)
.resource(BW1.bps());
assertThat(continuous.isSubTypeOf(Object.class), is(true));
}
@Test
public void testValueAsPrimitiveDouble() {
ContinuousResource resource = Resources.continuous(D1, P1, Bandwidth.class)
.resource(BW1.bps());
Optional<Double> volume = resource.valueAs(double.class);
assertThat(volume.get(), is(BW1.bps()));
}
@Test
public void testValueAsDouble() {
ContinuousResource resource = Resources.continuous(D1, P1, Bandwidth.class)
.resource(BW1.bps());
Optional<Double> value = resource.valueAs(Double.class);
assertThat(value.get(), is(BW1.bps()));
}
@Test
public void testValueAsObject() {
ContinuousResource resource = Resources.continuous(D1, P1, Bandwidth.class)
.resource(BW1.bps());
Optional<Double> value = resource.valueAs(Double.class);
assertThat(value.get(), is(BW1.bps()));
}
@Test
public void testValueAsIncompatibleType() {
ContinuousResource resource = Resources.continuous(D1, P1, Bandwidth.class)
.resource(BW1.bps());
Optional<VlanId> value = resource.valueAs(VlanId.class);
assertThat(value, is(Optional.empty()));
}
}
......
......@@ -76,6 +76,7 @@ public class DiscreteResourceTest {
@Test
public void testTypeOf() {
DiscreteResource discrete = Resources.discrete(D1, P1, VLAN1).resource();
assertThat(discrete.isTypeOf(DeviceId.class), is(false));
assertThat(discrete.isTypeOf(PortNumber.class), is(false));
assertThat(discrete.isTypeOf(VlanId.class), is(true));
......@@ -84,6 +85,7 @@ public class DiscreteResourceTest {
@Test
public void testSubTypeOf() {
DiscreteResource discrete = Resources.discrete(D1, P1, VLAN1).resource();
assertThat(discrete.isSubTypeOf(DeviceId.class), is(true));
assertThat(discrete.isSubTypeOf(PortNumber.class), is(true));
assertThat(discrete.isSubTypeOf(VlanId.class), is(true));
......@@ -91,6 +93,13 @@ public class DiscreteResourceTest {
}
@Test
public void testSubTypeOfObject() {
DiscreteResource discrete = Resources.discrete(D1, P1, VLAN1).resource();
assertThat(discrete.isSubTypeOf(Object.class), is(true));
}
@Test
public void testValueAs() {
DiscreteResource resource = Resources.discrete(D1).resource();
......