Sho SHIMIZU
Committed by Sho Shimizu

Fix bug causing ClassCastException

Change-Id: Ic8787f05d05871b4473ff437b98e0b307ee1cacd
(cherry picked from commit e63e356c)
......@@ -74,7 +74,10 @@ final class EncodedDiscreteResources {
@SuppressWarnings("unchecked")
boolean contains(DiscreteResource resource) {
return rangeSet.contains(codec.encode(resource));
return resource.valueAs(Object.class)
.map(x -> codec.encode(x))
.map(rangeSet::contains)
.orElse(false);
}
boolean isEmpty() {
......
/*
* Copyright 2016-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.store.resource.impl;
import com.google.common.collect.ImmutableSet;
import org.junit.Test;
import org.onlab.packet.VlanId;
import org.onosproject.net.DeviceId;
import org.onosproject.net.PortNumber;
import org.onosproject.net.resource.DiscreteResource;
import org.onosproject.net.resource.Resources;
import java.util.Set;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.*;
public class EncodedDiscreteResourcesTest {
private static final DeviceId DID = DeviceId.deviceId("device1");
private static final PortNumber PN = PortNumber.portNumber(1);
private static final VlanId VID1 = VlanId.vlanId((short) 1);
private static final VlanId VID2 = VlanId.vlanId((short) 2);
private static final VlanId VID3 = VlanId.vlanId((short) 3);
@Test
public void testContains() {
DiscreteResource res1 = Resources.discrete(DID, PN, VID1).resource();
DiscreteResource res2 = Resources.discrete(DID, PN, VID2).resource();
DiscreteResource res3 = Resources.discrete(DID, PN, VID3).resource();
Set<DiscreteResource> resources = ImmutableSet.of(res1, res2);
EncodedDiscreteResources sut = EncodedDiscreteResources.of(resources, new VlanIdCodec());
assertThat(sut.contains(res1), is(true));
assertThat(sut.contains(res3), is(false));
}
}