PointToPointintent unit tests
Change-Id: I5852862337cc4e67ca9d8fee2ba860a2751838d4
Showing
3 changed files
with
224 additions
and
1 deletions
| ... | @@ -43,7 +43,7 @@ public class PointToPointIntentCompiler | ... | @@ -43,7 +43,7 @@ public class PointToPointIntentCompiler |
| 43 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) | 43 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 44 | protected HostService hostService; | 44 | protected HostService hostService; |
| 45 | 45 | ||
| 46 | - private IdGenerator<IntentId> intentIdGenerator; | 46 | + protected IdGenerator<IntentId> intentIdGenerator; |
| 47 | 47 | ||
| 48 | @Activate | 48 | @Activate |
| 49 | public void activate() { | 49 | public void activate() { | ... | ... |
| 1 | +package org.onlab.onos.net.intent; | ||
| 2 | + | ||
| 3 | +import org.junit.Test; | ||
| 4 | +import org.onlab.onos.net.ConnectPoint; | ||
| 5 | +import org.onlab.onos.net.flow.TrafficSelector; | ||
| 6 | +import org.onlab.onos.net.flow.TrafficTreatment; | ||
| 7 | + | ||
| 8 | +import static org.hamcrest.MatcherAssert.assertThat; | ||
| 9 | +import static org.hamcrest.Matchers.equalTo; | ||
| 10 | +import static org.hamcrest.Matchers.is; | ||
| 11 | +import static org.hamcrest.Matchers.not; | ||
| 12 | +import static org.onlab.onos.net.NetTestTools.connectPoint; | ||
| 13 | + | ||
| 14 | +/** | ||
| 15 | + * Unit tests for the HostToHostIntent class. | ||
| 16 | + */ | ||
| 17 | +public class TestPointToPointIntent { | ||
| 18 | + | ||
| 19 | + private TrafficSelector selector = new IntentTestsMocks.MockSelector(); | ||
| 20 | + private TrafficTreatment treatment = new IntentTestsMocks.MockTreatment(); | ||
| 21 | + | ||
| 22 | + private ConnectPoint point1 = connectPoint("dev1", 1); | ||
| 23 | + private ConnectPoint point2 = connectPoint("dev2", 1); | ||
| 24 | + | ||
| 25 | + private PointToPointIntent makePointToPoint(long id, | ||
| 26 | + ConnectPoint ingress, | ||
| 27 | + ConnectPoint egress) { | ||
| 28 | + return new PointToPointIntent(new IntentId(id), | ||
| 29 | + selector, | ||
| 30 | + treatment, | ||
| 31 | + ingress, | ||
| 32 | + egress); | ||
| 33 | + } | ||
| 34 | + | ||
| 35 | + /** | ||
| 36 | + * Tests the equals() method where two PointToPointIntents have references | ||
| 37 | + * to the same ingress and egress points. These should compare equal. | ||
| 38 | + */ | ||
| 39 | + @Test | ||
| 40 | + public void testSameEquals() { | ||
| 41 | + PointToPointIntent i1 = makePointToPoint(12, point1, point2); | ||
| 42 | + PointToPointIntent i2 = makePointToPoint(12, point1, point2); | ||
| 43 | + | ||
| 44 | + assertThat(i1, is(equalTo(i2))); | ||
| 45 | + } | ||
| 46 | + | ||
| 47 | + /** | ||
| 48 | + * Tests the equals() method where two HostToHostIntents have references | ||
| 49 | + * to different Hosts. These should compare not equal. | ||
| 50 | + */ | ||
| 51 | + @Test | ||
| 52 | + public void testLinksDifferentEquals() { | ||
| 53 | + | ||
| 54 | + PointToPointIntent i1 = makePointToPoint(12, point1, point2); | ||
| 55 | + PointToPointIntent i2 = makePointToPoint(12, point2, point1); | ||
| 56 | + | ||
| 57 | + assertThat(i1, is(not(equalTo(i2)))); | ||
| 58 | + } | ||
| 59 | + | ||
| 60 | + /** | ||
| 61 | + * Tests the equals() method where two HostToHostIntents have different | ||
| 62 | + * ids. These should compare not equal. | ||
| 63 | + */ | ||
| 64 | + @Test | ||
| 65 | + public void testBaseDifferentEquals() { | ||
| 66 | + PointToPointIntent i1 = makePointToPoint(12, point1, point2); | ||
| 67 | + PointToPointIntent i2 = makePointToPoint(11, point1, point2); | ||
| 68 | + | ||
| 69 | + | ||
| 70 | + assertThat(i1, is(not(equalTo(i2)))); | ||
| 71 | + } | ||
| 72 | + | ||
| 73 | + /** | ||
| 74 | + * Tests that the hashCode() values for two equivalent HostToHostIntent | ||
| 75 | + * objects are the same. | ||
| 76 | + */ | ||
| 77 | + @Test | ||
| 78 | + public void testHashCodeEquals() { | ||
| 79 | + PointToPointIntent i1 = makePointToPoint(12, point1, point2); | ||
| 80 | + PointToPointIntent i2 = makePointToPoint(12, point1, point2); | ||
| 81 | + | ||
| 82 | + assertThat(i1.hashCode(), is(equalTo(i2.hashCode()))); | ||
| 83 | + } | ||
| 84 | + | ||
| 85 | + /** | ||
| 86 | + * Tests that the hashCode() values for two distinct LinkCollectionIntent | ||
| 87 | + * objects are different. | ||
| 88 | + */ | ||
| 89 | + @Test | ||
| 90 | + public void testHashCodeDifferent() { | ||
| 91 | + PointToPointIntent i1 = makePointToPoint(12, point1, point2); | ||
| 92 | + PointToPointIntent i2 = makePointToPoint(22, point1, point2); | ||
| 93 | + | ||
| 94 | + assertThat(i1.hashCode(), is(not(equalTo(i2.hashCode())))); | ||
| 95 | + } | ||
| 96 | +} |
core/net/src/test/java/org/onlab/onos/net/intent/impl/TestPointToPointIntentCompiler.java
0 → 100644
| 1 | +package org.onlab.onos.net.intent.impl; | ||
| 2 | + | ||
| 3 | +import java.util.List; | ||
| 4 | + | ||
| 5 | +import org.hamcrest.Matchers; | ||
| 6 | +import org.junit.Test; | ||
| 7 | +import org.onlab.onos.net.flow.TrafficSelector; | ||
| 8 | +import org.onlab.onos.net.flow.TrafficTreatment; | ||
| 9 | +import org.onlab.onos.net.intent.Intent; | ||
| 10 | +import org.onlab.onos.net.intent.IntentId; | ||
| 11 | +import org.onlab.onos.net.intent.IntentTestsMocks; | ||
| 12 | +import org.onlab.onos.net.intent.PathIntent; | ||
| 13 | +import org.onlab.onos.net.intent.PointToPointIntent; | ||
| 14 | + | ||
| 15 | +import static org.hamcrest.CoreMatchers.notNullValue; | ||
| 16 | +import static org.hamcrest.MatcherAssert.assertThat; | ||
| 17 | +import static org.hamcrest.Matchers.hasSize; | ||
| 18 | +import static org.hamcrest.Matchers.is; | ||
| 19 | +import static org.onlab.onos.net.NetTestTools.connectPoint; | ||
| 20 | +import static org.onlab.onos.net.intent.LinksHaveEntryWithSourceDestinationPairMatcher.linksHasPath; | ||
| 21 | + | ||
| 22 | +/** | ||
| 23 | + * Unit tests for the HostToHost intent compiler. | ||
| 24 | + */ | ||
| 25 | +public class TestPointToPointIntentCompiler { | ||
| 26 | + | ||
| 27 | + private TrafficSelector selector = new IntentTestsMocks.MockSelector(); | ||
| 28 | + private TrafficTreatment treatment = new IntentTestsMocks.MockTreatment(); | ||
| 29 | + | ||
| 30 | + /** | ||
| 31 | + * Creates a PointToPoint intent based on ingress and egress device Ids. | ||
| 32 | + * | ||
| 33 | + * @param ingressIdString string for id of ingress device | ||
| 34 | + * @param egressIdString string for id of egress device | ||
| 35 | + * @return PointToPointIntent for the two devices | ||
| 36 | + */ | ||
| 37 | + private PointToPointIntent makeIntent(String ingressIdString, | ||
| 38 | + String egressIdString) { | ||
| 39 | + return new PointToPointIntent(new IntentId(12), | ||
| 40 | + selector, | ||
| 41 | + treatment, | ||
| 42 | + connectPoint(ingressIdString, 1), | ||
| 43 | + connectPoint(egressIdString, 1)); | ||
| 44 | + } | ||
| 45 | + | ||
| 46 | + /** | ||
| 47 | + * Creates a compiler for HostToHost intents. | ||
| 48 | + * | ||
| 49 | + * @param hops string array describing the path hops to use when compiling | ||
| 50 | + * @return HostToHost intent compiler | ||
| 51 | + */ | ||
| 52 | + private PointToPointIntentCompiler makeCompiler(String[] hops) { | ||
| 53 | + PointToPointIntentCompiler compiler = | ||
| 54 | + new PointToPointIntentCompiler(); | ||
| 55 | + compiler.pathService = new IntentTestsMocks.MockPathService(hops); | ||
| 56 | + IdBlockAllocator idBlockAllocator = new DummyIdBlockAllocator(); | ||
| 57 | + compiler.intentIdGenerator = | ||
| 58 | + new IdBlockAllocatorBasedIntentIdGenerator(idBlockAllocator); | ||
| 59 | + return compiler; | ||
| 60 | + } | ||
| 61 | + | ||
| 62 | + | ||
| 63 | + /** | ||
| 64 | + * Tests a pair of devices in an 8 hop path, forward direction. | ||
| 65 | + */ | ||
| 66 | + @Test | ||
| 67 | + public void testForwardPathCompilation() { | ||
| 68 | + | ||
| 69 | + PointToPointIntent intent = makeIntent("d1", "d8"); | ||
| 70 | + assertThat(intent, is(notNullValue())); | ||
| 71 | + | ||
| 72 | + String[] hops = {"d1", "d2", "d3", "d4", "d5", "d6", "d7", "d8"}; | ||
| 73 | + PointToPointIntentCompiler compiler = makeCompiler(hops); | ||
| 74 | + assertThat(compiler, is(notNullValue())); | ||
| 75 | + | ||
| 76 | + List<Intent> result = compiler.compile(intent); | ||
| 77 | + assertThat(result, is(Matchers.notNullValue())); | ||
| 78 | + assertThat(result, hasSize(1)); | ||
| 79 | + Intent forwardResultIntent = result.get(0); | ||
| 80 | + assertThat(forwardResultIntent instanceof PathIntent, is(true)); | ||
| 81 | + | ||
| 82 | + if (forwardResultIntent instanceof PathIntent) { | ||
| 83 | + PathIntent forwardPathIntent = (PathIntent) forwardResultIntent; | ||
| 84 | + // 7 links for the hops, plus one default lnk on ingress and egress | ||
| 85 | + assertThat(forwardPathIntent.path().links(), hasSize(hops.length + 1)); | ||
| 86 | + assertThat(forwardPathIntent.path().links(), linksHasPath("d1", "d2")); | ||
| 87 | + assertThat(forwardPathIntent.path().links(), linksHasPath("d2", "d3")); | ||
| 88 | + assertThat(forwardPathIntent.path().links(), linksHasPath("d3", "d4")); | ||
| 89 | + assertThat(forwardPathIntent.path().links(), linksHasPath("d4", "d5")); | ||
| 90 | + assertThat(forwardPathIntent.path().links(), linksHasPath("d5", "d6")); | ||
| 91 | + assertThat(forwardPathIntent.path().links(), linksHasPath("d6", "d7")); | ||
| 92 | + assertThat(forwardPathIntent.path().links(), linksHasPath("d7", "d8")); | ||
| 93 | + } | ||
| 94 | + } | ||
| 95 | + | ||
| 96 | + /** | ||
| 97 | + * Tests a pair of devices in an 8 hop path, forward direction. | ||
| 98 | + */ | ||
| 99 | + @Test | ||
| 100 | + public void testReversePathCompilation() { | ||
| 101 | + | ||
| 102 | + PointToPointIntent intent = makeIntent("d8", "d1"); | ||
| 103 | + assertThat(intent, is(notNullValue())); | ||
| 104 | + | ||
| 105 | + String[] hops = {"d1", "d2", "d3", "d4", "d5", "d6", "d7", "d8"}; | ||
| 106 | + PointToPointIntentCompiler compiler = makeCompiler(hops); | ||
| 107 | + assertThat(compiler, is(notNullValue())); | ||
| 108 | + | ||
| 109 | + List<Intent> result = compiler.compile(intent); | ||
| 110 | + assertThat(result, is(Matchers.notNullValue())); | ||
| 111 | + assertThat(result, hasSize(1)); | ||
| 112 | + Intent reverseResultIntent = result.get(0); | ||
| 113 | + assertThat(reverseResultIntent instanceof PathIntent, is(true)); | ||
| 114 | + | ||
| 115 | + if (reverseResultIntent instanceof PathIntent) { | ||
| 116 | + PathIntent reversePathIntent = (PathIntent) reverseResultIntent; | ||
| 117 | + assertThat(reversePathIntent.path().links(), hasSize(hops.length + 1)); | ||
| 118 | + assertThat(reversePathIntent.path().links(), linksHasPath("d2", "d1")); | ||
| 119 | + assertThat(reversePathIntent.path().links(), linksHasPath("d3", "d2")); | ||
| 120 | + assertThat(reversePathIntent.path().links(), linksHasPath("d4", "d3")); | ||
| 121 | + assertThat(reversePathIntent.path().links(), linksHasPath("d5", "d4")); | ||
| 122 | + assertThat(reversePathIntent.path().links(), linksHasPath("d6", "d5")); | ||
| 123 | + assertThat(reversePathIntent.path().links(), linksHasPath("d7", "d6")); | ||
| 124 | + assertThat(reversePathIntent.path().links(), linksHasPath("d8", "d7")); | ||
| 125 | + } | ||
| 126 | + } | ||
| 127 | +} |
-
Please register or login to post a comment