blob: 03273907fa554f75a06c1565f424994339d834b5 [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 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.net.intent;
Ray Milkeyc8f481f2014-11-18 15:37:12 -080017
18import java.util.HashSet;
19import java.util.LinkedList;
20import java.util.List;
21import java.util.Set;
22
23import org.junit.Test;
Brian O'Connorabafb502014-12-02 22:26:20 -080024import org.onosproject.net.ConnectPoint;
25import org.onosproject.net.Link;
26import org.onosproject.net.NetTestTools;
27import org.onosproject.net.flow.TrafficSelector;
28import org.onosproject.net.intent.constraint.LambdaConstraint;
29import org.onosproject.net.resource.Lambda;
Ray Milkeyc8f481f2014-11-18 15:37:12 -080030
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;
Brian O'Connorabafb502014-12-02 22:26:20 -080040import static org.onosproject.net.NetTestTools.APP_ID;
41import static org.onosproject.net.NetTestTools.link;
Ray Milkeyc8f481f2014-11-18 15:37:12 -080042
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
Pavlin Radoslavov2811c402015-02-25 14:30:17 -080048 final ConnectPoint ingress = NetTestTools.connectPoint("ingress", 2);
Ray Milkeyc8f481f2014-11-18 15:37:12 -080049 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 */
Ray Milkey37f6a382014-11-25 14:54:42 -080064 @Test
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,
Pavlin Radoslavov2811c402015-02-25 14:30:17 -080074 ingress,
Ray Milkeyc8f481f2014-11-18 15:37:12 -080075 egress);
Ray Milkeyc8f481f2014-11-18 15:37:12 -080076
77 final HashSet<Link> links2 = new HashSet<>();
78 links2.add(link("src", 1, "dst", 3));
79 final LinkCollectionIntent collectionIntent2 =
80 new LinkCollectionIntent(APP_ID,
81 selector,
82 treatment,
83 links2,
Pavlin Radoslavov2811c402015-02-25 14:30:17 -080084 ingress,
Ray Milkeyc8f481f2014-11-18 15:37:12 -080085 egress);
86
87 new EqualsTester()
Ray Milkey37f6a382014-11-25 14:54:42 -080088 .addEqualityGroup(collectionIntent1)
Ray Milkeyc8f481f2014-11-18 15:37:12 -080089 .addEqualityGroup(collectionIntent2)
90 .testEquals();
91 }
92
93 /**
94 * Tests constructor without constraints.
95 */
96 @Test
97 public void testConstructor() {
98 final HashSet<Link> links1 = new HashSet<>();
99 links1.add(link("src", 1, "dst", 2));
100 final LinkCollectionIntent collectionIntent =
101 new LinkCollectionIntent(APP_ID,
102 selector,
103 treatment,
104 links1,
Pavlin Radoslavov2811c402015-02-25 14:30:17 -0800105 ingress,
Ray Milkeyc8f481f2014-11-18 15:37:12 -0800106 egress);
107
108 final Set<Link> createdLinks = collectionIntent.links();
109 assertThat(createdLinks, hasSize(1));
110 assertThat(collectionIntent.isInstallable(), is(true));
111 assertThat(collectionIntent.treatment(), is(treatment));
112 assertThat(collectionIntent.selector(), is(selector));
Pavlin Radoslavov2811c402015-02-25 14:30:17 -0800113 assertThat(collectionIntent.ingressPoints(), is(ImmutableSet.of(ingress)));
Michele Santuari4a338072014-11-05 18:38:55 +0100114 assertThat(collectionIntent.egressPoints(), is(ImmutableSet.of(egress)));
Ray Milkeyc8f481f2014-11-18 15:37:12 -0800115 assertThat(collectionIntent.resources(), hasSize(1));
116 final List<Constraint> createdConstraints = collectionIntent.constraints();
117 assertThat(createdConstraints, hasSize(0));
118 }
119
120 /**
121 * Tests constructor with constraints.
122 */
123 @Test
124 public void testConstructorWithConstraints() {
125 final HashSet<Link> links1 = new HashSet<>();
126 final LinkedList<Constraint> constraints = new LinkedList<>();
127
128 links1.add(link("src", 1, "dst", 2));
129 constraints.add(new LambdaConstraint(Lambda.valueOf(23)));
130 final LinkCollectionIntent collectionIntent =
131 new LinkCollectionIntent(APP_ID,
132 selector,
133 treatment,
134 links1,
Pavlin Radoslavov2811c402015-02-25 14:30:17 -0800135 ingress,
Ray Milkeyc8f481f2014-11-18 15:37:12 -0800136 egress,
137 constraints);
138
139 final Set<Link> createdLinks = collectionIntent.links();
140 assertThat(createdLinks, hasSize(1));
141 assertThat(collectionIntent.isInstallable(), is(true));
142 assertThat(collectionIntent.treatment(), is(treatment));
143 assertThat(collectionIntent.selector(), is(selector));
Pavlin Radoslavov2811c402015-02-25 14:30:17 -0800144 assertThat(collectionIntent.ingressPoints(), is(ImmutableSet.of(ingress)));
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());
Pavlin Radoslavov2811c402015-02-25 14:30:17 -0800166 assertThat(collectionIntent.ingressPoints(), nullValue());
Michele Santuari4a338072014-11-05 18:38:55 +0100167 assertThat(collectionIntent.egressPoints(), nullValue());
Ray Milkeyc8f481f2014-11-18 15:37:12 -0800168
169 final List<Constraint> createdConstraints = collectionIntent.constraints();
170 assertThat(createdConstraints, hasSize(0));
171 }
Brian O'Connor520c0522014-11-23 23:50:47 -0800172
173 @Override
174 protected Intent createOne() {
175 HashSet<Link> links1 = new HashSet<>();
176 links1.add(link("src", 1, "dst", 2));
177 return new LinkCollectionIntent(APP_ID,
178 selector,
179 treatment,
180 links1,
Pavlin Radoslavov2811c402015-02-25 14:30:17 -0800181 ingress,
Brian O'Connor520c0522014-11-23 23:50:47 -0800182 egress);
183 }
184
185 @Override
186 protected Intent createAnother() {
187 HashSet<Link> links2 = new HashSet<>();
188 links2.add(link("src", 1, "dst", 3));
189 return new LinkCollectionIntent(APP_ID,
190 selector,
191 treatment,
192 links2,
Pavlin Radoslavov2811c402015-02-25 14:30:17 -0800193 ingress,
Brian O'Connor520c0522014-11-23 23:50:47 -0800194 egress);
195 }
Ray Milkeyc8f481f2014-11-18 15:37:12 -0800196}