Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.net.newresource; |
| 17 | |
| 18 | import com.google.common.testing.EqualsTester; |
| 19 | import org.junit.Test; |
| 20 | import org.onlab.packet.VlanId; |
Sho SHIMIZU | 60ac58e | 2015-11-11 12:16:38 -0800 | [diff] [blame] | 21 | import org.onlab.util.Bandwidth; |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 22 | import org.onosproject.net.DeviceId; |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 23 | import org.onosproject.net.PortNumber; |
| 24 | |
| 25 | import java.util.Optional; |
| 26 | |
Sho SHIMIZU | 69dc584 | 2015-11-20 16:31:12 -0800 | [diff] [blame] | 27 | import static org.hamcrest.Matchers.contains; |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 28 | import static org.hamcrest.Matchers.is; |
| 29 | import static org.junit.Assert.assertThat; |
| 30 | |
| 31 | public 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 SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 36 | private static final VlanId VLAN1 = VlanId.vlanId((short) 100); |
Sho SHIMIZU | 60ac58e | 2015-11-11 12:16:38 -0800 | [diff] [blame] | 37 | private static final Bandwidth BW1 = Bandwidth.gbps(2); |
| 38 | private static final Bandwidth BW2 = Bandwidth.gbps(1); |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 39 | |
| 40 | @Test |
| 41 | public void testEquals() { |
Sho SHIMIZU | 44f3761 | 2015-11-25 16:23:22 -0800 | [diff] [blame] | 42 | 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); |
| 46 | ResourcePath sameAsResource3 = ResourcePath.continuous(BW2.bps(), D1, P1, BW1); |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 47 | |
| 48 | new EqualsTester() |
| 49 | .addEqualityGroup(resource1, sameAsResource1) |
| 50 | .addEqualityGroup(resource2) |
Sho SHIMIZU | 60ac58e | 2015-11-11 12:16:38 -0800 | [diff] [blame] | 51 | .addEqualityGroup(resource3, sameAsResource3) // this is intentional |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 52 | .testEquals(); |
| 53 | } |
| 54 | |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 55 | @Test |
Sho SHIMIZU | 69dc584 | 2015-11-20 16:31:12 -0800 | [diff] [blame] | 56 | public void testComponents() { |
| 57 | ResourcePath port = ResourcePath.discrete(D1, P1); |
| 58 | |
| 59 | assertThat(port.components(), contains(D1, P1)); |
| 60 | } |
| 61 | |
| 62 | @Test |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 63 | public void testThereIsParent() { |
Sho SHIMIZU | 44f3761 | 2015-11-25 16:23:22 -0800 | [diff] [blame] | 64 | ResourcePath path = ResourcePath.discrete(D1, P1, VLAN1); |
| 65 | ResourcePath parent = ResourcePath.discrete(D1, P1); |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 66 | |
| 67 | assertThat(path.parent(), is(Optional.of(parent))); |
| 68 | } |
| 69 | |
| 70 | @Test |
| 71 | public void testNoParent() { |
Sho SHIMIZU | 44f3761 | 2015-11-25 16:23:22 -0800 | [diff] [blame] | 72 | ResourcePath path = ResourcePath.discrete(D1); |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 73 | |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 74 | assertThat(path.parent(), is(Optional.of(ResourcePath.ROOT))); |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | @Test |
| 78 | public void testBase() { |
Sho SHIMIZU | 44f3761 | 2015-11-25 16:23:22 -0800 | [diff] [blame] | 79 | ResourcePath path = ResourcePath.discrete(D1); |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 80 | |
Sho SHIMIZU | 44f3761 | 2015-11-25 16:23:22 -0800 | [diff] [blame] | 81 | DeviceId child = (DeviceId) path.last(); |
| 82 | assertThat(child, is(D1)); |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 83 | } |
| 84 | } |