Skip to content
This repository has been archived by the owner on May 14, 2020. It is now read-only.

Commit

Permalink
#14 Reverted api code as enum changes
Browse files Browse the repository at this point in the history
axelerod committed Sep 30, 2015

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 7bd087b commit a60b8d0
Showing 9 changed files with 24 additions and 39 deletions.
Original file line number Diff line number Diff line change
@@ -20,6 +20,7 @@
import com.smartling.api.sdk.dto.ApiResponseWrapper;
import com.smartling.api.sdk.dto.Data;
import com.smartling.api.sdk.util.HttpUtils;

import org.apache.commons.lang3.CharEncoding;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.Validate;
@@ -31,7 +32,6 @@
import java.util.Collections;
import java.util.List;

import static com.smartling.api.sdk.dto.ApiCode.SUCCESS;
import static com.smartling.api.sdk.file.FileApiParams.API_KEY;
import static com.smartling.api.sdk.file.FileApiParams.PROJECT_ID;

@@ -48,6 +48,8 @@ public abstract class BaseApiClientAdapter

private HttpUtils httpUtils;

protected static final String SUCCESS_CODE = "SUCCESS";

protected String baseApiUrl;
protected String apiKey;
protected String projectId;
@@ -190,7 +192,7 @@ protected String getApiResponseMessages(final ApiResponse<?> apiResponse)
{
String responseMessages = StringUtils.EMPTY;

if (SUCCESS != apiResponse.getCode())
if (!SUCCESS_CODE.equals(apiResponse.getCode()))
responseMessages = String.format(RESPONSE_MESSAGES, StringUtils.join(apiResponse.getMessages(), ", "));

return responseMessages;
Original file line number Diff line number Diff line change
@@ -134,7 +134,7 @@ public StringResponse getFile(final GetFileParameterBuilder getFileParameterBuil
final HttpGet getRequest = new HttpGet(buildUrl(GET_FILE_API_URL, params));

final StringResponse stringResponse = getHttpUtils().executeHttpCall(getRequest, proxyConfiguration);
logger.debug("Get file: SUCCESS");
logger.debug(String.format("Get file: %s", SUCCESS_CODE));

return stringResponse;
}
6 changes: 0 additions & 6 deletions api-sdk/src/main/java/com/smartling/api/sdk/dto/ApiCode.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -32,7 +32,7 @@
public class ApiResponse<T extends Data>
{
private T data;
private ApiCode code;
private String code;
private List<String> messages;

/**
@@ -48,17 +48,15 @@ public T getData()

/**
* The response code returned from the Smartling Translation API.
*
* @return response code
*/
public ApiCode getCode()
public String getCode()
{
return code;
}

/**
* The messages returned form the Smartling Translation API.
*
* @return list of messages.
*/
public List<String> getMessages()
Original file line number Diff line number Diff line change
@@ -19,7 +19,6 @@

import com.google.gson.reflect.TypeToken;
import com.smartling.api.sdk.JsonReader;
import com.smartling.api.sdk.dto.ApiCode;
import com.smartling.api.sdk.dto.ApiResponse;
import com.smartling.api.sdk.dto.ApiResponseWrapper;
import com.smartling.api.sdk.dto.EmptyResponse;
@@ -35,19 +34,19 @@ public class ApiException extends Exception
{
private static final long serialVersionUID = -397098626101615761L;

private ApiCode apiCode;
private String apiCode;
private int httpCode;
private List<String> messages = new ArrayList<>();

ApiException(final String contents, List<String> messages, final ApiCode apiCode, int httpCode)
ApiException(final String contents, List<String> messages, final String apiCode, int httpCode)
{
super(contents);
this.messages = messages;
this.apiCode = apiCode;
this.httpCode = httpCode;
}

ApiException(final Exception e, ApiCode apiCode)
ApiException(final Exception e, String apiCode)
{
super(e);
messages.add(e.getMessage());
@@ -60,16 +59,16 @@ public static ApiException newException(String contents, int httpCode)
{
}
);
ApiCode apiCode = apiResponse.getCode();
String apiCode = apiResponse.getCode();
List<String> messages = apiResponse.getMessages();
return new ApiException(contents, messages, apiCode, httpCode);
}

public static ApiException newException(IOException e) {
return new ApiException(e, ApiCode.NETWORK_ERROR);
return new ApiException(e, "GENERAL_ERROR");
}

public ApiCode getApiCode()
public String getApiCode()
{
return apiCode;
}
Original file line number Diff line number Diff line change
@@ -22,12 +22,12 @@

import java.io.File;

import static com.smartling.api.sdk.dto.ApiCode.SUCCESS;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

public class ApiTestHelper
{
private static final String SUCCESS = "SUCCESS";
private static final FileType TEST_FILE_TYPE = FileType.JAVA_PROPERTIES;
private static final String TEST_FILE_LOCATION = "resources/test.properties";

Original file line number Diff line number Diff line change
@@ -31,6 +31,7 @@
import com.smartling.api.sdk.file.parameters.FileUploadParameterBuilder;
import com.smartling.api.sdk.util.DateFormatter;
import com.smartling.api.sdk.util.HttpUtils;

import org.apache.http.NameValuePair;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.client.utils.URLEncodedUtils;
@@ -45,15 +46,8 @@
import java.util.Date;
import java.util.List;

import static com.smartling.api.sdk.dto.ApiCode.SUCCESS;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;

public class FileApiClientAdapterTest
{
@@ -188,7 +182,7 @@ public void testRenameFile() throws ApiException, IOException
assertEquals(HOST, request.getURI().getHost());

// Validate the response
assertEquals(SUCCESS, apiResponse.getCode());
assertEquals("SUCCESS", apiResponse.getCode());
assertNull(apiResponse.getData());
}

@@ -212,7 +206,7 @@ public void testDeleteFile() throws ApiException, IOException
assertEquals(HOST, request.getURI().getHost());

// Validate the response
assertEquals(SUCCESS, apiResponse.getCode());
assertEquals("SUCCESS", apiResponse.getCode());
assertNull(apiResponse.getData());
}

@@ -240,7 +234,7 @@ public void testLastModified() throws ApiException, IOException
assertEquals(HOST, request.getURI().getHost());

// Validate the response
assertEquals(SUCCESS, apiResponse.getCode());
assertEquals("SUCCESS", apiResponse.getCode());
assertNotNull(apiResponse.getData());

FileLastModified fileLastModified = apiResponse.getData();
@@ -287,7 +281,7 @@ public void testUploadFile() throws ApiException, IOException
assertEquals(HOST, request.getURI().getHost());

// Validate the response
assertEquals(SUCCESS, apiResponse.getCode());
assertEquals("SUCCESS", apiResponse.getCode());
UploadFileData uploadFileData = apiResponse.getData();
assertEquals(1, uploadFileData.getStringCount());
assertEquals(2, uploadFileData.getWordCount());
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.smartling.api.sdk.exceptions;

import com.smartling.api.sdk.dto.ApiCode;
import org.junit.Test;

import java.io.IOException;
@@ -29,7 +28,7 @@ public void shouldRetrieveCode()
{
ApiException apiException = ApiException.newException(ERROR_RESPONSE, 0);

assertThat(apiException.getApiCode(), is(equalTo(ApiCode.VALIDATION_ERROR)));
assertThat(apiException.getApiCode(), is(equalTo("VALIDATION_ERROR")));
}

@Test
@@ -52,6 +51,6 @@ public void shouldPassHttpCode()
public void shouldSetNetworkErrorCodeInCaseIoException() {
ApiException apiException = ApiException.newException(new IOException("Some exception"));

assertThat(apiException.getApiCode(), is(ApiCode.NETWORK_ERROR));
assertThat(apiException.getApiCode(), is("GENERAL_ERROR"));
}
}
Original file line number Diff line number Diff line change
@@ -56,7 +56,6 @@ public class HttpUtilsTest
private StatusLine statusLine;

private static final String TEST_RESPONSE = "{\"response\":{\"data\":null,\"code\":\"VALIDATION_ERROR\",\"messages\":[\"apiKey parameter is required\"]}}";
private static final String TEST_MESSAGE = "apiKey parameter is required";

private static final String HOST = "host";
private static final String PASSWORD = "password";
@@ -114,7 +113,7 @@ public void testExecuteHttpCall404() throws ApiException, ClientProtocolExceptio
}
catch (ApiException e)
{
assertEquals(TEST_MESSAGE, e.getMessage());
assertEquals(TEST_RESPONSE, e.getMessage());
}
}

0 comments on commit a60b8d0

Please sign in to comment.