blob: 8e445364e86120aea2b6ceb5545324cb4d5dbd0b [file] [log] [blame]
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -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;
17
18import com.google.common.testing.EqualsTester;
19import org.junit.Test;
20import org.onlab.packet.VlanId;
Sho SHIMIZU60ac58e2015-11-11 12:16:38 -080021import org.onlab.util.Bandwidth;
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070022import org.onosproject.net.ConnectPoint;
23import org.onosproject.net.DeviceId;
24import org.onosproject.net.LinkKey;
25import org.onosproject.net.PortNumber;
26
27import java.util.Optional;
28
29import static org.hamcrest.Matchers.is;
30import static org.junit.Assert.assertThat;
31
32public class ResourcePathTest {
33
34 private static final DeviceId D1 = DeviceId.deviceId("of:001");
35 private static final DeviceId D2 = DeviceId.deviceId("of:002");
36 private static final PortNumber P1 = PortNumber.portNumber(1);
37 private static final ConnectPoint CP1_1 = new ConnectPoint(D1, P1);
38 private static final ConnectPoint CP2_1 = new ConnectPoint(D2, P1);
39 private static final VlanId VLAN1 = VlanId.vlanId((short) 100);
Sho SHIMIZU60ac58e2015-11-11 12:16:38 -080040 private static final Bandwidth BW1 = Bandwidth.gbps(2);
41 private static final Bandwidth BW2 = Bandwidth.gbps(1);
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070042
43 @Test
44 public void testEquals() {
Sho SHIMIZU60ac58e2015-11-11 12:16:38 -080045 ResourcePath resource1 = ResourcePath.discrete(LinkKey.linkKey(CP1_1, CP2_1), VLAN1);
46 ResourcePath sameAsResource1 = ResourcePath.discrete(LinkKey.linkKey(CP1_1, CP2_1), VLAN1);
47 ResourcePath resource2 = ResourcePath.discrete(LinkKey.linkKey(CP2_1, CP1_1), VLAN1);
48 ResourcePath resource3 = ResourcePath.continuous(BW1.bps(), LinkKey.linkKey(CP1_1, CP2_1), BW1);
49 ResourcePath sameAsResource3 = ResourcePath.continuous(BW2.bps(), LinkKey.linkKey(CP1_1, CP2_1), BW1);
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070050
51 new EqualsTester()
52 .addEqualityGroup(resource1, sameAsResource1)
53 .addEqualityGroup(resource2)
Sho SHIMIZU60ac58e2015-11-11 12:16:38 -080054 .addEqualityGroup(resource3, sameAsResource3) // this is intentional
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070055 .testEquals();
56 }
57
Sho SHIMIZUba41fc12015-08-12 15:43:22 -070058 @Test
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070059 public void testCreateWithZeroComponent() {
Sho SHIMIZU60ac58e2015-11-11 12:16:38 -080060 ResourcePath path = ResourcePath.discrete();
Sho SHIMIZUba41fc12015-08-12 15:43:22 -070061
62 assertThat(path, is(ResourcePath.ROOT));
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070063 }
64
65 @Test
66 public void testThereIsParent() {
Sho SHIMIZU60ac58e2015-11-11 12:16:38 -080067 ResourcePath path = ResourcePath.discrete(LinkKey.linkKey(CP1_1, CP2_1), VLAN1);
68 ResourcePath parent = ResourcePath.discrete(LinkKey.linkKey(CP1_1, CP2_1));
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070069
70 assertThat(path.parent(), is(Optional.of(parent)));
71 }
72
73 @Test
74 public void testNoParent() {
Sho SHIMIZU60ac58e2015-11-11 12:16:38 -080075 ResourcePath path = ResourcePath.discrete(LinkKey.linkKey(CP1_1, CP2_1));
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070076
Sho SHIMIZUba41fc12015-08-12 15:43:22 -070077 assertThat(path.parent(), is(Optional.of(ResourcePath.ROOT)));
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070078 }
79
80 @Test
81 public void testBase() {
82 LinkKey linkKey = LinkKey.linkKey(CP1_1, CP2_1);
Sho SHIMIZU60ac58e2015-11-11 12:16:38 -080083 ResourcePath path = ResourcePath.discrete(linkKey);
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070084
Sho SHIMIZUc9546a32015-11-10 11:22:28 -080085 LinkKey child = (LinkKey) path.last();
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070086 assertThat(child, is(linkKey));
87 }
88}