PageResource extends AssetResource<Page> { @RequestMapping(value = "/{pageId}", method = RequestMethod.GET) public MappingJacksonValue get(@PathVariable("pageId") String pageId) throws NotFoundException, RepositoryException { Page page = pageService.get(pageId); page.setAssets(assetVisitor.visit(page)); FilterProvider filters = new SimpleFilterProvider() .addFilter("valueAsArray", SimpleBeanPropertyFilter.serializeAllExcept("value")); MappingJacksonValue mapping = new MappingJacksonValue(page); mapping.setFilters(filters); return mapping; } @Inject  PageResource(
            PageService pageService,
            PageRepository pageRepository,
            SimpMessagingTemplate messagingTemplate,
            ContractToPageMapper contractToPageMapper,
            AssetService<Page> pageAssetService,
            AssetVisitor assetVisitor,
            ComponentVisitor componentVisitor, AuthRulesCollector authRulesCollector); @RequestMapping(method = RequestMethod.GET) @JsonView(JsonViewLight.class) List<Page> list(); @RequestMapping(method = RequestMethod.POST) ResponseEntity<Page> create(@RequestBody Page page,
            @RequestParam(value = "duplicata", required = false) String sourcePageId); @RequestMapping(value = "/contract/{scope}/{name}", method = RequestMethod.POST) ResponseEntity<Page> create(@RequestBody Contract contract, @PathVariable("scope") String scope,
            @PathVariable("name") String name); @RequestMapping(value = "/{pageId}", method = RequestMethod.PUT) ResponseEntity<Void> save(HttpServletRequest request, @PathVariable("pageId") String pageId,
            @RequestBody Page page); @RequestMapping(value = "/{pageId}/name", method = RequestMethod.PUT) ResponseEntity<Void> rename(HttpServletRequest request, @PathVariable("pageId") String pageId,
            @RequestBody String name); @RequestMapping(value = "/{pageId}/favorite", method = RequestMethod.PUT) void favorite(@PathVariable("pageId") String pageId, @RequestBody Boolean favorite); @RequestMapping(value = "/{pageId}", method = RequestMethod.GET) MappingJacksonValue get(@PathVariable("pageId") String pageId); @RequestMapping(value = "/{pageId}", method = RequestMethod.DELETE) void delete(@PathVariable("pageId") String pageId); @RequestMapping(value = "/{pageId}/resources", method = RequestMethod.GET) List<String> getResources(@PathVariable("pageId") String pageId); static final String BONITA_RESOURCE_REGEX; static final String EXTENSION_RESOURCE_REGEX; }
