blob: d91ead85fc446f53c8016ac869ffe23f7bd777af [file] [log] [blame]
Ray Milkey531bb232014-06-03 09:08:15 -07001package net.onrc.onos.api.rest;
2
Ray Milkeyd83844d2014-06-20 13:16:01 -07003import net.onrc.onos.core.intent.Intent;
Ray Milkey531bb232014-06-03 09:08:15 -07004import net.onrc.onos.core.intent.IntentOperation;
5import net.onrc.onos.core.intent.IntentOperationList;
6import net.onrc.onos.core.intent.ShortestPathIntent;
7import net.onrc.onos.core.intent.runtime.PathCalcRuntimeModule;
Ray Milkey531bb232014-06-03 09:08:15 -07008import org.junit.Assert;
Ray Milkey531bb232014-06-03 09:08:15 -07009import org.junit.Test;
10import org.junit.runner.RunWith;
11import org.powermock.core.classloader.annotations.PrepareForTest;
12import org.powermock.modules.junit4.PowerMockRunner;
13import org.restlet.data.Status;
14import org.restlet.resource.ClientResource;
15
16import java.util.Collection;
17import java.util.Map;
18
19import static net.onrc.onos.api.rest.ClientResourceStatusMatcher.hasStatusOf;
20import static org.hamcrest.MatcherAssert.assertThat;
21import static org.hamcrest.Matchers.hasSize;
22import 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)
29public class TestRestIntentHighDelete extends TestRestIntent {
30 private static final Long LOCAL_PORT = 0xFFFEL;
Yuta HIGUCHI91a8f502014-06-17 10:15:29 -070031 private static final String BAD_SWITCH_INTENT_NAME = "No Such Switch Intent";
Ray Milkey531bb232014-06-03 09:08:15 -070032
Ray Milkey531bb232014-06-03 09:08:15 -070033 /**
34 * Make a set of Intents that can be used as test data.
35 */
36 private void makeDefaultIntents() {
Ray Milkey531bb232014-06-03 09:08:15 -070037
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 Milkeyd83844d2014-06-20 13:16:01 -070066 @Test
Ray Milkey531bb232014-06-03 09:08:15 -070067 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 Milkeyd83844d2014-06-20 13:16:01 -070077 // 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 Milkey531bb232014-06-03 09:08:15 -070081 // 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 Milkeyd83844d2014-06-20 13:16:01 -070099 @Test
Ray Milkey531bb232014-06-03 09:08:15 -0700100 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 Milkeyd83844d2014-06-20 13:16:01 -0700111 // trigger the back end deletion of the Intent.
112 modifyIntentState("1:2___0", Intent.IntentState.DEL_ACK);
113
Ray Milkey531bb232014-06-03 09:08:15 -0700114 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}