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