Sho SHIMIZU
Committed by Gerrit Code Review

Fix bug causing ClassCastException

Change-Id: Ic8787f05d05871b4473ff437b98e0b307ee1cacd
...@@ -74,7 +74,10 @@ final class EncodedDiscreteResources { ...@@ -74,7 +74,10 @@ final class EncodedDiscreteResources {
74 74
75 @SuppressWarnings("unchecked") 75 @SuppressWarnings("unchecked")
76 boolean contains(DiscreteResource resource) { 76 boolean contains(DiscreteResource resource) {
77 - return rangeSet.contains(codec.encode(resource)); 77 + return resource.valueAs(Object.class)
78 + .map(x -> codec.encode(x))
79 + .map(rangeSet::contains)
80 + .orElse(false);
78 } 81 }
79 82
80 boolean isEmpty() { 83 boolean isEmpty() {
......
1 +/*
2 + * Copyright 2016-present Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +
17 +package org.onosproject.store.resource.impl;
18 +
19 +import com.google.common.collect.ImmutableSet;
20 +import org.junit.Test;
21 +import org.onlab.packet.VlanId;
22 +import org.onosproject.net.DeviceId;
23 +import org.onosproject.net.PortNumber;
24 +import org.onosproject.net.resource.DiscreteResource;
25 +import org.onosproject.net.resource.Resources;
26 +
27 +import java.util.Set;
28 +
29 +import static org.hamcrest.Matchers.is;
30 +import static org.junit.Assert.*;
31 +
32 +public class EncodedDiscreteResourcesTest {
33 + private static final DeviceId DID = DeviceId.deviceId("device1");
34 + private static final PortNumber PN = PortNumber.portNumber(1);
35 + private static final VlanId VID1 = VlanId.vlanId((short) 1);
36 + private static final VlanId VID2 = VlanId.vlanId((short) 2);
37 + private static final VlanId VID3 = VlanId.vlanId((short) 3);
38 +
39 + @Test
40 + public void testContains() {
41 + DiscreteResource res1 = Resources.discrete(DID, PN, VID1).resource();
42 + DiscreteResource res2 = Resources.discrete(DID, PN, VID2).resource();
43 + DiscreteResource res3 = Resources.discrete(DID, PN, VID3).resource();
44 +
45 + Set<DiscreteResource> resources = ImmutableSet.of(res1, res2);
46 +
47 + EncodedDiscreteResources sut = EncodedDiscreteResources.of(resources, new VlanIdCodec());
48 +
49 + assertThat(sut.contains(res1), is(true));
50 + assertThat(sut.contains(res3), is(false));
51 + }
52 +
53 +}