DirectiveFileGenerator { public String generateAllDirectivesFilesInOne(Previewable previewable, Path path) { List<Path> filename = getWidgetsFilesUsedInPage(previewable); byte[] content = concatenate(filename); content = minify(content); String fileHash = DigestUtils.sha1Hex(content); WidgetFileHelper.deleteOldConcatenateFiles(path, fileHash); Path file = WidgetFileHelper.writeFile(content, path, fileHash + ".min"); return file.getFileName().toString(); } @Inject  DirectiveFileGenerator(WorkspacePathResolver pathResolver, WidgetRepository widgetRepository,
                WidgetIdVisitor widgetIdVisitor); List<Path> getWidgetsFilesUsedInPage(Previewable previewable); byte[] concatenate(List<Path> paths); byte[] minify(byte[] content); String generateAllDirectivesFilesInOne(Previewable previewable, Path path);  }
@Test public void should_get_file_name_of_concatenation_widgets_directives_file_who_was_use_in_page() throws Exception { Page page = aPage().build(); initWidgetsFileWhoUsedInPage(page); mockWidgetIdVisitorAndWidgetRepository(page, "pbLabel", "paragraph"); Path pagePath = temporaryFolder.toPath().resolve("pages").resolve(page.getId()); String filename = generator.generateAllDirectivesFilesInOne(page, pagePath); assertThat(filename).isEqualTo("widgets-f66d6f4cc38b2259ec29fa16e9f171251cd3d346.min.js"); }
@Test public void should_concatenate_and_minify_widgets_directives_file_who_was_use_in_page() throws Exception { Page page = aPage().build(); initFilesForConcatAndMinify(page); mockWidgetIdVisitorAndWidgetRepository(page, "pbLabel", "paragraph"); Path path = temporaryFolder.toPath().resolve("pages").resolve(page.getId()); String expected = "\nList<String>filename=getWidgetsFilesUsedInPage(previewable);" + "byte[]content=getConcatenationWidgetsDirectives(filename);file2"; String filename = generator.generateAllDirectivesFilesInOne(page, path); assertThat(new String(readAllBytes(path.resolve(filename)),"UTF-8")).isEqualTo(expected); assertThat(filename).isEqualTo("widgets-0f2d4ba1fa1992794df3dca7a9b8e4ec735b4746.min.js"); }