@Test void renderNotification() throws Exception { ScriptContext scriptContext = mock(ScriptContext.class); when(this.scriptContextManager.getScriptContext()).thenReturn(scriptContext); DocumentReference userReference = new DocumentReference("xwiki", "XWiki", "U1"); when(this.documentReferenceResolver.resolve("xwiki:XWiki.U1")) .thenReturn(userReference); DocumentReference documentReference = new DocumentReference("xwiki", "XWiki", "Doc"); when(this.documentReferenceResolver.resolve("xwiki:XWiki.Doc")) .thenReturn(documentReference); when(this.contextProvider.get()).thenReturn(this.context); XWikiDocument userDocument = mock(XWikiDocument.class); when(this.documentAccess.getDocumentInstance(userReference)).thenReturn(userDocument); XWikiDocument pageDocument = mock(XWikiDocument.class); when(this.documentAccess.getDocumentInstance(documentReference)).thenReturn(pageDocument); when(pageDocument.getName()).thenReturn("Doc"); when(userDocument.getExternalURL("view", this.context)).thenReturn("http: String anchor = "myAnchor"; XWikiURLFactory urlFactory = mock(XWikiURLFactory.class); when(this.context.getURLFactory()).thenReturn(urlFactory); when(urlFactory.createExternalURL(any(), eq("Doc"), eq("view"), isNull(), eq(anchor), eq(this.context))) .thenReturn(new URL("http: DefaultEvent mentionEvent = new DefaultEvent(); HashMap<String, String> eventParameters = new HashMap<>(); String mpValue = "{ \"userReference\": \"xwiki:XWiki.U1\", " + "\"documentReference\": \"xwiki:XWiki.Doc\", " + "\"anchor\":\"" + anchor + "\" }"; eventParameters.put(MentionsRecordableEventConverter.MENTIONS_PARAMETER_KEY, mpValue); mentionEvent.setParameters(eventParameters); when(this.templateManager.execute("mentions/mention.vm")).thenReturn(new XDOM(emptyList())); MentionEventParams mentionEventParams = new MentionEventParams() .setDocumentReference("xwiki:XWiki.Doc") .setUserReference("xwiki:XWiki.U1") .setLocation(MentionLocation.DOCUMENT) .setAnchor(anchor); when(this.objectMapper.unserialize(mpValue)).thenReturn(Optional.of(mentionEventParams)); this.displayer.renderNotification(new CompositeEvent(mentionEvent)); Map<Event, MentionView> paramsMap = new HashMap<>(); MentionView mentionView = new MentionView() .setAuthorURL("http: .setDocumentURL("http: .setDocument(pageDocument) .setLocation("DOCUMENT"); paramsMap.put(mentionEvent, mentionView); verify(scriptContext).setAttribute("compositeEventParams", paramsMap, ScriptContext.ENGINE_SCOPE); verify(this.templateManager).execute("mentions/mention.vm"); }
@Override public Block renderNotification(CompositeEvent compositeEvent) throws NotificationException { GroupBlock ret = new GroupBlock(); String template = "mentions/mention.vm"; ScriptContext scriptContext = this.scriptContextManager.getScriptContext(); scriptContext.setAttribute(EVENT_BINDING_NAME, compositeEvent, ScriptContext.ENGINE_SCOPE); Map<Event, MentionView> paramsMap = buildParamsMap(compositeEvent); scriptContext.setAttribute(EVENT_PARAMS_BINDING_NAME, paramsMap, ScriptContext.ENGINE_SCOPE); try { ret.addChildren(this.templateManager.execute(template).getChildren()); } catch (Exception e) { throw new NotificationException(String.format("Failed to render a template [%s]", template), e); } return ret; }
MentionsNotificationDisplayer implements NotificationDisplayer { @Override public Block renderNotification(CompositeEvent compositeEvent) throws NotificationException { GroupBlock ret = new GroupBlock(); String template = "mentions/mention.vm"; ScriptContext scriptContext = this.scriptContextManager.getScriptContext(); scriptContext.setAttribute(EVENT_BINDING_NAME, compositeEvent, ScriptContext.ENGINE_SCOPE); Map<Event, MentionView> paramsMap = buildParamsMap(compositeEvent); scriptContext.setAttribute(EVENT_PARAMS_BINDING_NAME, paramsMap, ScriptContext.ENGINE_SCOPE); try { ret.addChildren(this.templateManager.execute(template).getChildren()); } catch (Exception e) { throw new NotificationException(String.format("Failed to render a template [%s]", template), e); } return ret; } }
MentionsNotificationDisplayer implements NotificationDisplayer { @Override public Block renderNotification(CompositeEvent compositeEvent) throws NotificationException { GroupBlock ret = new GroupBlock(); String template = "mentions/mention.vm"; ScriptContext scriptContext = this.scriptContextManager.getScriptContext(); scriptContext.setAttribute(EVENT_BINDING_NAME, compositeEvent, ScriptContext.ENGINE_SCOPE); Map<Event, MentionView> paramsMap = buildParamsMap(compositeEvent); scriptContext.setAttribute(EVENT_PARAMS_BINDING_NAME, paramsMap, ScriptContext.ENGINE_SCOPE); try { ret.addChildren(this.templateManager.execute(template).getChildren()); } catch (Exception e) { throw new NotificationException(String.format("Failed to render a template [%s]", template), e); } return ret; }  }
MentionsNotificationDisplayer implements NotificationDisplayer { @Override public Block renderNotification(CompositeEvent compositeEvent) throws NotificationException { GroupBlock ret = new GroupBlock(); String template = "mentions/mention.vm"; ScriptContext scriptContext = this.scriptContextManager.getScriptContext(); scriptContext.setAttribute(EVENT_BINDING_NAME, compositeEvent, ScriptContext.ENGINE_SCOPE); Map<Event, MentionView> paramsMap = buildParamsMap(compositeEvent); scriptContext.setAttribute(EVENT_PARAMS_BINDING_NAME, paramsMap, ScriptContext.ENGINE_SCOPE); try { ret.addChildren(this.templateManager.execute(template).getChildren()); } catch (Exception e) { throw new NotificationException(String.format("Failed to render a template [%s]", template), e); } return ret; }  @Override Block renderNotification(CompositeEvent compositeEvent); @Override List<String> getSupportedEvents(); }
MentionsNotificationDisplayer implements NotificationDisplayer { @Override public Block renderNotification(CompositeEvent compositeEvent) throws NotificationException { GroupBlock ret = new GroupBlock(); String template = "mentions/mention.vm"; ScriptContext scriptContext = this.scriptContextManager.getScriptContext(); scriptContext.setAttribute(EVENT_BINDING_NAME, compositeEvent, ScriptContext.ENGINE_SCOPE); Map<Event, MentionView> paramsMap = buildParamsMap(compositeEvent); scriptContext.setAttribute(EVENT_PARAMS_BINDING_NAME, paramsMap, ScriptContext.ENGINE_SCOPE); try { ret.addChildren(this.templateManager.execute(template).getChildren()); } catch (Exception e) { throw new NotificationException(String.format("Failed to render a template [%s]", template), e); } return ret; }  @Override Block renderNotification(CompositeEvent compositeEvent); @Override List<String> getSupportedEvents();  }