Last active
October 22, 2015 09:08
-
-
Save kyip-dev/622052bdfca1ab83bc7d to your computer and use it in GitHub Desktop.
Junit-argumentCaptorExample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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