Skip to content

Commit

Permalink
* Add ability to log api response when logging is in debug mode.
Browse files Browse the repository at this point in the history
* Fix failing test due to logging response change.
  • Loading branch information
yallen011 committed Aug 19, 2020
1 parent ed26b8d commit 1b89a5e
Show file tree
Hide file tree
Showing 11 changed files with 131 additions and 4 deletions.
6 changes: 4 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## [5.3.1]
## [5.4.0]
### Added
- Added AppEndpoint for ConnectAction
- Added AppEndpoint support for ConnectAction.
- Added the ability to log the API response in when logger is set to debug mode.
- Added ability to override base url for StopStream, ModifyCall, and Sn

## [5.3.0]
### Added
Expand Down
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ dependencies {
testCompile 'junit:junit:4.4'
testCompile "org.mockito:mockito-core:2.25.1"
testCompile 'org.hamcrest:hamcrest-all:1.3'
testCompile group: 'org.powermock', name: 'powermock-api-mockito2', version: '2.0.7'
testCompile group: 'org.powermock', name: 'powermock-module-junit4', version: '2.0.7'
}

test {
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/nexmo/client/AbstractMethod.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
package com.nexmo.client;

import com.nexmo.client.auth.AuthMethod;
import com.nexmo.client.logging.LoggingUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpEntity;
Expand Down Expand Up @@ -100,6 +101,9 @@ public ResultT execute(RequestT request) throws NexmoResponseParseException, Nex
LOG.debug(EntityUtils.toString(enclosingRequest.getEntity()));
}
HttpResponse response = this.httpWrapper.getHttpClient().execute(httpRequest);

LOG.debug("Response: " + LoggingUtils.logResponse(response));

try{
return parseResponse(response);
}
Expand Down
53 changes: 53 additions & 0 deletions src/main/java/com/nexmo/client/logging/LoggingUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright (c) 2011-2017 Nexmo Inc
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.nexmo.client.logging;

import org.apache.http.HttpResponse;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

public class LoggingUtils {

public static String logResponse(HttpResponse response) throws IOException {
StringBuilder log = new StringBuilder();
String responseBody = EntityUtils.toString(response.getEntity());
int statusCode = response.getStatusLine().getStatusCode();
String statusLine = response.getStatusLine().getReasonPhrase();

log.append("status_code: ")
.append(statusCode)
.append( ", ")
.append("status_line: ")
.append(statusLine)
.append(", ")
.append(" body: ")
.append(responseBody);

//Calling Http#getEntity will consume the entity so you have to replace the entity after logging
response.setEntity(new StringEntity(responseBody, ContentType.get(response.getEntity())));

return log.toString();
}
}
15 changes: 13 additions & 2 deletions src/test/java/com/nexmo/client/AbstractMethodTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,13 @@
import com.nexmo.client.auth.AuthCollection;
import com.nexmo.client.auth.AuthMethod;
import com.nexmo.client.auth.JWTAuthMethod;
import com.nexmo.client.logging.LoggingUtils;
import io.jsonwebtoken.lang.Assert;
import org.apache.commons.io.IOUtils;
import org.apache.http.*;
import org.apache.http.HttpEntity;
import org.apache.http.HttpEntityEnclosingRequest;
import org.apache.http.HttpResponse;
import org.apache.http.ProtocolVersion;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.methods.RequestBuilder;
Expand All @@ -38,16 +42,21 @@
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Set;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.*;
import static org.powermock.api.mockito.PowerMockito.mockStatic;


@RunWith(PowerMockRunner.class)
@PrepareForTest(LoggingUtils.class)
public class AbstractMethodTest {
private static class ConcreteMethod extends AbstractMethod<String, String> {
public ConcreteMethod(HttpWrapper httpWrapper) {
Expand Down Expand Up @@ -86,6 +95,7 @@ public String parseResponse(HttpResponse response) throws IOException{

@Before
public void setUp() throws Exception {
mockStatic(LoggingUtils.class);
mockWrapper = mock(HttpWrapper.class);
mockAuthMethods = mock(AuthCollection.class);
mockAuthMethod = mock(AuthMethod.class);
Expand All @@ -98,6 +108,7 @@ public void setUp() throws Exception {
1,
1
), 200, "OK")));
when(LoggingUtils.logResponse(any(HttpResponse.class))).thenReturn("response logged");
when(mockWrapper.getAuthCollection()).thenReturn(mockAuthMethods);
}

Expand Down
9 changes: 9 additions & 0 deletions src/test/java/com/nexmo/client/ClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,26 @@
package com.nexmo.client;

import com.nexmo.client.auth.TokenAuthMethod;
import com.nexmo.client.logging.LoggingUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpUriRequest;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import java.io.ByteArrayInputStream;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.powermock.api.mockito.PowerMockito.mockStatic;

@RunWith(PowerMockRunner.class)
@PrepareForTest(LoggingUtils.class)
public abstract class ClientTest<T extends AbstractClient> {
protected HttpWrapper wrapper;
protected T client;
Expand All @@ -46,12 +53,14 @@ public void setupWrapper() {

protected HttpClient stubHttpClient(int statusCode, String content) throws Exception {
HttpClient result = mock(HttpClient.class);
mockStatic(LoggingUtils.class);

HttpResponse response = mock(HttpResponse.class);
StatusLine sl = mock(StatusLine.class);
HttpEntity entity = mock(HttpEntity.class);

when(result.execute(any(HttpUriRequest.class))).thenReturn(response);
when(LoggingUtils.logResponse(any(HttpResponse.class))).thenReturn("response logged");
when(entity.getContent()).thenReturn(new ByteArrayInputStream(content.getBytes("UTF-8")));
when(sl.getStatusCode()).thenReturn(statusCode);
when(response.getStatusLine()).thenReturn(sl);
Expand Down
9 changes: 9 additions & 0 deletions src/test/java/com/nexmo/client/NexmoClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
package com.nexmo.client;

import com.nexmo.client.auth.*;
import com.nexmo.client.logging.LoggingUtils;
import com.nexmo.client.voice.Call;
import com.nexmo.client.voice.CallEvent;
import com.nexmo.client.voice.CallStatus;
Expand All @@ -36,6 +37,9 @@
import org.apache.http.client.methods.RequestBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import java.io.ByteArrayInputStream;
import java.nio.charset.StandardCharsets;
Expand All @@ -51,18 +55,23 @@
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.powermock.api.mockito.PowerMockito.mockStatic;

@RunWith(PowerMockRunner.class)
@PrepareForTest(LoggingUtils.class)
public class NexmoClientTest {
private TestUtils testUtils = new TestUtils();

private HttpClient stubHttpClient(int statusCode, String content) throws Exception {
HttpClient result = mock(HttpClient.class);
mockStatic(LoggingUtils.class);

HttpResponse response = mock(HttpResponse.class);
StatusLine sl = mock(StatusLine.class);
HttpEntity entity = mock(HttpEntity.class);

when(result.execute(any(HttpUriRequest.class))).thenReturn(response);
when(LoggingUtils.logResponse(any(HttpResponse.class))).thenReturn("response logged");
when(entity.getContent()).thenReturn(new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8)));
when(sl.getStatusCode()).thenReturn(statusCode);
when(response.getStatusLine()).thenReturn(sl);
Expand Down
9 changes: 9 additions & 0 deletions src/test/java/com/nexmo/client/numbers/NumbersClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,16 @@
import com.nexmo.client.TestUtils;
import com.nexmo.client.auth.AuthCollection;
import com.nexmo.client.auth.TokenAuthMethod;
import com.nexmo.client.logging.LoggingUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpUriRequest;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import java.io.ByteArrayInputStream;

Expand All @@ -39,18 +43,23 @@
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.powermock.api.mockito.PowerMockito.mockStatic;

@RunWith(PowerMockRunner.class)
@PrepareForTest(LoggingUtils.class)
public class NumbersClientTest {
private TestUtils testUtils = new TestUtils();

private HttpWrapper stubHttpWrapper(int statusCode, String content) throws Exception {
HttpClient client = mock(HttpClient.class);
mockStatic(LoggingUtils.class);

HttpResponse response = mock(HttpResponse.class);
StatusLine sl = mock(StatusLine.class);
HttpEntity entity = mock(HttpEntity.class);

when(client.execute(any(HttpUriRequest.class))).thenReturn(response);
when(LoggingUtils.logResponse(any(HttpResponse.class))).thenReturn("response logged");
when(entity.getContent()).thenReturn(new ByteArrayInputStream(content.getBytes("UTF-8")));
when(sl.getStatusCode()).thenReturn(statusCode);
when(response.getStatusLine()).thenReturn(sl);
Expand Down
10 changes: 10 additions & 0 deletions src/test/java/com/nexmo/client/sms/SmsClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.nexmo.client.HttpWrapper;
import com.nexmo.client.NexmoResponseParseException;
import com.nexmo.client.auth.TokenAuthMethod;
import com.nexmo.client.logging.LoggingUtils;
import com.nexmo.client.sms.messages.Message;
import com.nexmo.client.sms.messages.TextMessage;
import org.apache.http.HttpEntity;
Expand All @@ -35,6 +36,9 @@
import org.hamcrest.CoreMatchers;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import javax.xml.parsers.ParserConfigurationException;
import java.io.ByteArrayInputStream;
Expand All @@ -45,7 +49,10 @@

import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
import static org.powermock.api.mockito.PowerMockito.mockStatic;

@RunWith(PowerMockRunner.class)
@PrepareForTest(LoggingUtils.class)
public class SmsClientTest {
private HttpWrapper wrapper;
private SmsClient client;
Expand All @@ -57,13 +64,16 @@ public void setUp() throws ParserConfigurationException {
}

private HttpClient stubHttpClient(int statusCode, String content) throws Exception {
//Log log = mock(Log.class);
mockStatic(LoggingUtils.class);
HttpClient result = mock(HttpClient.class);

HttpResponse response = mock(HttpResponse.class);
StatusLine sl = mock(StatusLine.class);
HttpEntity entity = mock(HttpEntity.class);

when(result.execute(any(HttpUriRequest.class))).thenReturn(response);
when(LoggingUtils.logResponse(any(HttpResponse.class))).thenReturn("response logged");
when(entity.getContent()).thenReturn(new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8)));
when(sl.getStatusCode()).thenReturn(statusCode);
when(response.getStatusLine()).thenReturn(sl);
Expand Down
9 changes: 9 additions & 0 deletions src/test/java/com/nexmo/client/sns/SnsClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.nexmo.client.HttpWrapper;
import com.nexmo.client.NexmoResponseParseException;
import com.nexmo.client.auth.TokenAuthMethod;
import com.nexmo.client.logging.LoggingUtils;
import com.nexmo.client.sns.request.SnsPublishRequest;
import com.nexmo.client.sns.request.SnsSubscribeRequest;
import com.nexmo.client.sns.response.SnsPublishResponse;
Expand All @@ -36,14 +37,20 @@
import org.apache.http.client.methods.HttpUriRequest;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import java.io.ByteArrayInputStream;
import java.nio.charset.StandardCharsets;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.*;
import static org.powermock.api.mockito.PowerMockito.mockStatic;

@RunWith(PowerMockRunner.class)
@PrepareForTest(LoggingUtils.class)
public class SnsClientTest {
private HttpWrapper httpWrapper;
private SnsClient client;
Expand All @@ -56,12 +63,14 @@ public void setUp() throws Exception {

private HttpClient stubHttpClient(int statusCode, String content) throws Exception {
HttpClient result = mock(HttpClient.class);
mockStatic(LoggingUtils.class);

HttpResponse response = mock(HttpResponse.class);
StatusLine sl = mock(StatusLine.class);
HttpEntity entity = mock(HttpEntity.class);

when(result.execute(any(HttpUriRequest.class))).thenReturn(response);
when(LoggingUtils.logResponse(any(HttpResponse.class))).thenReturn("response logged");
when(entity.getContent()).thenReturn(new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8)));
when(sl.getStatusCode()).thenReturn(statusCode);
when(response.getStatusLine()).thenReturn(sl);
Expand Down
Loading

0 comments on commit 1b89a5e

Please sign in to comment.