WidgetResource extends AssetResource<Widget> { @RequestMapping(method = RequestMethod.POST) public Widget create(@RequestBody Widget widget, @RequestParam(value = "duplicata", required = false) String sourceWidgetId) throws IllegalArgumentException { Widget newWidget = widgetRepository.create(widget); if(isNotEmpty(sourceWidgetId)) { assetService.duplicateAsset(widgetPath, widgetRepository.resolvePath(sourceWidgetId), sourceWidgetId, newWidget.getId()); } return newWidget; } @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_create_a_new_widget() throws Exception { Widget customLabel = aWidget().name("label").custom().build(); when(widgetRepository.create(customLabel)).thenReturn(customLabel); mockMvc .perform(post("/rest/widgets") .contentType(MediaType.APPLICATION_JSON_VALUE) .content(convertObjectToJsonBytes(customLabel))) .andExpect(status().isOk()); verify(widgetRepository).create(notNull(Widget.class)); }
@Test public void should_not_allow_to_create_a_widget_with_an_empty_name() throws Exception { Widget customLabel = aWidget().name("").custom().build(); when(widgetRepository.create(customLabel)).thenThrow(new IllegalArgumentException()); mockMvc .perform(post("/rest/widgets") .contentType(MediaType.APPLICATION_JSON_VALUE) .content(convertObjectToJsonBytes(customLabel))) .andExpect(status().isBadRequest()); }
@Test public void should_not_allow_to_create_a_widget_with_an_existing_name() throws Exception { Widget customLabel = aWidget().name("alreadyExistingName").build(); when(widgetRepository.create(customLabel)).thenThrow(new NotAllowedException("already existing name")); mockMvc .perform(post("/rest/widgets") .contentType(MediaType.APPLICATION_JSON_VALUE) .content(convertObjectToJsonBytes(customLabel))) .andExpect(status().isForbidden()); }