blob: eea5fec192d299ac50aa33057cd547533d5151a5 [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
20import org.junit.Test;
21import org.onlab.onos.net.ConnectPoint;
22import org.onlab.onos.net.NetTestTools;
23import org.onlab.onos.net.flow.TrafficSelector;
24
25import com.google.common.testing.EqualsTester;
26
27import static org.hamcrest.MatcherAssert.assertThat;
28import static org.hamcrest.Matchers.hasSize;
29import static org.hamcrest.Matchers.is;
30import static org.hamcrest.Matchers.isOneOf;
31import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
32
33/**
34 * Tests for the IntentOperations class.
35 */
36public class IntentOperationsTest {
37
38 final ConnectPoint egress = NetTestTools.connectPoint("egress", 3);
39 final ConnectPoint ingress = NetTestTools.connectPoint("ingress", 3);
40 final TrafficSelector selector = new IntentTestsMocks.MockSelector();
41 final IntentTestsMocks.MockTreatment treatment = new IntentTestsMocks.MockTreatment();
42
43 final Intent intent = new PointToPointIntent(NetTestTools.APP_ID,
44 selector,
45 treatment,
46 ingress,
47 egress);
48
49 /**
50 * Checks that the IntentOperation and IntentOperations classes are immutable.
51 */
52 @Test
53 public void testImmutability() {
54 assertThatClassIsImmutable(IntentOperations.class);
55 assertThatClassIsImmutable(IntentOperations.Builder.class);
56 assertThatClassIsImmutable(IntentOperation.class);
57 }
58
59 /**
60 * Tests equals(), hashCode() and toString() methods.
61 */
62 @Test
63 public void testEquals() {
64 final IntentOperations operations1 =
65 IntentOperations.builder()
66 .addSubmitOperation(intent)
67 .build();
68 final IntentOperations sameAsOperations1 =
69 IntentOperations.builder()
70 .addSubmitOperation(intent)
71 .build();
72 final IntentOperations operations2 =
73 IntentOperations.builder()
74 .addReplaceOperation(intent.id(), intent)
75 .build();
76
77 new EqualsTester()
78 .addEqualityGroup(operations1, sameAsOperations1)
79 .addEqualityGroup(operations2)
80 .testEquals();
81 }
82
83 /**
84 * Checks that objects are created correctly.
85 */
86 @Test
87 public void testConstruction() {
88 final IntentOperations operations =
89 IntentOperations.builder()
90 .addUpdateOperation(intent.id())
91 .addWithdrawOperation(intent.id())
92 .build();
93 final List<IntentOperation> operationList = operations.operations();
94 assertThat(operationList, hasSize(2));
95 for (final IntentOperation operation : operationList) {
96 assertThat(operation.type(),
97 isOneOf(IntentOperation.Type.UPDATE,
98 IntentOperation.Type.WITHDRAW));
99 assertThat(operation.intent(), is((Intent) null));
100 assertThat(operation.intentId(), is(intent.id()));
101 }
102 }
103}