Skip to content

Commit

Permalink
feat: refactored after review
Browse files Browse the repository at this point in the history
-externalized json into resource directory
  • Loading branch information
DecarteAdam committed May 7, 2024
1 parent d83dc88 commit 7e5acf8
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ public CrowdinApiConstructorTest(Credentials credentials) {
super(credentials);
}

public HttpClient getHttpClient(){
public HttpClient getHttpClient() {
return this.httpClient;
}

public String geturl(){
public String geturl() {
return this.url;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,16 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.*;

import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;
Expand All @@ -39,19 +44,16 @@ public class ApacheHttpClientTest {

private ApacheHttpClient httpClient;

private final String json = "{\n" +
" \"id\": 123456789,\n" +
" \"title\": \"Example Label\",\n" +
" \"isSystem\": {\n" +
" \"value\": true\n" +
" }\n" +
"}\n";
private String jsonResponse;
private String errorResponse;


@BeforeEach
void setUp() {
void setUp() throws IOException {
String organization = "testOrganization";
String token = "testToken";
String resourceDir = "api/core/clientResponse.json";
String errorResponseDir = "api/core/errorResponse.json";

Credentials credentials = new Credentials(token, organization);
JsonTransformer jsonTransformer = new JacksonJsonTransformer();
Expand All @@ -63,21 +65,35 @@ void setUp() {

// Create an instance of ApacheHttpClient
httpClient = new ApacheHttpClient(credentials, jsonTransformer, defaultHeaders, client);

jsonResponse = getFile(resourceDir);
errorResponse = getFile(errorResponseDir);
}

private String getFile(String resourcePath) throws IOException {
try (InputStream responseInputStream = this.getClass().getClassLoader().getResourceAsStream(resourcePath)) {
if (responseInputStream != null) {
return jsonResponse = new BufferedReader(new InputStreamReader(responseInputStream)).lines().collect(Collectors.joining("\n"));
} else {
throw new IOException("File not found: " + resourcePath);
}

}
}

@Test
public void testGetRequest() throws IOException {
// Mock the behavior of client.execute to return mockedResponse
when(client.execute(any(HttpUriRequest.class))).thenReturn(mockedResponse);
when(mockedResponse.getStatusLine()).thenReturn(new BasicStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK"));
when(mockedResponse.getEntity()).thenReturn(new StringEntity(json));
when(mockedResponse.getEntity()).thenReturn(new StringEntity(jsonResponse));

// Make a GET request and check the response
try {
String response = httpClient.get(organizationUrl, new HttpRequestConfig(), String.class);
// Assert that the response is not null
Assertions.assertNotNull(response);
assertEquals(response, json);
assertEquals(response, jsonResponse);
} catch (HttpException | HttpBadRequestException e) {
e.printStackTrace();
Assertions.fail("HTTP request failed: " + e.getMessage());
Expand All @@ -92,21 +108,7 @@ public void testGetRequestError() throws IOException {
// Mock the behavior of client.execute to return mockedResponse
when(client.execute(any(HttpUriRequest.class))).thenReturn(mockedResponse);
when(mockedResponse.getStatusLine()).thenReturn(new BasicStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_BAD_REQUEST, "FAIL"));
when(mockedResponse.getEntity()).thenReturn(new StringEntity("{\n" +
" \"errors\": [\n" +
" {\n" +
" \"error\": {\n" +
" \"key\": \"400\",\n" +
" \"errors\": [\n" +
" {\n" +
" \"code\": \"400\",\n" +
" \"message\": \"Bad request exception!\"\n" +
" }\n" +
" ]\n" +
" }\n" +
" }\n" +
" ]\n" +
"}\n"));
when(mockedResponse.getEntity()).thenReturn(new StringEntity(errorResponse));


// Make a GET request and check the exception
Expand Down Expand Up @@ -142,13 +144,13 @@ public void tesPostRequest() throws IOException {
// Mock the behavior of client.execute to return mockedResponse
when(client.execute(any(HttpUriRequest.class))).thenReturn(mockedResponse);
when(mockedResponse.getStatusLine()).thenReturn(new BasicStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK"));
when(mockedResponse.getEntity()).thenReturn(new StringEntity(json));
when(mockedResponse.getEntity()).thenReturn(new StringEntity(jsonResponse));

// Make a POST request and check the response
try {
String response = httpClient.post(organizationUrl, requestData, new HttpRequestConfig(), String.class);
assertNotNull(response);
assertEquals(response, json);
assertEquals(response, jsonResponse);
} catch (HttpException | HttpBadRequestException e) {
e.printStackTrace();
Assertions.fail("HTTP request failed: " + e.getMessage());
Expand All @@ -160,17 +162,17 @@ public void tesPostRequest() throws IOException {

@Test
public void tesPostRequestWithInputStream() throws IOException {
InputStream requestData = new ByteArrayInputStream(json.getBytes(StandardCharsets.UTF_8));
InputStream requestData = new ByteArrayInputStream(jsonResponse.getBytes(StandardCharsets.UTF_8));
// Mock the behavior of client.execute to return mockedResponse
when(client.execute(any(HttpUriRequest.class))).thenReturn(mockedResponse);
when(mockedResponse.getStatusLine()).thenReturn(new BasicStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK"));
when(mockedResponse.getEntity()).thenReturn(new StringEntity(json));
when(mockedResponse.getEntity()).thenReturn(new StringEntity(jsonResponse));

// Make a POSt request and check the response
try {
String response = httpClient.post(organizationUrl, requestData, new HttpRequestConfig(), String.class);
Assertions.assertNotNull(response);
assertEquals(response, json);
assertEquals(response, jsonResponse);
} catch (HttpException | HttpBadRequestException e) {
e.printStackTrace();
Assertions.fail("HTTP request failed: " + e.getMessage());
Expand All @@ -185,13 +187,13 @@ public void tesPostRequestWithString() throws IOException {
// Mock the behavior of client.execute to return mockedResponse
when(client.execute(any(HttpUriRequest.class))).thenReturn(mockedResponse);
when(mockedResponse.getStatusLine()).thenReturn(new BasicStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK"));
when(mockedResponse.getEntity()).thenReturn(new StringEntity(json));
when(mockedResponse.getEntity()).thenReturn(new StringEntity(jsonResponse));

// Make a POST request and check the response
try {
String response = httpClient.post(organizationUrl, json, new HttpRequestConfig(), String.class);
String response = httpClient.post(organizationUrl, jsonResponse, new HttpRequestConfig(), String.class);
Assertions.assertNotNull(response);
assertEquals(response, json);
assertEquals(response, jsonResponse);
} catch (HttpException | HttpBadRequestException e) {
e.printStackTrace();
Assertions.fail("HTTP request failed: " + e.getMessage());
Expand All @@ -206,13 +208,13 @@ public void tesPostRequestWithNullData() throws IOException {
// Mock the behavior of client.execute to return mockedResponse
when(client.execute(any(HttpUriRequest.class))).thenReturn(mockedResponse);
when(mockedResponse.getStatusLine()).thenReturn(new BasicStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK"));
when(mockedResponse.getEntity()).thenReturn(new StringEntity(json));
when(mockedResponse.getEntity()).thenReturn(new StringEntity(jsonResponse));

// Make a POST request and check the response
try {
String response = httpClient.post(organizationUrl, null, new HttpRequestConfig(), String.class);
Assertions.assertNotNull(response);
assertEquals(response, json);
assertEquals(response, jsonResponse);
} catch (HttpException | HttpBadRequestException e) {
e.printStackTrace();
Assertions.fail("HTTP request failed: " + e.getMessage());
Expand Down
7 changes: 7 additions & 0 deletions src/test/resources/api/core/clientResponse.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"id": 123456789,
"title": "Example Label",
"isSystem": {
"value": true
}
}
15 changes: 15 additions & 0 deletions src/test/resources/api/core/errorResponse.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"errors": [
{
"error": {
"key": 400,
"errors": [
{
"code": 400,
"message": "Bad request exception!"
}
]
}
}
]
}

0 comments on commit 7e5acf8

Please sign in to comment.