blob: f69e4793bbcb8ef841fa00443e56197a68663038 [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 */
46public class LinkCollectionIntentTest {
47
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 */
63 @Test
64 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);
74 final LinkCollectionIntent sameAsCollectionIntent1 =
75 new LinkCollectionIntent(APP_ID,
76 selector,
77 treatment,
78 links1,
79 egress);
80
81 final HashSet<Link> links2 = new HashSet<>();
82 links2.add(link("src", 1, "dst", 3));
83 final LinkCollectionIntent collectionIntent2 =
84 new LinkCollectionIntent(APP_ID,
85 selector,
86 treatment,
87 links2,
88 egress);
89
90 new EqualsTester()
91 .addEqualityGroup(collectionIntent1, sameAsCollectionIntent1)
92 .addEqualityGroup(collectionIntent2)
93 .testEquals();
94 }
95
96 /**
97 * Tests constructor without constraints.
98 */
99 @Test
100 public void testConstructor() {
101 final HashSet<Link> links1 = new HashSet<>();
102 links1.add(link("src", 1, "dst", 2));
103 final LinkCollectionIntent collectionIntent =
104 new LinkCollectionIntent(APP_ID,
105 selector,
106 treatment,
107 links1,
108 egress);
109
110 final Set<Link> createdLinks = collectionIntent.links();
111 assertThat(createdLinks, hasSize(1));
112 assertThat(collectionIntent.isInstallable(), is(true));
113 assertThat(collectionIntent.treatment(), is(treatment));
114 assertThat(collectionIntent.selector(), is(selector));
Michele Santuari4a338072014-11-05 18:38:55 +0100115 assertThat(collectionIntent.egressPoints(), is(ImmutableSet.of(egress)));
Ray Milkeyc8f481f2014-11-18 15:37:12 -0800116 assertThat(collectionIntent.resources(), hasSize(1));
117 final List<Constraint> createdConstraints = collectionIntent.constraints();
118 assertThat(createdConstraints, hasSize(0));
119 }
120
121 /**
122 * Tests constructor with constraints.
123 */
124 @Test
125 public void testConstructorWithConstraints() {
126 final HashSet<Link> links1 = new HashSet<>();
127 final LinkedList<Constraint> constraints = new LinkedList<>();
128
129 links1.add(link("src", 1, "dst", 2));
130 constraints.add(new LambdaConstraint(Lambda.valueOf(23)));
131 final LinkCollectionIntent collectionIntent =
132 new LinkCollectionIntent(APP_ID,
133 selector,
134 treatment,
135 links1,
136 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));
Michele Santuari4a338072014-11-05 18:38:55 +0100144 assertThat(collectionIntent.egressPoints(), is(ImmutableSet.of(egress)));
Ray Milkeyc8f481f2014-11-18 15:37:12 -0800145
146 final List<Constraint> createdConstraints = collectionIntent.constraints();
147 assertThat(createdConstraints, hasSize(1));
148 assertThat(createdConstraints.get(0).toString(), startsWith("LambdaConstraint"));
149 }
150
151 /**
152 * Tests constructor with constraints.
153 */
154 @Test
155 public void testSerializerConstructor() {
156
157 final LinkCollectionIntent collectionIntent =
158 new LinkCollectionIntent();
159
160 final Set<Link> createdLinks = collectionIntent.links();
161 assertThat(createdLinks, nullValue());
162 assertThat(collectionIntent.isInstallable(), is(true));
163 assertThat(collectionIntent.treatment(), nullValue());
164 assertThat(collectionIntent.selector(), nullValue());
Michele Santuari4a338072014-11-05 18:38:55 +0100165 assertThat(collectionIntent.egressPoints(), nullValue());
Ray Milkeyc8f481f2014-11-18 15:37:12 -0800166
167 final List<Constraint> createdConstraints = collectionIntent.constraints();
168 assertThat(createdConstraints, hasSize(0));
169 }
170}