Skip to content

Commit

Permalink
Split exceptions, refactored request url
Browse files Browse the repository at this point in the history
  • Loading branch information
harshitvora committed Apr 30, 2018
1 parent e410922 commit bc08795
Show file tree
Hide file tree
Showing 11 changed files with 158 additions and 37 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

== Version 2.1.2 (April 24, 2018)
* Fixed error handling for Document Retrieve request
* Split exceptions into
- ChargebackDocumentException : thrown when there is error in retrieve document
- ChargebackWebException : thrown when http status code is not 200 (success)
- ChargebackException : thrown when there are any other kinds of exceptions in the SDK
* Fixed certification test cases
* Internal code refactoring

Expand Down
13 changes: 11 additions & 2 deletions src/functionalTest/java/com/cnp/sdk/TestChargebackDocument.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,20 @@ public void testErrorResponse(){
assertEquals("001", response.getResponseCode());
assertEquals("Invalid Merchant", response.getResponseMessage());

try{
cbk.retrieveDocument(123009L, "logo.tiff", "test.tiff");
fail("Expected Exception");
} catch (ChargebackDocumentException e){
assertEquals("Document Not Found", e.getMessage());
assertEquals("009", e.getCode());
}

try{
cbk.retrieveDocument(123404L, "logo.tiff", "test.tiff");
fail("Expected Exception");
} catch (ChargebackException e){
assertEquals("404 : Not Found - Could not find requested object.", e.getMessage());
} catch (ChargebackWebException e){
assertEquals("Could not find requested object.", e.getMessage());
assertEquals("404", e.getCode());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,9 @@ public void testErrorResponse(){
try{
ChargebackRetrievalResponse response = cbk.getChargebackByCaseId(404L);
fail("Expected Exception");
} catch (ChargebackException e){
assertEquals("404 : Not Found - Could not find requested object.", e.getMessage());
} catch (ChargebackWebException e){
assertEquals("Could not find requested object.", e.getMessage());
assertEquals("404", e.getCode());
}
}
}
5 changes: 3 additions & 2 deletions src/functionalTest/java/com/cnp/sdk/TestChargebackUpdate.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,9 @@ public void testErrorResponse(){
try{
cbk.addNoteToCase(404L, "ErrorResponse");
fail("Expected Exception");
} catch (ChargebackException e){
assertEquals("404 : Not Found - Could not find requested object.", e.getMessage());
} catch (ChargebackWebException e){
assertEquals("Could not find requested object.", e.getMessage());
assertEquals("404", e.getCode());
}
}
}
5 changes: 3 additions & 2 deletions src/main/java/com/cnp/sdk/ChargebackDocument.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ public class ChargebackDocument {
private Properties config;
private Communication communication;
private String baseUrl;
private final String SERVICE_ROUTE = "/services/chargebacks";

public ChargebackDocument() {
communication = new Communication();
config = (new Configuration()).getProperties();
baseUrl = config.getProperty("url");
baseUrl = config.getProperty("url") + SERVICE_ROUTE;
}

/**
Expand All @@ -47,7 +48,7 @@ public ChargebackDocument() {
public ChargebackDocument(Properties config) {
this.config = config;
communication = new Communication(config);
baseUrl = config.getProperty("url");
baseUrl = config.getProperty("url") + SERVICE_ROUTE;
}

public void setCommunication(Communication communication) {
Expand Down
31 changes: 31 additions & 0 deletions src/main/java/com/cnp/sdk/ChargebackDocumentException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.cnp.sdk;

public class ChargebackDocumentException extends RuntimeException {

private static final long serialVersionUID = 1L;
private String code;

public ChargebackDocumentException(String message, String code) {
super(message);
this.code = code;
}

public ChargebackDocumentException(String message, Exception ume) {
super(message, ume);
this.code = "0";
}

public ChargebackDocumentException(String message) {
super(message);
this.code = "0";
}

public String getCode() {
return code;
}

public void setCode(String code) {
this.code = code;
}

}
5 changes: 3 additions & 2 deletions src/main/java/com/cnp/sdk/ChargebackRetrieval.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ public class ChargebackRetrieval {
private Properties config;
private Communication communication;
private String baseurl;
private final String SERVICE_ROUTE = "/chargebacks";

public ChargebackRetrieval() {
communication = new Communication();
config = (new Configuration()).getProperties();
baseurl = config.getProperty("url");
baseurl = config.getProperty("url") + SERVICE_ROUTE;
}

/**
Expand All @@ -47,7 +48,7 @@ public ChargebackRetrieval() {
public ChargebackRetrieval(Properties config) {
this.config = config;
communication = new Communication(config);
baseurl = config.getProperty("url");
baseurl = config.getProperty("url") + SERVICE_ROUTE;
}

public void setCommunication(Communication communication) {
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/com/cnp/sdk/ChargebackUpdate.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ public class ChargebackUpdate {
private Properties config;
private Communication communication;
private String baseurl;
private final String SERVICE_ROUTE = "/chargebacks";

public ChargebackUpdate() {
communication = new Communication();
config = (new Configuration()).getProperties();
baseurl = config.getProperty("url");
baseurl = config.getProperty("url") + SERVICE_ROUTE;
}

/**
Expand All @@ -48,7 +49,7 @@ public ChargebackUpdate() {
public ChargebackUpdate(Properties config) {
this.config = config;
communication = new Communication(config);
baseurl = config.getProperty("url");
baseurl = config.getProperty("url") + SERVICE_ROUTE;
}

public void setCommunication(Communication communication) {
Expand Down Expand Up @@ -116,7 +117,7 @@ public ChargebackUpdateResponse requestArbitration(Long caseId, String note){

private ChargebackUpdateResponse getUpdateResponse(Long caseId, ChargebackUpdateRequest request){
String xmlRequest = XMLConverter.generateUpdateRequest(request);
String requestUrl = baseurl + caseId;
String requestUrl = baseurl + "/" + caseId;
return communication.httpPutUpdateRequest(xmlRequest, requestUrl);
}
}
47 changes: 47 additions & 0 deletions src/main/java/com/cnp/sdk/ChargebackWebException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.cnp.sdk;

import java.util.List;

public class ChargebackWebException extends RuntimeException {

private static final long serialVersionUID = 1L;
private String code;
private List<String> errorList;

public ChargebackWebException(String message, String code, List<String> errorList) {
super(message);
this.code = code;
this.errorList = errorList;
}

public ChargebackWebException(String message, String code) {
super(message);
this.code = code;
}

public ChargebackWebException(String message, Exception ume) {
super(message, ume);
this.code = "0";
}

public ChargebackWebException(String message) {
super(message);
this.code = "0";
}

public String getCode() {
return code;
}

public void setCode(String code) {
this.code = code;
}

public List<String> getErrorList() {
return errorList;
}

public void setErrorList(List<String> errorList) {
this.errorList = errorList;
}
}
65 changes: 45 additions & 20 deletions src/main/java/com/cnp/sdk/Communication.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public class Communication {
private CloseableHttpClient httpClient;
private final int KEEP_ALIVE_DURATION = 8000;
private final String CONTENT_TYPE_HEADER = "Content-Type";
private final String CONTENT_TYPE_VALUE = "application/com.vantivcnp.services-v2+xml";
private final String CNP_CONTENT_TYPE = "application/com.vantivcnp.services-v2+xml";
private final String ACCEPT_HEADER = "Accept";
private final String CONNECTION_EXCEPTION_MESSAGE = "Error connecting to Vantiv";
private final String XML_ENCODING = "UTF-8";
Expand Down Expand Up @@ -168,17 +168,17 @@ public ChargebackDocumentUploadResponse httpDeleteDocumentRequest(String request

public ChargebackRetrievalResponse httpGetRetrievalRequest(String requestUrl) {
HttpGet get = new HttpGet(requestUrl);
get.setHeader(CONTENT_TYPE_HEADER, CONTENT_TYPE_VALUE);
get.setHeader(ACCEPT_HEADER, CONTENT_TYPE_VALUE);
get.setHeader(CONTENT_TYPE_HEADER, CNP_CONTENT_TYPE);
get.setHeader(ACCEPT_HEADER, CNP_CONTENT_TYPE);
printToConsole("\nGET request to url: \n", requestUrl);
String response = sendHttpRequestToCnp(get);
return XMLConverter.generateRetrievalResponse(response);
}

public ChargebackUpdateResponse httpPutUpdateRequest(String xmlRequest, String requestUrl) {
HttpPut put = new HttpPut(requestUrl);
put.setHeader(CONTENT_TYPE_HEADER, CONTENT_TYPE_VALUE);
put.setHeader(ACCEPT_HEADER, CONTENT_TYPE_VALUE);
put.setHeader(CONTENT_TYPE_HEADER, CNP_CONTENT_TYPE);
put.setHeader(ACCEPT_HEADER, CNP_CONTENT_TYPE);
put.setEntity(new StringEntity(xmlRequest, XML_ENCODING));
printToConsole("\nPUT request to url: \n", requestUrl);
printToConsole("\nRequest XML: \n", xmlRequest);
Expand Down Expand Up @@ -285,17 +285,21 @@ private String validateResponse(HttpResponse response){
String xmlResponse;
try{
entity = response.getEntity();
if (response.getStatusLine().getStatusCode() != 200) {
xmlResponse = EntityUtils.toString(entity, XML_ENCODING);
System.out.println("\n" + xmlResponse);
ErrorResponse errorResponse = XMLConverter.generateErrorResponse(xmlResponse);
throw new ChargebackException(response.getStatusLine().getStatusCode() + " : " +
response.getStatusLine().getReasonPhrase() + " - " + getErrorMessage(errorResponse));
String contentType = entity.getContentType().getValue();
int statusCode = response.getStatusLine().getStatusCode();
xmlResponse = EntityUtils.toString(entity, XML_ENCODING);

if (statusCode != 200) {
printToConsole("\nErrorResponse: ", xmlResponse);
if(contentType.contains(CNP_CONTENT_TYPE)){
ErrorResponse errorResponse = XMLConverter.generateErrorResponse(xmlResponse);
throw new ChargebackWebException(getErrorMessage(errorResponse), String.valueOf(statusCode), errorResponse.getErrors().getErrors());
}
throw new ChargebackWebException(xmlResponse, String.valueOf(statusCode));
}
xmlResponse = EntityUtils.toString(entity,XML_ENCODING);
}
catch (IOException e) {
throw new ChargebackException("There was an exception while fetching the response xml.", e);
throw new ChargebackWebException("There was an exception while fetching the response xml.", e);
}
finally {
if (entity != null) {
Expand All @@ -314,12 +318,25 @@ private void validateDocumentResponse(HttpResponse response, String filepath){
String xmlResponse;
try{
entity = response.getEntity();
if (response.getStatusLine().getStatusCode() != 200 || !"image/tiff".equals(entity.getContentType().getValue())) {
String contentType = entity.getContentType().getValue();
int statusCode = response.getStatusLine().getStatusCode();

if(contentType.contains(CNP_CONTENT_TYPE)) {
xmlResponse = EntityUtils.toString(entity, XML_ENCODING);
printToConsole("\nErrorResponse: ", xmlResponse);
if (statusCode != 200) {
ErrorResponse errorResponse = XMLConverter.generateErrorResponse(xmlResponse);
throw new ChargebackWebException(getErrorMessage(errorResponse), String.valueOf(statusCode), errorResponse.getErrors().getErrors());
}
else{
ChargebackDocumentUploadResponse errorResponse = XMLConverter.generateDocumentResponse(xmlResponse);
throw new ChargebackDocumentException(errorResponse.getResponseMessage(), errorResponse.getResponseCode());
}
}
else if(statusCode != 200){
xmlResponse = EntityUtils.toString(entity, XML_ENCODING);
System.out.println("\n" + xmlResponse);
ErrorResponse errorResponse = XMLConverter.generateErrorResponse(xmlResponse);
throw new ChargebackException(response.getStatusLine().getStatusCode() + " : " +
response.getStatusLine().getReasonPhrase() + " - " + getErrorMessage(errorResponse));
printToConsole("\nErrorResponse: ", xmlResponse);
throw new ChargebackWebException(xmlResponse, String.valueOf(statusCode));
}

InputStream is = entity.getContent();
Expand All @@ -331,7 +348,7 @@ private void validateDocumentResponse(HttpResponse response, String filepath){
fos.close();
}
catch (IOException e) {
throw new ChargebackException("There was an exception while fetching the requested document.", e);
throw new ChargebackDocumentException("There was an exception while fetching the requested document.", e);
}
finally {
if (entity != null) {
Expand Down Expand Up @@ -370,6 +387,14 @@ public String neuterXml(String xml) {
}

private String getErrorMessage(ErrorResponse errorResponse){
return errorResponse.getErrors().getErrors().get(0);
StringBuilder errorMessage = new StringBuilder();
String prefix = "";

for (String error : errorResponse.getErrors().getErrors()) {
errorMessage.append(prefix).append(error);
prefix = "\n";
}

return errorMessage.toString();
}
}
8 changes: 4 additions & 4 deletions src/main/java/com/cnp/sdk/Setup.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ public class Setup {

/* List of environments for the configuration. */
private enum EnvironmentConfiguration {
SANDBOX("sandbox", "https://www.testvantivcnp.com/sandbox/new/sandbox/services/chargebacks"),
PRELIVE("prelive", "https://services.vantivprelive.com/services/chargebacks"),
POSTLIVE("postlive", "https://services.vantivpostlive.com/services/chargebacks"),
PRODUCTION("production", "https://services.vantivcnp.com/services/chargebacks"),
SANDBOX("sandbox", "https://www.testvantivcnp.com/sandbox/new"),
PRELIVE("prelive", "https://services.vantivprelive.com"),
POSTLIVE("postlive", "https://services.vantivpostlive.com"),
PRODUCTION("production", "https://services.vantivcnp.com"),
OTHER("other", "You will be asked for all the values");

private final String key;
Expand Down

0 comments on commit bc08795

Please sign in to comment.