blob: ea7ff5e77b9c9c0a243be552bb61af944bbfc3b8 [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.DeviceId;
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070023import org.onosproject.net.PortNumber;
24
25import java.util.Optional;
26
Sho SHIMIZU69dc5842015-11-20 16:31:12 -080027import static org.hamcrest.Matchers.contains;
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070028import static org.hamcrest.Matchers.is;
29import static org.junit.Assert.assertThat;
30
31public class ResourcePathTest {
32
33 private static final DeviceId D1 = DeviceId.deviceId("of:001");
34 private static final DeviceId D2 = DeviceId.deviceId("of:002");
35 private static final PortNumber P1 = PortNumber.portNumber(1);
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070036 private static final VlanId VLAN1 = VlanId.vlanId((short) 100);
Sho SHIMIZU60ac58e2015-11-11 12:16:38 -080037 private static final Bandwidth BW1 = Bandwidth.gbps(2);
38 private static final Bandwidth BW2 = Bandwidth.gbps(1);
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070039
40 @Test
41 public void testEquals() {
Sho SHIMIZU44f37612015-11-25 16:23:22 -080042 ResourcePath resource1 = ResourcePath.discrete(D1, P1, VLAN1);
43 ResourcePath sameAsResource1 = ResourcePath.discrete(D1, P1, VLAN1);
44 ResourcePath resource2 = ResourcePath.discrete(D2, P1, VLAN1);
45 ResourcePath resource3 = ResourcePath.continuous(BW1.bps(), D1, P1, BW1);
Sho SHIMIZU7e6d18e2016-01-07 18:44:33 -080046 ResourcePath sameAsResource3 = ResourcePath.continuous(BW1.bps(), D1, P1, BW1);
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070047
48 new EqualsTester()
49 .addEqualityGroup(resource1, sameAsResource1)
50 .addEqualityGroup(resource2)
Sho SHIMIZU7e6d18e2016-01-07 18:44:33 -080051 .addEqualityGroup(resource3, sameAsResource3)
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070052 .testEquals();
53 }
54
Sho SHIMIZUba41fc12015-08-12 15:43:22 -070055 @Test
Sho SHIMIZU69dc5842015-11-20 16:31:12 -080056 public void testComponents() {
57 ResourcePath port = ResourcePath.discrete(D1, P1);
58
59 assertThat(port.components(), contains(D1, P1));
60 }
61
62 @Test
Sho SHIMIZU76b30f72016-01-11 14:08:35 -080063 public void testIdEquality() {
64 ResourceId id1 = ResourcePath.discrete(D1, P1, VLAN1).id();
65 ResourceId sameAsId1 = ResourcePath.discrete(D1, P1, VLAN1).id();
66 ResourceId id2 = ResourcePath.discrete(D2, P1, VLAN1).id();
67 ResourceId id3 = ResourcePath.continuous(BW1.bps(), D1, P1, BW1).id();
Sho SHIMIZU7e6d18e2016-01-07 18:44:33 -080068 // intentionally set a different value
Sho SHIMIZU76b30f72016-01-11 14:08:35 -080069 ResourceId sameAsId3 = ResourcePath.continuous(BW2.bps(), D1, P1, BW1).id();
Sho SHIMIZU7e6d18e2016-01-07 18:44:33 -080070
71 new EqualsTester()
Sho SHIMIZU76b30f72016-01-11 14:08:35 -080072 .addEqualityGroup(id1, sameAsId1)
73 .addEqualityGroup(id2)
74 .addEqualityGroup(id3, sameAsId3);
Sho SHIMIZU7e6d18e2016-01-07 18:44:33 -080075 }
76
77 @Test
Sho SHIMIZU38f561c2016-01-14 09:07:47 -080078 public void testChild() {
79 ResourcePath r1 = ResourcePath.discrete(D1).child(P1);
80 ResourcePath sameAsR2 = ResourcePath.discrete(D1, P1);
81
82 assertThat(r1, is(sameAsR2));
83 }
84
85 @Test
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070086 public void testThereIsParent() {
Sho SHIMIZU44f37612015-11-25 16:23:22 -080087 ResourcePath path = ResourcePath.discrete(D1, P1, VLAN1);
88 ResourcePath parent = ResourcePath.discrete(D1, P1);
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070089
90 assertThat(path.parent(), is(Optional.of(parent)));
91 }
92
93 @Test
94 public void testNoParent() {
Sho SHIMIZU44f37612015-11-25 16:23:22 -080095 ResourcePath path = ResourcePath.discrete(D1);
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070096
Sho SHIMIZUba41fc12015-08-12 15:43:22 -070097 assertThat(path.parent(), is(Optional.of(ResourcePath.ROOT)));
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070098 }
99
100 @Test
101 public void testBase() {
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800102 ResourcePath path = ResourcePath.discrete(D1);
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700103
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800104 DeviceId child = (DeviceId) path.last();
105 assertThat(child, is(D1));
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700106 }
107}