Skip to content

Commit

Permalink
100% Video test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
SMadani committed Jan 22, 2025
1 parent 3e9172c commit 3995967
Show file tree
Hide file tree
Showing 10 changed files with 93 additions and 25 deletions.
16 changes: 10 additions & 6 deletions src/main/java/com/vonage/client/video/CaptionsRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,16 @@ private CaptionsRequest() {}

private CaptionsRequest(Builder builder) {
super(builder);
statusCallbackUrl = builder.statusCallbackUrl;
languageCode = builder.languageCode;
partialCaptions = builder.partialCaptions;
if (builder.statusCallbackUrl != null) {
if (builder.statusCallbackUrl.length() < 15 || builder.statusCallbackUrl.length() > 2048) {
throw new IllegalArgumentException("Status callback URL must be between 15 and 2048 characters.");
}
else {
statusCallbackUrl = URI.create(builder.statusCallbackUrl);
}
}
if ((maxDuration = builder.maxDuration) != null && (maxDuration < 300 || maxDuration > 14400)) {
throw new IllegalArgumentException("Max duration must be between 300 and 14400 seconds.");
}
Expand Down Expand Up @@ -95,7 +102,7 @@ public static Builder builder() {
* Builder for defining the fields in a StartCaptionsRequest object.
*/
public static final class Builder extends AbstractSessionTokenRequest.Builder<CaptionsRequest, Builder> {
private URI statusCallbackUrl;
private String statusCallbackUrl;
private Language languageCode;
private Integer maxDuration;
private Boolean partialCaptions;
Expand Down Expand Up @@ -125,10 +132,7 @@ public Builder languageCode(Language languageCode) {
* @return This Builder with the statusCallbackUrl property setting.
*/
public Builder statusCallbackUrl(String statusCallbackUrl) {
if (statusCallbackUrl == null || statusCallbackUrl.length() < 15 || statusCallbackUrl.length() > 2048) {
throw new IllegalArgumentException("Status callback URL must be between 15 and 2048 characters.");
}
this.statusCallbackUrl = URI.create(statusCallbackUrl);
this.statusCallbackUrl = statusCallbackUrl;
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public static CreateSessionResponse fromJson(String json) {
try {
CreateSessionResponse[] array = Jsonable.createDefaultObjectMapper()
.readValue(json, CreateSessionResponse[].class);
if (array == null || array.length == 0) {
if (array.length == 0) {
return new CreateSessionResponse();
}
return array[0];
Expand Down
1 change: 0 additions & 1 deletion src/main/java/com/vonage/client/video/Language.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ public static Language fromString(String name) {
@Override
public String toString() {
String[] split = name().split("_");
assert split.length == 2;
return split[0].toLowerCase() + '-' + split[1];
}
}
11 changes: 4 additions & 7 deletions src/main/java/com/vonage/client/video/VideoClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ public CreateSessionResponse createSession() {
*/
public CreateSessionResponse createSession(CreateSessionRequest request) {
CreateSessionResponse[] response = createSession.execute(request);
return response == null || response.length == 0 ? new CreateSessionResponse() : response[0];
return response.length == 0 ? new CreateSessionResponse() : response[0];
}

/**
Expand Down Expand Up @@ -407,7 +407,7 @@ public SipDialResponse sipDial(SipDialRequest request) {
}

/**
* Play DMTF tones into a specific connection.
* Play DTMF tones into a specific connection.
* Telephony events are negotiated over SDP and transmitted as RFC4733/RFC2833 digits to the remote endpoint.
*
* @param sessionId The session ID.
Expand All @@ -419,9 +419,7 @@ public SipDialResponse sipDial(SipDialRequest request) {
*/
public void sendDtmf(String sessionId, String connectionId, String digits) {
sendDtmfToConnection.execute(new SendDtmfRequest(
validateSessionId(sessionId),
validateConnectionId(connectionId),
String.valueOf(digits)
validateSessionId(sessionId), validateConnectionId(connectionId), digits
));
}

Expand All @@ -437,8 +435,7 @@ public void sendDtmf(String sessionId, String connectionId, String digits) {
*/
public void sendDtmf(String sessionId, String digits) {
sendDtmfToSession.execute(new SendDtmfRequest(
validateSessionId(sessionId),
null, String.valueOf(digits)
validateSessionId(sessionId), null, digits
));
}

Expand Down
20 changes: 20 additions & 0 deletions src/test/java/com/vonage/client/video/ArchiveTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,24 @@ public void testMaxBitrateBounds() {
public void testFromJsonInvalid() {
assertThrows(VonageResponseParseException.class, () -> Archive.fromJson("{malformed]"));
}

@Test
public void testFromJsonEmpty() {
var archive = Archive.fromJson("{}");
assertNotNull(archive);
assertNull(archive.getSessionId());
assertNull(archive.getName());
assertNull(archive.getMultiArchiveTag());
assertNull(archive.getResolution());
assertNull(archive.getOutputMode());
assertNull(archive.getStreamMode());
assertNull(archive.getLayout());
assertNull(archive.hasAudio());
assertNull(archive.hasVideo());
assertNull(archive.getMaxBitrate());
assertNull(archive.getDuration());
assertNull(archive.getDurationSeconds());
assertNull(archive.getCreatedAt());
assertNull(archive.getCreatedAtMillis());
}
}
2 changes: 2 additions & 0 deletions src/test/java/com/vonage/client/video/BroadcastTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,9 @@ public void testSerializeAndParseAllFields() {
assertEquals(multiBroadcastTag, response.getMultiBroadcastTag());
assertEquals(applicationId, response.getApplicationId());
assertEquals(createdAt, response.getCreatedAtMillis());
assertEquals(Instant.ofEpochMilli(createdAt), response.getCreatedAt());
assertEquals(updatedAt, response.getUpdatedAtMillis());
assertEquals(Instant.ofEpochMilli(updatedAt), response.getUpdatedAt());
assertEquals(maxDuration, response.getMaxDurationSeconds());
assertEquals(maxBitrate, response.getMaxBitrate());
assertEquals(hasAudio, response.hasAudio());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ public void testStatusCallbackUriLength() {
.statusCallbackUrl(twoThousandAndFourtySeven + 'y')
.build().getStatusCallbackUrl().toString().length()
);
assertThrows(IllegalArgumentException.class,
() -> builder.statusCallbackUrl(twoThousandAndFourtySeven + "yz")
assertThrows(IllegalArgumentException.class, () ->
builder.statusCallbackUrl(twoThousandAndFourtySeven + "yz").build()
);
}

Expand All @@ -118,4 +118,9 @@ public void testConstructMissingToken() {
public void testUnknownLanguage() {
assertNull(Language.fromString("fa-IR"));
}

@Test
public void testNullLanguage() {
assertNull(Language.fromString(null));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package com.vonage.client.video;

import com.vonage.client.TestUtils;
import static com.vonage.client.TestUtils.testJsonableBaseObject;
import com.vonage.client.VonageResponseParseException;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.*;
Expand All @@ -43,7 +44,7 @@ public void testFromJsonAllFields() {
"\"media_server_url\":\""+mediaServerUrl+"\"\n" +
"}]");

TestUtils.testJsonableBaseObject(response);
testJsonableBaseObject(response);
assertEquals(sessionId, response.getSessionId());
assertEquals(applicationId, response.getApplicationId());
assertEquals(createDt, response.getCreateDt());
Expand All @@ -58,7 +59,7 @@ public void testFromJsonInvalid() {
@Test
public void testFromJsonEmptyObject() {
CreateSessionResponse response = CreateSessionResponse.fromJson("[{}]");
TestUtils.testJsonableBaseObject(response);
testJsonableBaseObject(response);
assertNull(response.getApplicationId());
assertNull(response.getSessionId());
assertNull(response.getMediaServerUrl());
Expand All @@ -67,15 +68,21 @@ public void testFromJsonEmptyObject() {

@Test
public void testFromJsonEmptyArray() {
TestUtils.testJsonableBaseObject(CreateSessionResponse.fromJson("[]"));
testJsonableBaseObject(CreateSessionResponse.fromJson("[]"));
}

@Test
public void testFromJsonEmptyJson() {
assertThrows(VonageResponseParseException.class, () -> CreateSessionResponse.fromJson("{}"));
assertThrows(VonageResponseParseException.class, () -> CreateSessionResponse.fromJson(""));
}

@Test
public void testFromJsonMultipleEntries() {
String sessionId = "TheSessionIdYouWant";
String json = "[{\"session_id\":\""+sessionId+"\"},{},{\"session_id\":\"fake\"}]";
CreateSessionResponse response = CreateSessionResponse.fromJson(json);
TestUtils.testJsonableBaseObject(response);
testJsonableBaseObject(response);
assertEquals(sessionId, response.getSessionId());
assertNull(response.getApplicationId());
assertNull(response.getMediaServerUrl());
Expand Down
25 changes: 25 additions & 0 deletions src/test/java/com/vonage/client/video/SipDialRequestTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ public void testSerializeRequiredParams() {
"\"sip\":{\"uri\":\""+uri+"\"}}";
assertEquals(expectedJson, request.toJson());

assertEquals(uri, request.getUri());
assertEquals(sessionId, request.getSessionId());
assertEquals(token, request.getToken());
assertNull(request.getUsername());
assertNull(request.getPassword());
assertNull(request.getFrom());
Expand All @@ -80,6 +83,28 @@ public void testSerializeRequiredParams() {
assertNull(request.getObserveForceMute());
}

@Test
public void testSerializeUsername() {
String uri = "sip:name@sip.example.org", sessionId = "SESSION", token = "TOKEN", user = "Admin";
SipDialRequest request = SipDialRequest.builder().uri(URI.create(uri), false)
.sessionId(sessionId).token(token).username(user).build();

String expectedJson = "{\"sessionId\":\""+sessionId+"\",\"token\":\""+token+"\"," +
"\"sip\":{\"uri\":\""+uri+"\",\"auth\":{\"username\":\""+user+"\"}}}";
assertEquals(expectedJson, request.toJson());

assertEquals(user, request.getUsername());
assertEquals(uri, request.getUri());
assertEquals(sessionId, request.getSessionId());
assertEquals(token, request.getToken());
assertNull(request.getPassword());
assertNull(request.getFrom());
assertNull(request.getHeaders());
assertNull(request.getVideo());
assertNull(request.getSecure());
assertNull(request.getObserveForceMute());
}

@Test
public void testConstructMissingUrl() {
assertThrows(NullPointerException.class, () ->
Expand Down
17 changes: 13 additions & 4 deletions src/test/java/com/vonage/client/video/VideoClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,8 @@
*/
package com.vonage.client.video;

import com.vonage.client.AbstractClientTest;
import com.vonage.client.HttpWrapper;
import com.vonage.client.OrderedMap;
import com.vonage.client.*;
import static com.vonage.client.OrderedMap.entry;
import com.vonage.client.RestEndpoint;
import static com.vonage.client.TestUtils.*;
import com.vonage.client.auth.JWTAuthMethod;
import com.vonage.client.common.HttpMethod;
Expand Down Expand Up @@ -347,6 +344,16 @@ public void testCreateSession() throws Exception {
assertEquals(createDt, response.getCreateDt());
assertEquals(msUrl, response.getMediaServerUrl().toString());

stubResponse("[]");
response = client.createSession();
assertNotNull(response);
assertNull(response.getSessionId());
assertNull(response.getApplicationId());
assertNull(response.getCreateDt());
assertNull(response.getMediaServerUrl());

stubResponseAndAssertThrows(200, "{}", client::createSession, VonageResponseParseException.class);

responseJson = "{\n" + " \"code\": 400,\n" + " \"message\": "+
"\"Invalid request. This response may indicate that data in your request data is invalid JSON. "+
"Or it may indicate that you do not pass in a session ID or you passed in an invalid stream ID.\"\n}";
Expand Down Expand Up @@ -453,6 +460,7 @@ public void testSignalAll() throws Exception {
stubResponseAndRun(204, () -> client.signalAll(sessionId, signalRequest));
stubResponseAndAssertThrowsIAX(() -> client.signalAll(sessionId, null));
stubResponseAndAssertThrowsIAX(() -> client.signalAll(null, signalRequest));
stubResponseAndAssertThrowsIAX(() -> client.signalAll("", signalRequest));

String responseJson = "{\n \"code\": 413,\n \"message\": \"The type string exceeds the maximum" +
"length (128 bytes), or the data string exceeds the maximum size (8 kB).\"\n}";
Expand All @@ -465,6 +473,7 @@ public void testSignalAll() throws Exception {
public void testForceDisconnect() throws Exception {
stubResponseAndRun(204, () -> client.forceDisconnect(sessionId, connectionId));
stubResponseAndAssertThrowsIAX(() -> client.forceDisconnect(null, connectionId));
stubResponseAndAssertThrowsIAX(() -> client.forceDisconnect("", connectionId));
stubResponseAndAssertThrowsIAX(() -> client.forceDisconnect(sessionId, null));

String responseJson = "{\n \"code\": 403,\n \"message\": " +
Expand Down

0 comments on commit 3995967

Please sign in to comment.