Skip to content

Commit f433bc4

Browse files
committed
add more session creation test cases, add validation of SessionProperties location
1 parent 2a98662 commit f433bc4

File tree

3 files changed

+90
-108
lines changed

3 files changed

+90
-108
lines changed

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ dependencies {
3131
compile group: 'com.ning', name: 'async-http-client', version: '1.6.1'
3232
compile group: 'org.codehaus.jackson', name: 'jackson-mapper-asl', version: '1.9.13'
3333
compile group: 'org.codehaus.jackson', name: 'jackson-core-asl', version: '1.9.13'
34+
compile group: 'commons-validator', name: 'commons-validator', version: '1.4.0'
3435
// TODO: find out how to initialize these dependencies properly, or remove them
3536
//compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.5'
3637
//compile group: 'org.slf4j', name: 'slf4j-simple', version: '1.7.5'

src/main/java/com/opentok/api/constants/SessionProperties.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.opentok.api.constants;
22

3+
import com.opentok.exception.OpenTokInvalidArgumentException;
4+
import org.apache.commons.validator.routines.InetAddressValidator;
5+
36
import java.util.ArrayList;
47
import java.util.Collection;
58
import java.util.HashMap;
@@ -17,32 +20,30 @@ public class SessionProperties {
1720
private String location = null;
1821
private boolean p2p = false;
1922

20-
private SessionProperties(Builder builder)
21-
{
23+
private SessionProperties(Builder builder) {
2224
this.location = builder.location;
2325
this.p2p = builder.p2p;
2426
}
2527

26-
public static class Builder
27-
{
28+
public static class Builder {
2829
private String location = null;
2930
private boolean p2p = false;
3031

3132

32-
public Builder location(String location)
33-
{
33+
public Builder location(String location) throws OpenTokInvalidArgumentException {
34+
if (!InetAddressValidator.getInstance().isValidInet4Address(location)) {
35+
throw new OpenTokInvalidArgumentException("Location must be a valid IPv4 address. location="+location);
36+
}
3437
this.location = location;
3538
return this;
3639
}
3740

38-
public Builder p2p(boolean p2p)
39-
{
41+
public Builder p2p(boolean p2p) {
4042
this.p2p = p2p;
4143
return this;
4244
}
4345

44-
public SessionProperties build()
45-
{
46+
public SessionProperties build() {
4647
return new SessionProperties(this);
4748
}
4849
}

src/test/java/com/opentok/test/OpenTokTest.java

Lines changed: 78 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,25 @@
66

77
package com.opentok.test;
88

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;
1313

14-
//import com.ning.http.client.AsyncHttpClient;
15-
//import com.ning.http.client.AsyncHttpClient.BoundRequestBuilder;
16-
//import com.ning.http.client.Response;
1714
import com.opentok.api.OpenTok;
1815
import com.opentok.api.Session;
1916
import com.opentok.api.constants.Version;
17+
import com.opentok.api.constants.RoleConstants;
18+
import com.opentok.api.constants.SessionProperties;
2019
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;
2522

2623
import org.junit.Before;
2724
import org.junit.Rule;
2825
import org.junit.Test;
2926
import static org.junit.Assert.*;
27+
3028
import static com.github.tomakehurst.wiremock.client.WireMock.*;
3129
import com.github.tomakehurst.wiremock.junit.WireMockRule;
3230

@@ -83,62 +81,78 @@ public void testCreateDefaultSession() throws OpenTokException {
8381

8482
verify(postRequestedFor(urlMatching("/session/create"))
8583
// TODO: add p2p.preference=disabled
86-
//.withRequestBody(matching(".*<message>1234</message>.*"))
8784
.withHeader("X-TB-PARTNER-AUTH", matching(this.apiKey+":"+this.apiSecret))
8885
.withHeader("User-Agent", matching(".*Opentok-Java-SDK/"+ Version.VERSION+".*")));
8986
}
9087

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+
142156
// @Test
143157
// public void testRoleDefault() throws OpenTokException {
144158
// Session s= sdk.createSession();
@@ -381,39 +395,5 @@ public void testCreateDefaultSession() throws OpenTokException {
381395
// }
382396
// Assert.assertNotNull("Java SDK tests: connection data over 1000 characters should not be accepted. Test String: " + test_string , expected);
383397
// }
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+
419399
}

0 commit comments

Comments
 (0)