blob: 2a287a3b9c326e24312949563a0a52291bbf0544 [file] [log] [blame]
Ray Milkey85267002016-11-16 11:06:35 -08001/*
2 * Copyright 2016-present 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 */
Ray Milkeydbf59f02016-08-19 12:54:16 -070016package org.onosproject.net;
17
18import java.util.List;
19
20import org.junit.Test;
21
22import com.google.common.collect.ImmutableList;
23import com.google.common.testing.EqualsTester;
24
25import static org.hamcrest.MatcherAssert.assertThat;
26import static org.hamcrest.Matchers.is;
27import static org.onosproject.net.NetTestTools.PID;
28
29/**
30 * Unit tests for the DefaultDisjointPathTest.
31 */
32public class DefaultDisjointPathTest {
33 private static DefaultLink link1 =
34 DefaultLink.builder()
35 .type(Link.Type.DIRECT)
36 .providerId(PID)
37 .src(NetTestTools.connectPoint("dev1", 1))
38 .dst(NetTestTools.connectPoint("dev2", 1)).build();
39
40 private static DefaultLink link2 =
41 DefaultLink.builder()
42 .type(Link.Type.DIRECT)
43 .providerId(PID)
44 .src(NetTestTools.connectPoint("dev1", 1))
45 .dst(NetTestTools.connectPoint("dev2", 1)).build();
46
47 private static DefaultLink link3 =
48 DefaultLink.builder()
49 .type(Link.Type.DIRECT)
50 .providerId(PID)
51 .src(NetTestTools.connectPoint("dev2", 1))
52 .dst(NetTestTools.connectPoint("dev3", 1)).build();
53
54 private static List<Link> links1 = ImmutableList.of(link1, link2);
55 private static DefaultPath path1 =
56 new DefaultPath(PID, links1, 1.0);
57
58 private static List<Link> links2 = ImmutableList.of(link2, link1);
59 private static DefaultPath path2 =
60 new DefaultPath(PID, links2, 2.0);
61
62 private static List<Link> links3 = ImmutableList.of(link1, link2, link3);
63 private static DefaultPath path3 =
64 new DefaultPath(PID, links3, 3.0);
65
66 private static DefaultDisjointPath disjointPath1 =
67 new DefaultDisjointPath(PID, path1, path2);
68 private static DefaultDisjointPath sameAsDisjointPath1 =
69 new DefaultDisjointPath(PID, path1, path2);
70 private static DefaultDisjointPath disjointPath2 =
71 new DefaultDisjointPath(PID, path2, path1);
72 private static DefaultDisjointPath disjointPath3 =
73 new DefaultDisjointPath(PID, path1, path3);
74 private static DefaultDisjointPath disjointPath4 =
75 new DefaultDisjointPath(PID, path1, null);
76
77
78 /**
79 * Tests construction and fetching of member data.
80 */
81 @Test
82 public void testConstruction() {
83 assertThat(disjointPath1.primary(), is(path1));
84 assertThat(disjointPath1.backup(), is(path2));
85 assertThat(disjointPath1.links(), is(links1));
86 assertThat(disjointPath1.cost(), is(1.0));
87 }
88
89 /**
90 * Tests switching to the backup path.
91 */
92 @Test
93 public void testUseBackup() {
94 disjointPath1.useBackup();
95 assertThat(disjointPath1.primary(), is(path1));
96 assertThat(disjointPath1.backup(), is(path2));
97 assertThat(disjointPath1.links(), is(links2));
98 assertThat(disjointPath1.cost(), is(2.0));
99
100 disjointPath1.useBackup();
101 assertThat(disjointPath1.links(), is(links1));
102 assertThat(disjointPath1.cost(), is(1.0));
103
104 assertThat(disjointPath4.primary(), is(path1));
105 assertThat(disjointPath4.backup(), is((DefaultDisjointPath) null));
106 disjointPath4.useBackup();
107 assertThat(disjointPath4.primary(), is(path1));
108 assertThat(disjointPath4.backup(), is((DefaultDisjointPath) null));
109 }
110
111 /**
112 * Tests equals(), hashCode(), and toString() methods.
113 */
114 @Test
115 public void testEquals() {
116 new EqualsTester()
117 .addEqualityGroup(disjointPath1, sameAsDisjointPath1, disjointPath2)
118 .addEqualityGroup(disjointPath3)
119 .addEqualityGroup(disjointPath4)
120 .testEquals();
121 }
122}