Skip to content

Instantly share code, notes, and snippets.

@simcap
Created September 13, 2012 08:48
Show Gist options
  • Save simcap/3712982 to your computer and use it in GitHub Desktop.
Save simcap/3712982 to your computer and use it in GitHub Desktop.
Unit tests - Good & bad
public class SetCarrierServiceFacade {
@Autowired
private CarrierService carrierService;
@Autowired
private UserService userService;
@Autowired
private UserContactPointService userContactPointService;
@Autowired
private MessageService messageService;
@Autowired
private PreferenceService preferenceService;
@Transactional(propagation = Propagation.REQUIRED)
public void selectCarrier(Long carrierId, String msisdn, ApplicationEntity applicationEntity)
throws OnDoesNotExistsException {
final CarrierEntity carrier = carrierService.findById(carrierId);
Long userId = userService.findLegacyUserIdByValidatedContactPoint(msisdn);
UserEntity user = userService.findByIdWithCarrierPhoneNumbers(userId);
removeCarrierNumbersFromUserList(carrier, user);
addLatestActiveMaxWeightCarrierNumbersToUserList(carrier, user);
UserContactPointEntity validatedContactPoint = userContactPointService.getFirstValidatedContactPoint(msisdn);
validatedContactPoint.setCarrier(carrier);
validatedContactPoint.setVoicemailRedirectionStatus(VoicemailRedirectionStatus.ASKED);
sendVoicemailBoxCreationMessage(applicationEntity, user);
}
private void sendVoicemailBoxCreationMessage(ApplicationEntity applicationEntity, UserEntity user) {
String password = preferenceService.getVmsAccessCode(user);
if (applicationEntity.getName().equals(ApplicationEntity.IPHONE_LIBON)) {
messageService.createOnSystemMessage(user, "#web#VOICEMAIL_BOX_CREATED_TEXT", password);
} else {
messageService.createOnSystemMessage(user, "#web#VOICEMAIL_BOX_CREATED_TEXT_VOICEFEED", password);
}
}
private void addLatestActiveMaxWeightCarrierNumbersToUserList(final CarrierEntity carrier, UserEntity user) {
List<CarrierPhoneNumberEntity> oldNumbers = new ArrayList<CarrierPhoneNumberEntity>(
user.getCarrierPhoneNumbers());
for (CarrierPhoneNumberType type : CarrierPhoneNumberType.values()) {
CarrierPhoneNumberEntity number = carrier.findLatestActivePhoneNumberMaxWeightIfNotInListAndExpired(type,
oldNumbers);
if (number == null) {
throw new IllegalStateException("No number of type " + type + " found for carrier " + carrier);
}
user.getCarrierPhoneNumbers().add(number);
}
}
private void removeCarrierNumbersFromUserList(final CarrierEntity carrier, UserEntity user) {
Iterables.removeIf(user.getCarrierPhoneNumbers(), new Predicate<CarrierPhoneNumberEntity>() {
@Override
public boolean apply(CarrierPhoneNumberEntity cpn) {
return cpn.getCarrier().getId().equals(carrier.getId());
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment