Skip to content

Instantly share code, notes, and snippets.

@kyip-dev
Last active October 22, 2015 09:08
Show Gist options
  • Save kyip-dev/622052bdfca1ab83bc7d to your computer and use it in GitHub Desktop.
Save kyip-dev/622052bdfca1ab83bc7d to your computer and use it in GitHub Desktop.
Junit-argumentCaptorExample
public void updateUserCORandIP(String registerIpAddress, User consumer) {
logger.trace("COR of user [USER_ID:{}] is null, start getting COR by [IP_ADDRESS:{}]", consumer.getUserId(), registerIpAddress);
if (StringUtils.isBlank(registerIpAddress)) {
throw new WebApplicationException(new IllegalArgumentException("IP address is null, no country can be found"));
}
commonInternalApi.setBasePath(userConfiguration.getCommonModuleUrl());
String cor = null;
try {
GetCountryResponse response = commonInternalApi.getCountryMappingByIp(registerIpAddress);
cor = response.getCountryCode();
} catch (ApiException | IOException e) {
throw new WebApplicationException(new CountryNotFoundException("Country is not supported. [COUNTRY_CODE:" + cor + "]"));
}
if (StringUtils.isNotBlank(cor) || !countryService.isCountryIdSupported(cor)) {
throw new WebApplicationException(new CountryNotFoundException("Country is not supported. [COUNTRY_CODE:" + cor + "]"));
}
logger.trace("COR resolved [COR:{}]", cor);
if (StringUtils.isNotBlank(cor) && StringUtils.isBlank(consumer.getCountryOfResidence())) {
consumer.setCountryOfResidence(cor);
consumer.setDefaultCurrencyCode(countryDAO.getCurrency(cor));
}
if (StringUtils.isBlank(consumer.getRegistrationIpAddress())) {
consumer.setRegistrationIpAddress(registerIpAddress);
}
userDAO.updateUser(consumer);
}
@Test
public void testUpdateConsumerCORSuccess() throws ApiException, IOException {
// prepare
String targetCOR = "HK";
String targetCurrency = "HKD";
String registerIpAddress = "192.168.2.113";
Mockito.when(commonInternalApi.getCountryMappingByIp(Mockito.anyString())).thenReturn(getCountryResponse(targetCOR));
Mockito.when(countryDAO.getCurrency(Mockito.anyString())).thenReturn(targetCurrency);
Mockito.when(countryService.isCountryIdSupported(Mockito.anyString())).thenReturn(true);
User consumer = new User();
consumer.setUserId("10000");
// execute
userService.updateUserCORandIP(registerIpAddress, consumer);
// verify
Mockito.verify(commonInternalApi, Mockito.times(1)).getCountryMappingByIp(registerIpAddress);
Mockito.verify(countryDAO, Mockito.times(1)).getCurrency(targetCOR);
ArgumentCaptor<User> userCapture = ArgumentCaptor.forClass(User.class);
Mockito.verify(userDAO, Mockito.times(1)).updateUser(userCapture.capture());
User updatedUser = userCapture.getValue();
Assert.assertEquals(targetCOR, updatedUser.getCountryOfResidence());
Assert.assertEquals(targetCurrency, updatedUser.getDefaultCurrencyCode());
Assert.assertEquals(registerIpAddress, updatedUser.getRegistrationIpAddress());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment