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