Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add Unit Tests to core package #243

Merged
merged 6 commits into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ dependencies {
implementation 'org.apache.httpcomponents:httpclient:4.5.13'
implementation 'com.fasterxml.jackson.core:jackson-databind:2.14.0'

testImplementation 'org.junit.jupiter:junit-jupiter-api:5.9.0'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.9.0','org.mockito:mockito-core:4.11.0'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.9.0'
testAnnotationProcessor 'org.projectlombok:lombok:1.18.24'
}
Expand Down
10 changes: 9 additions & 1 deletion src/main/java/com/crowdin/client/core/CrowdinApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
import com.crowdin.client.core.http.impl.json.JacksonJsonTransformer;
import com.crowdin.client.core.model.ClientConfig;
import com.crowdin.client.core.model.Credentials;
import org.apache.http.client.config.CookieSpecs;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.impl.client.HttpClientBuilder;

import java.util.Collections;
import java.util.HashMap;
Expand All @@ -18,7 +21,12 @@ public abstract class CrowdinApi {
protected final String url;

public CrowdinApi(Credentials credentials) {
this(credentials, ClientConfig.builder().httpClient(new ApacheHttpClient(credentials, new JacksonJsonTransformer(), Collections.emptyMap())).build());
this(credentials, ClientConfig.builder()
.httpClient(new ApacheHttpClient(credentials, new JacksonJsonTransformer(), Collections.emptyMap(), HttpClientBuilder
.create()
.setDefaultRequestConfig(RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD).build())
.build()))
.build());
}

