Skip to content

Commit

Permalink
Improve coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
SMadani committed Dec 21, 2023
1 parent 38ef1fa commit d90251b
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.vonage.client.Jsonable;

/**
* TODO see if this is needed
*/
@JsonIgnoreProperties(ignoreUnknown = true)
public class MuteSessionResponse implements Jsonable {
class MuteSessionResponse implements Jsonable {
private String applicationId, name;
private ProjectStatus status;
private ProjectEnvironment environment;
Expand Down
10 changes: 7 additions & 3 deletions src/test/java/com/vonage/client/ClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public abstract class ClientTest<T> {
protected final String applicationId = UUID.randomUUID().toString();
protected final String apiKey = "a1b2c3d4";
protected final String apiSecret = "1234567890abcdef";
protected final String testReason = "Test reason";
protected HttpWrapper wrapper;
protected T client;

Expand All @@ -63,7 +64,7 @@ protected HttpClient stubHttpClient(int statusCode, String content, String... ad
InputStream[] contentsEncoded = Arrays.stream(additionalReturns).map(transformation).toArray(InputStream[]::new);
when(entity.getContent()).thenReturn(transformation.apply(content), contentsEncoded);
when(sl.getStatusCode()).thenReturn(statusCode);
when(sl.getReasonPhrase()).thenReturn("Test reason");
when(sl.getReasonPhrase()).thenReturn(testReason);
when(response.getStatusLine()).thenReturn(sl);
when(response.getEntity()).thenReturn(entity);

Expand Down Expand Up @@ -129,7 +130,6 @@ protected <E extends VonageApiResponseException> E assertApiResponseException(
int statusCode, String response, Class<E> exClass, Executable invocation) throws Exception {
E expectedResponse = (E) exClass.getDeclaredMethod("fromJson", String.class).invoke(exClass, response);
String expectedJson = expectedResponse.toJson();
assertTrue(expectedJson.length() > 1 && expectedJson.length() <= response.length());
wrapper.setHttpClient(stubHttpClient(statusCode, expectedJson));
java.lang.reflect.Method setStatusCode = exClass.getDeclaredMethod("setStatusCode", int.class);
setStatusCode.setAccessible(true);
Expand All @@ -142,8 +142,12 @@ protected <E extends VonageApiResponseException> E assertApiResponseException(
}
catch (Throwable ex) {
assertEquals(exClass, ex.getClass(), failPrefix + ex.getClass());
if (expectedResponse.getTitle() == null) {
expectedResponse.title = testReason;
}
assertEquals(expectedResponse, ex);
assertEquals(expectedJson, ((E) ex).toJson());
String actualJson = ((E) ex).toJson().replace("\"title\":\""+testReason+"\",", "");
assertEquals(expectedJson, actualJson);
}
return expectedResponse;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ public void testTopUpNoTransactionId() throws Exception {

@Test
public void testTopUpFailed() throws Exception {
String json = "{\"error-code\":\"420\",\"error-code-label\":\"topup failed\",\"title\":\"Test reason\"}";
String json = "{\"error-code\":\"420\",\"error-code-label\":\"topup failed\"}";
AccountResponseException ex = assertApiResponseException(401, json, AccountResponseException.class,
() -> client.topUp("ABC123")
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ public NumbersClientTest() {

void assert401ResponseException(Executable invocation) throws Exception {
String response = "{\n" +
" \"title\": \"Test reason\",\n" +
" \"error-code\": \"401\",\n" +
" \"error-code-label\": \"authentication failed\"\n" +
"}";
Expand Down
44 changes: 27 additions & 17 deletions src/test/java/com/vonage/client/video/VideoClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import com.vonage.client.TestUtils;
import com.vonage.client.auth.JWTAuthMethod;
import com.vonage.client.common.HttpMethod;
import org.apache.http.client.HttpResponseException;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.*;
import org.junit.jupiter.api.function.Executable;
Expand Down Expand Up @@ -125,11 +124,6 @@ void stubResponseAndRun(Runnable invocation) throws Exception {
stubResponseAndRun(200, invocation);
}

void stubResponseAndAssertThrowsHttpResponseException(int statusCode, String response,
Executable invocation) throws Exception {
stubResponseAndAssertThrows(statusCode, response, invocation, HttpResponseException.class);
}

void stubResponseAndAssertThrowsIAX(int statusCode, Executable invocation) throws Exception {
stubResponseAndAssertThrows(statusCode, invocation, IllegalArgumentException.class);
}
Expand All @@ -148,17 +142,13 @@ void stubResponseAndAssertThrowsNPE(Executable invocation) throws Exception {

void stubResponseAndAssertThrowsVideoException(int statusCode, String response,
Executable invocation) throws Exception {
stubResponseAndAssertThrows(statusCode, response, invocation, VideoResponseException.class);
assertApiResponseException(statusCode, response, VideoResponseException.class, invocation);
}

void stubArchiveJsonAndAssertThrows(Executable invocation) throws Exception {
stubResponseAndAssertThrowsIAX(archiveJson, invocation);
}

void stubListArchiveJsonAndAssertThrows(Executable invocation) throws Exception {
stubResponseAndAssertThrowsIAX(listArchiveJson, invocation);
}

void stubArchiveJsonAndAssertEquals(Supplier<Archive> invocation) throws Exception {
stubResponse(archiveJson);
assertArchiveEqualsExpectedJson(invocation.get());
Expand Down Expand Up @@ -212,10 +202,6 @@ void stubBroadcastJsonAndAssertThrows(Executable invocation) throws Exception {
stubResponseAndAssertThrowsIAX(broadcastJson, invocation);
}

void stubListBroadcastJsonAndAssertThrows(Executable invocation) throws Exception {
stubResponseAndAssertThrowsIAX(listBroadcastJson, invocation);
}

void stubBroadcastJsonAndAssertEquals(Supplier<Broadcast> invocation) throws Exception {
stubResponse(broadcastJson);
assertBroadcastEqualsExpectedJson(invocation.get());
Expand Down Expand Up @@ -1212,6 +1198,30 @@ protected MuteSessionRequest sampleRequest() {
protected String sampleRequestBodyString() {
return "{\"active\":true,\"excludedStreamIds\":[\"ID_0\",\"ID_1\",\"ID_2\"]}";
}

@Override
public void runTests() throws Exception {
super.runTests();
testParseResponse();
}

private void testParseResponse() throws Exception {
long createdAt = 1414642898000L;
String name = "Joe Montana", responseJson = "{\n" +
" \"applicationId\": \""+applicationId+"\",\n" +
" \"status\": \"ACTIVE\",\n" +
" \"name\": \""+name+"\",\n" +
" \"environment\": \"standard\",\n" +
" \"createdAt\": "+createdAt+"\n" +
"}";
MuteSessionResponse parsed = stubResponseAndGet(responseJson, this::executeEndpoint);
assertNotNull(parsed);
assertEquals(applicationId, parsed.getApplicationId());
assertEquals(name, parsed.getName());
assertEquals(ProjectStatus.ACTIVE, parsed.getStatus());
assertEquals(ProjectEnvironment.STANDARD, parsed.getEnvironment());
assertEquals(createdAt, parsed.getCreatedAt());
}
}
.runTests();
}
Expand Down Expand Up @@ -1544,13 +1554,13 @@ protected String expectedEndpointUri(SipDialRequest request) {

@Override
protected SipDialRequest sampleRequest() {
return SipDialRequest.builder().token("eYjwToken").sessionId(sessionId)
return SipDialRequest.builder().token(token).sessionId(sessionId)
.uri(URI.create("sip.example.com"), true).build();
}

@Override
protected String sampleRequestBodyString() {
return "{\"sessionId\":\""+sessionId+"\",\"token\":\"eYjwToken\"," +
return "{\"sessionId\":\""+sessionId+"\",\"token\":\""+token+"\"," +
"\"sip\":{\"uri\":\"sip.example.com;transport=tls\"}}";
}
}
Expand Down

0 comments on commit d90251b

Please sign in to comment.