@Test void renderNotificationWhenError() throws Exception { ScriptContext scriptContext = mock(ScriptContext.class); when(this.scriptContextManager.getScriptContext()).thenReturn(scriptContext); Event event1 = new DefaultEvent(); event1.setType("type1"); event1.setId("id1"); event1.setDate(new Date()); Event event2 = new DefaultEvent(); event2.setType("type2"); event2.setId("id2"); event2.setDate(DateUtils.addMinutes(new Date(), 5)); when(this.templateManager.execute("notification/default.vm")).thenThrow(new Exception("error")); CompositeEvent compositeEvent = new CompositeEvent(event1); compositeEvent.add(event2, 0); Throwable exception = assertThrows(NotificationException.class, () -> { this.displayer.renderNotification(compositeEvent); }); assertEquals("Failed to render the notification for events [id2,id1]", exception.getMessage()); }
@Override public Block renderNotification(CompositeEvent eventNotification) throws NotificationException { ScriptContext scriptContext = this.scriptContextManager.getScriptContext(); try { scriptContext.setAttribute(EVENT_BINDING_NAME, eventNotification, ScriptContext.ENGINE_SCOPE); Template template = getSpecificTemplate(eventNotification); return (template != null) ? this.templateManager.execute(template) : this.templateManager.execute("notification/default.vm"); } catch (Exception e) { throw new NotificationException(String.format("Failed to render the notification for events [%s]", StringUtils.join(eventNotification.getEventIds(), ',')), e); } finally { scriptContext.removeAttribute(EVENT_BINDING_NAME, ScriptContext.ENGINE_SCOPE); } }
DefaultNotificationDisplayer implements NotificationDisplayer { @Override public Block renderNotification(CompositeEvent eventNotification) throws NotificationException { ScriptContext scriptContext = this.scriptContextManager.getScriptContext(); try { scriptContext.setAttribute(EVENT_BINDING_NAME, eventNotification, ScriptContext.ENGINE_SCOPE); Template template = getSpecificTemplate(eventNotification); return (template != null) ? this.templateManager.execute(template) : this.templateManager.execute("notification/default.vm"); } catch (Exception e) { throw new NotificationException(String.format("Failed to render the notification for events [%s]", StringUtils.join(eventNotification.getEventIds(), ',')), e); } finally { scriptContext.removeAttribute(EVENT_BINDING_NAME, ScriptContext.ENGINE_SCOPE); } } }
DefaultNotificationDisplayer implements NotificationDisplayer { @Override public Block renderNotification(CompositeEvent eventNotification) throws NotificationException { ScriptContext scriptContext = this.scriptContextManager.getScriptContext(); try { scriptContext.setAttribute(EVENT_BINDING_NAME, eventNotification, ScriptContext.ENGINE_SCOPE); Template template = getSpecificTemplate(eventNotification); return (template != null) ? this.templateManager.execute(template) : this.templateManager.execute("notification/default.vm"); } catch (Exception e) { throw new NotificationException(String.format("Failed to render the notification for events [%s]", StringUtils.join(eventNotification.getEventIds(), ',')), e); } finally { scriptContext.removeAttribute(EVENT_BINDING_NAME, ScriptContext.ENGINE_SCOPE); } }  }
DefaultNotificationDisplayer implements NotificationDisplayer { @Override public Block renderNotification(CompositeEvent eventNotification) throws NotificationException { ScriptContext scriptContext = this.scriptContextManager.getScriptContext(); try { scriptContext.setAttribute(EVENT_BINDING_NAME, eventNotification, ScriptContext.ENGINE_SCOPE); Template template = getSpecificTemplate(eventNotification); return (template != null) ? this.templateManager.execute(template) : this.templateManager.execute("notification/default.vm"); } catch (Exception e) { throw new NotificationException(String.format("Failed to render the notification for events [%s]", StringUtils.join(eventNotification.getEventIds(), ',')), e); } finally { scriptContext.removeAttribute(EVENT_BINDING_NAME, ScriptContext.ENGINE_SCOPE); } }  @Override Block renderNotification(CompositeEvent eventNotification); @Override List<String> getSupportedEvents(); }
DefaultNotificationDisplayer implements NotificationDisplayer { @Override public Block renderNotification(CompositeEvent eventNotification) throws NotificationException { ScriptContext scriptContext = this.scriptContextManager.getScriptContext(); try { scriptContext.setAttribute(EVENT_BINDING_NAME, eventNotification, ScriptContext.ENGINE_SCOPE); Template template = getSpecificTemplate(eventNotification); return (template != null) ? this.templateManager.execute(template) : this.templateManager.execute("notification/default.vm"); } catch (Exception e) { throw new NotificationException(String.format("Failed to render the notification for events [%s]", StringUtils.join(eventNotification.getEventIds(), ',')), e); } finally { scriptContext.removeAttribute(EVENT_BINDING_NAME, ScriptContext.ENGINE_SCOPE); } }  @Override Block renderNotification(CompositeEvent eventNotification); @Override List<String> getSupportedEvents();  }