|
| 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