Skip to content

Commit 3bd7711

Browse files
committed
Added unit tests
1 parent d5df9b8 commit 3bd7711

File tree

2 files changed

+107
-0
lines changed

2 files changed

+107
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.taboola.rest.api.internal;
2+
3+
import retrofit2.http.GET;
4+
import retrofit2.http.Header;
5+
import retrofit2.http.Headers;
6+
7+
import java.util.Collections;
8+
9+
import org.junit.Assert;
10+
import org.junit.Before;
11+
import org.junit.Test;
12+
13+
import com.taboola.rest.api.exceptions.RestAPIException;
14+
import com.taboola.rest.api.internal.config.CommunicationConfig;
15+
import com.taboola.rest.api.internal.config.SerializationConfig;
16+
import com.taboola.rest.api.internal.config.UserAgentHeader;
17+
18+
/**
19+
* Created by vladi
20+
* Date: 6/27/2021
21+
* Time: 12:00 PM
22+
* By Taboola
23+
*/
24+
public class CommunicationFactoryTest {
25+
26+
private CommunicationFactory testInstance;
27+
28+
public interface TestEndpoint {
29+
@GET("/users/current/allowed-accounts")
30+
@Headers("Content-Type: application/json")
31+
Object getTestObject(@Header("Authorization") String accessToken) throws RestAPIException;
32+
}
33+
34+
@Before
35+
public void beforeTest() {
36+
CommunicationConfig communicationConfig = new CommunicationConfig("http://localhost",
37+
1L, 1L, 1L, 1, 60L,
38+
Collections.singleton(new UserAgentHeader("Dummy-Agent")),true);
39+
SerializationConfig serializationConfig = new SerializationConfig();
40+
testInstance = new CommunicationFactory(communicationConfig, serializationConfig);
41+
}
42+
43+
@Test
44+
public void testHappyFlowServices() {
45+
Assert.assertNotNull("Missing service instance", testInstance.createRetrofitEndpoint(TestEndpoint.class));
46+
}
47+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.taboola.rest.api.internal;
2+
3+
import org.apache.logging.log4j.LogManager;
4+
import org.apache.logging.log4j.Logger;
5+
import org.junit.Assert;
6+
import org.junit.Before;
7+
import org.junit.Test;
8+
9+
import com.fasterxml.jackson.databind.ObjectMapper;
10+
import com.taboola.rest.api.exceptions.RestAPIRequestException;
11+
import com.taboola.rest.api.model.APIError;
12+
13+
import retrofit2.Call;
14+
import retrofit2.CallAdapter;
15+
import static org.junit.Assert.assertEquals;
16+
17+
/**
18+
* Created by vladi
19+
* Date: 1/16/2018
20+
* Time: 10:16 PM
21+
* By Taboola
22+
*/
23+
public class SynchronousCallAdapterFactoryTest {
24+
25+
private static final Logger logger = LogManager.getLogger(SynchronousCallAdapterFactoryTest.class);
26+
private SynchronousCallAdapterFactory testInstance;
27+
28+
public static class DummyTestClass { }
29+
30+
@Before
31+
public void beforeTest() {
32+
testInstance = SynchronousCallAdapterFactory.create(new ObjectMapper());
33+
}
34+
35+
@Test
36+
public void testSynchronousAdapterForNonCallType() {
37+
CallAdapter<Object, Object> actualAdapter = testInstance.get(DummyTestClass.class, null, null);
38+
Assert.assertNotNull("Invalid adapter, expecting adapter", actualAdapter);
39+
assertEquals("Invalid adapter type", DummyTestClass.class, actualAdapter.responseType());
40+
41+
actualAdapter = testInstance.get(Object.class, null, null);
42+
Assert.assertNotNull("Invalid adapter, expecting adapter", actualAdapter);
43+
assertEquals("Invalid adapter type", Object.class, actualAdapter.responseType());
44+
}
45+
46+
@Test
47+
public void testSynchronousAdapterForCallType() {
48+
CallAdapter<Object, Object> actualAdapter = testInstance.get(Call.class, null, null);
49+
Assert.assertNull("Invalid adapter, expecting no adapter", actualAdapter);
50+
}
51+
52+
@Test
53+
public void testParseError_whenErrorMsgContainsPercentSign_expectingValidFormatForStringFormat() {
54+
APIError apiError = new APIError("Cpc boost value must be between -99% and 100%", 400);
55+
apiError = testInstance.normalizeError(apiError);
56+
RestAPIRequestException ex = new RestAPIRequestException(400, apiError);
57+
logger.info(ex);
58+
assertEquals("Failed to perform API call with response code [400]. Response payload status [400], message [Cpc boost value must be between -99% and 100%], offending field [null], message code [null]", ex.getMessage());
59+
}
60+
}

0 commit comments

Comments
 (0)