blob: 484a44ab95b14b52dab34895eb04d4e16a4619f4 [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 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.net.intent;
Ray Milkeyc8f481f2014-11-18 15:37:12 -080017
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;
Sho SHIMIZUe63e51e2014-12-03 10:54:38 -080023import org.onosproject.core.ApplicationId;
24import org.onosproject.core.DefaultApplicationId;
Brian O'Connorabafb502014-12-02 22:26:20 -080025import org.onosproject.core.IdGenerator;
26import org.onosproject.net.ConnectPoint;
27import org.onosproject.net.NetTestTools;
28import org.onosproject.net.flow.TrafficSelector;
Ray Milkeyc8f481f2014-11-18 15:37:12 -080029
30import com.google.common.testing.EqualsTester;
31
32import static org.hamcrest.MatcherAssert.assertThat;
33import static org.hamcrest.Matchers.hasSize;
34import static org.hamcrest.Matchers.is;
35import static org.hamcrest.Matchers.isOneOf;
36import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
37
38/**
39 * Tests for the IntentOperations class.
40 */
41public 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 Vachuska02aeb032015-01-06 22:36:30 -080048 private final ApplicationId appId = new DefaultApplicationId(1, "IntentOperationsTest");
Sho SHIMIZUe63e51e2014-12-03 10:54:38 -080049
Brian O'Connor520c0522014-11-23 23:50:47 -080050 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 Milkeyc8f481f2014-11-18 15:37:12 -080068
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 SHIMIZUe63e51e2014-12-03 10:54:38 -080085 IntentOperations.builder(appId)
Ray Milkeyc8f481f2014-11-18 15:37:12 -080086 .addSubmitOperation(intent)
87 .build();
88 final IntentOperations sameAsOperations1 =
Sho SHIMIZUe63e51e2014-12-03 10:54:38 -080089 IntentOperations.builder(appId)
Ray Milkeyc8f481f2014-11-18 15:37:12 -080090 .addSubmitOperation(intent)
91 .build();
92 final IntentOperations operations2 =
Sho SHIMIZUe63e51e2014-12-03 10:54:38 -080093 IntentOperations.builder(appId)
Ray Milkeyc8f481f2014-11-18 15:37:12 -080094 .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 SHIMIZUe63e51e2014-12-03 10:54:38 -0800109 IntentOperations.builder(appId)
Ray Milkeyc8f481f2014-11-18 15:37:12 -0800110 .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}