### Question:
ContextualPreferenceManager { @Transactional public ContextualPreference upsert(final ContextualPreferenceVO preferenceVO) { validatePreferenceFields(preferenceVO); validatePreferenceTypeAccordingToPreferencesWithTheSameName(preferenceVO); Assert.isTrue(preferenceVO.getResource().getLevel() != ContextualPreferenceLevel.SYSTEM, messageHelper.getMessage( MessageConstants.ERROR_SAVE_CONTEXTUAL_PREFERENCE_EXTERNAL_RESOURCE_LEVEL_INVALID)); final ContextualPreference preference = preferenceFromVO(preferenceVO); if (contextualPreferenceHandler.isValid(preference)) { return contextualPreferenceDao.upsert(preference); } else { throw new IllegalArgumentException(messageHelper.getMessage( MessageConstants.ERROR_CONTEXTUAL_PREFERENCE_EXTERNAL_RESOURCE_NOT_FOUND, preference.getResource())); } }  List<ContextualPreference> loadAll(); ContextualPreference load(final String name, final ContextualPreferenceExternalResource resource); ContextualPreference search(final List<String> preferences,
                                       final ContextualPreferenceExternalResource resource); ContextualPreference search(final List<String> preferences); @Transactional ContextualPreference upsert(final ContextualPreferenceVO preferenceVO); @Transactional ContextualPreference delete(final String name, final ContextualPreferenceExternalResource resource);  }### Answer:
@Test public void upsertShouldFailIfPreferenceHasEmptyFields() { when(contextualPreferenceHandler.isValid(any())).thenReturn(true); assertThrows(IllegalArgumentException.class, () -> manager.upsert(new ContextualPreferenceVO(null, VALUE, TYPE, toolResource))); assertThrows(IllegalArgumentException.class, () -> manager.upsert(new ContextualPreferenceVO(NAME, null, TYPE, toolResource))); assertThrows(IllegalArgumentException.class, () -> manager.upsert(new ContextualPreferenceVO(NAME, VALUE, null, toolResource))); assertThrows(IllegalArgumentException.class, () -> manager.upsert(new ContextualPreferenceVO(NAME, VALUE, TYPE, new ContextualPreferenceExternalResource(null, TOOL_ID)))); assertThrows(IllegalArgumentException.class, () -> manager.upsert(new ContextualPreferenceVO(NAME, VALUE, TYPE, new ContextualPreferenceExternalResource(LEVEL, null)))); }
@Test public void upsertShouldFailIfPreferenceLevelIsSystem() { final ContextualPreferenceExternalResource resource = new ContextualPreferenceExternalResource( ContextualPreferenceLevel.SYSTEM, TOOL_ID); assertThrows(IllegalArgumentException.class, () -> manager.upsert(new ContextualPreferenceVO(NAME, VALUE, TYPE, resource))); }
@Test public void upsertShouldFailIfPreferenceExternalResourceDoesNotExist() { final ContextualPreferenceVO preferenceVO = new ContextualPreferenceVO(NAME, VALUE, TYPE, toolResource); final ContextualPreference preference = new ContextualPreference(NAME, VALUE, TYPE, toolResource); when(contextualPreferenceHandler.isValid(eq(preference))).thenReturn(false); assertThrows(IllegalArgumentException.class, () -> manager.upsert(preferenceVO)); }
@Test public void upsertShouldSavePreference() { final ContextualPreferenceVO preferenceVO = new ContextualPreferenceVO(NAME, VALUE, TYPE, toolResource); final ContextualPreference preference = new ContextualPreference(NAME, VALUE, TYPE, toolResource); when(contextualPreferenceHandler.isValid(eq(preference))).thenReturn(true); manager.upsert(preferenceVO); verify(contextualPreferenceDao).upsert(eq(preference)); }