package io.ljunggren.ups.api;
import static org.junit.Assert.assertSame;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.lang.reflect.Field;
import org.junit.Before;
import org.junit.Test;
import io.ljunggren.ups.api.model.Address;
import io.ljunggren.ups.api.model.AuthToken;
import io.ljunggren.ups.api.model.UpsResponse;
import io.ljunggren.ups.api.model.rate.request.RateRequest;
import io.ljunggren.ups.api.service.AddressService;
import io.ljunggren.ups.api.service.OauthService;
import io.ljunggren.ups.api.service.RateService;
import io.ljunggren.ups.api.service.TrackingService;
public class UpsApiTest {
private UpsApi upsApi;
private OauthService oauthService;
private TrackingService trackingService;
private RateService rateService;
private AddressService addressService;
@Before
public void setUp() throws Exception {
upsApi = new UpsApi(UpsEnvironment.CIE, "clientId", "clientSecret", "accountNumber");
oauthService = mock(OauthService.class);
trackingService = mock(TrackingService.class);
rateService = mock(RateService.class);
addressService = mock(AddressService.class);
setField("oauthService", oauthService);
setField("trackingService", trackingService);
setField("rateService", rateService);
setField("addressService", addressService);
}
private void setField(String name, Object value) throws Exception {
Field field = UpsApi.class.getDeclaredField(name);
field.setAccessible(true);
field.set(upsApi, value);
}
private AuthToken authToken(String accessToken) {
AuthToken token = new AuthToken();
token.setAccessToken(accessToken);
return token;
}
@Test
public void trackAcquiresTokenThenDelegatesToTrackingService() throws Exception {
when(oauthService.getAuthToken("clientId", "clientSecret", "accountNumber")).thenReturn(authToken("abc"));
UpsResponse expected = new UpsResponse();
when(trackingService.track("1Z999", "abc")).thenReturn(expected);
UpsResponse actual = upsApi.track("1Z999");
assertSame(expected, actual);
verify(trackingService).track("1Z999", "abc");
}
@Test
public void trackWithExplicitTokenSkipsOauthCall() throws Exception {
UpsResponse expected = new UpsResponse();
when(trackingService.track("1Z999", "explicit-token")).thenReturn(expected);
UpsResponse actual = upsApi.track("1Z999", "explicit-token");
assertSame(expected, actual);
verify(trackingService).track("1Z999", "explicit-token");
}
@Test
public void requestRateAcquiresTokenThenDelegatesToRateService() throws Exception {
when(oauthService.getAuthToken("clientId", "clientSecret", "accountNumber")).thenReturn(authToken("abc"));
RateRequest rateRequest = new RateRequest();
UpsResponse expected = new UpsResponse();
when(rateService.request(rateRequest, "abc")).thenReturn(expected);
UpsResponse actual = upsApi.requestRate(rateRequest);
assertSame(expected, actual);
verify(rateService).request(rateRequest, "abc");
}
@Test
public void validateAddressAcquiresTokenThenDelegatesToAddressService() throws Exception {
when(oauthService.getAuthToken("clientId", "clientSecret", "accountNumber")).thenReturn(authToken("abc"));
Address address = new Address();
UpsResponse expected = new UpsResponse();
when(addressService.validate(address, "abc")).thenReturn(expected);
UpsResponse actual = upsApi.validateAddress(address);
assertSame(expected, actual);
verify(addressService).validate(address, "abc");
}
}