|
6 | 6 |
|
7 | 7 | package com.opentok.test;
|
8 | 8 |
|
9 |
| -//import java.util.Date; |
10 |
| -//import java.util.HashMap; |
11 |
| -//import java.util.Map; |
12 |
| -//import java.util.Map.Entry; |
| 9 | +import java.util.Date; |
| 10 | +import java.util.HashMap; |
| 11 | +import java.util.Map; |
| 12 | +import java.util.Map.Entry; |
13 | 13 |
|
14 |
| -//import com.ning.http.client.AsyncHttpClient; |
15 |
| -//import com.ning.http.client.AsyncHttpClient.BoundRequestBuilder; |
16 |
| -//import com.ning.http.client.Response; |
17 | 14 | import com.opentok.api.OpenTok;
|
18 | 15 | import com.opentok.api.Session;
|
19 | 16 | import com.opentok.api.constants.Version;
|
| 17 | +import com.opentok.api.constants.RoleConstants; |
| 18 | +import com.opentok.api.constants.SessionProperties; |
20 | 19 | import com.opentok.exception.OpenTokException;
|
21 |
| -//import com.opentok.api.constants.RoleConstants; |
22 |
| -//import com.opentok.api.constants.SessionProperties; |
23 |
| -//import com.opentok.exception.OpenTokRequestException; |
24 |
| -//import com.opentok.util.TokBoxXML; |
| 20 | +import com.opentok.exception.OpenTokInvalidArgumentException; |
| 21 | +import com.opentok.exception.OpenTokRequestException; |
25 | 22 |
|
26 | 23 | import org.junit.Before;
|
27 | 24 | import org.junit.Rule;
|
28 | 25 | import org.junit.Test;
|
29 | 26 | import static org.junit.Assert.*;
|
| 27 | + |
30 | 28 | import static com.github.tomakehurst.wiremock.client.WireMock.*;
|
31 | 29 | import com.github.tomakehurst.wiremock.junit.WireMockRule;
|
32 | 30 |
|
@@ -83,62 +81,78 @@ public void testCreateDefaultSession() throws OpenTokException {
|
83 | 81 |
|
84 | 82 | verify(postRequestedFor(urlMatching("/session/create"))
|
85 | 83 | // TODO: add p2p.preference=disabled
|
86 |
| - //.withRequestBody(matching(".*<message>1234</message>.*")) |
87 | 84 | .withHeader("X-TB-PARTNER-AUTH", matching(this.apiKey+":"+this.apiSecret))
|
88 | 85 | .withHeader("User-Agent", matching(".*Opentok-Java-SDK/"+ Version.VERSION+".*")));
|
89 | 86 | }
|
90 | 87 |
|
91 |
| -// private TokBoxXML get_session_info(String session_id) throws OpenTokException { |
92 |
| -// String token = sdk.generateToken(session_id); |
93 |
| -// Map<String, String> headers = new HashMap<String, String>(); |
94 |
| -// headers.put("X-TB-TOKEN-AUTH", token ); |
95 |
| -// TokBoxXML xml; |
96 |
| -// xml = new TokBoxXML(makePostRequest("/session/" + session_id + "?extended=true", headers, new HashMap<String, String>(), null)); |
97 |
| -// return xml; |
98 |
| -// } |
99 |
| -// |
100 |
| -// private TokBoxXML get_token_info(String token) throws OpenTokException { |
101 |
| -// Map<String, String> headers = new HashMap<String, String>(); |
102 |
| -// headers.put("X-TB-TOKEN-AUTH",token); |
103 |
| -// TokBoxXML xml; |
104 |
| -// xml = new TokBoxXML(makePostRequest("/token/validate", headers, new HashMap<String, String>(), null)); |
105 |
| -// return xml; |
106 |
| -// } |
107 |
| -// |
108 |
| -// @Test |
109 |
| -// public void testCreateSessionNoParams() throws OpenTokException { |
110 |
| -// Session session = sdk.createSession(); |
111 |
| -// TokBoxXML xml = get_session_info(session.getSessionId()); |
112 |
| -// String expected = session.getSessionId(); |
113 |
| -// String actual = xml.getElementValue("session_id", "Session"); |
114 |
| -// Assert.assertEquals("Java SDK tests: Session create with no params failed", expected, actual); |
115 |
| -// } |
116 |
| -// |
117 |
| -// @Test |
118 |
| -// public void testCreateSessionWithLocation() throws OpenTokException { |
119 |
| -// SessionProperties properties = new SessionProperties.Builder() |
120 |
| -// .location("216.38.134.114") |
121 |
| -// .build(); |
122 |
| -// Session session = sdk.createSession(properties); |
123 |
| -// TokBoxXML xml = get_session_info(session.getSessionId()); |
124 |
| -// String expected = session.getSessionId(); |
125 |
| -// String actual = xml.getElementValue("session_id", "Session"); |
126 |
| -// Assert.assertEquals("Java SDK tests: Session create with location failed", expected, actual); |
127 |
| -// } |
128 |
| -// |
129 |
| -// @Test |
130 |
| -// public void testP2PEnable() throws OpenTokException { |
131 |
| -// String expected = "enabled"; |
132 |
| -// SessionProperties properties = new SessionProperties.Builder() |
133 |
| -// .location("216.38.134.114") |
134 |
| -// .p2p(true) |
135 |
| -// .build(); |
136 |
| -// Session s = sdk.createSession(properties); |
137 |
| -// TokBoxXML xml = get_session_info(s.getSessionId()); |
138 |
| -// String actual = xml.getElementValue("preference", "p2p"); |
139 |
| -// Assert.assertEquals("Java SDK tests: p2p not enabled", expected, actual); |
140 |
| -// } |
141 |
| -// |
| 88 | + @Test |
| 89 | + public void testCreateP2pSession() throws OpenTokException { |
| 90 | + String sessionId = "SESSIONID"; |
| 91 | + stubFor(post(urlEqualTo("/session/create")) |
| 92 | + .willReturn(aResponse() |
| 93 | + .withStatus(200) |
| 94 | + .withHeader("Content-Type", "text/xml") |
| 95 | + .withBody("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><sessions><Session><" + |
| 96 | + "session_id>" + sessionId + "</session_id><partner_id>123456</partner_id><create_dt>" + |
| 97 | + "Mon Mar 17 00:41:31 PDT 2014</create_dt></Session></sessions>"))); |
| 98 | + |
| 99 | + SessionProperties properties = new SessionProperties.Builder() |
| 100 | + .p2p(true) |
| 101 | + .build(); |
| 102 | + Session session = sdk.createSession(properties); |
| 103 | + |
| 104 | + assertNotNull(session); |
| 105 | + assertEquals(this.apiKey, session.getApiKey()); |
| 106 | + assertEquals(sessionId, session.getSessionId()); |
| 107 | + assertTrue(session.getProperties().isP2p()); |
| 108 | + assertNull(session.getProperties().getLocation()); |
| 109 | + |
| 110 | + verify(postRequestedFor(urlMatching("/session/create")) |
| 111 | + // TODO: this is a pretty bad way to verify, ideally we can decode the body and then query the object |
| 112 | + .withRequestBody(matching(".*p2p.preference=enabled.*")) |
| 113 | + .withHeader("X-TB-PARTNER-AUTH", matching(this.apiKey+":"+this.apiSecret)) |
| 114 | + .withHeader("User-Agent", matching(".*Opentok-Java-SDK/"+ Version.VERSION+".*"))); |
| 115 | + } |
| 116 | + |
| 117 | + @Test |
| 118 | + public void testCreateLocationHintSession() throws OpenTokException { |
| 119 | + String sessionId = "SESSIONID"; |
| 120 | + String locationHint = "12.34.56.78"; |
| 121 | + stubFor(post(urlEqualTo("/session/create")) |
| 122 | + .willReturn(aResponse() |
| 123 | + .withStatus(200) |
| 124 | + .withHeader("Content-Type", "text/xml") |
| 125 | + .withBody("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><sessions><Session><" + |
| 126 | + "session_id>" + sessionId + "</session_id><partner_id>123456</partner_id><create_dt>" + |
| 127 | + "Mon Mar 17 00:41:31 PDT 2014</create_dt></Session></sessions>"))); |
| 128 | + |
| 129 | + SessionProperties properties = new SessionProperties.Builder() |
| 130 | + .location(locationHint) |
| 131 | + .build(); |
| 132 | + Session session = sdk.createSession(properties); |
| 133 | + |
| 134 | + assertNotNull(session); |
| 135 | + assertEquals(this.apiKey, session.getApiKey()); |
| 136 | + assertEquals(sessionId, session.getSessionId()); |
| 137 | + assertFalse(session.getProperties().isP2p()); |
| 138 | + assertEquals(locationHint, session.getProperties().getLocation()); |
| 139 | + |
| 140 | + verify(postRequestedFor(urlMatching("/session/create")) |
| 141 | + // TODO: this is a pretty bad way to verify, ideally we can decode the body and then query the object |
| 142 | + .withRequestBody(matching(".*location="+locationHint+".*")) |
| 143 | + .withHeader("X-TB-PARTNER-AUTH", matching(this.apiKey+":"+this.apiSecret)) |
| 144 | + .withHeader("User-Agent", matching(".*Opentok-Java-SDK/"+ Version.VERSION+".*"))); |
| 145 | + } |
| 146 | + |
| 147 | + @Test(expected = OpenTokInvalidArgumentException.class) |
| 148 | + public void testCreateBadSession() throws OpenTokException { |
| 149 | + SessionProperties properties = new SessionProperties.Builder() |
| 150 | + .location("NOT A VALID IP") |
| 151 | + .build(); |
| 152 | + } |
| 153 | + |
| 154 | + // TODO: test session creation conditions that result in errors |
| 155 | + |
142 | 156 | // @Test
|
143 | 157 | // public void testRoleDefault() throws OpenTokException {
|
144 | 158 | // Session s= sdk.createSession();
|
@@ -381,39 +395,5 @@ public void testCreateDefaultSession() throws OpenTokException {
|
381 | 395 | // }
|
382 | 396 | // Assert.assertNotNull("Java SDK tests: connection data over 1000 characters should not be accepted. Test String: " + test_string , expected);
|
383 | 397 | // }
|
384 |
| -// |
385 |
| -// private String makePostRequest(String resource, Map<String, String> headers, Map<String, String> params, |
386 |
| -// String postData) throws OpenTokException { |
387 |
| -// BoundRequestBuilder post = this.client.preparePost(apiUrl + resource); |
388 |
| -// if (params != null) { |
389 |
| -// for (Entry<String, String> pair : params.entrySet()) { |
390 |
| -// post.addParameter(pair.getKey(), pair.getValue()); |
391 |
| -// } |
392 |
| -// } |
393 |
| -// |
394 |
| -// if (headers != null) { |
395 |
| -// for (Entry<String, String> pair : headers.entrySet()) { |
396 |
| -// post.addHeader(pair.getKey(), pair.getValue()); |
397 |
| -// } |
398 |
| -// } |
399 |
| -// |
400 |
| -// post.addHeader("X-TB-PARTNER-AUTH", String.format("%s:%s", apiKey, apiSecret)); |
401 |
| -// post.addHeader("X-TB-VERSION", "1"); |
402 |
| -// |
403 |
| -// if (postData != null) { |
404 |
| -// post.setBody(postData); |
405 |
| -// } |
406 |
| -// |
407 |
| -// try { |
408 |
| -// Response result = post.execute().get(); |
409 |
| -// |
410 |
| -// if (result.getStatusCode() < 200 || result.getStatusCode() > 299) { |
411 |
| -// throw new OpenTokRequestException(result.getStatusCode(), result.getStatusText()); |
412 |
| -// } |
413 |
| -// |
414 |
| -// return result.getResponseBody(); |
415 |
| -// } catch (Exception e) { |
416 |
| -// throw new OpenTokRequestException(500, "Error response: message: " + e.getMessage()); |
417 |
| -// } |
418 |
| -// } |
| 398 | + |
419 | 399 | }
|
0 commit comments