blob: a9ae782fcaaa61e97851e3acc5b962c465d353c9 [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,
Ray Milkeyc24cde32015-03-10 18:20:18 -0700137 constraints,
138 8888);
Ray Milkeyc8f481f2014-11-18 15:37:12 -0800139
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));
Pavlin Radoslavov2811c402015-02-25 14:30:17 -0800145 assertThat(collectionIntent.ingressPoints(), is(ImmutableSet.of(ingress)));
Michele Santuari4a338072014-11-05 18:38:55 +0100146 assertThat(collectionIntent.egressPoints(), is(ImmutableSet.of(egress)));
Ray Milkeyc8f481f2014-11-18 15:37:12 -0800147
148 final List<Constraint> createdConstraints = collectionIntent.constraints();
149 assertThat(createdConstraints, hasSize(1));
150 assertThat(createdConstraints.get(0).toString(), startsWith("LambdaConstraint"));
151 }
152
153 /**
154 * Tests constructor with constraints.
155 */
156 @Test
157 public void testSerializerConstructor() {
158
159 final LinkCollectionIntent collectionIntent =
160 new LinkCollectionIntent();
161
162 final Set<Link> createdLinks = collectionIntent.links();
163 assertThat(createdLinks, nullValue());
164 assertThat(collectionIntent.isInstallable(), is(true));
165 assertThat(collectionIntent.treatment(), nullValue());
166 assertThat(collectionIntent.selector(), nullValue());
Pavlin Radoslavov2811c402015-02-25 14:30:17 -0800167 assertThat(collectionIntent.ingressPoints(), nullValue());
Michele Santuari4a338072014-11-05 18:38:55 +0100168 assertThat(collectionIntent.egressPoints(), nullValue());
Ray Milkeyc8f481f2014-11-18 15:37:12 -0800169
170 final List<Constraint> createdConstraints = collectionIntent.constraints();
171 assertThat(createdConstraints, hasSize(0));
172 }
Brian O'Connor520c0522014-11-23 23:50:47 -0800173
174 @Override
175 protected Intent createOne() {
176 HashSet<Link> links1 = new HashSet<>();
177 links1.add(link("src", 1, "dst", 2));
178 return new LinkCollectionIntent(APP_ID,
179 selector,
180 treatment,
181 links1,
Pavlin Radoslavov2811c402015-02-25 14:30:17 -0800182 ingress,
Brian O'Connor520c0522014-11-23 23:50:47 -0800183 egress);
184 }
185
186 @Override
187 protected Intent createAnother() {
188 HashSet<Link> links2 = new HashSet<>();
189 links2.add(link("src", 1, "dst", 3));
190 return new LinkCollectionIntent(APP_ID,
191 selector,
192 treatment,
193 links2,
Pavlin Radoslavov2811c402015-02-25 14:30:17 -0800194 ingress,
Brian O'Connor520c0522014-11-23 23:50:47 -0800195 egress);
196 }
Ray Milkeyc8f481f2014-11-18 15:37:12 -0800197}