Sho SHIMIZU
Committed by Gerrit Code Review

Fix bug in releasing a continuous resource

Change-Id: Idcc1faaf0ce9cd7856233dc2107dcc8329e6be94
...@@ -86,7 +86,7 @@ final class ContinuousResourceAllocation { ...@@ -86,7 +86,7 @@ final class ContinuousResourceAllocation {
86 ((ContinuousResource) x.resource()).value() == resource.value())) 86 ((ContinuousResource) x.resource()).value() == resource.value()))
87 .collect(Collectors.toList()); 87 .collect(Collectors.toList());
88 88
89 - if (matched.size() > 1) { 89 + if (matched.size() > 0) {
90 matched.remove(0); 90 matched.remove(0);
91 } 91 }
92 92
......
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.ImmutableList;
20 +import org.junit.Test;
21 +import org.onlab.util.Bandwidth;
22 +import org.onosproject.net.DeviceId;
23 +import org.onosproject.net.PortNumber;
24 +import org.onosproject.net.intent.IntentId;
25 +import org.onosproject.net.resource.ContinuousResource;
26 +import org.onosproject.net.resource.ResourceAllocation;
27 +import org.onosproject.net.resource.ResourceConsumer;
28 +import org.onosproject.net.resource.Resources;
29 +
30 +import static org.hamcrest.Matchers.is;
31 +import static org.junit.Assert.assertThat;
32 +
33 +/**
34 + * Unit tests for ContinuousResourceAllocation.
35 + */
36 +public class ContinuousResourceAllocationTest {
37 +
38 + private static final DeviceId DID = DeviceId.deviceId("a");
39 + private static final PortNumber PN1 = PortNumber.portNumber(1);
40 +
41 + @Test
42 + public void testReleaseWhenAllocatedResourceIsRequested() {
43 + ContinuousResource original =
44 + Resources.continuous(DID, PN1, Bandwidth.class).resource(Bandwidth.gbps(1).bps());
45 + ContinuousResource allocated =
46 + Resources.continuous(DID, PN1, Bandwidth.class).resource(Bandwidth.mbps(500).bps());
47 + ResourceConsumer consumer = IntentId.valueOf(1);
48 +
49 + ContinuousResourceAllocation sut = new ContinuousResourceAllocation(original,
50 + ImmutableList.of(new ResourceAllocation(allocated, consumer)));
51 +
52 + ContinuousResource request =
53 + Resources.continuous(DID, PN1, Bandwidth.class).resource(Bandwidth.mbps(500).bps());
54 +
55 + ContinuousResourceAllocation released = sut.release(request, consumer.consumerId());
56 +
57 + assertThat(released.allocations().isEmpty(), is(true));
58 + }
59 +
60 + @Test
61 + public void testReleaseWhenDifferentConsumerIsSpecified() {
62 + ContinuousResource original =
63 + Resources.continuous(DID, PN1, Bandwidth.class).resource(Bandwidth.gbps(1).bps());
64 + ContinuousResource allocated =
65 + Resources.continuous(DID, PN1, Bandwidth.class).resource(Bandwidth.mbps(500).bps());
66 + ResourceConsumer consumer = IntentId.valueOf(1);
67 + ResourceConsumer otherConsumer = IntentId.valueOf(2);
68 +
69 + ContinuousResourceAllocation sut = new ContinuousResourceAllocation(original,
70 + ImmutableList.of(new ResourceAllocation(allocated, consumer)));
71 +
72 + ContinuousResource request =
73 + Resources.continuous(DID, PN1, Bandwidth.class).resource(Bandwidth.mbps(500).bps());
74 +
75 + ImmutableList<ResourceAllocation> allocations = sut.release(request, otherConsumer.consumerId()).allocations();
76 +
77 + assertThat(allocations.size(), is(1));
78 + assertThat(allocations.get(0).resource().equals(allocated), is(true));
79 + }
80 +}