@Test public void should_list_pages() throws Exception { Page page = new Page(); page.setId("id"); page.setName("name"); page.setLastUpdate(Instant.parse("2015-02-02")); when(pageRepository.getAll()).thenReturn(Arrays.asList(page)); mockMvc.perform(get("/rest/pages")) .andExpect(status().isOk()) .andExpect(jsonPath("$", hasSize(1))) .andExpect(jsonPath("$[*].id").value("id")) .andExpect(jsonPath("$[*].name").value("name")) .andExpect(jsonPath("$[*].lastUpdate").value(Instant.parse("2015-02-02").getMillis())); }
@Test public void should_retrieve_a_page_representation_by_its_id() throws Exception { Page expectedPage = aFilledPage("my-page"); when(pageService.get("my-page")).thenReturn(expectedPage); mockMvc .perform(get("/rest/pages/my-page")) .andDo(print()) .andExpect(status().isOk()) .andExpect(content().contentType("application/json;charset=UTF-8")) .andExpect(jsonPath("$", notNullValue())); }
@Test public void should_respond_404_not_found_if_page_is_not_existing() throws Exception { when(pageService.get("my-page")).thenThrow(new NotFoundException("page not found")); mockMvc.perform(get("/rest/pages/my-page")).andExpect(status().isNotFound()); }
@Test public void should_respond_404_not_found_if_page_is_not_existing_when_renaming() throws Exception { when(pageService.get("my-page")).thenThrow(new NotFoundException("page not found")); mockMvc .perform(put("/rest/pages/my-page/name").contentType(MediaType.APPLICATION_JSON_VALUE).content(convertObjectToJsonBytes("hello"))) .andExpect(status().isNotFound()); }
@Test public void should_list_page_assets() throws Exception { Page page = mockPageOfId("my-page"); Asset[] assets = new Asset[]{ anAsset().withName("myCss.css").withType(AssetType.CSS).withScope(AssetScope.WIDGET).withComponentId("widget-id").build(), anAsset().withName("myJs.js").withType(JAVASCRIPT).withScope(AssetScope.PAGE).build(), anAsset().withName("https: }; when(assetVisitor.visit(page)).thenReturn(Sets.newHashSet(assets)); mockMvc.perform(get("/rest/pages/my-page/assets")) .andExpect(status().isOk()) .andExpect(jsonPath("$", hasSize(3))) .andExpect(jsonPath("$[*].name", Matchers.containsInAnyOrder("https: .andExpect(jsonPath("$[*].type", Matchers.containsInAnyOrder("js", "js", "css"))) .andExpect(jsonPath("$[*].scope", Matchers.containsInAnyOrder("page", "page", "widget"))) .andExpect(jsonPath("$[*].componentId", Matchers.containsInAnyOrder("widget-id"))); }
@Test public void should_list_page_assets_while_getting_a_page() throws Exception { Page page = mockPageOfId("my-page"); Asset[] assets = new Asset[]{ anAsset().withName("myCss.css").withType(AssetType.CSS).withScope(AssetScope.WIDGET).withComponentId("widget-id").build(), anAsset().withName("myJs.js").withType(AssetType.JAVASCRIPT).withScope(AssetScope.PAGE).build(), anAsset().withName("https: }; when(assetVisitor.visit(page)).thenReturn(Sets.newHashSet(assets)); mockMvc.perform(get("/rest/pages/my-page")) .andExpect(status().isOk()) .andExpect(jsonPath("$.assets", hasSize(3))) .andExpect(jsonPath("$.assets[*].name", Matchers.containsInAnyOrder("https: .andExpect(jsonPath("$.assets[*].type", Matchers.containsInAnyOrder("js", "js", "css"))) .andExpect(jsonPath("$.assets[*].scope", Matchers.containsInAnyOrder("page", "page", "widget"))) .andExpect(jsonPath("$.assets[*].componentId", Matchers.containsInAnyOrder("widget-id"))); }
@Test public void should_load_page_asset_on_disk_with_content_type_text() throws Exception { Path expectedFile = widgetRepositoryPath.resolve("pbLabel/pbLabel.js"); when(pageAssetService.findAssetPath("page-id", "asset.js", AssetType.JAVASCRIPT.getPrefix())).thenReturn(expectedFile); mockMvc .perform(get("/rest/pages/page-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_page_asset() throws Exception { Path expectedFile = widgetRepositoryPath.resolve("pbLabel/pbLabel.js"); when(pageAssetService.findAssetPath("page-id", "asset.js", AssetType.JAVASCRIPT.getPrefix())).thenReturn(expectedFile); mockMvc .perform(get("/rest/pages/page-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(pageAssetService.findAssetPath("page-id", "asset.js", AssetType.JAVASCRIPT.getPrefix())).thenReturn(null); mockMvc.perform(get("/rest/pages/page-id/assets/js/asset.js?format=text")).andExpect(status().isNotFound()); mockMvc.perform(get("/rest/pages/page-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(pageAssetService.findAssetPath("page-id", "asset.js", AssetType.JAVASCRIPT.getPrefix())).thenThrow(new IOException("can't read file")); mockMvc.perform(get("/rest/pages/page-id/assets/js/asset.js?format=text")).andExpect(status().isInternalServerError()); mockMvc.perform(get("/rest/pages/page-id/assets/js/asset.js")).andExpect(status().isInternalServerError()); }
@Test public void should_show_the_correct_information_for_variables() throws Exception { Path expectedFilePath = Paths.get(getClass().getResource("/page-with-variables/").toURI()).resolve("page-with-variables.json"); String expectedFileString = String.join("", readAllLines(expectedFilePath)); Map<String, Variable> variables = new HashMap<>(); variables.put("constantVar", new Variable(DataType.CONSTANT, "constantVariableValue")); variables.put("jsonVar", new Variable(DataType.JSON, "{\"var1\":1, \"var2\":2, \"var3\":\"value3\"}")); variables.put("jsVar", new Variable(DataType.EXPRESSION, "var variable = \"hello\"; return variable;")); Page page = new Page(); page.setId("page-with-variables"); page.setVariables(variables); page.setDesignerVersion("1.10.6"); page.setName("page"); page.setLastUpdate(new Instant(1514989634397L)); page.setRows(new ArrayList<>()); when(pageService.get("id")).thenReturn(page); mockMvc.perform(get("/rest/pages/id")) .andExpect(status().isOk()) .andExpect(content().string(expectedFileString)); }