blob: a14b94778505014b9706e9fb2e931dcc8c005e67 [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
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -080031public class ResourceTest {
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070032
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 SHIMIZUf95b96e2016-01-25 19:35:15 -080042 Resource resource1 = Resource.discrete(D1, P1, VLAN1).resource();
43 Resource sameAsResource1 = Resource.discrete(D1, P1, VLAN1).resource();
44 Resource resource2 = Resource.discrete(D2, P1, VLAN1).resource();
45 Resource resource3 = Resource.continuous(D1, P1, Bandwidth.class).resource(BW1.bps());
46 Resource sameAsResource3 = Resource.continuous(D1, P1, Bandwidth.class).resource(BW1.bps());
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() {
Sho SHIMIZUf95b96e2016-01-25 19:35:15 -080057 Resource port = Resource.discrete(D1, P1).resource();
Sho SHIMIZU69dc5842015-11-20 16:31:12 -080058
59 assertThat(port.components(), contains(D1, P1));
60 }
61
62 @Test
Sho SHIMIZU76b30f72016-01-11 14:08:35 -080063 public void testIdEquality() {
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -080064 ResourceId id1 = Resource.discrete(D1, P1, VLAN1).id();
65 ResourceId sameAsId1 = Resource.discrete(D1, P1, VLAN1).id();
66 ResourceId id2 = Resource.discrete(D2, P1, VLAN1).id();
Sho SHIMIZUf95b96e2016-01-25 19:35:15 -080067 ResourceId id3 = Resource.continuous(D1, P1, Bandwidth.class).resource(BW1.bps()).id();
Sho SHIMIZU7e6d18e2016-01-07 18:44:33 -080068 // intentionally set a different value
Sho SHIMIZUf95b96e2016-01-25 19:35:15 -080069 ResourceId sameAsId3 = Resource.continuous(D1, P1, Bandwidth.class).resource(BW2.bps()).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() {
Sho SHIMIZUf95b96e2016-01-25 19:35:15 -080079 Resource r1 = Resource.discrete(D1).resource().child(P1);
80 Resource sameAsR2 = Resource.discrete(D1, P1).resource();
Sho SHIMIZU38f561c2016-01-14 09:07:47 -080081
82 assertThat(r1, is(sameAsR2));
83 }
84
85 @Test
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070086 public void testThereIsParent() {
Sho SHIMIZUf95b96e2016-01-25 19:35:15 -080087 Resource resource = Resource.discrete(D1, P1, VLAN1).resource();
88 Resource parent = Resource.discrete(D1, P1).resource();
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070089
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -080090 assertThat(resource.parent(), is(Optional.of(parent)));
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070091 }
92
93 @Test
94 public void testNoParent() {
Sho SHIMIZUf95b96e2016-01-25 19:35:15 -080095 Resource resource = Resource.discrete(D1).resource();
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070096
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -080097 assertThat(resource.parent(), is(Optional.of(Resource.ROOT)));
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070098 }
99
100 @Test
101 public void testBase() {
Sho SHIMIZUf95b96e2016-01-25 19:35:15 -0800102 Resource resource = Resource.discrete(D1).resource();
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700103
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -0800104 DeviceId child = (DeviceId) resource.last();
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800105 assertThat(child, is(D1));
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700106 }
Sho SHIMIZU2d310222016-01-22 11:45:11 -0800107
108 @Test
109 public void testVolumeOfDiscrete() {
Sho SHIMIZUf95b96e2016-01-25 19:35:15 -0800110 Resource resource = Resource.discrete(D1).resource();
Sho SHIMIZU2d310222016-01-22 11:45:11 -0800111
112 DeviceId volume = resource.volume();
113 assertThat(volume, is(D1));
114 }
115
116 @Test
117 public void testVolumeOfContinuous() {
Sho SHIMIZUf95b96e2016-01-25 19:35:15 -0800118 Resource resource = Resource.continuous(D1, P1, Bandwidth.class).resource(BW1.bps());
Sho SHIMIZU2d310222016-01-22 11:45:11 -0800119
120 double volume = resource.volume();
121 assertThat(volume, is(BW1.bps()));
122 }
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700123}