blob: dd83194323be56f385b45a6c9e4e82ac1f614859 [file] [log] [blame]
Sho SHIMIZU7226e012014-08-14 14:21:30 -07001package net.onrc.onos.api.newintent;
2
3import net.onrc.onos.core.matchaction.action.Action;
4import org.junit.Test;
5
6import java.util.Arrays;
7import java.util.HashSet;
8import java.util.Set;
9
10import static org.junit.Assert.assertEquals;
11import static org.junit.Assert.assertFalse;
12import static org.junit.Assert.assertTrue;
13
14/**
15 * Base facilities to test various intent tests.
16 */
17public abstract class IntentTest {
18 /**
19 * Produces a set of items from the supplied items.
20 *
21 * @param items items to be placed in set
22 * @param <T> item type
23 * @return set of items
24 */
25 protected static <T> Set<T> itemSet(T[] items) {
26 return new HashSet<>(Arrays.asList(items));
27 }
28
29 @Test
30 public void equalsAndHashCode() {
31 Intent one = createOne();
32 Intent like = createOne();
33 Intent another = createAnother();
34
35 assertTrue("should be equal", one.equals(like));
36 assertEquals("incorrect hashCode", one.hashCode(), like.hashCode());
37
38 assertFalse("should not be equal", one.equals(another));
39
40 assertFalse("should not be equal", one.equals(null));
41 assertFalse("should not be equal", one.equals("foo"));
42 }
43
44 @Test
45 public void testToString() {
46 Intent one = createOne();
47 Intent like = createOne();
48 assertEquals("incorrect toString", one.toString(), like.toString());
49 }
50
51 /**
52 * Creates a new intent, but always a like intent, i.e. all instances will
53 * be equal, but should not be the same.
54 *
55 * @return intent
56 */
57 protected abstract Intent createOne();
58
59 /**
60 * Creates another intent, not equals to the one created by
61 * {@link #createOne()} and with a different hash code.
62 *
63 * @return another intent
64 */
65 protected abstract Intent createAnother();
66
67 // TODO: move to the match-action related package
68 protected static class NoAction implements Action {
69 }
70}