blob: 2c31102d42db717fbb1f120d99d2e190f4532efa [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.List;
19
Brian O'Connor520c0522014-11-23 23:50:47 -080020import org.junit.After;
21import org.junit.Before;
Ray Milkeyc8f481f2014-11-18 15:37:12 -080022import org.junit.Test;
Brian O'Connor520c0522014-11-23 23:50:47 -080023import org.onlab.onos.core.IdGenerator;
Ray Milkeyc8f481f2014-11-18 15:37:12 -080024import org.onlab.onos.net.ConnectPoint;
25import org.onlab.onos.net.NetTestTools;
26import org.onlab.onos.net.flow.TrafficSelector;
27
28import com.google.common.testing.EqualsTester;
29
30import static org.hamcrest.MatcherAssert.assertThat;
31import static org.hamcrest.Matchers.hasSize;
32import static org.hamcrest.Matchers.is;
33import static org.hamcrest.Matchers.isOneOf;
34import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
35
36/**
37 * Tests for the IntentOperations class.
38 */
39public 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'Connor520c0522014-11-23 23:50:47 -080046 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 Milkeyc8f481f2014-11-18 15:37:12 -080064
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'Connor72a034c2014-11-26 18:24:23 -080081 IntentOperations.builder(null) //FIXME null
Ray Milkeyc8f481f2014-11-18 15:37:12 -080082 .addSubmitOperation(intent)
83 .build();
84 final IntentOperations sameAsOperations1 =
Brian O'Connor72a034c2014-11-26 18:24:23 -080085 IntentOperations.builder(null) //FIXME null
Ray Milkeyc8f481f2014-11-18 15:37:12 -080086 .addSubmitOperation(intent)
87 .build();
88 final IntentOperations operations2 =
Brian O'Connor72a034c2014-11-26 18:24:23 -080089 IntentOperations.builder(null) //FIXME null
Ray Milkeyc8f481f2014-11-18 15:37:12 -080090 .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'Connor72a034c2014-11-26 18:24:23 -0800105 IntentOperations.builder(null) //FIXME
Ray Milkeyc8f481f2014-11-18 15:37:12 -0800106 .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}