blob: 4b477349c193492a03507d0d2c1fdaa06a67c654 [file] [log] [blame]
Ray Milkeyc8f481f2014-11-18 15:37:12 -08001/*
2 * Copyright 2014 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.onlab.onos.net.intent;
17
18import java.util.HashSet;
19import java.util.LinkedList;
20import java.util.List;
21import java.util.Set;
22
Brian O'Connor520c0522014-11-23 23:50:47 -080023import org.junit.Ignore;
Ray Milkeyc8f481f2014-11-18 15:37:12 -080024import org.junit.Test;
25import org.onlab.onos.net.ConnectPoint;
26import org.onlab.onos.net.Link;
27import org.onlab.onos.net.NetTestTools;
28import org.onlab.onos.net.flow.TrafficSelector;
29import org.onlab.onos.net.intent.constraint.LambdaConstraint;
30import org.onlab.onos.net.resource.Lambda;
31
Michele Santuari4a338072014-11-05 18:38:55 +010032import com.google.common.collect.ImmutableSet;
Ray Milkeyc8f481f2014-11-18 15:37:12 -080033import com.google.common.testing.EqualsTester;
34
35import static org.hamcrest.MatcherAssert.assertThat;
36import static org.hamcrest.Matchers.hasSize;
37import static org.hamcrest.Matchers.is;
38import static org.hamcrest.Matchers.nullValue;
39import static org.hamcrest.Matchers.startsWith;
40import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
41import static org.onlab.onos.net.NetTestTools.APP_ID;
42import static org.onlab.onos.net.NetTestTools.link;
43
44/**
45 * Unit tests for the LinkCollectionIntent class.
46 */
Brian O'Connor520c0522014-11-23 23:50:47 -080047public class LinkCollectionIntentTest extends IntentTest {
Ray Milkeyc8f481f2014-11-18 15:37:12 -080048
49 final ConnectPoint egress = NetTestTools.connectPoint("egress", 3);
50 final TrafficSelector selector = new IntentTestsMocks.MockSelector();
51 final IntentTestsMocks.MockTreatment treatment = new IntentTestsMocks.MockTreatment();
52
53 /**
54 * Checks that the LinkCollectionIntent class is immutable.
55 */
56 @Test
57 public void testImmutability() {
58 assertThatClassIsImmutable(LinkCollectionIntent.class);
59 }
60
61 /**
62 * Tests equals(), hashCode() and toString() methods.
63 */
Brian O'Connor520c0522014-11-23 23:50:47 -080064 @Test @Ignore("Equality is based on ids, which will be different")
Ray Milkeyc8f481f2014-11-18 15:37:12 -080065 public void testEquals() {
66
67 final HashSet<Link> links1 = new HashSet<>();
68 links1.add(link("src", 1, "dst", 2));
69 final LinkCollectionIntent collectionIntent1 =
70 new LinkCollectionIntent(APP_ID,
71 selector,
72 treatment,
73 links1,
74 egress);
75 final LinkCollectionIntent sameAsCollectionIntent1 =
76 new LinkCollectionIntent(APP_ID,
77 selector,
78 treatment,
79 links1,
80 egress);
81
82 final HashSet<Link> links2 = new HashSet<>();
83 links2.add(link("src", 1, "dst", 3));
84 final LinkCollectionIntent collectionIntent2 =
85 new LinkCollectionIntent(APP_ID,
86 selector,
87 treatment,
88 links2,
89 egress);
90
91 new EqualsTester()
92 .addEqualityGroup(collectionIntent1, sameAsCollectionIntent1)
93 .addEqualityGroup(collectionIntent2)
94 .testEquals();
95 }
96
97 /**
98 * Tests constructor without constraints.
99 */
100 @Test
101 public void testConstructor() {
102 final HashSet<Link> links1 = new HashSet<>();
103 links1.add(link("src", 1, "dst", 2));
104 final LinkCollectionIntent collectionIntent =
105 new LinkCollectionIntent(APP_ID,
106 selector,
107 treatment,
108 links1,
109 egress);
110
111 final Set<Link> createdLinks = collectionIntent.links();
112 assertThat(createdLinks, hasSize(1));
113 assertThat(collectionIntent.isInstallable(), is(true));
114 assertThat(collectionIntent.treatment(), is(treatment));
115 assertThat(collectionIntent.selector(), is(selector));
Michele Santuari4a338072014-11-05 18:38:55 +0100116 assertThat(collectionIntent.egressPoints(), is(ImmutableSet.of(egress)));
Ray Milkeyc8f481f2014-11-18 15:37:12 -0800117 assertThat(collectionIntent.resources(), hasSize(1));
118 final List<Constraint> createdConstraints = collectionIntent.constraints();
119 assertThat(createdConstraints, hasSize(0));
120 }
121
122 /**
123 * Tests constructor with constraints.
124 */
125 @Test
126 public void testConstructorWithConstraints() {
127 final HashSet<Link> links1 = new HashSet<>();
128 final LinkedList<Constraint> constraints = new LinkedList<>();
129
130 links1.add(link("src", 1, "dst", 2));
131 constraints.add(new LambdaConstraint(Lambda.valueOf(23)));
132 final LinkCollectionIntent collectionIntent =
133 new LinkCollectionIntent(APP_ID,
134 selector,
135 treatment,
136 links1,
137 egress,
138 constraints);
139
140 final Set<Link> createdLinks = collectionIntent.links();
141 assertThat(createdLinks, hasSize(1));
142 assertThat(collectionIntent.isInstallable(), is(true));
143 assertThat(collectionIntent.treatment(), is(treatment));
144 assertThat(collectionIntent.selector(), is(selector));
Michele Santuari4a338072014-11-05 18:38:55 +0100145 assertThat(collectionIntent.egressPoints(), is(ImmutableSet.of(egress)));
Ray Milkeyc8f481f2014-11-18 15:37:12 -0800146
147 final List<Constraint> createdConstraints = collectionIntent.constraints();
148 assertThat(createdConstraints, hasSize(1));
149 assertThat(createdConstraints.get(0).toString(), startsWith("LambdaConstraint"));
150 }
151
152 /**
153 * Tests constructor with constraints.
154 */
155 @Test
156 public void testSerializerConstructor() {
157
158 final LinkCollectionIntent collectionIntent =
159 new LinkCollectionIntent();
160
161 final Set<Link> createdLinks = collectionIntent.links();
162 assertThat(createdLinks, nullValue());
163 assertThat(collectionIntent.isInstallable(), is(true));
164 assertThat(collectionIntent.treatment(), nullValue());
165 assertThat(collectionIntent.selector(), nullValue());
Michele Santuari4a338072014-11-05 18:38:55 +0100166 assertThat(collectionIntent.egressPoints(), nullValue());
Ray Milkeyc8f481f2014-11-18 15:37:12 -0800167
168 final List<Constraint> createdConstraints = collectionIntent.constraints();
169 assertThat(createdConstraints, hasSize(0));
170 }
Brian O'Connor520c0522014-11-23 23:50:47 -0800171
172 @Override
173 protected Intent createOne() {
174 HashSet<Link> links1 = new HashSet<>();
175 links1.add(link("src", 1, "dst", 2));
176 return new LinkCollectionIntent(APP_ID,
177 selector,
178 treatment,
179 links1,
180 egress);
181 }
182
183 @Override
184 protected Intent createAnother() {
185 HashSet<Link> links2 = new HashSet<>();
186 links2.add(link("src", 1, "dst", 3));
187 return new LinkCollectionIntent(APP_ID,
188 selector,
189 treatment,
190 links2,
191 egress);
192 }
Ray Milkeyc8f481f2014-11-18 15:37:12 -0800193}