TemplateManager { public AlertTemplate saveTemplate(EntityManager em, AlertTemplates templates, Tenant tenant, AlertTemplate currTemplate, ApplicationManager am) throws Exception { if (currTemplate == null || templates == null || tenant == null) { logger.info("Template was null can't save"); throw new IllegalArgumentException("Templates can't be null"); } AlertTemplateValidator validator = new AlertTemplateValidator(); validator.validate(currTemplate); logger.info("Template is valid attempting to save"); try { em.getTransaction().begin(); if (templates.getTenant() == null) { templates.setTenant(tenant); } if (currTemplate.getTemplateId() > 0) { templates.setTemplateId(currTemplate.getTemplateId()); } templates = em.merge(templates); em.flush(); em.getTransaction().commit(); currTemplate.setTemplateId(templates.getTemplateId()); em.getTransaction().begin(); templates.setTemplateContent(AlertTemplateSerializer.serialize(currTemplate, false)); em.merge(templates); em.flush(); logger.info("Template " + templates.getTemplateId() + ":" + templates.getTemplateContent() + " saved"); sendTemplateToKafka(false, tenant.getTenant_id(), templates.getTemplateContent(), am); em.getTransaction().commit(); logger.info("Completed Transaction for template " + templates.getTemplateId() + ":" + templates.getTemplateContent() + ""); return currTemplate; } catch (Exception e) { e.printStackTrace(); if (em.getTransaction().isActive()) { em.getTransaction().rollback(); } throw e; } } private  TemplateManager(); static TemplateManager getInstance(); String getTemplateContents(EntityManager em, String tenantId, boolean pretty); AlertTemplates getTemplate(EntityManager em, String tenantId, short templateId); List<AlertTemplates> getTemplates(EntityManager em, String tenantId); AlertTemplate createNewTemplate(EntityManager em, AlertTemplates templates, Tenant tenant); AlertTemplate saveTemplate(EntityManager em, AlertTemplates templates, Tenant tenant, AlertTemplate currTemplate,
			ApplicationManager am); AlertTemplate getTemplateObj(EntityManager em, String tenantId, short templateId); void deleteTemplate(EntityManager em, String tenantId, short templateId, ApplicationManager am); void deleteTemplates(EntityManager em, Tenant tenant, ApplicationManager am); void sendTemplateToKafka(boolean delete, String tenantId, AlertTemplate template, ApplicationManager am); void sendTemplateToKafka(boolean delete, String tenantId, String templateJson, ApplicationManager am); Tenant getTenant(EntityManager em, String tenantId);  }
@Test public void testSaveTemplate() throws Exception { AlertTemplate tpl = new AlertTemplate(); tenant = TemplateManager.getInstance().getTenant(em, TENANT_ID_1); AlertTemplates template = TemplateManager.getInstance().getTemplate(em, tenant.getTenant_id(), id); tpl.setTemplateId(template.getTemplateId()); tpl.setBody("test"); tpl.setDestination("test@xyz.com"); tpl.setMedia("mail"); tpl.setTemplateName("Test"); tpl.setThrottleDuration(2); tpl.setThrottleLimit(2); short id = TemplateManager.getInstance().saveTemplate(em, template, template.getTenant(), tpl, am).getTemplateId(); assertEquals(id, template.getTemplateId()); }