Ray Milkey | e827423 | 2014-05-27 16:03:12 -0700 | [diff] [blame] | 1 | package net.onrc.onos.api.rest; |
| 2 | |
| 3 | |
Ray Milkey | 2fa6ca4 | 2014-06-13 15:38:20 -0700 | [diff] [blame] | 4 | import com.google.common.net.InetAddresses; |
Ray Milkey | e827423 | 2014-05-27 16:03:12 -0700 | [diff] [blame] | 5 | import net.onrc.onos.core.intent.IntentOperation; |
| 6 | import net.onrc.onos.core.intent.IntentOperationList; |
| 7 | import net.onrc.onos.core.intent.ShortestPathIntent; |
Ray Milkey | e827423 | 2014-05-27 16:03:12 -0700 | [diff] [blame] | 8 | import net.onrc.onos.core.intent.runtime.PathCalcRuntimeModule; |
Ray Milkey | e827423 | 2014-05-27 16:03:12 -0700 | [diff] [blame] | 9 | import org.junit.After; |
| 10 | import org.junit.Assert; |
| 11 | import org.junit.Before; |
| 12 | import org.junit.Test; |
| 13 | import org.junit.runner.RunWith; |
| 14 | import org.powermock.core.classloader.annotations.PrepareForTest; |
| 15 | import org.powermock.modules.junit4.PowerMockRunner; |
| 16 | import org.restlet.data.Status; |
| 17 | import org.restlet.resource.ClientResource; |
| 18 | import org.restlet.resource.ResourceException; |
| 19 | |
| 20 | import java.util.Collection; |
Ray Milkey | e827423 | 2014-05-27 16:03:12 -0700 | [diff] [blame] | 21 | import java.util.Map; |
| 22 | |
Ray Milkey | 531bb23 | 2014-06-03 09:08:15 -0700 | [diff] [blame] | 23 | import static net.onrc.onos.api.rest.ClientResourceStatusMatcher.hasStatusOf; |
Ray Milkey | e827423 | 2014-05-27 16:03:12 -0700 | [diff] [blame] | 24 | import static org.hamcrest.MatcherAssert.assertThat; |
| 25 | import static org.hamcrest.Matchers.equalTo; |
| 26 | import static org.hamcrest.Matchers.hasKey; |
| 27 | import static org.hamcrest.Matchers.hasSize; |
| 28 | import static org.hamcrest.Matchers.is; |
| 29 | import static org.hamcrest.Matchers.notNullValue; |
| 30 | |
| 31 | /** |
Ray Milkey | 531bb23 | 2014-06-03 09:08:15 -0700 | [diff] [blame] | 32 | * Unit tests to test the Intents GET REST APIs. |
Ray Milkey | e827423 | 2014-05-27 16:03:12 -0700 | [diff] [blame] | 33 | */ |
| 34 | @RunWith(PowerMockRunner.class) |
| 35 | @PrepareForTest(PathCalcRuntimeModule.class) |
Ray Milkey | 531bb23 | 2014-06-03 09:08:15 -0700 | [diff] [blame] | 36 | public class TestRestIntentHighGet extends TestRestIntent { |
Ray Milkey | e827423 | 2014-05-27 16:03:12 -0700 | [diff] [blame] | 37 | private static final Long LOCAL_PORT = 0xFFFEL; |
Ray Milkey | 531bb23 | 2014-06-03 09:08:15 -0700 | [diff] [blame] | 38 | private static final String BAD_SWITCH_INTENT_NAME = "No Such Switch Intent"; |
Ray Milkey | 2fa6ca4 | 2014-06-13 15:38:20 -0700 | [diff] [blame] | 39 | private static final String IP_ADDRESS_1 = "127.0.0.1"; |
| 40 | private static final String IP_ADDRESS_2 = "127.0.0.2"; |
| 41 | private static final String IP_ADDRESS_3 = "127.0.0.3"; |
Ray Milkey | e827423 | 2014-05-27 16:03:12 -0700 | [diff] [blame] | 42 | |
| 43 | /** |
| 44 | * Create the web server, PathCalcRuntime, and mocks required for |
| 45 | * all of the tests. |
Ray Milkey | e827423 | 2014-05-27 16:03:12 -0700 | [diff] [blame] | 46 | */ |
| 47 | @Before |
Ray Milkey | 531bb23 | 2014-06-03 09:08:15 -0700 | [diff] [blame] | 48 | public void beforeTest() { |
| 49 | setRestPort(generateRandomPort()); |
| 50 | setUp(); |
Ray Milkey | e827423 | 2014-05-27 16:03:12 -0700 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | |
| 54 | /** |
| 55 | * Remove anything that will interfere with the next test running correctly. |
| 56 | * Shuts down the test REST web server and removes the mocks. |
Ray Milkey | e827423 | 2014-05-27 16:03:12 -0700 | [diff] [blame] | 57 | */ |
| 58 | @After |
Ray Milkey | 531bb23 | 2014-06-03 09:08:15 -0700 | [diff] [blame] | 59 | public void afterTest() { |
| 60 | tearDown(); |
Ray Milkey | e827423 | 2014-05-27 16:03:12 -0700 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | |
| 64 | /** |
| 65 | * Make a set of Intents that can be used as test data. |
| 66 | */ |
| 67 | private void makeDefaultIntents() { |
Ray Milkey | 2fa6ca4 | 2014-06-13 15:38:20 -0700 | [diff] [blame] | 68 | final int ipAddress1AsInt = InetAddresses.coerceToInteger( |
| 69 | InetAddresses.forString(IP_ADDRESS_1)); |
| 70 | final int ipAddress2AsInt = InetAddresses.coerceToInteger( |
| 71 | InetAddresses.forString(IP_ADDRESS_2)); |
| 72 | final int ipAddress3AsInt = InetAddresses.coerceToInteger( |
| 73 | InetAddresses.forString(IP_ADDRESS_3)); |
| 74 | |
Ray Milkey | e827423 | 2014-05-27 16:03:12 -0700 | [diff] [blame] | 75 | // create shortest path intents |
| 76 | final IntentOperationList opList = new IntentOperationList(); |
| 77 | opList.add(IntentOperation.Operator.ADD, |
| 78 | new ShortestPathIntent(BAD_SWITCH_INTENT_NAME, 111L, 12L, |
| 79 | LOCAL_PORT, 2L, 21L, LOCAL_PORT)); |
| 80 | opList.add(IntentOperation.Operator.ADD, |
Ray Milkey | 2fa6ca4 | 2014-06-13 15:38:20 -0700 | [diff] [blame] | 81 | new ShortestPathIntent("1:2", 1L, 14L, LOCAL_PORT, ipAddress1AsInt, |
| 82 | 4L, 41L, LOCAL_PORT, ipAddress2AsInt)); |
Ray Milkey | e827423 | 2014-05-27 16:03:12 -0700 | [diff] [blame] | 83 | opList.add(IntentOperation.Operator.ADD, |
Ray Milkey | 2fa6ca4 | 2014-06-13 15:38:20 -0700 | [diff] [blame] | 84 | new ShortestPathIntent("1:3", 2L, 23L, LOCAL_PORT, ipAddress2AsInt, |
| 85 | 3L, 32L, LOCAL_PORT, ipAddress3AsInt)); |
Ray Milkey | e827423 | 2014-05-27 16:03:12 -0700 | [diff] [blame] | 86 | |
| 87 | // compile high-level intent operations into low-level intent |
| 88 | // operations (calculate paths) |
| 89 | |
| 90 | final IntentOperationList pathIntentOpList = |
Ray Milkey | 531bb23 | 2014-06-03 09:08:15 -0700 | [diff] [blame] | 91 | getRuntime().executeIntentOperations(opList); |
Ray Milkey | e827423 | 2014-05-27 16:03:12 -0700 | [diff] [blame] | 92 | assertThat(pathIntentOpList, notNullValue()); |
| 93 | |
| 94 | } |
| 95 | |
| 96 | /** |
Ray Milkey | e827423 | 2014-05-27 16:03:12 -0700 | [diff] [blame] | 97 | * Test that the GET of all Intents REST call returns the proper result. |
| 98 | * The call to get all Intents should return 3 items, an HTTP status of OK, |
| 99 | * and the proper Intent data. |
Ray Milkey | e827423 | 2014-05-27 16:03:12 -0700 | [diff] [blame] | 100 | */ |
| 101 | @Test |
Ray Milkey | 531bb23 | 2014-06-03 09:08:15 -0700 | [diff] [blame] | 102 | public void testFetchOfAllIntents() { |
Ray Milkey | e827423 | 2014-05-27 16:03:12 -0700 | [diff] [blame] | 103 | |
| 104 | makeDefaultIntents(); |
| 105 | |
Ray Milkey | 531bb23 | 2014-06-03 09:08:15 -0700 | [diff] [blame] | 106 | final ClientResource client = new ClientResource(getHighRestIntentUrl()); |
Ray Milkey | e827423 | 2014-05-27 16:03:12 -0700 | [diff] [blame] | 107 | final Collection<Map<String, String>> intents = getIntentsCollection(client); |
| 108 | |
| 109 | // HTTP status should be OK |
Ray Milkey | 531bb23 | 2014-06-03 09:08:15 -0700 | [diff] [blame] | 110 | assertThat(client, hasStatusOf(Status.SUCCESS_OK)); |
Ray Milkey | e827423 | 2014-05-27 16:03:12 -0700 | [diff] [blame] | 111 | |
| 112 | // 3 intents should have been fetched |
| 113 | assertThat(intents, hasSize(3)); |
| 114 | |
| 115 | // check that the Intent with id "3" is present, and has the right data |
| 116 | final Map<String, String> mapForIntent3 = findIntentWithId(intents, "1:3"); |
| 117 | // Intent 3 must exist |
| 118 | assertThat(mapForIntent3, notNullValue()); |
| 119 | // Data must be correct |
| 120 | assertThat(mapForIntent3, hasKey("state")); |
| 121 | final String state = mapForIntent3.get("state"); |
| 122 | assertThat(state, is(equalTo("INST_REQ"))); |
Ray Milkey | 531bb23 | 2014-06-03 09:08:15 -0700 | [diff] [blame] | 123 | |
| 124 | // check that the Intent with the bad switch ID is present, and has the right data |
| 125 | final Map<String, String> mapForIntentBadIntent = |
| 126 | findIntentWithId(intents, BAD_SWITCH_INTENT_NAME); |
| 127 | // Intent must exist |
| 128 | assertThat(mapForIntentBadIntent, notNullValue()); |
| 129 | // Data must be correct |
| 130 | assertThat(mapForIntentBadIntent, hasKey("state")); |
| 131 | final String badIntentState = mapForIntentBadIntent.get("state"); |
| 132 | assertThat(badIntentState, is(equalTo("INST_NACK"))); |
| 133 | |
Ray Milkey | e827423 | 2014-05-27 16:03:12 -0700 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Test that the GET of a single Intent REST call returns the proper result |
| 138 | * when given a bad Intent id. The call to get the Intent should return a |
| 139 | * status of NOT_FOUND. |
Ray Milkey | e827423 | 2014-05-27 16:03:12 -0700 | [diff] [blame] | 140 | */ |
| 141 | @Test |
Ray Milkey | 531bb23 | 2014-06-03 09:08:15 -0700 | [diff] [blame] | 142 | public void testFetchOfBadIntent() { |
Ray Milkey | e827423 | 2014-05-27 16:03:12 -0700 | [diff] [blame] | 143 | |
| 144 | makeDefaultIntents(); |
| 145 | |
Ray Milkey | 531bb23 | 2014-06-03 09:08:15 -0700 | [diff] [blame] | 146 | final ClientResource client = new ClientResource(getHighRestIntentUrl() + "/2334"); |
Ray Milkey | e827423 | 2014-05-27 16:03:12 -0700 | [diff] [blame] | 147 | |
| 148 | try { |
| 149 | getIntent(client); |
| 150 | // The get operation should have thrown a ResourceException. |
| 151 | // Fail because the Exception was not seen. |
| 152 | Assert.fail("Invalid intent fetch did not cause an exception"); |
| 153 | } catch (ResourceException resourceError) { |
| 154 | // The HTTP status should be NOT FOUND |
Ray Milkey | 531bb23 | 2014-06-03 09:08:15 -0700 | [diff] [blame] | 155 | assertThat(client, hasStatusOf(Status.CLIENT_ERROR_NOT_FOUND)); |
Ray Milkey | e827423 | 2014-05-27 16:03:12 -0700 | [diff] [blame] | 156 | } |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Test that the GET of a single Intent REST call returns the proper result |
| 161 | * for an existing Intent. The call to get the Intent should return a |
| 162 | * status of OK, and the data for the Intent should be correct. |
Ray Milkey | e827423 | 2014-05-27 16:03:12 -0700 | [diff] [blame] | 163 | */ |
| 164 | @Test |
Ray Milkey | 531bb23 | 2014-06-03 09:08:15 -0700 | [diff] [blame] | 165 | public void testFetchOfGoodIntent() { |
Ray Milkey | e827423 | 2014-05-27 16:03:12 -0700 | [diff] [blame] | 166 | |
| 167 | makeDefaultIntents(); |
| 168 | |
Ray Milkey | 531bb23 | 2014-06-03 09:08:15 -0700 | [diff] [blame] | 169 | final ClientResource client = new ClientResource(getHighRestIntentUrl() + "/2"); |
Ray Milkey | e827423 | 2014-05-27 16:03:12 -0700 | [diff] [blame] | 170 | final Map<String, String> intent; |
| 171 | intent = getIntent(client); |
| 172 | |
| 173 | // HTTP status should be OK |
Ray Milkey | 531bb23 | 2014-06-03 09:08:15 -0700 | [diff] [blame] | 174 | assertThat(client, hasStatusOf(Status.SUCCESS_OK)); |
Ray Milkey | e827423 | 2014-05-27 16:03:12 -0700 | [diff] [blame] | 175 | |
| 176 | // Intent data should be correct |
| 177 | assertThat(intent, is(notNullValue())); |
| 178 | assertThat(intent, hasKey("id")); |
| 179 | assertThat(intent.get("id"), is(equalTo("1:2"))); |
| 180 | assertThat(intent, hasKey("state")); |
| 181 | assertThat(intent.get("state"), is(equalTo("INST_REQ"))); |
Ray Milkey | 2fa6ca4 | 2014-06-13 15:38:20 -0700 | [diff] [blame] | 182 | |
| 183 | assertThat(intent, hasKey("srcSwitchDpid")); |
| 184 | assertThat(intent.get("srcSwitchDpid"), is(equalTo("00:00:00:00:00:00:00:01"))); |
| 185 | assertThat(intent, hasKey("srcPortNumber")); |
| 186 | assertThat(intent.get("srcPortNumber"), is(equalTo("14"))); |
| 187 | assertThat(intent, hasKey("srcMac")); |
Pavlin Radoslavov | dc59c15 | 2014-06-17 17:04:41 -0700 | [diff] [blame] | 188 | assertThat(intent.get("srcMac"), is(equalTo("00:00:00:00:ff:fe"))); |
Ray Milkey | 2fa6ca4 | 2014-06-13 15:38:20 -0700 | [diff] [blame] | 189 | assertThat(intent, hasKey("srcMac")); |
| 190 | assertThat(intent.get("srcIp"), is(equalTo(IP_ADDRESS_1))); |
| 191 | |
| 192 | assertThat(intent, hasKey("dstSwitchDpid")); |
| 193 | assertThat(intent.get("dstSwitchDpid"), is(equalTo("00:00:00:00:00:00:00:04"))); |
| 194 | assertThat(intent, hasKey("dstPortNumber")); |
| 195 | assertThat(intent.get("dstPortNumber"), is(equalTo("41"))); |
| 196 | assertThat(intent, hasKey("dstMac")); |
Pavlin Radoslavov | dc59c15 | 2014-06-17 17:04:41 -0700 | [diff] [blame] | 197 | assertThat(intent.get("dstMac"), is(equalTo("00:00:00:00:ff:fe"))); |
Ray Milkey | 2fa6ca4 | 2014-06-13 15:38:20 -0700 | [diff] [blame] | 198 | assertThat(intent, hasKey("dstMac")); |
| 199 | assertThat(intent.get("dstIp"), is(equalTo(IP_ADDRESS_2))); |
Ray Milkey | e827423 | 2014-05-27 16:03:12 -0700 | [diff] [blame] | 200 | } |
| 201 | } |