Ray Milkey | 531bb23 | 2014-06-03 09:08:15 -0700 | [diff] [blame] | 1 | package net.onrc.onos.api.rest; |
| 2 | |
Ray Milkey | d83844d | 2014-06-20 13:16:01 -0700 | [diff] [blame] | 3 | import net.onrc.onos.core.intent.Intent; |
Ray Milkey | 531bb23 | 2014-06-03 09:08:15 -0700 | [diff] [blame] | 4 | import net.onrc.onos.core.intent.IntentOperation; |
| 5 | import net.onrc.onos.core.intent.IntentOperationList; |
| 6 | import net.onrc.onos.core.intent.ShortestPathIntent; |
| 7 | import net.onrc.onos.core.intent.runtime.PathCalcRuntimeModule; |
Ray Milkey | 531bb23 | 2014-06-03 09:08:15 -0700 | [diff] [blame] | 8 | import org.junit.Assert; |
Ray Milkey | 531bb23 | 2014-06-03 09:08:15 -0700 | [diff] [blame] | 9 | import org.junit.Test; |
| 10 | import org.junit.runner.RunWith; |
| 11 | import org.powermock.core.classloader.annotations.PrepareForTest; |
| 12 | import org.powermock.modules.junit4.PowerMockRunner; |
| 13 | import org.restlet.data.Status; |
| 14 | import org.restlet.resource.ClientResource; |
| 15 | |
| 16 | import java.util.Collection; |
| 17 | import java.util.Map; |
| 18 | |
| 19 | import static net.onrc.onos.api.rest.ClientResourceStatusMatcher.hasStatusOf; |
| 20 | import static org.hamcrest.MatcherAssert.assertThat; |
| 21 | import static org.hamcrest.Matchers.hasSize; |
| 22 | import static org.hamcrest.Matchers.notNullValue; |
| 23 | |
| 24 | /** |
| 25 | * Unit tests to test the Intents DELETE REST APIs. |
| 26 | */ |
| 27 | @RunWith(PowerMockRunner.class) |
| 28 | @PrepareForTest(PathCalcRuntimeModule.class) |
| 29 | public class TestRestIntentHighDelete extends TestRestIntent { |
| 30 | private static final Long LOCAL_PORT = 0xFFFEL; |
Yuta HIGUCHI | 91a8f50 | 2014-06-17 10:15:29 -0700 | [diff] [blame] | 31 | private static final String BAD_SWITCH_INTENT_NAME = "No Such Switch Intent"; |
Ray Milkey | 531bb23 | 2014-06-03 09:08:15 -0700 | [diff] [blame] | 32 | |
Ray Milkey | 531bb23 | 2014-06-03 09:08:15 -0700 | [diff] [blame] | 33 | /** |
| 34 | * Make a set of Intents that can be used as test data. |
| 35 | */ |
| 36 | private void makeDefaultIntents() { |
Ray Milkey | 531bb23 | 2014-06-03 09:08:15 -0700 | [diff] [blame] | 37 | |
| 38 | // create shortest path intents |
| 39 | final IntentOperationList opList = new IntentOperationList(); |
| 40 | opList.add(IntentOperation.Operator.ADD, |
| 41 | new ShortestPathIntent(BAD_SWITCH_INTENT_NAME, 111L, 12L, |
| 42 | LOCAL_PORT, 2L, 21L, LOCAL_PORT)); |
| 43 | opList.add(IntentOperation.Operator.ADD, |
| 44 | new ShortestPathIntent("1:2", 1L, 14L, LOCAL_PORT, 4L, 41L, |
| 45 | LOCAL_PORT)); |
| 46 | opList.add(IntentOperation.Operator.ADD, |
| 47 | new ShortestPathIntent("1:3", 2L, 23L, LOCAL_PORT, 3L, 32L, |
| 48 | LOCAL_PORT)); |
| 49 | |
| 50 | // compile high-level intent operations into low-level intent |
| 51 | // operations (calculate paths) |
| 52 | |
| 53 | final IntentOperationList pathIntentOpList = |
| 54 | getRuntime().executeIntentOperations(opList); |
| 55 | assertThat(pathIntentOpList, notNullValue()); |
| 56 | |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Test that the DELETE of all high level Intents REST call returns the |
| 61 | * proper result. |
| 62 | * The HTTP status of the delete call should be NO CONTENT |
| 63 | * Once the Intents are removed, an empty list should be |
| 64 | * returned by the fetch of all high level Intents. |
| 65 | */ |
Ray Milkey | d83844d | 2014-06-20 13:16:01 -0700 | [diff] [blame] | 66 | @Test |
Ray Milkey | 531bb23 | 2014-06-03 09:08:15 -0700 | [diff] [blame] | 67 | public void testDeleteOfAllIntents() { |
| 68 | |
| 69 | makeDefaultIntents(); |
| 70 | |
| 71 | final ClientResource deleteClient = new ClientResource(getHighRestIntentUrl()); |
| 72 | deleteClient.delete(); |
| 73 | |
| 74 | // HTTP status should be NO CONTENT |
| 75 | assertThat(deleteClient, hasStatusOf(Status.SUCCESS_NO_CONTENT)); |
| 76 | |
Ray Milkey | d83844d | 2014-06-20 13:16:01 -0700 | [diff] [blame] | 77 | // trigger the back end deletion of the Intents |
| 78 | modifyIntentState("1:2___0", Intent.IntentState.DEL_ACK); |
| 79 | modifyIntentState("1:3___0", Intent.IntentState.DEL_ACK); |
| 80 | |
Ray Milkey | 531bb23 | 2014-06-03 09:08:15 -0700 | [diff] [blame] | 81 | // Now query the intents to make sure they all got deleted |
| 82 | final ClientResource client = new ClientResource(getHighRestIntentUrl()); |
| 83 | final Collection<Map<String, String>> intents = getIntentsCollection(client); |
| 84 | |
| 85 | // HTTP status should be OK |
| 86 | assertThat(client, hasStatusOf(Status.SUCCESS_OK)); |
| 87 | |
| 88 | // There should be no intents left |
| 89 | assertThat(intents, hasSize(0)); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Test that the DELETE of an existing high level Intents REST call returns the |
| 94 | * proper result. |
| 95 | * The HTTP status of the delete call should be NO CONTENT |
| 96 | * Once the Intent is removed, an empty list should be |
| 97 | * returned by the fetch of the Intent. |
| 98 | */ |
Ray Milkey | d83844d | 2014-06-20 13:16:01 -0700 | [diff] [blame] | 99 | @Test |
Ray Milkey | 531bb23 | 2014-06-03 09:08:15 -0700 | [diff] [blame] | 100 | public void testDeleteOfSingleExistingIntent() { |
| 101 | |
| 102 | makeDefaultIntents(); |
| 103 | |
| 104 | final String intentUrl = getHighRestIntentUrl() + "/2"; |
| 105 | final ClientResource deleteClient = new ClientResource(intentUrl); |
| 106 | deleteClient.delete(); |
| 107 | |
| 108 | // HTTP status should be NO CONTENT |
| 109 | assertThat(deleteClient, hasStatusOf(Status.SUCCESS_NO_CONTENT)); |
| 110 | |
Ray Milkey | d83844d | 2014-06-20 13:16:01 -0700 | [diff] [blame] | 111 | // trigger the back end deletion of the Intent. |
| 112 | modifyIntentState("1:2___0", Intent.IntentState.DEL_ACK); |
| 113 | |
Ray Milkey | 531bb23 | 2014-06-03 09:08:15 -0700 | [diff] [blame] | 114 | ClientResource client = new ClientResource(intentUrl); |
| 115 | try { |
| 116 | // Now query the intent to make sure it got deleted |
| 117 | getIntent(client); |
| 118 | Assert.fail("Fetch of deleted intent did not throw an exception"); |
| 119 | } catch (Exception ex) { |
| 120 | // HTTP status should be NOT FOUND |
| 121 | assertThat(client, hasStatusOf(Status.CLIENT_ERROR_NOT_FOUND)); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Test that the DELETE of an existing high level Intents REST call returns the |
| 127 | * proper result. |
| 128 | * The HTTP status of the delete call should be NO CONTENT |
| 129 | * Once the Intent remove API is called, an empty list should be |
| 130 | * returned by the fetch of the Intent. |
| 131 | */ |
| 132 | @Test |
| 133 | public void testDeleteOfSingleNonExistingIntent() { |
| 134 | |
| 135 | makeDefaultIntents(); |
| 136 | |
| 137 | final String intentUrl = getHighRestIntentUrl() + "/2345678"; |
| 138 | final ClientResource deleteClient = new ClientResource(intentUrl); |
| 139 | deleteClient.delete(); |
| 140 | |
| 141 | // HTTP status should be NO CONTENT |
| 142 | assertThat(deleteClient, hasStatusOf(Status.SUCCESS_NO_CONTENT)); |
| 143 | |
| 144 | ClientResource client = new ClientResource(intentUrl); |
| 145 | try { |
| 146 | // Now query the intent to make sure its not there |
| 147 | getIntent(client); |
| 148 | Assert.fail("Fetch of deleted intent did not throw an exception"); |
| 149 | } catch (Exception ex) { |
| 150 | // HTTP status should be NOT FOUND |
| 151 | assertThat(client, hasStatusOf(Status.CLIENT_ERROR_NOT_FOUND)); |
| 152 | } |
| 153 | } |
| 154 | } |