WidgetResource extends AssetResource<Widget> { @RequestMapping(value = "/{widgetId}", method = RequestMethod.GET) public Widget get(@PathVariable("widgetId") String widgetId) throws RepositoryException, NotAllowedException { Widget widget = widgetService.get(widgetId); widget.setAssets(assetVisitor.visit(widget)); return widget; } @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_get_a_widget_by_its_id() throws Exception { Widget input = aWidget().id("input").build(); when(widgetService.get("input")).thenReturn(input); mockMvc.perform(get("/rest/widgets/input")) .andExpect(status().isOk()) .andExpect(content().json(toJson(input))); }
@Test public void should_get_a_widget_with_asset_by_its_id() throws Exception { Widget input = aWidget().id("input").assets(anAsset().withName("myScopeWidgetAsset").withType(AssetType.CSS)) .build(); when(widgetService.get("input")).thenReturn(input); mockMvc.perform(get("/rest/widgets/input")) .andExpect(status().isOk()) .andExpect(content().json(toJson(input))) .andExpect(jsonPath("assets[*].scope").exists()) .andExpect(jsonPath("assets[*].scope").value(everyItem(is("widget")))); }
@Test public void should_respond_404_when_getting_an_unexisting_widget() throws Exception { when(widgetService.get("notExistingWidget")).thenThrow(new NotFoundException("not found")); mockMvc.perform(get("/rest/widgets/notExistingWidget")) .andExpect(status().isNotFound()); }
@Test public void should_not_allow_to_save_a_not_custom_widget() throws Exception { Widget pbWidget = aWidget().id("input").build(); when(widgetService.get("input")).thenReturn(pbWidget); mockMvc.perform(put("/rest/widgets/customLabel") .contentType(MediaType.APPLICATION_JSON_VALUE) .content(convertObjectToJsonBytes(pbWidget))) .andExpect(status().isForbidden()); }
@Test public void should_respond_404_not_found_if_custom_widget_is_not_existing_when_renaming() throws Exception { when(widgetRepository.get("my-widget")).thenThrow(new NotFoundException("page not found")); mockMvc .perform(put("/rest/widgets/my-widget/name").contentType(MediaType.APPLICATION_JSON_VALUE).content("hello")) .andExpect(status().isNotFound()); }
@Test public void should_upload_a_local_asset() throws Exception { MockMultipartFile file = new MockMultipartFile("file", "myfile.js", "application/javascript", "foo".getBytes()); Widget widget = aWidget().id("my-widget").custom().build(); Asset expectedAsset = anAsset().withId("assetId").active().withName("myfile.js").withOrder(2).withType(AssetType.JAVASCRIPT).build(); when(widgetRepository.get("my-widget")).thenReturn(widget); when(widgetAssetService.upload(file, widget, "js")).thenReturn(expectedAsset); mockMvc.perform(fileUpload("/rest/widgets/my-widget/assets/js").file(file)) .andExpect(status().isCreated()) .andExpect(jsonPath("$.id").value("assetId")) .andExpect(jsonPath("$.name").value("myfile.js")) .andExpect(jsonPath("$.type").value("js")) .andExpect(jsonPath("$.order").value(2)); verify(widgetAssetService).upload(file, widget, "js"); }
@Test public void should_not_upload_an_asset() throws Exception { MockMultipartFile file = new MockMultipartFile("file", "myfile.js", "application/javascript", "foo".getBytes()); Widget widget = aWidget().id("my-widget").custom().build(); when(widgetRepository.get("my-widget")).thenReturn(widget); doThrow(IllegalArgumentException.class).when(widgetAssetService).upload(file, widget, "js"); mockMvc.perform(fileUpload("/rest/widgets/my-widget/assets/js").file(file)) .andExpect(status().isBadRequest()); verify(widgetAssetService).upload(file, widget, "js"); }
@Test public void should_not_upload_an_asset_for_custom_widget() throws Exception { MockMultipartFile file = new MockMultipartFile("file", "myfile.js", "application/javascript", "foo".getBytes()); Widget widget = aWidget().id("my-widget").build(); when(widgetRepository.get("my-widget")).thenReturn(widget); doThrow(IllegalArgumentException.class).when(widgetAssetService).upload(file, widget, "js"); mockMvc.perform(fileUpload("/rest/widgets/my-widget/assets/js").file(file)) .andExpect(status().isBadRequest()); }
@Test public void should_not_save_an_external_asset_for_internal_widget() throws Exception { Widget widget = aWidget().id("pb-widget").build(); Asset asset = anAsset().build(); when(widgetRepository.get("pb-widget")).thenReturn(widget); mockMvc.perform( post("/rest/widgets/pb-widget/assets") .contentType(MediaType.APPLICATION_JSON_VALUE) .content(convertObjectToJsonBytes(asset))) .andExpect(status().isForbidden()); }
@Test public void should_increment_an_asset() throws Exception { Widget widget = aWidget().id("my-widget").custom().build(); Asset asset = anAsset().withComponentId("my-widget").withOrder(3).build(); when(widgetRepository.get("my-widget")).thenReturn(widget); mockMvc.perform( put("/rest/widgets/my-widget/assets/UIID?increment=true") .contentType(MediaType.APPLICATION_JSON_VALUE) .content(convertObjectToJsonBytes(asset))) .andExpect(status().isOk()); verify(widgetAssetService).changeAssetOrderInComponent(widget, "UIID", INCREMENT); }
@Test public void should_decrement_an_asset() throws Exception { Widget widget = aWidget().id("my-widget").custom().build(); Asset asset = anAsset().withComponentId("my-widget").withOrder(3).build(); when(widgetRepository.get("my-widget")).thenReturn(widget); mockMvc.perform( put("/rest/widgets/my-widget/assets/UIID?decrement=true") .contentType(MediaType.APPLICATION_JSON_VALUE) .content(convertObjectToJsonBytes(asset))) .andExpect(status().isOk()); verify(widgetAssetService).changeAssetOrderInComponent(widget, "UIID", DECREMENT); }
@Test public void should_load_widget_asset_on_disk_with_content_type_text() throws Exception { Path expectedFile = widgetRepositoryPath.resolve("pbLabel/pbLabel.js"); when(widgetAssetService.findAssetPath("widget-id", "asset.js", AssetType.JAVASCRIPT.getPrefix())).thenReturn(expectedFile); mockMvc .perform(get("/rest/widgets/widget-id/assets/js/asset.js?format=text")) .andExpect(status().isOk()) .andExpect(content().bytes(readAllBytes(expectedFile))) .andExpect(content().contentType(MediaType.TEXT_PLAIN)) .andExpect(header().string("Content-Length", String.valueOf(expectedFile.toFile().length()))) .andExpect(header().string("Content-Disposition", "inline; filename=\"pbLabel.js\"")) .andExpect(content().encoding("UTF-8")); }
@Test public void should_download_widget_asset() throws Exception { Path expectedFile = widgetRepositoryPath.resolve("pbLabel/pbLabel.js"); when(widgetAssetService.findAssetPath("widget-id", "asset.js", AssetType.JAVASCRIPT.getPrefix())).thenReturn(expectedFile); mockMvc .perform(get("/rest/widgets/widget-id/assets/js/asset.js")) .andExpect(status().isOk()) .andExpect(content().bytes(readAllBytes(expectedFile))) .andExpect(content().contentType(MediaType.APPLICATION_OCTET_STREAM_VALUE)) .andExpect(header().string("Content-Length", String.valueOf(expectedFile.toFile().length()))) .andExpect(header().string("Content-Disposition", "attachment; filename=\"pbLabel.js\"")) .andExpect(content().encoding("UTF-8")); }
@Test public void should_respond_404_when_widget_asset_included_in_page_is_not_found() throws Exception { when(widgetAssetService.findAssetPath("widget-id", "asset.js", AssetType.JAVASCRIPT.getPrefix())).thenReturn(null); mockMvc.perform(get("/rest/widgets/widget-id/assets/js/asset.js?format=text")).andExpect(status().isNotFound()); mockMvc.perform(get("/rest/widgets/widget-id/assets/js/asset.js")).andExpect(status().isNotFound()); }
@Test public void should_respond_500_when_widget_asset_included_in_page_produce_IOException() throws Exception { when(widgetAssetService.findAssetPath("widget-id", "asset.js", AssetType.JAVASCRIPT.getPrefix())).thenThrow(new IOException("can't read file")); mockMvc.perform(get("/rest/widgets/widget-id/assets/js/asset.js?format=text")).andExpect(status().isInternalServerError()); mockMvc.perform(get("/rest/widgets/widget-id/assets/js/asset.js")).andExpect(status().isInternalServerError()); }
@Test @Ignore("Test ignored because failed on CI") public void should_download_help() throws Exception { Path expectedFile = widgetRepositoryPath.resolve("pbText/help.html"); mockMvc .perform(get("/rest/widgets/pbText/help")) .andExpect(status().isOk()) .andExpect(content().bytes(readAllBytes(expectedFile))) .andExpect(content().contentType(MediaType.TEXT_HTML)) .andExpect(header().string("Content-Length", String.valueOf(expectedFile.toFile().length()))) .andExpect(header().string("Content-Disposition", "inline; filename=\"help.html\"")) .andExpect(content().encoding("UTF-8")); }
@Test public void should_respond_404_when_widget_help_is_not_found() throws Exception { mockMvc.perform(get("/rest/widgets/pbLabel/help")).andExpect(status().isNotFound()); }