public CrowdinApi(Credentials credentials, ClientConfig clientConfig) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,11 @@ public class ApacheHttpClient implements HttpClient {

private final CloseableHttpClient httpClient;

public ApacheHttpClient(Credentials credentials, JsonTransformer jsonTransformer, Map<String, ?> defaultHeaders) {
public ApacheHttpClient(Credentials credentials, JsonTransformer jsonTransformer, Map<String, ?> defaultHeaders, CloseableHttpClient httpClient) {
this.credentials = credentials;
this.jsonTransformer = jsonTransformer;
this.defaultHeaders = defaultHeaders;
this.httpClient = HttpClientBuilder
.create()
.setDefaultRequestConfig(RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD).build())
.build();
this.httpClient = httpClient;
andrii-bodnar marked this conversation as resolved.
Show resolved Hide resolved
}

public ApacheHttpClient(Credentials credentials, JsonTransformer jsonTransformer, Map<String, ?> defaultHeaders, ClientConfig.Host proxy, ClientConfig.UsernamePasswordCredentials proxyCreds, Integer timeoutMs) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.crowdin.client.core.http.impl.json;

import com.fasterxml.jackson.core.JacksonException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.*;
Expand All @@ -23,7 +22,7 @@ public EmptyArrayToNullDeserializer(JavaType type) {
}

@Override
public Object deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JacksonException {
public Object deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
if (p.getCurrentToken() == JsonToken.VALUE_NULL) {
return null;
}
Expand All @@ -43,11 +42,11 @@ public Object deserialize(JsonParser p, DeserializationContext ctxt) throws IOEx
}

private static boolean isCollectionType(Class<?> type) {
return type.isArray() && Collection.class.isAssignableFrom(type);
return type.isArray() || Collection.class.isAssignableFrom(type);
andrii-bodnar marked this conversation as resolved.
Show resolved Hide resolved
}

@Override
public JsonDeserializer<?> createContextual(DeserializationContext ctxt, BeanProperty property) throws JsonMappingException {
public JsonDeserializer<?> createContextual(DeserializationContext ctxt, BeanProperty property) {
return new EmptyArrayToNullDeserializer(property.getType());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.crowdin.client.core.http;

import com.crowdin.client.Client;
import com.crowdin.client.core.model.ClientConfig;
import com.crowdin.client.core.model.Credentials;

public class CrowdinApiConstructorTest extends Client {

public CrowdinApiConstructorTest(Credentials credentials, ClientConfig clientConfig) {
super(credentials, clientConfig);
}

public CrowdinApiConstructorTest(Credentials credentials) {
super(credentials);
}

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

public String geturl() {
return this.url;
}
}
79 changes: 79 additions & 0 deletions src/test/java/com/crowdin/client/core/http/CrowdinApiTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package com.crowdin.client.core.http;

import com.crowdin.client.core.http.impl.http.ApacheHttpClient;
import com.crowdin.client.core.http.impl.json.JacksonJsonTransformer;
import com.crowdin.client.core.model.ClientConfig;
import com.crowdin.client.core.model.Credentials;
import com.crowdin.client.framework.TestHttpClient;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;

import static org.junit.jupiter.api.Assertions.*;

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class CrowdinApiTest {

private final String organization = "testOrganization";
private final String token = "testToken";
private final String regularUrl = "https://api.crowdin.com/api/v2";
private final String organizationUrl = "https://testOrganization.api.crowdin.com/api/v2";

@Test
public void testCrowdinApiWithNullInputs() {
CrowdinApiConstructorTest client = new CrowdinApiConstructorTest(new Credentials(null, null, null), ClientConfig.builder()
.userAgent("User-Agent")
.build());
assertNotNull(client);
assertInstanceOf(ApacheHttpClient.class, client.getHttpClient());
}

@Test
public void testCrowdinApiWithAgent() {
CrowdinApiConstructorTest client = new CrowdinApiConstructorTest(new Credentials(null, null, null), ClientConfig.builder()
.integrationUserAgent("X-Crowdin-Integrations-User-Agent")
.jsonTransformer(new JacksonJsonTransformer())
.build());
assertNotNull(client);
assertInstanceOf(ApacheHttpClient.class, client.getHttpClient());
assertEquals(client.geturl(), regularUrl);
}

@Test
public void testCrowdinApiWithBaseUrl() {
CrowdinApiConstructorTest client = new CrowdinApiConstructorTest(new Credentials(token, organization, "/"), ClientConfig.builder()
.httpClient(new TestHttpClient()).build());
assertEquals(client.geturl(), "/api/v2");
}

@Test
public void testCrowdinApiWithBaseUrlEmpty() {
CrowdinApiConstructorTest client = new CrowdinApiConstructorTest(new Credentials(token, organization, ""), ClientConfig.builder()
.userAgent("User-Agent")
.httpClient(new TestHttpClient()).build());
assertEquals(client.geturl(), "/api/v2");
}

@Test
public void testCrowdinApiForRegularUrl() {
CrowdinApiConstructorTest client = new CrowdinApiConstructorTest(new Credentials(token, null, null), ClientConfig.builder()
.userAgent("User-Agent")
.httpClient(new TestHttpClient()).build());
assertEquals(client.geturl(), regularUrl);
}

@Test
public void testCrowdinApiForOrganizationUrl() {
CrowdinApiConstructorTest client = new CrowdinApiConstructorTest(new Credentials(token, organization, null), ClientConfig.builder()
.userAgent("User-Agent")
.httpClient(new TestHttpClient()).build());
assertEquals(client.geturl(), organizationUrl);
}

@Test
public void testCrowdinApiConstructor() {
CrowdinApiConstructorTest client = new CrowdinApiConstructorTest(new Credentials(token, organization, null));
assertInstanceOf(ApacheHttpClient.class, client.getHttpClient());
assertEquals(client.geturl(), organizationUrl);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.crowdin.client.core.http.exception;

import com.crowdin.client.core.http.exceptions.HttpException;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class HttpExceptionTest {

@Test
public void httpExceptionFromTest() {
HttpException message = HttpException.fromMessage("Exception message!");
assertEquals(message.getError().getMessage(), "Exception message!");
}
}
Loading
Loading