Ray Milkey | 9af384f | 2014-06-24 15:11:55 -0700 | [diff] [blame] | 1 | package net.onrc.onos.api.rest; |
| 2 | |
| 3 | import net.onrc.onos.core.intent.runtime.PathCalcRuntimeModule; |
| 4 | import org.json.JSONArray; |
| 5 | import org.json.JSONObject; |
| 6 | import org.junit.After; |
| 7 | import org.junit.Before; |
| 8 | import org.junit.Test; |
| 9 | import org.junit.runner.RunWith; |
| 10 | import org.powermock.core.classloader.annotations.PrepareForTest; |
| 11 | import org.powermock.modules.junit4.PowerMockRunner; |
| 12 | import org.restlet.data.MediaType; |
| 13 | import org.restlet.data.Status; |
| 14 | import org.restlet.representation.Representation; |
| 15 | import org.restlet.representation.StringRepresentation; |
| 16 | import org.restlet.resource.ClientResource; |
| 17 | |
| 18 | import static net.onrc.onos.api.rest.ClientResourceStatusMatcher.hasStatusOf; |
| 19 | import static org.hamcrest.MatcherAssert.assertThat; |
| 20 | import static org.hamcrest.Matchers.equalTo; |
| 21 | import static org.hamcrest.Matchers.is; |
| 22 | import static org.hamcrest.Matchers.notNullValue; |
| 23 | |
| 24 | /** |
| 25 | * Unit tests to test the Intents POST REST APIs. |
| 26 | */ |
| 27 | @RunWith(PowerMockRunner.class) |
| 28 | @PrepareForTest(PathCalcRuntimeModule.class) |
| 29 | public class TestRestIntentHighPost extends TestRestIntent { |
| 30 | private static final String JSON_STRING_FOR_NEW_INTENT = |
| 31 | "[" + |
| 32 | "{" + |
| 33 | " \"dstSwitchDpid\": \"00:00:00:00:00:00:04:02\"," + |
| 34 | " \"staticPath\": true," + |
| 35 | " \"dstSwitchPort\": 1," + |
| 36 | " \"intentType\": \"SHORTEST_PATH\"," + |
| 37 | " \"matchDstMac\": \"00:00:00:02:02:02\"," + |
| 38 | " \"srcSwitchPort\": 1," + |
| 39 | " \"srcSwitchDpid\": \"00:00:00:00:00:00:02:02\"," + |
| 40 | " \"intentId\": 1," + |
| 41 | " \"matchSrcMac\": \"00:00:00:01:01:01\"" + |
| 42 | "}" + |
| 43 | "]"; |
| 44 | |
| 45 | |
| 46 | /** |
| 47 | * Create the web server, PathCalcRuntime, and mocks required for |
| 48 | * all of the tests. |
| 49 | */ |
| 50 | @Before |
| 51 | public void beforeTest() { |
| 52 | setRestPort(generateRandomPort()); |
| 53 | setUp(); |
| 54 | } |
| 55 | |
| 56 | |
| 57 | /** |
| 58 | * Remove anything that will interfere with the next test running correctly. |
| 59 | * Shuts down the test REST web server and removes the mocks. |
| 60 | */ |
| 61 | @After |
| 62 | public void afterTest() { |
| 63 | tearDown(); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Test that a POST operation to create a high level Intent |
| 68 | * creates an object correctly. |
| 69 | * The HTTP status of the POST call should be CREATED and |
| 70 | * the POST operation should return the new object. |
| 71 | * Once the Intent is created, a fetch operation should return the |
| 72 | * new Intent. |
| 73 | * |
| 74 | * @throws Exception if any of the JSON conversions fail |
| 75 | */ |
| 76 | @Test |
| 77 | public void testPostHighIntent() throws Exception { |
| 78 | |
| 79 | final ClientResource postClient = new ClientResource(getHighRestIntentUrl()); |
| 80 | final Representation postResource = |
| 81 | new StringRepresentation(JSON_STRING_FOR_NEW_INTENT, |
| 82 | MediaType.APPLICATION_JSON); |
| 83 | postClient.post(postResource); |
| 84 | |
| 85 | // HTTP status should be CREATED |
| 86 | assertThat(postClient, hasStatusOf(Status.SUCCESS_CREATED)); |
| 87 | |
| 88 | // Check that the returned entity is correct |
| 89 | final String responseString = postClient.getResponse().getEntityAsText(); |
| 90 | final JSONArray responseIntents = new JSONArray(responseString); |
| 91 | assertThat(responseIntents.length(), is(equalTo(1))); |
| 92 | |
| 93 | final JSONObject responseIntent = responseIntents.getJSONObject(0); |
| 94 | assertThat(responseIntent, notNullValue()); |
| 95 | assertThat(responseIntent.getBoolean("staticPath"), is(true)); |
| 96 | assertThat(responseIntent.getString("dstSwitchDpid"), |
| 97 | is(equalTo("00:00:00:00:00:00:04:02"))); |
| 98 | assertThat(responseIntent.getInt("dstSwitchPort"), |
| 99 | is(equalTo(1))); |
| 100 | assertThat(responseIntent.getString("intentType"), |
| 101 | is(equalTo("SHORTEST_PATH"))); |
| 102 | assertThat(responseIntent.getString("matchDstMac"), |
| 103 | is(equalTo("00:00:00:02:02:02"))); |
| 104 | assertThat(responseIntent.getInt("srcSwitchPort"), |
| 105 | is(equalTo(1))); |
| 106 | assertThat(responseIntent.getString("srcSwitchDpid"), |
| 107 | is(equalTo("00:00:00:00:00:00:02:02"))); |
| 108 | assertThat(responseIntent.getInt("intentId"), |
| 109 | is(equalTo(1))); |
| 110 | assertThat(responseIntent.getString("matchSrcMac"), |
| 111 | is(equalTo("00:00:00:01:01:01"))); |
| 112 | |
| 113 | // Now query the intent to make sure it was created properly |
| 114 | final ClientResource client = new ClientResource(getHighRestIntentUrl()); |
| 115 | final JSONArray intents = getJSONArray(client); |
| 116 | |
| 117 | // HTTP status should be OK |
| 118 | assertThat(client, hasStatusOf(Status.SUCCESS_OK)); |
| 119 | |
| 120 | // There should be 1 intent |
| 121 | assertThat(intents.length(), is(equalTo(1))); |
| 122 | |
| 123 | final JSONObject queriedIntent = intents.getJSONObject(0); |
| 124 | assertThat(queriedIntent, notNullValue()); |
| 125 | |
| 126 | // Check the attributes of the intent |
| 127 | assertThat(queriedIntent.getString("id"), |
| 128 | is(equalTo("1:1"))); |
| 129 | assertThat(queriedIntent.getString("srcPortNumber"), |
| 130 | is(equalTo("1"))); |
| 131 | assertThat(queriedIntent.getString("srcMac"), |
| 132 | is(equalTo("00:00:00:01:01:01"))); |
| 133 | assertThat(queriedIntent.getString("srcSwitchDpid"), |
| 134 | is(equalTo("00:00:00:00:00:00:02:02"))); |
| 135 | assertThat(queriedIntent.getString("dstPortNumber"), |
| 136 | is(equalTo("1"))); |
| 137 | assertThat(queriedIntent.getString("dstMac"), |
| 138 | is(equalTo("00:00:00:02:02:02"))); |
| 139 | assertThat(queriedIntent.getString("dstSwitchDpid"), |
| 140 | is(equalTo("00:00:00:00:00:00:04:02"))); |
| 141 | } |
| 142 | } |