blob: ddee9f9fa4f154f03f421302e6b32f0c3574777e [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
31import com.google.common.testing.EqualsTester;
32
33import static org.hamcrest.MatcherAssert.assertThat;
34import static org.hamcrest.Matchers.hasSize;
35import static org.hamcrest.Matchers.is;
36import static org.hamcrest.Matchers.nullValue;
37import static org.hamcrest.Matchers.startsWith;
38import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
39import static org.onlab.onos.net.NetTestTools.APP_ID;
40import static org.onlab.onos.net.NetTestTools.link;
41
42/**
43 * Unit tests for the LinkCollectionIntent class.
44 */
45public class LinkCollectionIntentTest {
46
47 final ConnectPoint egress = NetTestTools.connectPoint("egress", 3);
48 final TrafficSelector selector = new IntentTestsMocks.MockSelector();
49 final IntentTestsMocks.MockTreatment treatment = new IntentTestsMocks.MockTreatment();
50
51 /**
52 * Checks that the LinkCollectionIntent class is immutable.
53 */
54 @Test
55 public void testImmutability() {
56 assertThatClassIsImmutable(LinkCollectionIntent.class);
57 }
58
59 /**
60 * Tests equals(), hashCode() and toString() methods.
61 */
62 @Test
63 public void testEquals() {
64
65 final HashSet<Link> links1 = new HashSet<>();
66 links1.add(link("src", 1, "dst", 2));
67 final LinkCollectionIntent collectionIntent1 =
68 new LinkCollectionIntent(APP_ID,
69 selector,
70 treatment,
71 links1,
72 egress);
73 final LinkCollectionIntent sameAsCollectionIntent1 =
74 new LinkCollectionIntent(APP_ID,
75 selector,
76 treatment,
77 links1,
78 egress);
79
80 final HashSet<Link> links2 = new HashSet<>();
81 links2.add(link("src", 1, "dst", 3));
82 final LinkCollectionIntent collectionIntent2 =
83 new LinkCollectionIntent(APP_ID,
84 selector,
85 treatment,
86 links2,
87 egress);
88
89 new EqualsTester()
90 .addEqualityGroup(collectionIntent1, sameAsCollectionIntent1)
91 .addEqualityGroup(collectionIntent2)
92 .testEquals();
93 }
94
95 /**
96 * Tests constructor without constraints.
97 */
98 @Test
99 public void testConstructor() {
100 final HashSet<Link> links1 = new HashSet<>();
101 links1.add(link("src", 1, "dst", 2));
102 final LinkCollectionIntent collectionIntent =
103 new LinkCollectionIntent(APP_ID,
104 selector,
105 treatment,
106 links1,
107 egress);
108
109 final Set<Link> createdLinks = collectionIntent.links();
110 assertThat(createdLinks, hasSize(1));
111 assertThat(collectionIntent.isInstallable(), is(true));
112 assertThat(collectionIntent.treatment(), is(treatment));
113 assertThat(collectionIntent.selector(), is(selector));
114 assertThat(collectionIntent.egressPoint(), is(egress));
115 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,
135 egress,
136 constraints);
137
138 final Set<Link> createdLinks = collectionIntent.links();
139 assertThat(createdLinks, hasSize(1));
140 assertThat(collectionIntent.isInstallable(), is(true));
141 assertThat(collectionIntent.treatment(), is(treatment));
142 assertThat(collectionIntent.selector(), is(selector));
143 assertThat(collectionIntent.egressPoint(), is(egress));
144
145 final List<Constraint> createdConstraints = collectionIntent.constraints();
146 assertThat(createdConstraints, hasSize(1));
147 assertThat(createdConstraints.get(0).toString(), startsWith("LambdaConstraint"));
148 }
149
150 /**
151 * Tests constructor with constraints.
152 */
153 @Test
154 public void testSerializerConstructor() {
155
156 final LinkCollectionIntent collectionIntent =
157 new LinkCollectionIntent();
158
159 final Set<Link> createdLinks = collectionIntent.links();
160 assertThat(createdLinks, nullValue());
161 assertThat(collectionIntent.isInstallable(), is(true));
162 assertThat(collectionIntent.treatment(), nullValue());
163 assertThat(collectionIntent.selector(), nullValue());
164 assertThat(collectionIntent.egressPoint(), nullValue());
165
166 final List<Constraint> createdConstraints = collectionIntent.constraints();
167 assertThat(createdConstraints, hasSize(0));
168 }
169}