blob: f785196ca33832e75e390e6b46ba3aa800c1fb35 [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
23import org.junit.Test;
24import org.onlab.onos.net.ConnectPoint;
25import org.onlab.onos.net.Link;
26import org.onlab.onos.net.NetTestTools;
27import org.onlab.onos.net.flow.TrafficSelector;
28import org.onlab.onos.net.intent.constraint.LambdaConstraint;
29import org.onlab.onos.net.resource.Lambda;
30
Michele Santuari4a338072014-11-05 18:38:55 +010031import com.google.common.collect.ImmutableSet;
Ray Milkeyc8f481f2014-11-18 15:37:12 -080032import com.google.common.testing.EqualsTester;
33
34import static org.hamcrest.MatcherAssert.assertThat;
35import static org.hamcrest.Matchers.hasSize;
36import static org.hamcrest.Matchers.is;
37import static org.hamcrest.Matchers.nullValue;
38import static org.hamcrest.Matchers.startsWith;
39import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
40import static org.onlab.onos.net.NetTestTools.APP_ID;
41import static org.onlab.onos.net.NetTestTools.link;
42
43/**
44 * Unit tests for the LinkCollectionIntent class.
45 */
Brian O'Connor520c0522014-11-23 23:50:47 -080046public class LinkCollectionIntentTest extends IntentTest {
Ray Milkeyc8f481f2014-11-18 15:37:12 -080047
48 final ConnectPoint egress = NetTestTools.connectPoint("egress", 3);
49 final TrafficSelector selector = new IntentTestsMocks.MockSelector();
50 final IntentTestsMocks.MockTreatment treatment = new IntentTestsMocks.MockTreatment();
51
52 /**
53 * Checks that the LinkCollectionIntent class is immutable.
54 */
55 @Test
56 public void testImmutability() {
57 assertThatClassIsImmutable(LinkCollectionIntent.class);
58 }
59
60 /**
61 * Tests equals(), hashCode() and toString() methods.
62 */
Ray Milkey37f6a382014-11-25 14:54:42 -080063 @Test
Ray Milkeyc8f481f2014-11-18 15:37:12 -080064 public void testEquals() {
65
66 final HashSet<Link> links1 = new HashSet<>();
67 links1.add(link("src", 1, "dst", 2));
68 final LinkCollectionIntent collectionIntent1 =
69 new LinkCollectionIntent(APP_ID,
70 selector,
71 treatment,
72 links1,
73 egress);
Ray Milkeyc8f481f2014-11-18 15:37:12 -080074
75 final HashSet<Link> links2 = new HashSet<>();
76 links2.add(link("src", 1, "dst", 3));
77 final LinkCollectionIntent collectionIntent2 =
78 new LinkCollectionIntent(APP_ID,
79 selector,
80 treatment,
81 links2,
82 egress);
83
84 new EqualsTester()
Ray Milkey37f6a382014-11-25 14:54:42 -080085 .addEqualityGroup(collectionIntent1)
Ray Milkeyc8f481f2014-11-18 15:37:12 -080086 .addEqualityGroup(collectionIntent2)
87 .testEquals();
88 }
89
90 /**
91 * Tests constructor without constraints.
92 */
93 @Test
94 public void testConstructor() {
95 final HashSet<Link> links1 = new HashSet<>();
96 links1.add(link("src", 1, "dst", 2));
97 final LinkCollectionIntent collectionIntent =
98 new LinkCollectionIntent(APP_ID,
99 selector,
100 treatment,
101 links1,
102 egress);
103
104 final Set<Link> createdLinks = collectionIntent.links();
105 assertThat(createdLinks, hasSize(1));
106 assertThat(collectionIntent.isInstallable(), is(true));
107 assertThat(collectionIntent.treatment(), is(treatment));
108 assertThat(collectionIntent.selector(), is(selector));
Michele Santuari4a338072014-11-05 18:38:55 +0100109 assertThat(collectionIntent.egressPoints(), is(ImmutableSet.of(egress)));
Ray Milkeyc8f481f2014-11-18 15:37:12 -0800110 assertThat(collectionIntent.resources(), hasSize(1));
111 final List<Constraint> createdConstraints = collectionIntent.constraints();
112 assertThat(createdConstraints, hasSize(0));
113 }
114
115 /**
116 * Tests constructor with constraints.
117 */
118 @Test
119 public void testConstructorWithConstraints() {
120 final HashSet<Link> links1 = new HashSet<>();
121 final LinkedList<Constraint> constraints = new LinkedList<>();
122
123 links1.add(link("src", 1, "dst", 2));
124 constraints.add(new LambdaConstraint(Lambda.valueOf(23)));
125 final LinkCollectionIntent collectionIntent =
126 new LinkCollectionIntent(APP_ID,
127 selector,
128 treatment,
129 links1,
130 egress,
131 constraints);
132
133 final Set<Link> createdLinks = collectionIntent.links();
134 assertThat(createdLinks, hasSize(1));
135 assertThat(collectionIntent.isInstallable(), is(true));
136 assertThat(collectionIntent.treatment(), is(treatment));
137 assertThat(collectionIntent.selector(), is(selector));
Michele Santuari4a338072014-11-05 18:38:55 +0100138 assertThat(collectionIntent.egressPoints(), is(ImmutableSet.of(egress)));
Ray Milkeyc8f481f2014-11-18 15:37:12 -0800139
140 final List<Constraint> createdConstraints = collectionIntent.constraints();
141 assertThat(createdConstraints, hasSize(1));
142 assertThat(createdConstraints.get(0).toString(), startsWith("LambdaConstraint"));
143 }
144
145 /**
146 * Tests constructor with constraints.
147 */
148 @Test
149 public void testSerializerConstructor() {
150
151 final LinkCollectionIntent collectionIntent =
152 new LinkCollectionIntent();
153
154 final Set<Link> createdLinks = collectionIntent.links();
155 assertThat(createdLinks, nullValue());
156 assertThat(collectionIntent.isInstallable(), is(true));
157 assertThat(collectionIntent.treatment(), nullValue());
158 assertThat(collectionIntent.selector(), nullValue());
Michele Santuari4a338072014-11-05 18:38:55 +0100159 assertThat(collectionIntent.egressPoints(), nullValue());
Ray Milkeyc8f481f2014-11-18 15:37:12 -0800160
161 final List<Constraint> createdConstraints = collectionIntent.constraints();
162 assertThat(createdConstraints, hasSize(0));
163 }
Brian O'Connor520c0522014-11-23 23:50:47 -0800164
165 @Override
166 protected Intent createOne() {
167 HashSet<Link> links1 = new HashSet<>();
168 links1.add(link("src", 1, "dst", 2));
169 return new LinkCollectionIntent(APP_ID,
170 selector,
171 treatment,
172 links1,
173 egress);
174 }
175
176 @Override
177 protected Intent createAnother() {
178 HashSet<Link> links2 = new HashSet<>();
179 links2.add(link("src", 1, "dst", 3));
180 return new LinkCollectionIntent(APP_ID,
181 selector,
182 treatment,
183 links2,
184 egress);
185 }
Ray Milkeyc8f481f2014-11-18 15:37:12 -0800186}