PreferenceManager { public Map<String, String> update(String userId, Map<String, String> preferences) throws ServerException { requireNonNull(userId, "Required non-null user id"); requireNonNull(preferences, "Required non-null preferences"); final Lock reentrantLock = UPDATE_REENTRANT_LOCKS.get(userId); reentrantLock.lock(); try { final Map<String, String> found = preferenceDao.getPreferences(userId); found.putAll(preferences); preferenceDao.setPreferences(userId, found); return found; } finally { reentrantLock.unlock(); } }  void save(String userId, Map<String, String> preferences); Map<String, String> update(String userId, Map<String, String> preferences); Map<String, String> find(String userId); Map<String, String> find(String userId, String keyFilter); void remove(String userId); void remove(String userId, List<String> names);  }
@Test public void shouldUseMergeStrategyForPreferencesUpdate() throws Exception { final Map<String, String> existingPreferences = new HashMap<>(); existingPreferences.put("pKey1", "pValue1"); existingPreferences.put("pKey2", "pValue2"); existingPreferences.put("pKey3", "pValue3"); existingPreferences.put("pKey4", "pValue4"); when(preferenceDao.getPreferences(any())).thenReturn(existingPreferences); final Map<String, String> newPreferences = new HashMap<>(); newPreferences.put("pKey5", "pValue5"); newPreferences.put("pKey1", "new-value"); preferenceManager.update("user123", newPreferences); verify(preferenceDao).setPreferences(anyString(), preferencesCaptor.capture()); assertEquals( preferencesCaptor.getValue(), ImmutableMap.of( "pKey1", "new-value", "pKey2", "pValue2", "pKey3", "pValue3", "pKey4", "pValue4", "pKey5", "pValue5")); }