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.List; |
| 19 | |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 20 | import org.junit.After; |
| 21 | import org.junit.Before; |
Ray Milkey | c8f481f | 2014-11-18 15:37:12 -0800 | [diff] [blame] | 22 | import org.junit.Test; |
Sho SHIMIZU | e63e51e | 2014-12-03 10:54:38 -0800 | [diff] [blame] | 23 | import org.onosproject.core.ApplicationId; |
| 24 | import org.onosproject.core.DefaultApplicationId; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 25 | import org.onosproject.core.IdGenerator; |
| 26 | import org.onosproject.net.ConnectPoint; |
| 27 | import org.onosproject.net.NetTestTools; |
| 28 | import org.onosproject.net.flow.TrafficSelector; |
Ray Milkey | c8f481f | 2014-11-18 15:37:12 -0800 | [diff] [blame] | 29 | |
| 30 | import com.google.common.testing.EqualsTester; |
| 31 | |
| 32 | import static org.hamcrest.MatcherAssert.assertThat; |
| 33 | import static org.hamcrest.Matchers.hasSize; |
| 34 | import static org.hamcrest.Matchers.is; |
| 35 | import static org.hamcrest.Matchers.isOneOf; |
| 36 | import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable; |
| 37 | |
| 38 | /** |
| 39 | * Tests for the IntentOperations class. |
| 40 | */ |
| 41 | public class IntentOperationsTest { |
| 42 | |
| 43 | final ConnectPoint egress = NetTestTools.connectPoint("egress", 3); |
| 44 | final ConnectPoint ingress = NetTestTools.connectPoint("ingress", 3); |
| 45 | final TrafficSelector selector = new IntentTestsMocks.MockSelector(); |
| 46 | final IntentTestsMocks.MockTreatment treatment = new IntentTestsMocks.MockTreatment(); |
| 47 | |
Thomas Vachuska | 02aeb03 | 2015-01-06 22:36:30 -0800 | [diff] [blame] | 48 | private final ApplicationId appId = new DefaultApplicationId(1, "IntentOperationsTest"); |
Sho SHIMIZU | e63e51e | 2014-12-03 10:54:38 -0800 | [diff] [blame] | 49 | |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 50 | private Intent intent; |
| 51 | protected IdGenerator idGenerator = new MockIdGenerator(); |
| 52 | |
| 53 | @Before |
| 54 | public void setUp() { |
| 55 | Intent.bindIdGenerator(idGenerator); |
| 56 | |
| 57 | intent = new PointToPointIntent(NetTestTools.APP_ID, |
| 58 | selector, |
| 59 | treatment, |
| 60 | ingress, |
| 61 | egress); |
| 62 | } |
| 63 | |
| 64 | @After |
| 65 | public void tearDown() { |
| 66 | Intent.unbindIdGenerator(idGenerator); |
| 67 | } |
Ray Milkey | c8f481f | 2014-11-18 15:37:12 -0800 | [diff] [blame] | 68 | |
| 69 | /** |
| 70 | * Checks that the IntentOperation and IntentOperations classes are immutable. |
| 71 | */ |
| 72 | @Test |
| 73 | public void testImmutability() { |
| 74 | assertThatClassIsImmutable(IntentOperations.class); |
| 75 | assertThatClassIsImmutable(IntentOperations.Builder.class); |
| 76 | assertThatClassIsImmutable(IntentOperation.class); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Tests equals(), hashCode() and toString() methods. |
| 81 | */ |
| 82 | @Test |
| 83 | public void testEquals() { |
| 84 | final IntentOperations operations1 = |
Sho SHIMIZU | e63e51e | 2014-12-03 10:54:38 -0800 | [diff] [blame] | 85 | IntentOperations.builder(appId) |
Ray Milkey | c8f481f | 2014-11-18 15:37:12 -0800 | [diff] [blame] | 86 | .addSubmitOperation(intent) |
| 87 | .build(); |
| 88 | final IntentOperations sameAsOperations1 = |
Sho SHIMIZU | e63e51e | 2014-12-03 10:54:38 -0800 | [diff] [blame] | 89 | IntentOperations.builder(appId) |
Ray Milkey | c8f481f | 2014-11-18 15:37:12 -0800 | [diff] [blame] | 90 | .addSubmitOperation(intent) |
| 91 | .build(); |
| 92 | final IntentOperations operations2 = |
Sho SHIMIZU | e63e51e | 2014-12-03 10:54:38 -0800 | [diff] [blame] | 93 | IntentOperations.builder(appId) |
Ray Milkey | c8f481f | 2014-11-18 15:37:12 -0800 | [diff] [blame] | 94 | .addReplaceOperation(intent.id(), intent) |
| 95 | .build(); |
| 96 | |
| 97 | new EqualsTester() |
| 98 | .addEqualityGroup(operations1, sameAsOperations1) |
| 99 | .addEqualityGroup(operations2) |
| 100 | .testEquals(); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Checks that objects are created correctly. |
| 105 | */ |
| 106 | @Test |
| 107 | public void testConstruction() { |
| 108 | final IntentOperations operations = |
Sho SHIMIZU | e63e51e | 2014-12-03 10:54:38 -0800 | [diff] [blame] | 109 | IntentOperations.builder(appId) |
Ray Milkey | c8f481f | 2014-11-18 15:37:12 -0800 | [diff] [blame] | 110 | .addUpdateOperation(intent.id()) |
| 111 | .addWithdrawOperation(intent.id()) |
| 112 | .build(); |
| 113 | final List<IntentOperation> operationList = operations.operations(); |
| 114 | assertThat(operationList, hasSize(2)); |
| 115 | for (final IntentOperation operation : operationList) { |
| 116 | assertThat(operation.type(), |
| 117 | isOneOf(IntentOperation.Type.UPDATE, |
| 118 | IntentOperation.Type.WITHDRAW)); |
| 119 | assertThat(operation.intent(), is((Intent) null)); |
| 120 | assertThat(operation.intentId(), is(intent.id())); |
| 121 | } |
| 122 | } |
| 123 | } |