### Question:
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);  }### Answer:
@Test public void saveNotificationPreferences() throws Exception { DocumentReference userRef = new DocumentReference("xwiki", "XWiki", "UserA"); NotificationPreferenceImpl existingPref1 = new NotificationPreferenceImpl(true, NotificationFormat.ALERT, "create"); NotificationPreferenceImpl existingPref2 = new NotificationPreferenceImpl(true, NotificationFormat.EMAIL, "update"); NotificationPreferenceImpl existingPref3 = new NotificationPreferenceImpl(false, NotificationFormat.EMAIL, "delete"); when(notificationPreferenceManager.getAllPreferences(eq(userRef))).thenReturn( Arrays.asList(existingPref1, existingPref2, existingPref3)); final MutableBoolean isOk = new MutableBoolean(false); doAnswer(invocationOnMock -> { List<NotificationPreference> prefsToSave = invocationOnMock.getArgument(0); assertEquals(9, prefsToSave.size()); assertTrue(prefsToSave.contains(existingPref1)); assertTrue(prefsToSave.contains(existingPref2)); assertFalse(prefsToSave.contains(existingPref3)); isOk.setTrue(); return true; }).when(notificationPreferenceManager).savePreferences(any(List.class)); mocker.getComponentUnderTest().saveNotificationPreferences( IOUtils.toString(getClass().getResourceAsStream("/preferences.json")), userRef); assertTrue(isOk.booleanValue()); }
@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); }