@Test public void saveNotificationPreferencesWithoutRight() throws Exception { DocumentReference userDoc = new DocumentReference("wikiA", "SpaceA", "UserA"); AccessDeniedException e = mock(AccessDeniedException.class); doThrow(e).when(authorizationManager).checkAccess(Right.EDIT, userDoc); String json = ""; Exception caughtException = null; try { mocker.getComponentUnderTest().saveNotificationPreferences(json, userDoc); } catch (Exception ex) { caughtException = ex; } assertNotNull(caughtException); assertEquals(e, caughtException); }
private void saveNotificationPreferences(String json, String providerHint, EntityReference target, NotificationPreferenceCategory category) throws NotificationException { List<NotificationPreference> existingPreferences = Collections.emptyList(); if (target instanceof DocumentReference) { existingPreferences = notificationPreferenceManager.getAllPreferences((DocumentReference) target); } else if (target instanceof WikiReference) { existingPreferences = notificationPreferenceManager.getAllPreferences((WikiReference) target); } TargetableNotificationPreferenceBuilder targetableNotificationPreferenceBuilder = targetableNotificationPreferenceBuilderProvider.get(); List<NotificationPreference> toSave = new ArrayList<>(); try { ObjectMapper objectMapper = new ObjectMapper(); List<Map<String, Object>> preferences = objectMapper.reader().forType(List.class).readValue(json); for (Map<String, Object> item : preferences) { String eventType = (String) item.get("eventType"); NotificationFormat format = NotificationFormat.valueOf(((String) item.get("format")).toUpperCase()); boolean enabled = (Boolean) item.get("enabled"); TargetableNotificationPreference newPreference = targetableNotificationPreferenceBuilder.prepare() .setEnabled(enabled) .setFormat(format) .setProviderHint(providerHint) .setProperties(Collections.singletonMap(NotificationPreferenceProperty.EVENT_TYPE, eventType)) .setTarget(target) .setCategory(category) .build(); NotificationPreference correspondingPreference = getCorrespondingPreference(existingPreferences, newPreference); if (correspondingPreference == null || correspondingPreference.isNotificationEnabled() != newPreference.isNotificationEnabled()) { toSave.add(newPreference); } } notificationPreferenceManager.savePreferences(toSave); } catch (Exception e) { throw new NotificationException("Failed to save preferences for notifications given as JSON.", e); } }
NotificationPreferenceScriptService implements ScriptService { private void saveNotificationPreferences(String json, String providerHint, EntityReference target, NotificationPreferenceCategory category) throws NotificationException { List<NotificationPreference> existingPreferences = Collections.emptyList(); if (target instanceof DocumentReference) { existingPreferences = notificationPreferenceManager.getAllPreferences((DocumentReference) target); } else if (target instanceof WikiReference) { existingPreferences = notificationPreferenceManager.getAllPreferences((WikiReference) target); } TargetableNotificationPreferenceBuilder targetableNotificationPreferenceBuilder = targetableNotificationPreferenceBuilderProvider.get(); List<NotificationPreference> toSave = new ArrayList<>(); try { ObjectMapper objectMapper = new ObjectMapper(); List<Map<String, Object>> preferences = objectMapper.reader().forType(List.class).readValue(json); for (Map<String, Object> item : preferences) { String eventType = (String) item.get("eventType"); NotificationFormat format = NotificationFormat.valueOf(((String) item.get("format")).toUpperCase()); boolean enabled = (Boolean) item.get("enabled"); TargetableNotificationPreference newPreference = targetableNotificationPreferenceBuilder.prepare() .setEnabled(enabled) .setFormat(format) .setProviderHint(providerHint) .setProperties(Collections.singletonMap(NotificationPreferenceProperty.EVENT_TYPE, eventType)) .setTarget(target) .setCategory(category) .build(); NotificationPreference correspondingPreference = getCorrespondingPreference(existingPreferences, newPreference); if (correspondingPreference == null || correspondingPreference.isNotificationEnabled() != newPreference.isNotificationEnabled()) { toSave.add(newPreference); } } notificationPreferenceManager.savePreferences(toSave); } catch (Exception e) { throw new NotificationException("Failed to save preferences for notifications given as JSON.", e); } } }
NotificationPreferenceScriptService implements ScriptService { private void saveNotificationPreferences(String json, String providerHint, EntityReference target, NotificationPreferenceCategory category) throws NotificationException { List<NotificationPreference> existingPreferences = Collections.emptyList(); if (target instanceof DocumentReference) { existingPreferences = notificationPreferenceManager.getAllPreferences((DocumentReference) target); } else if (target instanceof WikiReference) { existingPreferences = notificationPreferenceManager.getAllPreferences((WikiReference) target); } TargetableNotificationPreferenceBuilder targetableNotificationPreferenceBuilder = targetableNotificationPreferenceBuilderProvider.get(); List<NotificationPreference> toSave = new ArrayList<>(); try { ObjectMapper objectMapper = new ObjectMapper(); List<Map<String, Object>> preferences = objectMapper.reader().forType(List.class).readValue(json); for (Map<String, Object> item : preferences) { String eventType = (String) item.get("eventType"); NotificationFormat format = NotificationFormat.valueOf(((String) item.get("format")).toUpperCase()); boolean enabled = (Boolean) item.get("enabled"); TargetableNotificationPreference newPreference = targetableNotificationPreferenceBuilder.prepare() .setEnabled(enabled) .setFormat(format) .setProviderHint(providerHint) .setProperties(Collections.singletonMap(NotificationPreferenceProperty.EVENT_TYPE, eventType)) .setTarget(target) .setCategory(category) .build(); NotificationPreference correspondingPreference = getCorrespondingPreference(existingPreferences, newPreference); if (correspondingPreference == null || correspondingPreference.isNotificationEnabled() != newPreference.isNotificationEnabled()) { toSave.add(newPreference); } } notificationPreferenceManager.savePreferences(toSave); } catch (Exception e) { throw new NotificationException("Failed to save preferences for notifications given as JSON.", e); } }  }
NotificationPreferenceScriptService implements ScriptService { private void saveNotificationPreferences(String json, String providerHint, EntityReference target, NotificationPreferenceCategory category) throws NotificationException { List<NotificationPreference> existingPreferences = Collections.emptyList(); if (target instanceof DocumentReference) { existingPreferences = notificationPreferenceManager.getAllPreferences((DocumentReference) target); } else if (target instanceof WikiReference) { existingPreferences = notificationPreferenceManager.getAllPreferences((WikiReference) target); } TargetableNotificationPreferenceBuilder targetableNotificationPreferenceBuilder = targetableNotificationPreferenceBuilderProvider.get(); List<NotificationPreference> toSave = new ArrayList<>(); try { ObjectMapper objectMapper = new ObjectMapper(); List<Map<String, Object>> preferences = objectMapper.reader().forType(List.class).readValue(json); for (Map<String, Object> item : preferences) { String eventType = (String) item.get("eventType"); NotificationFormat format = NotificationFormat.valueOf(((String) item.get("format")).toUpperCase()); boolean enabled = (Boolean) item.get("enabled"); TargetableNotificationPreference newPreference = targetableNotificationPreferenceBuilder.prepare() .setEnabled(enabled) .setFormat(format) .setProviderHint(providerHint) .setProperties(Collections.singletonMap(NotificationPreferenceProperty.EVENT_TYPE, eventType)) .setTarget(target) .setCategory(category) .build(); NotificationPreference correspondingPreference = getCorrespondingPreference(existingPreferences, newPreference); if (correspondingPreference == null || correspondingPreference.isNotificationEnabled() != newPreference.isNotificationEnabled()) { toSave.add(newPreference); } } notificationPreferenceManager.savePreferences(toSave); } catch (Exception e) { throw new NotificationException("Failed to save preferences for notifications given as JSON.", e); } }  void saveNotificationPreferences(String json, DocumentReference userReference); void saveNotificationPreferences(String json); void saveNotificationPreferencesForCurrentWiki(String json); void setStartDate(Date startDate); void setStartDate(String userId, Date startDate); boolean hasAnyEnabledNotificationPreferences(); NotificationEmailDiffType getDiffType(); NotificationEmailDiffType getDiffType(String userId); boolean isEventTypeEnabled(String eventType, NotificationFormat format, String wiki); boolean isEventTypeEnabled(String eventType, NotificationFormat format); }
NotificationPreferenceScriptService implements ScriptService { private void saveNotificationPreferences(String json, String providerHint, EntityReference target, NotificationPreferenceCategory category) throws NotificationException { List<NotificationPreference> existingPreferences = Collections.emptyList(); if (target instanceof DocumentReference) { existingPreferences = notificationPreferenceManager.getAllPreferences((DocumentReference) target); } else if (target instanceof WikiReference) { existingPreferences = notificationPreferenceManager.getAllPreferences((WikiReference) target); } TargetableNotificationPreferenceBuilder targetableNotificationPreferenceBuilder = targetableNotificationPreferenceBuilderProvider.get(); List<NotificationPreference> toSave = new ArrayList<>(); try { ObjectMapper objectMapper = new ObjectMapper(); List<Map<String, Object>> preferences = objectMapper.reader().forType(List.class).readValue(json); for (Map<String, Object> item : preferences) { String eventType = (String) item.get("eventType"); NotificationFormat format = NotificationFormat.valueOf(((String) item.get("format")).toUpperCase()); boolean enabled = (Boolean) item.get("enabled"); TargetableNotificationPreference newPreference = targetableNotificationPreferenceBuilder.prepare() .setEnabled(enabled) .setFormat(format) .setProviderHint(providerHint) .setProperties(Collections.singletonMap(NotificationPreferenceProperty.EVENT_TYPE, eventType)) .setTarget(target) .setCategory(category) .build(); NotificationPreference correspondingPreference = getCorrespondingPreference(existingPreferences, newPreference); if (correspondingPreference == null || correspondingPreference.isNotificationEnabled() != newPreference.isNotificationEnabled()) { toSave.add(newPreference); } } notificationPreferenceManager.savePreferences(toSave); } catch (Exception e) { throw new NotificationException("Failed to save preferences for notifications given as JSON.", e); } }  void saveNotificationPreferences(String json, DocumentReference userReference); void saveNotificationPreferences(String json); void saveNotificationPreferencesForCurrentWiki(String json); void setStartDate(Date startDate); void setStartDate(String userId, Date startDate); boolean hasAnyEnabledNotificationPreferences(); NotificationEmailDiffType getDiffType(); NotificationEmailDiffType getDiffType(String userId); boolean isEventTypeEnabled(String eventType, NotificationFormat format, String wiki); boolean isEventTypeEnabled(String eventType, NotificationFormat format);  }