Ray Milkey | c127a5a | 2014-08-20 11:22:12 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.matchaction; |
| 2 | |
Ray Milkey | 18b44ac | 2014-08-22 08:29:47 -0700 | [diff] [blame^] | 3 | import net.onrc.onos.core.util.TestUtils; |
| 4 | import org.hamcrest.Matchers; |
| 5 | import org.junit.Test; |
| 6 | |
| 7 | import java.util.Set; |
| 8 | |
Ray Milkey | c127a5a | 2014-08-20 11:22:12 -0700 | [diff] [blame] | 9 | import static org.hamcrest.MatcherAssert.assertThat; |
| 10 | import static org.hamcrest.Matchers.equalTo; |
Ray Milkey | 18b44ac | 2014-08-22 08:29:47 -0700 | [diff] [blame^] | 11 | import static org.hamcrest.Matchers.hasItem; |
| 12 | import static org.hamcrest.Matchers.hasSize; |
Ray Milkey | c127a5a | 2014-08-20 11:22:12 -0700 | [diff] [blame] | 13 | import static org.hamcrest.Matchers.is; |
Ray Milkey | 18b44ac | 2014-08-22 08:29:47 -0700 | [diff] [blame^] | 14 | import static org.hamcrest.Matchers.not; |
Ray Milkey | c127a5a | 2014-08-20 11:22:12 -0700 | [diff] [blame] | 15 | import static org.hamcrest.Matchers.notNullValue; |
| 16 | |
Ray Milkey | c127a5a | 2014-08-20 11:22:12 -0700 | [diff] [blame] | 17 | /** |
| 18 | * Unit tests for Match Action Operations. |
| 19 | */ |
| 20 | public 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 Milkey | 9ed4b96 | 2014-08-20 15:43:40 -0700 | [diff] [blame] | 29 | new MatchActionOperationsId(1L); |
Ray Milkey | c127a5a | 2014-08-20 11:22:12 -0700 | [diff] [blame] | 30 | final MatchActionOperations operations1 = |
Ray Milkey | 9ed4b96 | 2014-08-20 15:43:40 -0700 | [diff] [blame] | 31 | new MatchActionOperations(id1); |
Ray Milkey | c127a5a | 2014-08-20 11:22:12 -0700 | [diff] [blame] | 32 | assertThat(id1, is(notNullValue())); |
| 33 | assertThat(id1, is(equalTo(operations1.getOperationsId()))); |
| 34 | |
| 35 | final MatchActionOperationsId id2 = |
Ray Milkey | 9ed4b96 | 2014-08-20 15:43:40 -0700 | [diff] [blame] | 36 | new MatchActionOperationsId(2L); |
Ray Milkey | c127a5a | 2014-08-20 11:22:12 -0700 | [diff] [blame] | 37 | final MatchActionOperations operations2 = |
Ray Milkey | 9ed4b96 | 2014-08-20 15:43:40 -0700 | [diff] [blame] | 38 | new MatchActionOperations(id2); |
Ray Milkey | c127a5a | 2014-08-20 11:22:12 -0700 | [diff] [blame] | 39 | 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 Milkey | 9ed4b96 | 2014-08-20 15:43:40 -0700 | [diff] [blame] | 53 | new MatchActionOperationsId(1L); |
Ray Milkey | c127a5a | 2014-08-20 11:22:12 -0700 | [diff] [blame] | 54 | final MatchActionOperationsId id2 = |
Ray Milkey | 9ed4b96 | 2014-08-20 15:43:40 -0700 | [diff] [blame] | 55 | new MatchActionOperationsId(2L); |
Ray Milkey | c127a5a | 2014-08-20 11:22:12 -0700 | [diff] [blame] | 56 | final MatchActionOperationsId id1Copy = |
Ray Milkey | 9ed4b96 | 2014-08-20 15:43:40 -0700 | [diff] [blame] | 57 | new MatchActionOperationsId(1L); |
Ray Milkey | c127a5a | 2014-08-20 11:22:12 -0700 | [diff] [blame] | 58 | |
| 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 Milkey | 9ed4b96 | 2014-08-20 15:43:40 -0700 | [diff] [blame] | 81 | new MatchActionOperationsId(22L); |
| 82 | assertThat(id1.hashCode(), is(equalTo(22))); |
Ray Milkey | c127a5a | 2014-08-20 11:22:12 -0700 | [diff] [blame] | 83 | } |
Ray Milkey | 18b44ac | 2014-08-22 08:29:47 -0700 | [diff] [blame^] | 84 | |
| 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 Milkey | c127a5a | 2014-08-20 11:22:12 -0700 | [diff] [blame] | 116 | } |