blob: 4a8886a4c9344873c8366dcb126b05fa79e33f77 [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;
21import org.onosproject.net.ConnectPoint;
22import org.onosproject.net.DeviceId;
23import org.onosproject.net.LinkKey;
24import org.onosproject.net.PortNumber;
25
26import java.util.Optional;
27
28import 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);
36 private static final ConnectPoint CP1_1 = new ConnectPoint(D1, P1);
37 private static final ConnectPoint CP2_1 = new ConnectPoint(D2, P1);
38 private static final VlanId VLAN1 = VlanId.vlanId((short) 100);
39
40 @Test
41 public void testEquals() {
42 ResourcePath resource1 = new ResourcePath(LinkKey.linkKey(CP1_1, CP2_1), VLAN1);
43 ResourcePath sameAsResource1 = new ResourcePath(LinkKey.linkKey(CP1_1, CP2_1), VLAN1);
44 ResourcePath resource2 = new ResourcePath(LinkKey.linkKey(CP2_1, CP1_1), VLAN1);
45
46 new EqualsTester()
47 .addEqualityGroup(resource1, sameAsResource1)
48 .addEqualityGroup(resource2)
49 .testEquals();
50 }
51
Sho SHIMIZUba41fc12015-08-12 15:43:22 -070052 @Test
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070053 public void testCreateWithZeroComponent() {
54 ResourcePath path = new ResourcePath();
Sho SHIMIZUba41fc12015-08-12 15:43:22 -070055
56 assertThat(path, is(ResourcePath.ROOT));
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070057 }
58
59 @Test
60 public void testThereIsParent() {
61 ResourcePath path = new ResourcePath(LinkKey.linkKey(CP1_1, CP2_1), VLAN1);
62 ResourcePath parent = new ResourcePath(LinkKey.linkKey(CP1_1, CP2_1));
63
64 assertThat(path.parent(), is(Optional.of(parent)));
65 }
66
67 @Test
68 public void testNoParent() {
69 ResourcePath path = new ResourcePath(LinkKey.linkKey(CP1_1, CP2_1));
70
Sho SHIMIZUba41fc12015-08-12 15:43:22 -070071 assertThat(path.parent(), is(Optional.of(ResourcePath.ROOT)));
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -070072 }
73
74 @Test
75 public void testBase() {
76 LinkKey linkKey = LinkKey.linkKey(CP1_1, CP2_1);
77 ResourcePath path = new ResourcePath(linkKey);
78
79 LinkKey child = (LinkKey) path.lastComponent();
80 assertThat(child, is(linkKey));
81 }
82}