### Question:
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();  }### Answer:
@Test void renderNotificationWhenSpecificTemplateExists() throws Exception { ScriptContext scriptContext = mock(ScriptContext.class); when(this.scriptContextManager.getScriptContext()).thenReturn(scriptContext); Event event = mock(Event.class); when(event.getType()).thenReturn("some/type"); Template template = mock(Template.class); when(this.templateManager.getTemplate("notification/some.type.vm")).thenReturn(template); when(this.templateManager.execute(template)).thenReturn(new XDOM(Collections.emptyList())); CompositeEvent compositeEvent = new CompositeEvent(event); Block block = this.displayer.renderNotification(compositeEvent); assertNotNull(block); }
@Test void renderNotificationWhenSpecificTemplateDoesntExists() throws Exception { ScriptContext scriptContext = mock(ScriptContext.class); when(this.scriptContextManager.getScriptContext()).thenReturn(scriptContext); Event event = mock(Event.class); when(event.getType()).thenReturn("some/type"); when(this.templateManager.getTemplate("notification/some.type.vm")).thenReturn(null); when(this.templateManager.execute("notification/default.vm")).thenReturn(new XDOM(Collections.emptyList())); CompositeEvent compositeEvent = new CompositeEvent(event); Block block = this.displayer.renderNotification(compositeEvent); assertNotNull(block); }
@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()); }