blob: 89ab77100bfcac362c6519f0b8fc1e166d16acac [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;
8import org.junit.After;
9import org.junit.Assert;
10import org.junit.Before;
11import org.junit.Test;
12import org.junit.runner.RunWith;
13import org.powermock.core.classloader.annotations.PrepareForTest;
14import org.powermock.modules.junit4.PowerMockRunner;
15import org.restlet.data.Status;
16import org.restlet.resource.ClientResource;
17
18import java.util.Collection;
19import java.util.Map;
20
21import static net.onrc.onos.api.rest.ClientResourceStatusMatcher.hasStatusOf;
22import static org.hamcrest.MatcherAssert.assertThat;
23import static org.hamcrest.Matchers.hasSize;
24import static org.hamcrest.Matchers.notNullValue;
25
26/**
27 * Unit tests to test the Intents DELETE REST APIs.
28 */
29@RunWith(PowerMockRunner.class)
30@PrepareForTest(PathCalcRuntimeModule.class)
31public class TestRestIntentHighDelete extends TestRestIntent {
32 private static final Long LOCAL_PORT = 0xFFFEL;
Yuta HIGUCHI91a8f502014-06-17 10:15:29 -070033 private static final String BAD_SWITCH_INTENT_NAME = "No Such Switch Intent";
Ray Milkey531bb232014-06-03 09:08:15 -070034
35
36 /**
37 * Create the web server, PathCalcRuntime, and mocks required for
38 * all of the tests.
39 */
40 @Before
41 public void beforeTest() {
42 setRestPort(generateRandomPort());
43 setUp();
44 }
45
46
47 /**
48 * Remove anything that will interfere with the next test running correctly.
49 * Shuts down the test REST web server and removes the mocks.
50 */
51 @After
52 public void afterTest() {
53 tearDown();
54 }
55
56
57 /**
58 * Make a set of Intents that can be used as test data.
59 */
60 private void makeDefaultIntents() {
Ray Milkey531bb232014-06-03 09:08:15 -070061
62 // create shortest path intents
63 final IntentOperationList opList = new IntentOperationList();
64 opList.add(IntentOperation.Operator.ADD,
65 new ShortestPathIntent(BAD_SWITCH_INTENT_NAME, 111L, 12L,
66 LOCAL_PORT, 2L, 21L, LOCAL_PORT));
67 opList.add(IntentOperation.Operator.ADD,
68 new ShortestPathIntent("1:2", 1L, 14L, LOCAL_PORT, 4L, 41L,
69 LOCAL_PORT));
70 opList.add(IntentOperation.Operator.ADD,
71 new ShortestPathIntent("1:3", 2L, 23L, LOCAL_PORT, 3L, 32L,
72 LOCAL_PORT));
73
74 // compile high-level intent operations into low-level intent
75 // operations (calculate paths)
76
77 final IntentOperationList pathIntentOpList =
78 getRuntime().executeIntentOperations(opList);
79 assertThat(pathIntentOpList, notNullValue());
80
81 }
82
83 /**
84 * Test that the DELETE of all high level Intents REST call returns the
85 * proper result.
86 * The HTTP status of the delete call should be NO CONTENT
87 * Once the Intents are removed, an empty list should be
88 * returned by the fetch of all high level Intents.
89 */
Ray Milkeyd83844d2014-06-20 13:16:01 -070090 @Test
Ray Milkey531bb232014-06-03 09:08:15 -070091 public void testDeleteOfAllIntents() {
92
93 makeDefaultIntents();
94
95 final ClientResource deleteClient = new ClientResource(getHighRestIntentUrl());
96 deleteClient.delete();
97
98 // HTTP status should be NO CONTENT
99 assertThat(deleteClient, hasStatusOf(Status.SUCCESS_NO_CONTENT));
100
Ray Milkeyd83844d2014-06-20 13:16:01 -0700101 // trigger the back end deletion of the Intents
102 modifyIntentState("1:2___0", Intent.IntentState.DEL_ACK);
103 modifyIntentState("1:3___0", Intent.IntentState.DEL_ACK);
104
Ray Milkey531bb232014-06-03 09:08:15 -0700105 // Now query the intents to make sure they all got deleted
106 final ClientResource client = new ClientResource(getHighRestIntentUrl());
107 final Collection<Map<String, String>> intents = getIntentsCollection(client);
108
109 // HTTP status should be OK
110 assertThat(client, hasStatusOf(Status.SUCCESS_OK));
111
112 // There should be no intents left
113 assertThat(intents, hasSize(0));
114 }
115
116 /**
117 * Test that the DELETE of an existing high level Intents REST call returns the
118 * proper result.
119 * The HTTP status of the delete call should be NO CONTENT
120 * Once the Intent is removed, an empty list should be
121 * returned by the fetch of the Intent.
122 */
Ray Milkeyd83844d2014-06-20 13:16:01 -0700123 @Test
Ray Milkey531bb232014-06-03 09:08:15 -0700124 public void testDeleteOfSingleExistingIntent() {
125
126 makeDefaultIntents();
127
128 final String intentUrl = getHighRestIntentUrl() + "/2";
129 final ClientResource deleteClient = new ClientResource(intentUrl);
130 deleteClient.delete();
131
132 // HTTP status should be NO CONTENT
133 assertThat(deleteClient, hasStatusOf(Status.SUCCESS_NO_CONTENT));
134
Ray Milkeyd83844d2014-06-20 13:16:01 -0700135 // trigger the back end deletion of the Intent.
136 modifyIntentState("1:2___0", Intent.IntentState.DEL_ACK);
137
Ray Milkey531bb232014-06-03 09:08:15 -0700138 ClientResource client = new ClientResource(intentUrl);
139 try {
140 // Now query the intent to make sure it got deleted
141 getIntent(client);
142 Assert.fail("Fetch of deleted intent did not throw an exception");
143 } catch (Exception ex) {
144 // HTTP status should be NOT FOUND
145 assertThat(client, hasStatusOf(Status.CLIENT_ERROR_NOT_FOUND));
146 }
147 }
148
149 /**
150 * Test that the DELETE of an existing high level Intents REST call returns the
151 * proper result.
152 * The HTTP status of the delete call should be NO CONTENT
153 * Once the Intent remove API is called, an empty list should be
154 * returned by the fetch of the Intent.
155 */
156 @Test
157 public void testDeleteOfSingleNonExistingIntent() {
158
159 makeDefaultIntents();
160
161 final String intentUrl = getHighRestIntentUrl() + "/2345678";
162 final ClientResource deleteClient = new ClientResource(intentUrl);
163 deleteClient.delete();
164
165 // HTTP status should be NO CONTENT
166 assertThat(deleteClient, hasStatusOf(Status.SUCCESS_NO_CONTENT));
167
168 ClientResource client = new ClientResource(intentUrl);
169 try {
170 // Now query the intent to make sure its not there
171 getIntent(client);
172 Assert.fail("Fetch of deleted intent did not throw an exception");
173 } catch (Exception ex) {
174 // HTTP status should be NOT FOUND
175 assertThat(client, hasStatusOf(Status.CLIENT_ERROR_NOT_FOUND));
176 }
177 }
178}