blob: d677f46eba704b6dfac6211e7d3da9daf2501886 [file] [log] [blame]
Ray Milkeyc127a5a2014-08-20 11:22:12 -07001package net.onrc.onos.core.matchaction;
2
Ray Milkey18b44ac2014-08-22 08:29:47 -07003import net.onrc.onos.core.util.TestUtils;
4import org.hamcrest.Matchers;
5import org.junit.Test;
6
7import java.util.Set;
8
Ray Milkeyc127a5a2014-08-20 11:22:12 -07009import static org.hamcrest.MatcherAssert.assertThat;
10import static org.hamcrest.Matchers.equalTo;
Ray Milkey18b44ac2014-08-22 08:29:47 -070011import static org.hamcrest.Matchers.hasItem;
12import static org.hamcrest.Matchers.hasSize;
Ray Milkeyc127a5a2014-08-20 11:22:12 -070013import static org.hamcrest.Matchers.is;
Ray Milkey18b44ac2014-08-22 08:29:47 -070014import static org.hamcrest.Matchers.not;
Ray Milkeyc127a5a2014-08-20 11:22:12 -070015import static org.hamcrest.Matchers.notNullValue;
16
Ray Milkeyc127a5a2014-08-20 11:22:12 -070017/**
18 * Unit tests for Match Action Operations.
19 */
20public class MatchActionOperationsTest {
21
22 /**
23 * Test that creation of MatchActionOperations objects is correct and that
24 * the objects have unique identifiers.
25 */
26 @Test
27 public void testMatchActionoperationsCreate() {
28 final MatchActionOperationsId id1 =
Ray Milkey9ed4b962014-08-20 15:43:40 -070029 new MatchActionOperationsId(1L);
Ray Milkeyc127a5a2014-08-20 11:22:12 -070030 final MatchActionOperations operations1 =
Ray Milkey9ed4b962014-08-20 15:43:40 -070031 new MatchActionOperations(id1);
Ray Milkeyc127a5a2014-08-20 11:22:12 -070032 assertThat(id1, is(notNullValue()));
33 assertThat(id1, is(equalTo(operations1.getOperationsId())));
34
35 final MatchActionOperationsId id2 =
Ray Milkey9ed4b962014-08-20 15:43:40 -070036 new MatchActionOperationsId(2L);
Ray Milkeyc127a5a2014-08-20 11:22:12 -070037 final MatchActionOperations operations2 =
Ray Milkey9ed4b962014-08-20 15:43:40 -070038 new MatchActionOperations(id2);
Ray Milkeyc127a5a2014-08-20 11:22:12 -070039 assertThat(id2, is(notNullValue()));
40 assertThat(id2, is(equalTo(operations2.getOperationsId())));
41
42 assertThat(id1.getId(), is(not(equalTo(id2.getId()))));
43 }
44
45 /**
46 * Test the correctness of the equals() operation for
47 * MatchActionOperationsId objects.
48 * objects.
49 */
50 @Test
51 public void testMatchActionOperationsIdEquals() {
52 final MatchActionOperationsId id1 =
Ray Milkey9ed4b962014-08-20 15:43:40 -070053 new MatchActionOperationsId(1L);
Ray Milkeyc127a5a2014-08-20 11:22:12 -070054 final MatchActionOperationsId id2 =
Ray Milkey9ed4b962014-08-20 15:43:40 -070055 new MatchActionOperationsId(2L);
Ray Milkeyc127a5a2014-08-20 11:22:12 -070056 final MatchActionOperationsId id1Copy =
Ray Milkey9ed4b962014-08-20 15:43:40 -070057 new MatchActionOperationsId(1L);
Ray Milkeyc127a5a2014-08-20 11:22:12 -070058
59
60 // Check that null does not match
61 assertThat(id1, is(not(equalTo(null))));
62
63 // Check that different objects do not match
64 assertThat(id1, is(not(equalTo(id2))));
65
66 // Check that copies match
67 TestUtils.setField(id1Copy, "id", id1.getId());
68 assertThat(id1, is(equalTo(id1Copy)));
69
70 // Check that the same object matches
71 assertThat(id1, is(equalTo(id1Copy)));
72 }
73
74 /**
75 * Test the correctness of the hashCode() operation for
76 * MatchActionOperationsId objects.
77 */
78 @Test
79 public void testMatchActionOperationsIdHashCode() {
80 final MatchActionOperationsId id1 =
Ray Milkey9ed4b962014-08-20 15:43:40 -070081 new MatchActionOperationsId(22L);
82 assertThat(id1.hashCode(), is(equalTo(22)));
Ray Milkeyc127a5a2014-08-20 11:22:12 -070083 }
Ray Milkey18b44ac2014-08-22 08:29:47 -070084
85 /**
86 * Test that dependencies can be added to operations.
87 */
88 @Test
89 public void testMatchActionOperationsDependencies() {
90 final MatchActionOperationsId id =
91 new MatchActionOperationsId(12345678L);
92 final MatchActionOperations operations =
93 new MatchActionOperations(id);
94
95 assertThat(operations.getDependencies(), hasSize(0));
96
97 operations.addDependency(new MatchActionOperationsId(1L));
98 assertThat(operations.getDependencies(), hasSize(1));
99
100 operations.addDependency(new MatchActionOperationsId(2L));
101 assertThat(operations.getDependencies(), hasSize(2));
102
103 operations.addDependency(new MatchActionOperationsId(3L));
104
105 final Set<MatchActionOperationsId> operationEntries =
106 operations.getDependencies();
107 assertThat(operationEntries, hasSize(3));
108 final long[] expectedIds = {1, 2, 3};
109
110 for (long expectedId : expectedIds) {
111 assertThat(operationEntries,
112 hasItem(Matchers.<MatchActionOperationsId>hasProperty("id",
113 equalTo(expectedId))));
114 }
115 }
Ray Milkeyc127a5a2014-08-20 11:22:12 -0700116}