WidgetResource extends AssetResource<Widget> { @RequestMapping(value = "/{widgetId}/properties", method = RequestMethod.POST) public List<Property> addProperty(@PathVariable("widgetId") String widgetId, @RequestBody Property property) throws RepositoryException, NotFoundException, NotAllowedException { checkWidgetIdIsNotAPbWidget(widgetId); return widgetRepository.addProperty(widgetId, property); } @Inject  WidgetResource(JacksonObjectMapper objectMapper,
                          WidgetRepository widgetRepository,
                          WidgetService widgetService,
                          AssetService<Widget> widgetAssetService,
                          @Named("widgetPath") Path widgetPath,
                          List<WidgetContainerRepository> widgetContainerRepositories,
                          AssetVisitor assetVisitor); @RequestMapping(method = RequestMethod.GET) ResponseEntity<String> getAll(@RequestParam(value = "view", defaultValue = "full") String view); @RequestMapping(value = "/{widgetId}", method = RequestMethod.GET) Widget get(@PathVariable("widgetId") String widgetId); @RequestMapping(method = RequestMethod.POST) Widget create(@RequestBody Widget widget, @RequestParam(value = "duplicata", required = false) String sourceWidgetId); @RequestMapping(value = "/{widgetId}", method = RequestMethod.PUT) void save(@PathVariable("widgetId") String widgetId, @RequestBody Widget widget); @RequestMapping(value = "/{widgetId}", method = RequestMethod.DELETE) void delete(@PathVariable("widgetId") String widgetId); @RequestMapping(value = "/{widgetId}/properties", method = RequestMethod.POST) List<Property> addProperty(@PathVariable("widgetId") String widgetId, @RequestBody Property property); @RequestMapping(value = "/{widgetId}/properties/{propertyName}", method = RequestMethod.PUT) List<Property> updateProperty(@PathVariable("widgetId") String widgetId, @PathVariable("propertyName") String propertyName, @RequestBody Property property); @RequestMapping(value = "/{widgetId}/properties/{propertyName}", method = RequestMethod.DELETE) List<Property> deleteProperty(@PathVariable("widgetId") String widgetId, @PathVariable("propertyName") String propertyName); @RequestMapping(value = "/{widgetId}/favorite", method = RequestMethod.PUT) void favorite(@PathVariable("widgetId") String pageId, @RequestBody Boolean favorite); @RequestMapping(value = "/{widgetId}/help", method = RequestMethod.GET, produces = "text/html; charset=UTF-8") void serveWidgetFiles(HttpServletRequest request, HttpServletResponse response, @PathVariable("widgetId") String widgetId);  }
@Test public void should_add_a_property_to_a_widget_and_return_the_list_of_properties() throws Exception { Property property = aProperty().build(); List<Property> expectedProperties = asList(property); when(widgetRepository.addProperty("customLabel", property)).thenReturn(expectedProperties); mockMvc.perform(post("/rest/widgets/customLabel/properties") .contentType(MediaType.APPLICATION_JSON_VALUE) .content(convertObjectToJsonBytes(property))) .andExpect(status().isOk()) .andExpect(content().json(toJson(expectedProperties))); verify(widgetRepository).addProperty("customLabel", property); }
@Test public void should_respond_404_when_adding_a_property_to_an_unexisting_widget() throws Exception { when(widgetRepository.addProperty(eq("unknownWidget"), any(Property.class))).thenThrow(new NotFoundException("not found")); mockMvc.perform(post("/rest/widgets/unknownWidget/properties") .contentType(MediaType.APPLICATION_JSON_VALUE) .content(convertObjectToJsonBytes(aProperty().build()))) .andExpect(status().isNotFound()); }
@Test public void should_respond_500_when_error_appear_while_saving_property() throws Exception { when(widgetRepository.addProperty(eq("label"), any(Property.class))).thenThrow(RepositoryException.class); mockMvc.perform(post("/rest/widgets/label/properties") .contentType(MediaType.APPLICATION_JSON_VALUE) .content(convertObjectToJsonBytes(aProperty().build()))) .andExpect(status().isInternalServerError()); }