Skip to content

Commit

Permalink
100% Voice branch coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
SMadani committed Jan 17, 2025
1 parent 675d384 commit 9ce3f4d
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 12 deletions.
10 changes: 5 additions & 5 deletions src/main/java/com/vonage/client/voice/Call.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
public class Call extends JsonableBaseObject {
private Endpoint[] to;
private Endpoint from;
private HttpMethod answerMethod = HttpMethod.GET, eventMethod;
private HttpMethod answerMethod, eventMethod;
private String answerUrl, eventUrl;
private MachineDetection machineDetection;
private AdvancedMachineDetection advancedMachineDetection;
Expand All @@ -54,13 +54,13 @@ public class Call extends JsonableBaseObject {
if ((ringingTimer = builder.ringingTimer) != null && (ringingTimer > 120 || ringingTimer < 1)) {
throw new IllegalArgumentException("Ringing timer must be between 1 and 120.");
}
if (builder.answerMethod != null) switch (answerMethod = builder.answerMethod) {
if ((answerMethod = builder.answerMethod) != null) switch (answerMethod) {
case GET: case POST: break;
default: throw new IllegalArgumentException("Answer method must be GET or POST");
default: throw new IllegalArgumentException("Answer method must be GET or POST.");
}
if ((eventMethod = builder.eventMethod) != null) switch (eventMethod) {
case GET: case POST: break;
default: throw new IllegalArgumentException("Event method must be GET or POST");
default: throw new IllegalArgumentException("Event method must be GET or POST.");
}
if ((from = builder.from) == null) {
fromRandomNumber = true;
Expand Down Expand Up @@ -208,7 +208,7 @@ public static Builder builder() {
public static class Builder {
private Endpoint[] to;
private Endpoint from;
private HttpMethod answerMethod, eventMethod;
private HttpMethod answerMethod = HttpMethod.GET, eventMethod;
private String answerUrl, eventUrl;
private MachineDetection machineDetection;
private AdvancedMachineDetection advancedMachineDetection;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/vonage/client/voice/VoiceClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class Endpoint<T, R> extends DynamicEndpoint<T, R> {
.requestMethod(method).wrapper(wrapper).pathGetter((de, req) -> {
String base = de.getHttpWrapper().getHttpConfig().getVersionedApiBaseUri("v1");
String path = pathGetter.apply(req);
if (path.startsWith("http") && method == HttpMethod.GET) {
if (method == HttpMethod.GET && path.startsWith("http")) {
return path;
}
return base + "/calls" + (path.isEmpty() ? "" : "/" + path);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public static class Builder {
private Boolean randomFromNumber;
private URI ringbackTone;

Builder(Collection<Endpoint> endpoint) {
private Builder(Collection<Endpoint> endpoint) {
this.endpoint = endpoint;
}

Expand Down
10 changes: 10 additions & 0 deletions src/test/java/com/vonage/client/voice/AnswerWebhookTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,14 @@ public void testFromUserOnly() {
assertNull(parsed.getRegionUrl());
assertNull(parsed.getCustomData());
}

@Test
public void testEndpointTypeFromString() {
assertNull(EndpointType.fromString(null));
assertEquals(EndpointType.PHONE, EndpointType.fromString("phone"));
assertEquals(EndpointType.VBC, EndpointType.fromString("vbc"));
assertEquals(EndpointType.APP, EndpointType.fromString("app"));
assertEquals(EndpointType.SIP, EndpointType.fromString("sip"));
assertEquals(EndpointType.WEBSOCKET, EndpointType.fromString("websocket"));
}
}
24 changes: 21 additions & 3 deletions src/test/java/com/vonage/client/voice/CallTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public void testFromJsonWithEveryActionAndEndpoint() {
};
com.vonage.client.voice.Endpoint fromEndpoint = new com.vonage.client.voice.AppEndpoint("nexmo");
Call expectedCall = new Call(endpoints, fromEndpoint, "http://example.com/answer");
String jsonString = toFromJsonStart + ",\"answer_url\":\"http://example.com/answer\"}";
String jsonString = toFromJsonStart + ",\"answer_method\":\"GET\",\"answer_url\":\"http://example.com/answer\"}";
Call fromJson = Call.fromJson(jsonString);
assertEquals(expectedCall.toJson(), fromJson.toJson());

Expand Down Expand Up @@ -310,7 +310,7 @@ public void testConstructAllParams() {
RecordAction.builder().build(),
ConnectAction.builder(com.vonage.client.voice.ncco.VbcEndpoint.builder("123").build()).build()
)
.answerMethod(HttpMethod.POST).eventMethod(HttpMethod.POST)
.answerMethod(HttpMethod.POST).eventMethod(HttpMethod.GET)
.eventUrl("https://example.com/voice/event")
.answerUrl("https://example.com/voice/answer")
.fromRandomNumber(false).machineDetection(MachineDetection.HANGUP)
Expand All @@ -321,7 +321,7 @@ public void testConstructAllParams() {
assertNotNull(call.getAnswerUrl());
assertNotNull(call.getEventUrl());
assertEquals("POST", call.getAnswerMethod());
assertEquals("POST", call.getEventMethod());
assertEquals("GET", call.getEventMethod());
assertFalse(call.getFromRandomNumber());
assertEquals(MachineDetection.HANGUP, call.getMachineDetection());
assertEquals("phone", call.getFrom().getType());
Expand Down Expand Up @@ -428,4 +428,22 @@ public void testRandomNumberIsTrueWhenFromIsNotSet() {
assertNotNull(call.getFrom());
assertNull(call.getFromRandomNumber());
}

@Test
public void testSipHeader() {
SipHeader header = SipHeader.USER_TO_USER;
assertEquals("User-to-User", header.toString());
assertEquals(SipHeader.USER_TO_USER, SipHeader.fromString("user-to-user"));
assertNull(SipHeader.fromString(null));
assertNull(SipHeader.fromString(""));
assertThrows(IllegalArgumentException.class, () -> SipHeader.fromString("unknown"));
}

@Test
public void testNulllAnswerMethod() {
Call call = Call.builder().to(new VbcEndpoint("123"))
.answerUrl("http://example.com/answer")
.answerMethod(null).build();
assertNull(call.getAnswerMethod());
}
}
9 changes: 8 additions & 1 deletion src/test/java/com/vonage/client/voice/VoiceClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ public void testCreateCall() throws Exception {
assertEquals("63f61863-4a51-4f6b-86e1-46edebio0391", evt.getConversationUuid());
assertEquals(SAMPLE_CALL_ID, evt.getUuid());
assertEquals(CallDirection.OUTBOUND, evt.getDirection());
assert401Response(() -> client.createCall(Call.builder().to(new VbcEndpoint("123")).build()));
assert401Response(() -> client.createCall(Call.builder()
.to(new VbcEndpoint("123")).eventMethod(HttpMethod.POST).build())
);
}

@Test
Expand Down Expand Up @@ -139,6 +141,7 @@ public void testSendDtmf() throws Exception {
assertThrows(IllegalArgumentException.class, () ->
client.sendDtmf("944dd293-ca13-4a58-bc37-6252e11474be", null)
);
assertThrows(IllegalArgumentException.class, () -> client.sendDtmf(SAMPLE_CALL_ID, " "));
assertThrows(NullPointerException.class, () -> client.sendDtmf(null, "1234"));
assert401Response(() -> client.sendDtmf(SAMPLE_CALL_ID, "789#0"));
}
Expand Down Expand Up @@ -429,6 +432,10 @@ public void testDownloadRecording() throws Exception {
client.saveRecording(";not_a url£", recordingPath),
IllegalArgumentException.class
);
stubResponseAndAssertThrows(content, () ->
client.saveRecording(" \t", recordingPath),
IllegalArgumentException.class
);
}

// ENDPOINT TESTS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.vonage.client.voice.MachineDetection;
import org.junit.jupiter.api.*;
import static org.junit.jupiter.api.Assertions.*;
import java.util.Collection;
import java.util.Map;

public class ConnectActionTest {
Expand Down Expand Up @@ -276,4 +277,9 @@ public void testTimeOutBoundaries() {
assertEquals(min, builder.timeOut(min).build().getTimeOut());
assertThrows(IllegalArgumentException.class, () -> builder.timeOut(min - 1).build());
}

@Test
public void testNullEndpoint() {
assertThrows(IllegalStateException.class, () -> ConnectAction.builder((Collection<Endpoint>) null).build());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -235,5 +235,6 @@ public void testInputModeDeserialization() {
assertEquals(InputMode.SYNCHRONOUS, InputMode.fromString("synchronous"));
assertEquals(InputMode.ASYNCHRONOUS, InputMode.fromString("asynchronous"));
assertNull(InputMode.fromString("invalid"));
assertNull(InputMode.fromString(null));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public void testAllFields() {
String user2UserHeader = "342342ef34;encoding=hex";
SipEndpoint endpoint = SipEndpoint.builder(URI.create("foo"))
.uri(uri).headers(Collections.singletonMap("k1", "v1"))
.userToUserHeader(user2UserHeader).build();
.userToUserHeader(null).userToUserHeader(user2UserHeader).build();

ConnectAction connect = ConnectAction.builder(endpoint).build();

Expand Down

0 comments on commit 9ce3f4d

Please sign in to comment.