use of org.apache.cloudstack.quota.vo.QuotaEmailTemplatesVO in project cloudstack by apache.
the class QuotaResponseBuilderImpl method updateQuotaEmailTemplate.
@Override
public boolean updateQuotaEmailTemplate(QuotaEmailTemplateUpdateCmd cmd) {
final String templateName = cmd.getTemplateName();
final String templateSubject = StringEscapeUtils.escapeJavaScript(cmd.getTemplateSubject());
final String templateBody = StringEscapeUtils.escapeJavaScript(cmd.getTemplateBody());
final String locale = cmd.getLocale();
final List<QuotaEmailTemplatesVO> templates = _quotaEmailTemplateDao.listAllQuotaEmailTemplates(templateName);
if (templates.size() == 1) {
final QuotaEmailTemplatesVO template = templates.get(0);
template.setTemplateSubject(templateSubject);
template.setTemplateBody(templateBody);
if (locale != null) {
template.setLocale(locale);
}
return _quotaEmailTemplateDao.updateQuotaEmailTemplate(template);
}
return false;
}
use of org.apache.cloudstack.quota.vo.QuotaEmailTemplatesVO in project cloudstack by apache.
the class QuotaAlertManagerImplTest method testSendQuotaAlert.
@Test
public void testSendQuotaAlert() throws UnsupportedEncodingException, MessagingException {
Mockito.doCallRealMethod().when(quotaAlertManager).sendQuotaAlert(Mockito.any(QuotaAlertManagerImpl.DeferredQuotaEmail.class));
AccountVO account = new AccountVO();
account.setId(2L);
account.setDomainId(1L);
account.setType(Account.ACCOUNT_TYPE_NORMAL);
account.setAccountName("admin");
account.setUuid("uuid");
QuotaAccountVO quotaAccount = new QuotaAccountVO(2L);
quotaAccount.setQuotaBalance(new BigDecimal(404));
quotaAccount.setQuotaMinBalance(new BigDecimal(100));
quotaAccount.setQuotaBalanceDate(new Date());
quotaAccount.setQuotaAlertDate(null);
quotaAccount.setQuotaEnforce(0);
QuotaAlertManagerImpl.DeferredQuotaEmail email = new QuotaAlertManagerImpl.DeferredQuotaEmail(account, quotaAccount, new BigDecimal(100), QuotaConfig.QuotaEmailTemplateTypes.QUOTA_LOW);
QuotaEmailTemplatesVO quotaEmailTemplatesVO = new QuotaEmailTemplatesVO();
quotaEmailTemplatesVO.setTemplateSubject("Low quota");
quotaEmailTemplatesVO.setTemplateBody("Low quota {{accountID}}");
List<QuotaEmailTemplatesVO> emailTemplates = new ArrayList<>();
emailTemplates.add(quotaEmailTemplatesVO);
Mockito.when(quotaEmailTemplateDao.listAllQuotaEmailTemplates(Mockito.anyString())).thenReturn(emailTemplates);
DomainVO domain = new DomainVO();
domain.setUuid("uuid");
domain.setName("/domain");
Mockito.when(domainDao.findByIdIncludingRemoved(Mockito.anyLong())).thenReturn(new DomainVO());
UserVO user = new UserVO();
user.setUsername("user1");
user.setEmail("user1@apache.org");
List<UserVO> users = new ArrayList<>();
users.add(user);
Mockito.when(userDao.listByAccount(Mockito.anyLong())).thenReturn(users);
quotaAlertManager.sendQuotaAlert(email);
assertTrue(email.getSendDate() != null);
Mockito.verify(emailQuotaAlert, Mockito.times(1)).sendQuotaAlert(Mockito.anyList(), Mockito.anyString(), Mockito.anyString());
}
use of org.apache.cloudstack.quota.vo.QuotaEmailTemplatesVO in project cloudstack by apache.
the class QuotaAlertManagerImpl method sendQuotaAlert.
public void sendQuotaAlert(DeferredQuotaEmail emailToBeSent) {
final AccountVO account = emailToBeSent.getAccount();
final BigDecimal balance = emailToBeSent.getQuotaBalance();
final BigDecimal usage = emailToBeSent.getQuotaUsage();
final QuotaConfig.QuotaEmailTemplateTypes emailType = emailToBeSent.getEmailTemplateType();
final List<QuotaEmailTemplatesVO> emailTemplates = _quotaEmailTemplateDao.listAllQuotaEmailTemplates(emailType.toString());
if (emailTemplates != null && emailTemplates.get(0) != null) {
final QuotaEmailTemplatesVO emailTemplate = emailTemplates.get(0);
final DomainVO accountDomain = _domainDao.findByIdIncludingRemoved(account.getDomainId());
final List<UserVO> usersInAccount = _userDao.listByAccount(account.getId());
String userNames = "";
final List<String> emailRecipients = new ArrayList<String>();
for (UserVO user : usersInAccount) {
userNames += String.format("%s <%s>,", user.getUsername(), user.getEmail());
emailRecipients.add(user.getEmail());
}
if (userNames.endsWith(",")) {
userNames = userNames.substring(0, userNames.length() - 1);
}
final Map<String, String> optionMap = new HashMap<String, String>();
optionMap.put("accountName", account.getAccountName());
optionMap.put("accountID", account.getUuid());
optionMap.put("accountUsers", userNames);
optionMap.put("domainName", accountDomain.getName());
optionMap.put("domainID", accountDomain.getUuid());
optionMap.put("quotaBalance", QuotaConfig.QuotaCurrencySymbol.value() + " " + balance.toString());
if (emailType == QuotaEmailTemplateTypes.QUOTA_STATEMENT) {
optionMap.put("quotaUsage", QuotaConfig.QuotaCurrencySymbol.value() + " " + usage.toString());
}
if (s_logger.isDebugEnabled()) {
s_logger.debug("accountName" + account.getAccountName() + "accountID" + account.getUuid() + "accountUsers" + userNames + "domainName" + accountDomain.getName() + "domainID" + accountDomain.getUuid());
}
final StrSubstitutor templateEngine = new StrSubstitutor(optionMap);
final String subject = templateEngine.replace(emailTemplate.getTemplateSubject());
final String body = templateEngine.replace(emailTemplate.getTemplateBody());
try {
_emailQuotaAlert.sendQuotaAlert(emailRecipients, subject, body);
emailToBeSent.sentSuccessfully(_quotaAcc);
} catch (Exception e) {
s_logger.error(String.format("Unable to send quota alert email (subject=%s; body=%s) to account %s (%s) recipients (%s) due to error (%s)", subject, body, account.getAccountName(), account.getUuid(), emailRecipients, e));
if (s_logger.isDebugEnabled()) {
s_logger.debug("Exception", e);
}
}
} else {
s_logger.error(String.format("No quota email template found for type %s, cannot send quota alert email to account %s(%s)", emailType, account.getAccountName(), account.getUuid()));
}
}
use of org.apache.cloudstack.quota.vo.QuotaEmailTemplatesVO in project cloudstack by apache.
the class QuotaResponseBuilderImplTest method testListQuotaEmailTemplates.
@Test
public void testListQuotaEmailTemplates() {
QuotaEmailTemplateListCmd cmd = new QuotaEmailTemplateListCmd();
cmd.setTemplateName("some name");
List<QuotaEmailTemplatesVO> templates = new ArrayList<>();
QuotaEmailTemplatesVO template = new QuotaEmailTemplatesVO();
template.setTemplateName("template");
templates.add(template);
Mockito.when(quotaEmailTemplateDao.listAllQuotaEmailTemplates(Mockito.anyString())).thenReturn(templates);
assertTrue(quotaResponseBuilder.listQuotaEmailTemplates(cmd).size() == 1);
}
use of org.apache.cloudstack.quota.vo.QuotaEmailTemplatesVO in project cloudstack by apache.
the class QuotaResponseBuilderImplTest method testUpdateQuotaEmailTemplate.
@Test
public void testUpdateQuotaEmailTemplate() {
QuotaEmailTemplateUpdateCmd cmd = new QuotaEmailTemplateUpdateCmd();
cmd.setTemplateBody("some body");
cmd.setTemplateName("some name");
cmd.setTemplateSubject("some subject");
List<QuotaEmailTemplatesVO> templates = new ArrayList<>();
Mockito.when(quotaEmailTemplateDao.listAllQuotaEmailTemplates(Mockito.anyString())).thenReturn(templates);
Mockito.when(quotaEmailTemplateDao.updateQuotaEmailTemplate(Mockito.any(QuotaEmailTemplatesVO.class))).thenReturn(true);
// invalid template test
assertFalse(quotaResponseBuilder.updateQuotaEmailTemplate(cmd));
// valid template test
QuotaEmailTemplatesVO template = new QuotaEmailTemplatesVO();
template.setTemplateName("template");
templates.add(template);
assertTrue(quotaResponseBuilder.updateQuotaEmailTemplate(cmd));
}
Aggregations