@Test(expected = InsufficientPrivilegesException.class) public void registerWikiMacroWhenGlobalVisibilityAndNotAllowed() throws Exception { WikiMacro wikiMacro = generateWikiMacro(WikiMacroVisibility.GLOBAL); WikiMacroFactory wikiMacroFactory = this.mocker.getInstance(WikiMacroFactory.class); when(wikiMacroFactory.isAllowed(wikiMacro.getDocumentReference(), WikiMacroVisibility.GLOBAL)) .thenReturn(false); this.mocker.getComponentUnderTest().registerWikiMacro(wikiMacro.getDocumentReference(), wikiMacro); }
@Override public void registerWikiMacro(DocumentReference documentReference, WikiMacro wikiMacro) throws InsufficientPrivilegesException, WikiMacroException { WikiMacroDescriptor macroDescriptor = (WikiMacroDescriptor) wikiMacro.getDescriptor(); WikiMacroVisibility visibility = macroDescriptor.getVisibility(); if (visibility == WikiMacroVisibility.GLOBAL && !this.wikis.isMainWiki(documentReference.getWikiReference().getName())) { visibility = WikiMacroVisibility.WIKI; } if (this.wikiMacroFactory.isAllowed(documentReference, visibility)) { DefaultComponentDescriptor<Macro> componentDescriptor = new DefaultComponentDescriptor<>(); componentDescriptor.setRoleType(Macro.class); componentDescriptor.setRoleHint(wikiMacro.getDescriptor().getId().getId()); String currentUser = this.bridge.getCurrentUser(); EntityReference currentEntityReference = this.modelContext.getCurrentEntityReference(); try { this.bridge.setCurrentUser(this.serializer.serialize(wikiMacro.getAuthorReference() != null ? wikiMacro.getAuthorReference() : this.bridge.getCurrentUserReference())); this.modelContext.setCurrentEntityReference(documentReference); findComponentManager(visibility).registerComponent(componentDescriptor, wikiMacro); this.wikiMacroMap.put(documentReference, new WikiMacroData(componentDescriptor.getRoleHint(), wikiMacro)); } catch (Exception e) { throw new WikiMacroException(String.format("Failed to register macro [%s] in [%s] for visibility [%s]", wikiMacro.getDescriptor().getId().getId(), documentReference, visibility), e); } finally { this.bridge.setCurrentUser(currentUser); this.modelContext.setCurrentEntityReference(currentEntityReference); } } else { throw new InsufficientPrivilegesException(String.format( "Unable to register macro [%s] in [%s] for visibility [%s] due to insufficient privileges", wikiMacro.getDescriptor().getId().getId(), documentReference, macroDescriptor.getVisibility())); } }
DefaultWikiMacroManager implements WikiMacroManager { @Override public void registerWikiMacro(DocumentReference documentReference, WikiMacro wikiMacro) throws InsufficientPrivilegesException, WikiMacroException { WikiMacroDescriptor macroDescriptor = (WikiMacroDescriptor) wikiMacro.getDescriptor(); WikiMacroVisibility visibility = macroDescriptor.getVisibility(); if (visibility == WikiMacroVisibility.GLOBAL && !this.wikis.isMainWiki(documentReference.getWikiReference().getName())) { visibility = WikiMacroVisibility.WIKI; } if (this.wikiMacroFactory.isAllowed(documentReference, visibility)) { DefaultComponentDescriptor<Macro> componentDescriptor = new DefaultComponentDescriptor<>(); componentDescriptor.setRoleType(Macro.class); componentDescriptor.setRoleHint(wikiMacro.getDescriptor().getId().getId()); String currentUser = this.bridge.getCurrentUser(); EntityReference currentEntityReference = this.modelContext.getCurrentEntityReference(); try { this.bridge.setCurrentUser(this.serializer.serialize(wikiMacro.getAuthorReference() != null ? wikiMacro.getAuthorReference() : this.bridge.getCurrentUserReference())); this.modelContext.setCurrentEntityReference(documentReference); findComponentManager(visibility).registerComponent(componentDescriptor, wikiMacro); this.wikiMacroMap.put(documentReference, new WikiMacroData(componentDescriptor.getRoleHint(), wikiMacro)); } catch (Exception e) { throw new WikiMacroException(String.format("Failed to register macro [%s] in [%s] for visibility [%s]", wikiMacro.getDescriptor().getId().getId(), documentReference, visibility), e); } finally { this.bridge.setCurrentUser(currentUser); this.modelContext.setCurrentEntityReference(currentEntityReference); } } else { throw new InsufficientPrivilegesException(String.format( "Unable to register macro [%s] in [%s] for visibility [%s] due to insufficient privileges", wikiMacro.getDescriptor().getId().getId(), documentReference, macroDescriptor.getVisibility())); } } }
DefaultWikiMacroManager implements WikiMacroManager { @Override public void registerWikiMacro(DocumentReference documentReference, WikiMacro wikiMacro) throws InsufficientPrivilegesException, WikiMacroException { WikiMacroDescriptor macroDescriptor = (WikiMacroDescriptor) wikiMacro.getDescriptor(); WikiMacroVisibility visibility = macroDescriptor.getVisibility(); if (visibility == WikiMacroVisibility.GLOBAL && !this.wikis.isMainWiki(documentReference.getWikiReference().getName())) { visibility = WikiMacroVisibility.WIKI; } if (this.wikiMacroFactory.isAllowed(documentReference, visibility)) { DefaultComponentDescriptor<Macro> componentDescriptor = new DefaultComponentDescriptor<>(); componentDescriptor.setRoleType(Macro.class); componentDescriptor.setRoleHint(wikiMacro.getDescriptor().getId().getId()); String currentUser = this.bridge.getCurrentUser(); EntityReference currentEntityReference = this.modelContext.getCurrentEntityReference(); try { this.bridge.setCurrentUser(this.serializer.serialize(wikiMacro.getAuthorReference() != null ? wikiMacro.getAuthorReference() : this.bridge.getCurrentUserReference())); this.modelContext.setCurrentEntityReference(documentReference); findComponentManager(visibility).registerComponent(componentDescriptor, wikiMacro); this.wikiMacroMap.put(documentReference, new WikiMacroData(componentDescriptor.getRoleHint(), wikiMacro)); } catch (Exception e) { throw new WikiMacroException(String.format("Failed to register macro [%s] in [%s] for visibility [%s]", wikiMacro.getDescriptor().getId().getId(), documentReference, visibility), e); } finally { this.bridge.setCurrentUser(currentUser); this.modelContext.setCurrentEntityReference(currentEntityReference); } } else { throw new InsufficientPrivilegesException(String.format( "Unable to register macro [%s] in [%s] for visibility [%s] due to insufficient privileges", wikiMacro.getDescriptor().getId().getId(), documentReference, macroDescriptor.getVisibility())); } }  }
DefaultWikiMacroManager implements WikiMacroManager { @Override public void registerWikiMacro(DocumentReference documentReference, WikiMacro wikiMacro) throws InsufficientPrivilegesException, WikiMacroException { WikiMacroDescriptor macroDescriptor = (WikiMacroDescriptor) wikiMacro.getDescriptor(); WikiMacroVisibility visibility = macroDescriptor.getVisibility(); if (visibility == WikiMacroVisibility.GLOBAL && !this.wikis.isMainWiki(documentReference.getWikiReference().getName())) { visibility = WikiMacroVisibility.WIKI; } if (this.wikiMacroFactory.isAllowed(documentReference, visibility)) { DefaultComponentDescriptor<Macro> componentDescriptor = new DefaultComponentDescriptor<>(); componentDescriptor.setRoleType(Macro.class); componentDescriptor.setRoleHint(wikiMacro.getDescriptor().getId().getId()); String currentUser = this.bridge.getCurrentUser(); EntityReference currentEntityReference = this.modelContext.getCurrentEntityReference(); try { this.bridge.setCurrentUser(this.serializer.serialize(wikiMacro.getAuthorReference() != null ? wikiMacro.getAuthorReference() : this.bridge.getCurrentUserReference())); this.modelContext.setCurrentEntityReference(documentReference); findComponentManager(visibility).registerComponent(componentDescriptor, wikiMacro); this.wikiMacroMap.put(documentReference, new WikiMacroData(componentDescriptor.getRoleHint(), wikiMacro)); } catch (Exception e) { throw new WikiMacroException(String.format("Failed to register macro [%s] in [%s] for visibility [%s]", wikiMacro.getDescriptor().getId().getId(), documentReference, visibility), e); } finally { this.bridge.setCurrentUser(currentUser); this.modelContext.setCurrentEntityReference(currentEntityReference); } } else { throw new InsufficientPrivilegesException(String.format( "Unable to register macro [%s] in [%s] for visibility [%s] due to insufficient privileges", wikiMacro.getDescriptor().getId().getId(), documentReference, macroDescriptor.getVisibility())); } }  @Override boolean hasWikiMacro(DocumentReference documentReference); @Override void registerWikiMacro(DocumentReference documentReference, WikiMacro wikiMacro); @Override void unregisterWikiMacro(DocumentReference documentReference); }
DefaultWikiMacroManager implements WikiMacroManager { @Override public void registerWikiMacro(DocumentReference documentReference, WikiMacro wikiMacro) throws InsufficientPrivilegesException, WikiMacroException { WikiMacroDescriptor macroDescriptor = (WikiMacroDescriptor) wikiMacro.getDescriptor(); WikiMacroVisibility visibility = macroDescriptor.getVisibility(); if (visibility == WikiMacroVisibility.GLOBAL && !this.wikis.isMainWiki(documentReference.getWikiReference().getName())) { visibility = WikiMacroVisibility.WIKI; } if (this.wikiMacroFactory.isAllowed(documentReference, visibility)) { DefaultComponentDescriptor<Macro> componentDescriptor = new DefaultComponentDescriptor<>(); componentDescriptor.setRoleType(Macro.class); componentDescriptor.setRoleHint(wikiMacro.getDescriptor().getId().getId()); String currentUser = this.bridge.getCurrentUser(); EntityReference currentEntityReference = this.modelContext.getCurrentEntityReference(); try { this.bridge.setCurrentUser(this.serializer.serialize(wikiMacro.getAuthorReference() != null ? wikiMacro.getAuthorReference() : this.bridge.getCurrentUserReference())); this.modelContext.setCurrentEntityReference(documentReference); findComponentManager(visibility).registerComponent(componentDescriptor, wikiMacro); this.wikiMacroMap.put(documentReference, new WikiMacroData(componentDescriptor.getRoleHint(), wikiMacro)); } catch (Exception e) { throw new WikiMacroException(String.format("Failed to register macro [%s] in [%s] for visibility [%s]", wikiMacro.getDescriptor().getId().getId(), documentReference, visibility), e); } finally { this.bridge.setCurrentUser(currentUser); this.modelContext.setCurrentEntityReference(currentEntityReference); } } else { throw new InsufficientPrivilegesException(String.format( "Unable to register macro [%s] in [%s] for visibility [%s] due to insufficient privileges", wikiMacro.getDescriptor().getId().getId(), documentReference, macroDescriptor.getVisibility())); } }  @Override boolean hasWikiMacro(DocumentReference documentReference); @Override void registerWikiMacro(DocumentReference documentReference, WikiMacro wikiMacro); @Override void unregisterWikiMacro(DocumentReference documentReference);  }