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.ConnectPoint; |
| 23 | import org.onosproject.net.DeviceId; |
| 24 | import org.onosproject.net.LinkKey; |
| 25 | import org.onosproject.net.PortNumber; |
| 26 | |
| 27 | import java.util.Optional; |
| 28 | |
Sho SHIMIZU | 69dc584 | 2015-11-20 16:31:12 -0800 | [diff] [blame^] | 29 | import static org.hamcrest.Matchers.contains; |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 30 | import static org.hamcrest.Matchers.is; |
| 31 | import static org.junit.Assert.assertThat; |
| 32 | |
| 33 | public class ResourcePathTest { |
| 34 | |
| 35 | private static final DeviceId D1 = DeviceId.deviceId("of:001"); |
| 36 | private static final DeviceId D2 = DeviceId.deviceId("of:002"); |
| 37 | private static final PortNumber P1 = PortNumber.portNumber(1); |
| 38 | private static final ConnectPoint CP1_1 = new ConnectPoint(D1, P1); |
| 39 | private static final ConnectPoint CP2_1 = new ConnectPoint(D2, P1); |
| 40 | private static final VlanId VLAN1 = VlanId.vlanId((short) 100); |
Sho SHIMIZU | 60ac58e | 2015-11-11 12:16:38 -0800 | [diff] [blame] | 41 | private static final Bandwidth BW1 = Bandwidth.gbps(2); |
| 42 | private static final Bandwidth BW2 = Bandwidth.gbps(1); |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 43 | |
| 44 | @Test |
| 45 | public void testEquals() { |
Sho SHIMIZU | 60ac58e | 2015-11-11 12:16:38 -0800 | [diff] [blame] | 46 | ResourcePath resource1 = ResourcePath.discrete(LinkKey.linkKey(CP1_1, CP2_1), VLAN1); |
| 47 | ResourcePath sameAsResource1 = ResourcePath.discrete(LinkKey.linkKey(CP1_1, CP2_1), VLAN1); |
| 48 | ResourcePath resource2 = ResourcePath.discrete(LinkKey.linkKey(CP2_1, CP1_1), VLAN1); |
| 49 | ResourcePath resource3 = ResourcePath.continuous(BW1.bps(), LinkKey.linkKey(CP1_1, CP2_1), BW1); |
| 50 | ResourcePath sameAsResource3 = ResourcePath.continuous(BW2.bps(), LinkKey.linkKey(CP1_1, CP2_1), BW1); |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 51 | |
| 52 | new EqualsTester() |
| 53 | .addEqualityGroup(resource1, sameAsResource1) |
| 54 | .addEqualityGroup(resource2) |
Sho SHIMIZU | 60ac58e | 2015-11-11 12:16:38 -0800 | [diff] [blame] | 55 | .addEqualityGroup(resource3, sameAsResource3) // this is intentional |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 56 | .testEquals(); |
| 57 | } |
| 58 | |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 59 | @Test |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 60 | public void testCreateWithZeroComponent() { |
Sho SHIMIZU | 60ac58e | 2015-11-11 12:16:38 -0800 | [diff] [blame] | 61 | ResourcePath path = ResourcePath.discrete(); |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 62 | |
| 63 | assertThat(path, is(ResourcePath.ROOT)); |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | @Test |
Sho SHIMIZU | 69dc584 | 2015-11-20 16:31:12 -0800 | [diff] [blame^] | 67 | public void testComponents() { |
| 68 | ResourcePath port = ResourcePath.discrete(D1, P1); |
| 69 | |
| 70 | assertThat(port.components(), contains(D1, P1)); |
| 71 | } |
| 72 | |
| 73 | @Test |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 74 | public void testThereIsParent() { |
Sho SHIMIZU | 60ac58e | 2015-11-11 12:16:38 -0800 | [diff] [blame] | 75 | ResourcePath path = ResourcePath.discrete(LinkKey.linkKey(CP1_1, CP2_1), VLAN1); |
| 76 | ResourcePath parent = ResourcePath.discrete(LinkKey.linkKey(CP1_1, CP2_1)); |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 77 | |
| 78 | assertThat(path.parent(), is(Optional.of(parent))); |
| 79 | } |
| 80 | |
| 81 | @Test |
| 82 | public void testNoParent() { |
Sho SHIMIZU | 60ac58e | 2015-11-11 12:16:38 -0800 | [diff] [blame] | 83 | ResourcePath path = ResourcePath.discrete(LinkKey.linkKey(CP1_1, CP2_1)); |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 84 | |
Sho SHIMIZU | ba41fc1 | 2015-08-12 15:43:22 -0700 | [diff] [blame] | 85 | assertThat(path.parent(), is(Optional.of(ResourcePath.ROOT))); |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | @Test |
| 89 | public void testBase() { |
| 90 | LinkKey linkKey = LinkKey.linkKey(CP1_1, CP2_1); |
Sho SHIMIZU | 60ac58e | 2015-11-11 12:16:38 -0800 | [diff] [blame] | 91 | ResourcePath path = ResourcePath.discrete(linkKey); |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 92 | |
Sho SHIMIZU | c9546a3 | 2015-11-10 11:22:28 -0800 | [diff] [blame] | 93 | LinkKey child = (LinkKey) path.last(); |
Sho SHIMIZU | 1f5e591 | 2015-08-10 17:00:00 -0700 | [diff] [blame] | 94 | assertThat(child, is(linkKey)); |
| 95 | } |
| 96 | } |