Ray Milkey | c8f481f | 2014-11-18 15:37:12 -0800 | [diff] [blame] | 1 | /* |
| 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'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.net.intent; |
Ray Milkey | c8f481f | 2014-11-18 15:37:12 -0800 | [diff] [blame] | 17 | |
| 18 | import java.util.HashSet; |
| 19 | import java.util.LinkedList; |
| 20 | import java.util.List; |
| 21 | import java.util.Set; |
| 22 | |
| 23 | import org.junit.Test; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 24 | import org.onosproject.net.ConnectPoint; |
| 25 | import org.onosproject.net.Link; |
| 26 | import org.onosproject.net.NetTestTools; |
| 27 | import org.onosproject.net.flow.TrafficSelector; |
| 28 | import org.onosproject.net.intent.constraint.LambdaConstraint; |
| 29 | import org.onosproject.net.resource.Lambda; |
Ray Milkey | c8f481f | 2014-11-18 15:37:12 -0800 | [diff] [blame] | 30 | |
Michele Santuari | 4a33807 | 2014-11-05 18:38:55 +0100 | [diff] [blame] | 31 | import com.google.common.collect.ImmutableSet; |
Ray Milkey | c8f481f | 2014-11-18 15:37:12 -0800 | [diff] [blame] | 32 | import com.google.common.testing.EqualsTester; |
| 33 | |
| 34 | import static org.hamcrest.MatcherAssert.assertThat; |
| 35 | import static org.hamcrest.Matchers.hasSize; |
| 36 | import static org.hamcrest.Matchers.is; |
| 37 | import static org.hamcrest.Matchers.nullValue; |
| 38 | import static org.hamcrest.Matchers.startsWith; |
| 39 | import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 40 | import static org.onosproject.net.NetTestTools.APP_ID; |
| 41 | import static org.onosproject.net.NetTestTools.link; |
Ray Milkey | c8f481f | 2014-11-18 15:37:12 -0800 | [diff] [blame] | 42 | |
| 43 | /** |
| 44 | * Unit tests for the LinkCollectionIntent class. |
| 45 | */ |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 46 | public class LinkCollectionIntentTest extends IntentTest { |
Ray Milkey | c8f481f | 2014-11-18 15:37:12 -0800 | [diff] [blame] | 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 | */ |
Ray Milkey | 37f6a38 | 2014-11-25 14:54:42 -0800 | [diff] [blame] | 63 | @Test |
Ray Milkey | c8f481f | 2014-11-18 15:37:12 -0800 | [diff] [blame] | 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); |
Ray Milkey | c8f481f | 2014-11-18 15:37:12 -0800 | [diff] [blame] | 74 | |
| 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 Milkey | 37f6a38 | 2014-11-25 14:54:42 -0800 | [diff] [blame] | 85 | .addEqualityGroup(collectionIntent1) |
Ray Milkey | c8f481f | 2014-11-18 15:37:12 -0800 | [diff] [blame] | 86 | .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 Santuari | 4a33807 | 2014-11-05 18:38:55 +0100 | [diff] [blame] | 109 | assertThat(collectionIntent.egressPoints(), is(ImmutableSet.of(egress))); |
Ray Milkey | c8f481f | 2014-11-18 15:37:12 -0800 | [diff] [blame] | 110 | 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 Santuari | 4a33807 | 2014-11-05 18:38:55 +0100 | [diff] [blame] | 138 | assertThat(collectionIntent.egressPoints(), is(ImmutableSet.of(egress))); |
Ray Milkey | c8f481f | 2014-11-18 15:37:12 -0800 | [diff] [blame] | 139 | |
| 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 Santuari | 4a33807 | 2014-11-05 18:38:55 +0100 | [diff] [blame] | 159 | assertThat(collectionIntent.egressPoints(), nullValue()); |
Ray Milkey | c8f481f | 2014-11-18 15:37:12 -0800 | [diff] [blame] | 160 | |
| 161 | final List<Constraint> createdConstraints = collectionIntent.constraints(); |
| 162 | assertThat(createdConstraints, hasSize(0)); |
| 163 | } |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 164 | |
| 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 Milkey | c8f481f | 2014-11-18 15:37:12 -0800 | [diff] [blame] | 186 | } |