### Question:
RegionWidgetApi extends AbstractRestApi { @ResponseBody @RequestMapping(value = "/{regionWidgetId}/preferences/{regionWidgetPreferenceName}", method = RequestMethod.PUT) public RegionWidgetPreference createOrReplaceRegionWidgetPreference(@PathVariable String regionWidgetId, @PathVariable String regionWidgetPreferenceName, @RequestBody RegionWidgetPreference regionWidgetPreference) { if (logger.isDebugEnabled()) { logger.debug("PUT received to create or replace preference: " + regionWidgetPreferenceName + " for regionWidget: " + regionWidgetId + " with data: " + regionWidgetPreference); } Validate.isTrue(regionWidgetPreferenceName.equalsIgnoreCase(regionWidgetPreference.getName()), "The preference name in the URL does not match the preference name in the RegionWidgetPreference object."); return regionWidgetService.saveRegionWidgetPreference(regionWidgetId, regionWidgetPreference); } @Autowired  RegionWidgetApi(RegionWidgetService regionWidgetService); @ResponseBody @RequestMapping(value = "/{regionWidgetId}/preferences", method = RequestMethod.PUT) RegionWidgetPreferenceListWrapper replaceAllRegionWidgetPreferences(@PathVariable String regionWidgetId,
                                                                               @RequestBody RegionWidgetPreferenceListWrapper
                                                                                       regionWidgetPreferenceListWrapper); @ResponseBody @RequestMapping(value = "/{regionWidgetId}/preferences/{regionWidgetPreferenceName}", method = RequestMethod.PUT) RegionWidgetPreference createOrReplaceRegionWidgetPreference(@PathVariable String regionWidgetId,
                                                                        @PathVariable String regionWidgetPreferenceName,
                                                                        @RequestBody RegionWidgetPreference regionWidgetPreference); @ResponseBody @RequestMapping(value = "/{regionWidgetId}/collapsed", method = RequestMethod.PUT) RegionWidget updateRegionWidgetCollapsedStatus(@PathVariable String regionWidgetId,
                                                          @RequestBody Boolean collapsed);  }### Answer:
@Test public void createOrReplaceRegionWidgetPreference_validParams() { RegionWidgetPreference PREFERENCE = new RegionWidgetPreferenceImpl(VALID_REGION_WIDGET_ID, VALID_PREFERENCE_NAME, VALID_PREFERENCE_VALUE); expect(regionWidgetService.saveRegionWidgetPreference(VALID_REGION_WIDGET_ID, PREFERENCE)).andReturn(PREFERENCE); replay(regionWidgetService); RegionWidgetPreference result = regionWidgetApi.createOrReplaceRegionWidgetPreference(VALID_REGION_WIDGET_ID, VALID_PREFERENCE_NAME, PREFERENCE); verify(regionWidgetService); assertThat(result, sameInstance(PREFERENCE)); }
@Test(expected = IllegalArgumentException.class) public void createOrReplaceRegionWidgetPreference_invalidParams_preferenceName() { RegionWidgetPreference PREFERENCE = new RegionWidgetPreferenceImpl(VALID_REGION_WIDGET_ID, VALID_PREFERENCE_NAME, VALID_PREFERENCE_VALUE); regionWidgetApi.createOrReplaceRegionWidgetPreference(VALID_REGION_WIDGET_ID, "different", PREFERENCE); }
@Test(expected = IllegalArgumentException.class) public void createOrReplaceRegionWidgetPreference_invalidParams_regionWidgetId() { RegionWidgetPreference PREFERENCE = new RegionWidgetPreferenceImpl(VALID_REGION_WIDGET_ID, VALID_PREFERENCE_NAME, VALID_PREFERENCE_VALUE); expect(regionWidgetService.saveRegionWidgetPreference(INVALID_REGION_WIDGET_ID, PREFERENCE)).andThrow( new IllegalArgumentException()); replay(regionWidgetService); regionWidgetApi.createOrReplaceRegionWidgetPreference(INVALID_REGION_WIDGET_ID, VALID_PREFERENCE_NAME, PREFERENCE); }