blob: f3b61fc9b4e9d8829a2f5c999eccf098f56dc91c [file] [log] [blame]
Sho SHIMIZU70ee1ee2015-08-06 11:11:52 -07001/*
2 * Copyright 2015 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 */
16package org.onosproject.net.newresource.impl;
17
18import org.junit.Before;
19import org.junit.Test;
20import org.onlab.packet.VlanId;
21import org.onosproject.net.ConnectPoint;
22import org.onosproject.net.DeviceId;
23import org.onosproject.net.LinkKey;
24import org.onosproject.net.PortNumber;
25import org.onosproject.net.newresource.DefaultResource;
26
27import java.util.function.Predicate;
28
29import static org.hamcrest.Matchers.is;
30import static org.junit.Assert.*;
31
32/**
33 * Unit tests for ResourceManager.
34 */
35public class ResourceManagerTest {
36
37 private static final DeviceId D1 = DeviceId.deviceId("of:001");
38 private static final DeviceId D2 = DeviceId.deviceId("of:002");
39 private static final PortNumber P1 = PortNumber.portNumber(1);
40 private static final ConnectPoint CP1_1 = new ConnectPoint(D1, P1);
41 private static final ConnectPoint CP2_1 = new ConnectPoint(D2, P1);
42 private static final short VLAN_LOWER_LIMIT = 0;
43 private static final short VLAN_UPPER_LIMIT = 1024;
44
45 private final Predicate<VlanId> vlanPredicate =
46 x -> x.toShort() >= VLAN_LOWER_LIMIT && x.toShort() < VLAN_UPPER_LIMIT;
47 private ResourceManager manager;
48
49 @Before
50 public void setUp() {
51 manager = new ResourceManager();
52 }
53
54 /**
55 * Tests resource boundaries.
56 */
57 @Test
58 public void testBoundary() {
59 manager.defineResourceBoundary(VlanId.class, vlanPredicate);
60
61 LinkKey linkKey = LinkKey.linkKey(CP1_1, CP2_1);
62
63 assertThat(manager.isValid(new DefaultResource<>(linkKey, VlanId.vlanId((short) (VLAN_LOWER_LIMIT - 1)))),
64 is(false));
65
66 assertThat(manager.isValid(new DefaultResource<>(linkKey, VlanId.vlanId(VLAN_LOWER_LIMIT))),
67 is(true));
68
69 assertThat(manager.isValid(new DefaultResource<>(linkKey, VlanId.vlanId((short) 100))),
70 is(true));
71
72 assertThat(manager.isValid(new DefaultResource<>(linkKey, VlanId.vlanId((short) (VLAN_UPPER_LIMIT - 1)))),
73 is(true));
74
75 assertThat(manager.isValid(new DefaultResource<>(linkKey, VlanId.vlanId(VLAN_UPPER_LIMIT))),
76 is(false));
77 }
78
79 /**
80 * Tests the case that a boundary is not set.
81 */
82 @Test
83 public void testWhenBoundaryNotSet() {
84 LinkKey linkKey = LinkKey.linkKey(CP1_1, CP2_1);
85
86 assertThat(manager.isValid(new DefaultResource<>(linkKey, VlanId.vlanId((short) (VLAN_LOWER_LIMIT - 1)))),
87 is(true));
88
89 assertThat(manager.isValid(new DefaultResource<>(linkKey, VlanId.vlanId(VLAN_LOWER_LIMIT))),
90 is(true));
91
92 assertThat(manager.isValid(new DefaultResource<>(linkKey, VlanId.vlanId((short) 100))),
93 is(true));
94
95 assertThat(manager.isValid(new DefaultResource<>(linkKey, VlanId.vlanId((short) (VLAN_UPPER_LIMIT - 1)))),
96 is(true));
97
98 assertThat(manager.isValid(new DefaultResource<>(linkKey, VlanId.vlanId(VLAN_UPPER_LIMIT))),
99 is(true));
100 }
101}