From c86919c26807caee346fcbfa649dd21b399c811e Mon Sep 17 00:00:00 2001 From: Ramesh Babu Prudhvi Date: Tue, 16 Jan 2024 20:03:13 +0530 Subject: [PATCH 01/16] [DataBind] Added support to create CSV file --- .../selcukes/collections/DataTable.java | 17 +++++++++++- .../selcukes/collections/Resources.java | 24 +++++++++++++++-- .../github/selcukes/collections/Streams.java | 11 ++++++++ .../selcukes/collections/TextTable.java | 22 +++++++++++++++ .../selcukes/databind/csv/CsvMapper.java | 27 +++++++++++++++++++ .../selcukes/databind/tests/CsvTest.java | 5 ++++ 6 files changed, 103 insertions(+), 3 deletions(-) diff --git a/selcukes-collections/src/main/java/io/github/selcukes/collections/DataTable.java b/selcukes-collections/src/main/java/io/github/selcukes/collections/DataTable.java index 82c74eaac..1b11ffcd3 100644 --- a/selcukes-collections/src/main/java/io/github/selcukes/collections/DataTable.java +++ b/selcukes-collections/src/main/java/io/github/selcukes/collections/DataTable.java @@ -455,7 +455,7 @@ public Map aggregateByColumn(@NonNull K columnName, @NonNull K groupColumn /** * Returns a string representation of a {@link DataTable}. The output table - * is formatted to align columns and provide a separator line between the + * is formatted to align columns and provides a separator line between the * header and data rows. The width of each column is determined by the * length of the longest data value for that column. * @@ -489,4 +489,19 @@ public String toString() { forEach(row -> table.add(row.values().toString())); return table.toString(); } + + /** + * Converts the {@link DataTable} to a CSV format. + *

+ * This method uses the {@link TextTable} utility class to format the CSV + * output. The resulting CSV string represents the DataTable in a + * standardized format with aligned columns and a separator line between the + * header and data rows. The width of each column is determined by the + * length of the longest data value for that column. + * + * @return a string representing the DataTable in CSV format + */ + public String toCSV() { + return TextTable.of(this).printCSV(); + } } diff --git a/selcukes-collections/src/main/java/io/github/selcukes/collections/Resources.java b/selcukes-collections/src/main/java/io/github/selcukes/collections/Resources.java index ba5e1157a..375379548 100644 --- a/selcukes-collections/src/main/java/io/github/selcukes/collections/Resources.java +++ b/selcukes-collections/src/main/java/io/github/selcukes/collections/Resources.java @@ -95,15 +95,15 @@ public String createFile(final Path filePath) { * Writes the provided content to a file at the specified path using UTF-8 * encoding. * - * @param fileContent The content to be written to the file. * @param filePath The path of the file to which the content * will be written. + * @param fileContent The content to be written to the file. * @return The path of the file where the content was * successfully written. * @throws DataStreamException If an I/O error occurs while writing the * content to the file. */ - public Path writeToFile(final @NonNull String fileContent, final Path filePath) { + public Path writeToFile(final Path filePath, final @NonNull String fileContent) { try { return Files.write(filePath, fileContent.getBytes(UTF_8)); } catch (IOException e) { @@ -111,6 +111,26 @@ public Path writeToFile(final @NonNull String fileContent, final Path filePath) } } + /** + * Writes the provided content to a file at the specified path using UTF-8 + * encoding. + * + * @param filePath The path of the file to which the content + * will be written. + * @param fileContent The content to be written to the file. + * @return The path of the file where the content was + * successfully written. + * @throws DataStreamException If an I/O error occurs while writing the + * content to the file. + */ + public Path writeToFile(final Path filePath, final @NonNull Iterable fileContent) { + try { + return Files.write(filePath, fileContent, UTF_8); + } catch (IOException e) { + throw new DataStreamException("Failed to write content to file: " + filePath.toAbsolutePath(), e); + } + } + /** * Copies a file from the source path to the destination path. * diff --git a/selcukes-collections/src/main/java/io/github/selcukes/collections/Streams.java b/selcukes-collections/src/main/java/io/github/selcukes/collections/Streams.java index 6208501b9..40e2e02ac 100644 --- a/selcukes-collections/src/main/java/io/github/selcukes/collections/Streams.java +++ b/selcukes-collections/src/main/java/io/github/selcukes/collections/Streams.java @@ -45,6 +45,17 @@ public Stream of(final Iterator iterator) { .stream(Spliterators.spliteratorUnknownSize(iterator, Spliterator.ORDERED), false); } + /** + * Converts an Iterable to a Stream. + * + * @param iterable The iterable to convert to a stream. + * @param The type of elements in the iterable. + * @return A stream of the iterable. + */ + public static Stream of(final Iterable iterable) { + return StreamSupport.stream(iterable.spliterator(), false); + } + /** * Returns an OptionalInt that contains the index of the first element in * the list that matches the given predicate, or an empty OptionalInt if no diff --git a/selcukes-collections/src/main/java/io/github/selcukes/collections/TextTable.java b/selcukes-collections/src/main/java/io/github/selcukes/collections/TextTable.java index cc884f579..c760033d7 100644 --- a/selcukes-collections/src/main/java/io/github/selcukes/collections/TextTable.java +++ b/selcukes-collections/src/main/java/io/github/selcukes/collections/TextTable.java @@ -19,6 +19,7 @@ import java.util.Map; import java.util.Objects; import java.util.stream.Collectors; +import java.util.stream.Stream; class TextTable { private final DataTable table; @@ -66,6 +67,27 @@ public String printTable() { return separator + "\n" + header + "|\n" + separator + "\n" + rows + "\n" + separator + "\n"; } + public String printCSV() { + return Stream.concat( + Stream.of(getCSVRow(table.getColumns())), + table.stream().map(row -> getCSVRow(row.values()))) + .collect(Collectors.joining("\n")); + } + + private String getCSVRow(Iterable values) { + return Streams.of(values) + .map(Object::toString) + .map(this::escapeCsvValue) + .collect(Collectors.joining(",")); + } + + private String escapeCsvValue(String value) { + if (value.contains(",") || value.contains("\"") || value.contains("\n")) { + return "\"" + value.replace("\"", "\"\"") + "\""; + } + return value; + } + public String printHtmlTable() { var htmlTable = new HtmlTable(); return htmlTable.buildTable(table); diff --git a/selcukes-databind/src/main/java/io/github/selcukes/databind/csv/CsvMapper.java b/selcukes-databind/src/main/java/io/github/selcukes/databind/csv/CsvMapper.java index 0f315d2ff..70af32c3e 100644 --- a/selcukes-databind/src/main/java/io/github/selcukes/databind/csv/CsvMapper.java +++ b/selcukes-databind/src/main/java/io/github/selcukes/databind/csv/CsvMapper.java @@ -18,6 +18,7 @@ import io.github.selcukes.collections.DataTable; import io.github.selcukes.collections.Lists; +import io.github.selcukes.collections.Resources; import io.github.selcukes.collections.Streams; import io.github.selcukes.databind.exception.DataMapperException; import lombok.experimental.UtilityClass; @@ -64,4 +65,30 @@ public DataTable parse(Path filePath, String regex) { public DataTable parse(Path filePath) { return parse(filePath, CSV_REGEX); } + + /** + * Writes a DataTable to a CSV file at the specified path. + * + * @param filePath the path to the CSV file + * @param dataTable the data to be written to the CSV file + * @throws DataMapperException if an error occurs while writing to the file + */ + public void write(Path filePath, DataTable dataTable) { + write(filePath, dataTable.toCSV()); + } + + /** + * Writes a string of CSV data to a CSV file at the specified path. + * + * @param filePath the path to the CSV file + * @param data the CSV data to be written + * @throws DataMapperException if an error occurs while writing to the file + */ + public void write(Path filePath, String data) { + try { + Resources.writeToFile(filePath, Arrays.asList(data.split("\n"))); + } catch (Exception e) { + throw new DataMapperException("Failed writing to CSV File: ", e); + } + } } diff --git a/selcukes-databind/src/test/java/io/github/selcukes/databind/tests/CsvTest.java b/selcukes-databind/src/test/java/io/github/selcukes/databind/tests/CsvTest.java index 0b5eca3a1..b456c559c 100644 --- a/selcukes-databind/src/test/java/io/github/selcukes/databind/tests/CsvTest.java +++ b/selcukes-databind/src/test/java/io/github/selcukes/databind/tests/CsvTest.java @@ -42,6 +42,11 @@ public void csvDataReaderTest() { return row; }); table.forEach(System.out::println); + + var filePath1 = Resources.ofTest("employee1.csv"); + CsvMapper.write(filePath1, table); + var table1 = CsvMapper.parse(filePath1, CSV_STRIP_REGEX); + table1.forEach(System.out::println); } private String updateID(String phone, String country) { From 9f70000ae38b547c4d8383119218e1cf6f9b2c22 Mon Sep 17 00:00:00 2001 From: Ramesh Babu Prudhvi Date: Tue, 16 Jan 2024 20:20:43 +0530 Subject: [PATCH 02/16] [WDB] Fixed deprecated changes --- .../java/io/github/selcukes/wdb/util/FileExtractUtil.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/webdriver-binaries/src/main/java/io/github/selcukes/wdb/util/FileExtractUtil.java b/webdriver-binaries/src/main/java/io/github/selcukes/wdb/util/FileExtractUtil.java index b1bdc4f84..56b4cbc4e 100644 --- a/webdriver-binaries/src/main/java/io/github/selcukes/wdb/util/FileExtractUtil.java +++ b/webdriver-binaries/src/main/java/io/github/selcukes/wdb/util/FileExtractUtil.java @@ -64,7 +64,7 @@ private static Path unZipFile(Path source, Path destination) { try (var fis = Files.newInputStream(source, StandardOpenOption.READ); var zis = new ZipArchiveInputStream(fis)) { ZipArchiveEntry entry; - while ((entry = zis.getNextZipEntry()) != null) { + while ((entry = zis.getNextEntry()) != null) { if (!entry.getName().toUpperCase().contains("LICENSE")) { entryDestination = uncompress(zis, destination, entry); } @@ -81,7 +81,7 @@ private static Path unTarFile(Path source, Path destination) { var gZIPInputStream = new GZIPInputStream(fis); final var tis = new TarArchiveInputStream(gZIPInputStream)) { TarArchiveEntry entry; - while ((entry = tis.getNextTarEntry()) != null) { + while ((entry = tis.getNextEntry()) != null) { if (!entry.getName().toUpperCase().contains("LICENSE")) { entryDestination = uncompress(tis, destination, entry); } From 44ced170bccd23edd4a41be00421ac51f2995564 Mon Sep 17 00:00:00 2001 From: Ramesh Babu Prudhvi Date: Tue, 16 Jan 2024 20:21:56 +0530 Subject: [PATCH 03/16] Updated .editorconfig --- .editorconfig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.editorconfig b/.editorconfig index 318a84fae..969b3204f 100644 --- a/.editorconfig +++ b/.editorconfig @@ -9,7 +9,7 @@ insert_final_newline = true indent_style = space indent_size = 4 -[*.{feature, yml}] +[*.{feature,yml}] indent_style = space indent_size = 2 From b75da4e9a031e3696f3b9bfc1065df19de744d6b Mon Sep 17 00:00:00 2001 From: Ramesh Babu Prudhvi Date: Tue, 16 Jan 2024 20:25:21 +0530 Subject: [PATCH 04/16] [All] Code cleanup --- .../io/github/selcukes/collections/Try.java | 20 +++++++++---------- .../selcukes/commons/http/WebResponse.java | 2 +- .../selcukes/commons/os/Architecture.java | 2 +- .../selcukes/core/page/ui/Dropdown.java | 3 +-- .../selcukes/extent/report/ExtentService.java | 6 ++---- .../selcukes/notifier/enums/NotifierEnum.java | 2 +- 6 files changed, 16 insertions(+), 19 deletions(-) diff --git a/selcukes-collections/src/main/java/io/github/selcukes/collections/Try.java b/selcukes-collections/src/main/java/io/github/selcukes/collections/Try.java index 4969b5d61..8b8d5fb20 100644 --- a/selcukes-collections/src/main/java/io/github/selcukes/collections/Try.java +++ b/selcukes-collections/src/main/java/io/github/selcukes/collections/Try.java @@ -16,6 +16,8 @@ package io.github.selcukes.collections; +import lombok.Getter; + import java.util.Optional; import java.util.function.Function; import java.util.function.Supplier; @@ -30,6 +32,14 @@ */ public class Try { private final T result; + /** + * -- GETTER -- + * Returns the cause of the failure represented by this Try object. + * + * @return the cause of the failure represented by this Try object, or null + * if this Try object represents a success + */ + @Getter private final Exception exception; /** @@ -168,16 +178,6 @@ public Optional getResult() { return Optional.ofNullable(result); } - /** - * Returns the cause of the failure represented by this Try object. - * - * @return the cause of the failure represented by this Try object, or null - * if this Try object represents a success - */ - public Exception getException() { - return exception; - } - /** * Applies the given function to the result of this Try, which produces a * new Try. If this Try contains an exception, a new Try containing the diff --git a/selcukes-commons/src/main/java/io/github/selcukes/commons/http/WebResponse.java b/selcukes-commons/src/main/java/io/github/selcukes/commons/http/WebResponse.java index d0be70af8..ec8ecc0c3 100644 --- a/selcukes-commons/src/main/java/io/github/selcukes/commons/http/WebResponse.java +++ b/selcukes-commons/src/main/java/io/github/selcukes/commons/http/WebResponse.java @@ -36,8 +36,8 @@ * It takes the HTTP response and provides a number of methods to access the * response body, headers, and status code */ +@Getter public class WebResponse { - @Getter private final HttpResponse httpResponse; public WebResponse(final HttpResponse httpResponse) { diff --git a/selcukes-commons/src/main/java/io/github/selcukes/commons/os/Architecture.java b/selcukes-commons/src/main/java/io/github/selcukes/commons/os/Architecture.java index f8fa78f4a..4c5aa7fcb 100644 --- a/selcukes-commons/src/main/java/io/github/selcukes/commons/os/Architecture.java +++ b/selcukes-commons/src/main/java/io/github/selcukes/commons/os/Architecture.java @@ -19,11 +19,11 @@ import lombok.AllArgsConstructor; import lombok.Getter; +@Getter @AllArgsConstructor public enum Architecture { X32(32), X64(64); - @Getter final int value; } diff --git a/selcukes-core/src/main/java/io/github/selcukes/core/page/ui/Dropdown.java b/selcukes-core/src/main/java/io/github/selcukes/core/page/ui/Dropdown.java index 67fec7f4e..c4f3d7f79 100644 --- a/selcukes-core/src/main/java/io/github/selcukes/core/page/ui/Dropdown.java +++ b/selcukes-core/src/main/java/io/github/selcukes/core/page/ui/Dropdown.java @@ -48,15 +48,14 @@ public Object selected(Select select, String optionLocator) { return type.getRetriever().apply(select); } + @Getter @AllArgsConstructor enum SelectionType { LABEL(Select::selectByVisibleText, select -> select.getFirstSelectedOption().getText()), VALUE(Select::selectByValue, select -> select.getFirstSelectedOption().getAttribute(ATTRIBUTE)), INDEX((select, value) -> select.selectByIndex(parseInt(value)), select -> select.getOptions().indexOf(select.getFirstSelectedOption())); - @Getter private final BiConsumer selector; - @Getter private final Function retriever; } diff --git a/selcukes-extent-reports/src/main/java/io/github/selcukes/extent/report/ExtentService.java b/selcukes-extent-reports/src/main/java/io/github/selcukes/extent/report/ExtentService.java index 2ba88f19a..829de5950 100644 --- a/selcukes-extent-reports/src/main/java/io/github/selcukes/extent/report/ExtentService.java +++ b/selcukes-extent-reports/src/main/java/io/github/selcukes/extent/report/ExtentService.java @@ -28,6 +28,7 @@ import io.github.selcukes.collections.Resources; import io.github.selcukes.collections.StringHelper; import io.github.selcukes.databind.properties.PropertiesMapper; +import lombok.Getter; import java.io.IOException; import java.util.Arrays; @@ -51,6 +52,7 @@ public class ExtentService { private static final String VIEW_ORDER_SPARK_KEY = "extent.reporter.spark.vieworder"; private static final String REPORT_NAME = "Automation Report"; private static final String REPORT_TITLE = "Automation Report"; + @Getter private final ExtentReports extentReports; private final Map propertiesMap; @@ -63,10 +65,6 @@ public ExtentService(ExtentReports extentReports) { addSystemInfo(); } - public ExtentReports getExtentReports() { - return extentReports; - } - private Map initProperties() { try { return PropertiesMapper.parse(Resources.ofTest("extent.properties")); diff --git a/selcukes-notifier/src/main/java/io/github/selcukes/notifier/enums/NotifierEnum.java b/selcukes-notifier/src/main/java/io/github/selcukes/notifier/enums/NotifierEnum.java index 8d351466c..9de80ce7a 100644 --- a/selcukes-notifier/src/main/java/io/github/selcukes/notifier/enums/NotifierEnum.java +++ b/selcukes-notifier/src/main/java/io/github/selcukes/notifier/enums/NotifierEnum.java @@ -21,6 +21,7 @@ import lombok.AllArgsConstructor; import lombok.Getter; +@Getter @AllArgsConstructor public enum NotifierEnum { PRETEXT("Selcukes Automation Report"), @@ -37,6 +38,5 @@ public enum NotifierEnum { MESSAGE_CARD("MessageCard"), TIME_STAMP("Time Stamp"), EXCEPTION("Exception"); - @Getter final String value; } From ecc74277bf3a37bed0401340a1dd869479187468 Mon Sep 17 00:00:00 2001 From: Ramesh Babu Prudhvi Date: Wed, 17 Jan 2024 12:13:46 +0530 Subject: [PATCH 05/16] [All] Code cleanup --- .../selcukes/collections/Resources.java | 29 +++---------------- .../selcukes/collections/TextTable.java | 10 +++---- .../io/github/selcukes/collections/Try.java | 8 +---- .../github/selcukes/commons/exec/Shell.java | 15 +++++----- .../selcukes/commons/helper/FileHelper.java | 4 +-- .../selcukes/commons/http/WebClient.java | 2 +- .../extent/report/TestSourcesModel.java | 1 + 7 files changed, 21 insertions(+), 48 deletions(-) diff --git a/selcukes-collections/src/main/java/io/github/selcukes/collections/Resources.java b/selcukes-collections/src/main/java/io/github/selcukes/collections/Resources.java index 375379548..6708a0863 100644 --- a/selcukes-collections/src/main/java/io/github/selcukes/collections/Resources.java +++ b/selcukes-collections/src/main/java/io/github/selcukes/collections/Resources.java @@ -266,11 +266,8 @@ public static OutputStream newOutputStream(Path filePath) { * be parsed */ public URL toURL(String urlStr) { - try { - return new URL(urlStr); - } catch (MalformedURLException e) { - throw new IllegalArgumentException("Invalid URL string: " + urlStr, e); - } + return tryURL(urlStr) + .orElseThrow(() -> new IllegalArgumentException("Invalid URL string: " + urlStr)); } /** @@ -284,27 +281,9 @@ public URL toURL(String urlStr) { */ public Optional tryURL(String urlStr) { try { - return Optional.of(new URL(urlStr)); - } catch (MalformedURLException e) { + return Optional.of(new URI(urlStr).toURL()); + } catch (MalformedURLException | URISyntaxException e) { return Optional.empty(); } } - - /** - * Returns a new URI object by parsing the given URI string. - * - * @param uriStr the URI string to be parsed into a URI - * object - * @return the URI object representing the parsed - * URI string - * @throws IllegalArgumentException if the URI string is invalid and cannot - * be parsed - */ - public URI toURI(String uriStr) { - try { - return new URI(uriStr); - } catch (URISyntaxException e) { - throw new IllegalArgumentException("Invalid URI string: " + uriStr, e); - } - } } diff --git a/selcukes-collections/src/main/java/io/github/selcukes/collections/TextTable.java b/selcukes-collections/src/main/java/io/github/selcukes/collections/TextTable.java index c760033d7..9663a4c49 100644 --- a/selcukes-collections/src/main/java/io/github/selcukes/collections/TextTable.java +++ b/selcukes-collections/src/main/java/io/github/selcukes/collections/TextTable.java @@ -96,13 +96,11 @@ public String printHtmlTable() { private static class HtmlTable { public String buildTable(DataTable table) { - StringBuilder sb = new StringBuilder(); - sb.append("\n"); - sb.append(buildHeaderRow(table.getFirst())); - sb.append(buildDataRows(table)); - sb.append("
"); - return sb.toString(); + return "\n" + + buildHeaderRow(table.getFirst()) + + buildDataRows(table) + + "
"; } private String buildHeaderRow(Map row) { diff --git a/selcukes-collections/src/main/java/io/github/selcukes/collections/Try.java b/selcukes-collections/src/main/java/io/github/selcukes/collections/Try.java index 8b8d5fb20..f40cf4c51 100644 --- a/selcukes-collections/src/main/java/io/github/selcukes/collections/Try.java +++ b/selcukes-collections/src/main/java/io/github/selcukes/collections/Try.java @@ -32,13 +32,7 @@ */ public class Try { private final T result; - /** - * -- GETTER -- - * Returns the cause of the failure represented by this Try object. - * - * @return the cause of the failure represented by this Try object, or null - * if this Try object represents a success - */ + @Getter private final Exception exception; diff --git a/selcukes-commons/src/main/java/io/github/selcukes/commons/exec/Shell.java b/selcukes-commons/src/main/java/io/github/selcukes/commons/exec/Shell.java index de2321c1e..b2819ea85 100644 --- a/selcukes-commons/src/main/java/io/github/selcukes/commons/exec/Shell.java +++ b/selcukes-commons/src/main/java/io/github/selcukes/commons/exec/Shell.java @@ -76,13 +76,14 @@ private ExecResults interactWithProcess(final Process process) { } var output = new StreamGuzzler(process.getInputStream()); var error = new StreamGuzzler(process.getErrorStream()); - var executors = Executors.newFixedThreadPool(2); - executors.submit(error); - executors.submit(output); - executors.shutdown(); - while (!executors.isTerminated()) { - // Wait for all the tasks to complete. - Await.until(1); + try (var executors = Executors.newFixedThreadPool(2)) { + executors.submit(error); + executors.submit(output); + executors.shutdown(); + while (!executors.isTerminated()) { + // Wait for all the tasks to complete. + Await.until(1); + } } return new ExecResults(output.getContent(), error.getContent(), process.exitValue()); diff --git a/selcukes-commons/src/main/java/io/github/selcukes/commons/helper/FileHelper.java b/selcukes-commons/src/main/java/io/github/selcukes/commons/helper/FileHelper.java index 0f3eb75ce..88a3ac021 100644 --- a/selcukes-commons/src/main/java/io/github/selcukes/commons/helper/FileHelper.java +++ b/selcukes-commons/src/main/java/io/github/selcukes/commons/helper/FileHelper.java @@ -369,8 +369,8 @@ public String downloadToUsersFolder(final String url) { * @return The content of the file. */ public String readContent(final String filePath) { - try { - return new String(loadResourceFromJar(filePath).readAllBytes(), StandardCharsets.UTF_8); + try (var inputStream = loadResourceFromJar(filePath)) { + return new String(inputStream.readAllBytes(), StandardCharsets.UTF_8); } catch (Exception e) { throw new ConfigurationException(String.format("Cannot load [%s] from classpath", filePath)); } diff --git a/selcukes-commons/src/main/java/io/github/selcukes/commons/http/WebClient.java b/selcukes-commons/src/main/java/io/github/selcukes/commons/http/WebClient.java index de22f848b..c1a128117 100644 --- a/selcukes-commons/src/main/java/io/github/selcukes/commons/http/WebClient.java +++ b/selcukes-commons/src/main/java/io/github/selcukes/commons/http/WebClient.java @@ -315,6 +315,6 @@ private String encode(String value) { private URI buildUri() { var joiner = new StringJoiner("&"); queryParams.forEach((key, value) -> joiner.add(encode(key) + "=" + encode(value))); - return Resources.toURI(baseUri + endpoint + (baseUri.contains("?") ? "&" : "?") + joiner); + return URI.create(baseUri + endpoint + (baseUri.contains("?") ? "&" : "?") + joiner); } } diff --git a/selcukes-extent-reports/src/main/java/io/github/selcukes/extent/report/TestSourcesModel.java b/selcukes-extent-reports/src/main/java/io/github/selcukes/extent/report/TestSourcesModel.java index 66aa1ad0e..366ed3c41 100644 --- a/selcukes-extent-reports/src/main/java/io/github/selcukes/extent/report/TestSourcesModel.java +++ b/selcukes-extent-reports/src/main/java/io/github/selcukes/extent/report/TestSourcesModel.java @@ -41,6 +41,7 @@ import java.util.Optional; import java.util.stream.Stream; +@SuppressWarnings("OptionalGetWithoutIsPresent") public final class TestSourcesModel { private final Map pathToReadEventMap = new HashMap<>(); From 11d78e7c37f61e492b5e32db2d1b87f2c77000c3 Mon Sep 17 00:00:00 2001 From: Ramesh Babu Prudhvi Date: Wed, 17 Jan 2024 12:16:24 +0530 Subject: [PATCH 06/16] [All] Code cleanup --- .../io/github/selcukes/commons/exec/Shell.java | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/selcukes-commons/src/main/java/io/github/selcukes/commons/exec/Shell.java b/selcukes-commons/src/main/java/io/github/selcukes/commons/exec/Shell.java index b2819ea85..de2321c1e 100644 --- a/selcukes-commons/src/main/java/io/github/selcukes/commons/exec/Shell.java +++ b/selcukes-commons/src/main/java/io/github/selcukes/commons/exec/Shell.java @@ -76,14 +76,13 @@ private ExecResults interactWithProcess(final Process process) { } var output = new StreamGuzzler(process.getInputStream()); var error = new StreamGuzzler(process.getErrorStream()); - try (var executors = Executors.newFixedThreadPool(2)) { - executors.submit(error); - executors.submit(output); - executors.shutdown(); - while (!executors.isTerminated()) { - // Wait for all the tasks to complete. - Await.until(1); - } + var executors = Executors.newFixedThreadPool(2); + executors.submit(error); + executors.submit(output); + executors.shutdown(); + while (!executors.isTerminated()) { + // Wait for all the tasks to complete. + Await.until(1); } return new ExecResults(output.getContent(), error.getContent(), process.exitValue()); From 8d6afeb7bac0e32a1cb8175b633da2bdcaf22675 Mon Sep 17 00:00:00 2001 From: Ramesh Babu Prudhvi Date: Tue, 16 Jan 2024 20:03:13 +0530 Subject: [PATCH 07/16] [DataBind] Added support to create CSV file --- .../selcukes/collections/DataTable.java | 17 +++++++++++- .../selcukes/collections/Resources.java | 24 +++++++++++++++-- .../github/selcukes/collections/Streams.java | 11 ++++++++ .../selcukes/collections/TextTable.java | 22 +++++++++++++++ .../selcukes/databind/csv/CsvMapper.java | 27 +++++++++++++++++++ .../selcukes/databind/tests/CsvTest.java | 5 ++++ 6 files changed, 103 insertions(+), 3 deletions(-) diff --git a/selcukes-collections/src/main/java/io/github/selcukes/collections/DataTable.java b/selcukes-collections/src/main/java/io/github/selcukes/collections/DataTable.java index 82c74eaac..1b11ffcd3 100644 --- a/selcukes-collections/src/main/java/io/github/selcukes/collections/DataTable.java +++ b/selcukes-collections/src/main/java/io/github/selcukes/collections/DataTable.java @@ -455,7 +455,7 @@ public Map aggregateByColumn(@NonNull K columnName, @NonNull K groupColumn /** * Returns a string representation of a {@link DataTable}. The output table - * is formatted to align columns and provide a separator line between the + * is formatted to align columns and provides a separator line between the * header and data rows. The width of each column is determined by the * length of the longest data value for that column. * @@ -489,4 +489,19 @@ public String toString() { forEach(row -> table.add(row.values().toString())); return table.toString(); } + + /** + * Converts the {@link DataTable} to a CSV format. + *

+ * This method uses the {@link TextTable} utility class to format the CSV + * output. The resulting CSV string represents the DataTable in a + * standardized format with aligned columns and a separator line between the + * header and data rows. The width of each column is determined by the + * length of the longest data value for that column. + * + * @return a string representing the DataTable in CSV format + */ + public String toCSV() { + return TextTable.of(this).printCSV(); + } } diff --git a/selcukes-collections/src/main/java/io/github/selcukes/collections/Resources.java b/selcukes-collections/src/main/java/io/github/selcukes/collections/Resources.java index ba5e1157a..375379548 100644 --- a/selcukes-collections/src/main/java/io/github/selcukes/collections/Resources.java +++ b/selcukes-collections/src/main/java/io/github/selcukes/collections/Resources.java @@ -95,15 +95,15 @@ public String createFile(final Path filePath) { * Writes the provided content to a file at the specified path using UTF-8 * encoding. * - * @param fileContent The content to be written to the file. * @param filePath The path of the file to which the content * will be written. + * @param fileContent The content to be written to the file. * @return The path of the file where the content was * successfully written. * @throws DataStreamException If an I/O error occurs while writing the * content to the file. */ - public Path writeToFile(final @NonNull String fileContent, final Path filePath) { + public Path writeToFile(final Path filePath, final @NonNull String fileContent) { try { return Files.write(filePath, fileContent.getBytes(UTF_8)); } catch (IOException e) { @@ -111,6 +111,26 @@ public Path writeToFile(final @NonNull String fileContent, final Path filePath) } } + /** + * Writes the provided content to a file at the specified path using UTF-8 + * encoding. + * + * @param filePath The path of the file to which the content + * will be written. + * @param fileContent The content to be written to the file. + * @return The path of the file where the content was + * successfully written. + * @throws DataStreamException If an I/O error occurs while writing the + * content to the file. + */ + public Path writeToFile(final Path filePath, final @NonNull Iterable fileContent) { + try { + return Files.write(filePath, fileContent, UTF_8); + } catch (IOException e) { + throw new DataStreamException("Failed to write content to file: " + filePath.toAbsolutePath(), e); + } + } + /** * Copies a file from the source path to the destination path. * diff --git a/selcukes-collections/src/main/java/io/github/selcukes/collections/Streams.java b/selcukes-collections/src/main/java/io/github/selcukes/collections/Streams.java index 6208501b9..40e2e02ac 100644 --- a/selcukes-collections/src/main/java/io/github/selcukes/collections/Streams.java +++ b/selcukes-collections/src/main/java/io/github/selcukes/collections/Streams.java @@ -45,6 +45,17 @@ public Stream of(final Iterator iterator) { .stream(Spliterators.spliteratorUnknownSize(iterator, Spliterator.ORDERED), false); } + /** + * Converts an Iterable to a Stream. + * + * @param iterable The iterable to convert to a stream. + * @param The type of elements in the iterable. + * @return A stream of the iterable. + */ + public static Stream of(final Iterable iterable) { + return StreamSupport.stream(iterable.spliterator(), false); + } + /** * Returns an OptionalInt that contains the index of the first element in * the list that matches the given predicate, or an empty OptionalInt if no diff --git a/selcukes-collections/src/main/java/io/github/selcukes/collections/TextTable.java b/selcukes-collections/src/main/java/io/github/selcukes/collections/TextTable.java index cc884f579..c760033d7 100644 --- a/selcukes-collections/src/main/java/io/github/selcukes/collections/TextTable.java +++ b/selcukes-collections/src/main/java/io/github/selcukes/collections/TextTable.java @@ -19,6 +19,7 @@ import java.util.Map; import java.util.Objects; import java.util.stream.Collectors; +import java.util.stream.Stream; class TextTable { private final DataTable table; @@ -66,6 +67,27 @@ public String printTable() { return separator + "\n" + header + "|\n" + separator + "\n" + rows + "\n" + separator + "\n"; } + public String printCSV() { + return Stream.concat( + Stream.of(getCSVRow(table.getColumns())), + table.stream().map(row -> getCSVRow(row.values()))) + .collect(Collectors.joining("\n")); + } + + private String getCSVRow(Iterable values) { + return Streams.of(values) + .map(Object::toString) + .map(this::escapeCsvValue) + .collect(Collectors.joining(",")); + } + + private String escapeCsvValue(String value) { + if (value.contains(",") || value.contains("\"") || value.contains("\n")) { + return "\"" + value.replace("\"", "\"\"") + "\""; + } + return value; + } + public String printHtmlTable() { var htmlTable = new HtmlTable(); return htmlTable.buildTable(table); diff --git a/selcukes-databind/src/main/java/io/github/selcukes/databind/csv/CsvMapper.java b/selcukes-databind/src/main/java/io/github/selcukes/databind/csv/CsvMapper.java index 0f315d2ff..70af32c3e 100644 --- a/selcukes-databind/src/main/java/io/github/selcukes/databind/csv/CsvMapper.java +++ b/selcukes-databind/src/main/java/io/github/selcukes/databind/csv/CsvMapper.java @@ -18,6 +18,7 @@ import io.github.selcukes.collections.DataTable; import io.github.selcukes.collections.Lists; +import io.github.selcukes.collections.Resources; import io.github.selcukes.collections.Streams; import io.github.selcukes.databind.exception.DataMapperException; import lombok.experimental.UtilityClass; @@ -64,4 +65,30 @@ public DataTable parse(Path filePath, String regex) { public DataTable parse(Path filePath) { return parse(filePath, CSV_REGEX); } + + /** + * Writes a DataTable to a CSV file at the specified path. + * + * @param filePath the path to the CSV file + * @param dataTable the data to be written to the CSV file + * @throws DataMapperException if an error occurs while writing to the file + */ + public void write(Path filePath, DataTable dataTable) { + write(filePath, dataTable.toCSV()); + } + + /** + * Writes a string of CSV data to a CSV file at the specified path. + * + * @param filePath the path to the CSV file + * @param data the CSV data to be written + * @throws DataMapperException if an error occurs while writing to the file + */ + public void write(Path filePath, String data) { + try { + Resources.writeToFile(filePath, Arrays.asList(data.split("\n"))); + } catch (Exception e) { + throw new DataMapperException("Failed writing to CSV File: ", e); + } + } } diff --git a/selcukes-databind/src/test/java/io/github/selcukes/databind/tests/CsvTest.java b/selcukes-databind/src/test/java/io/github/selcukes/databind/tests/CsvTest.java index 0b5eca3a1..b456c559c 100644 --- a/selcukes-databind/src/test/java/io/github/selcukes/databind/tests/CsvTest.java +++ b/selcukes-databind/src/test/java/io/github/selcukes/databind/tests/CsvTest.java @@ -42,6 +42,11 @@ public void csvDataReaderTest() { return row; }); table.forEach(System.out::println); + + var filePath1 = Resources.ofTest("employee1.csv"); + CsvMapper.write(filePath1, table); + var table1 = CsvMapper.parse(filePath1, CSV_STRIP_REGEX); + table1.forEach(System.out::println); } private String updateID(String phone, String country) { From 9ae052d96ea039c9630260de71ccd081b33eeaf7 Mon Sep 17 00:00:00 2001 From: Ramesh Babu Prudhvi Date: Tue, 16 Jan 2024 20:20:43 +0530 Subject: [PATCH 08/16] [WDB] Fixed deprecated changes --- .../java/io/github/selcukes/wdb/util/FileExtractUtil.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/webdriver-binaries/src/main/java/io/github/selcukes/wdb/util/FileExtractUtil.java b/webdriver-binaries/src/main/java/io/github/selcukes/wdb/util/FileExtractUtil.java index b1bdc4f84..56b4cbc4e 100644 --- a/webdriver-binaries/src/main/java/io/github/selcukes/wdb/util/FileExtractUtil.java +++ b/webdriver-binaries/src/main/java/io/github/selcukes/wdb/util/FileExtractUtil.java @@ -64,7 +64,7 @@ private static Path unZipFile(Path source, Path destination) { try (var fis = Files.newInputStream(source, StandardOpenOption.READ); var zis = new ZipArchiveInputStream(fis)) { ZipArchiveEntry entry; - while ((entry = zis.getNextZipEntry()) != null) { + while ((entry = zis.getNextEntry()) != null) { if (!entry.getName().toUpperCase().contains("LICENSE")) { entryDestination = uncompress(zis, destination, entry); } @@ -81,7 +81,7 @@ private static Path unTarFile(Path source, Path destination) { var gZIPInputStream = new GZIPInputStream(fis); final var tis = new TarArchiveInputStream(gZIPInputStream)) { TarArchiveEntry entry; - while ((entry = tis.getNextTarEntry()) != null) { + while ((entry = tis.getNextEntry()) != null) { if (!entry.getName().toUpperCase().contains("LICENSE")) { entryDestination = uncompress(tis, destination, entry); } From c47d6ffc43203e3443f4f2aab1f7defee3bb128a Mon Sep 17 00:00:00 2001 From: Ramesh Babu Prudhvi Date: Tue, 16 Jan 2024 20:21:56 +0530 Subject: [PATCH 09/16] Updated .editorconfig --- .editorconfig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.editorconfig b/.editorconfig index 318a84fae..969b3204f 100644 --- a/.editorconfig +++ b/.editorconfig @@ -9,7 +9,7 @@ insert_final_newline = true indent_style = space indent_size = 4 -[*.{feature, yml}] +[*.{feature,yml}] indent_style = space indent_size = 2 From cefc1168ccb27dbc7c3addf5aab560c189e48cad Mon Sep 17 00:00:00 2001 From: Ramesh Babu Prudhvi Date: Tue, 16 Jan 2024 20:25:21 +0530 Subject: [PATCH 10/16] [All] Code cleanup --- .../io/github/selcukes/collections/Try.java | 20 +++++++++---------- .../selcukes/commons/http/WebResponse.java | 2 +- .../selcukes/commons/os/Architecture.java | 2 +- .../selcukes/core/page/ui/Dropdown.java | 3 +-- .../selcukes/extent/report/ExtentService.java | 6 ++---- .../selcukes/notifier/enums/NotifierEnum.java | 2 +- 6 files changed, 16 insertions(+), 19 deletions(-) diff --git a/selcukes-collections/src/main/java/io/github/selcukes/collections/Try.java b/selcukes-collections/src/main/java/io/github/selcukes/collections/Try.java index 4969b5d61..8b8d5fb20 100644 --- a/selcukes-collections/src/main/java/io/github/selcukes/collections/Try.java +++ b/selcukes-collections/src/main/java/io/github/selcukes/collections/Try.java @@ -16,6 +16,8 @@ package io.github.selcukes.collections; +import lombok.Getter; + import java.util.Optional; import java.util.function.Function; import java.util.function.Supplier; @@ -30,6 +32,14 @@ */ public class Try { private final T result; + /** + * -- GETTER -- + * Returns the cause of the failure represented by this Try object. + * + * @return the cause of the failure represented by this Try object, or null + * if this Try object represents a success + */ + @Getter private final Exception exception; /** @@ -168,16 +178,6 @@ public Optional getResult() { return Optional.ofNullable(result); } - /** - * Returns the cause of the failure represented by this Try object. - * - * @return the cause of the failure represented by this Try object, or null - * if this Try object represents a success - */ - public Exception getException() { - return exception; - } - /** * Applies the given function to the result of this Try, which produces a * new Try. If this Try contains an exception, a new Try containing the diff --git a/selcukes-commons/src/main/java/io/github/selcukes/commons/http/WebResponse.java b/selcukes-commons/src/main/java/io/github/selcukes/commons/http/WebResponse.java index d0be70af8..ec8ecc0c3 100644 --- a/selcukes-commons/src/main/java/io/github/selcukes/commons/http/WebResponse.java +++ b/selcukes-commons/src/main/java/io/github/selcukes/commons/http/WebResponse.java @@ -36,8 +36,8 @@ * It takes the HTTP response and provides a number of methods to access the * response body, headers, and status code */ +@Getter public class WebResponse { - @Getter private final HttpResponse httpResponse; public WebResponse(final HttpResponse httpResponse) { diff --git a/selcukes-commons/src/main/java/io/github/selcukes/commons/os/Architecture.java b/selcukes-commons/src/main/java/io/github/selcukes/commons/os/Architecture.java index f8fa78f4a..4c5aa7fcb 100644 --- a/selcukes-commons/src/main/java/io/github/selcukes/commons/os/Architecture.java +++ b/selcukes-commons/src/main/java/io/github/selcukes/commons/os/Architecture.java @@ -19,11 +19,11 @@ import lombok.AllArgsConstructor; import lombok.Getter; +@Getter @AllArgsConstructor public enum Architecture { X32(32), X64(64); - @Getter final int value; } diff --git a/selcukes-core/src/main/java/io/github/selcukes/core/page/ui/Dropdown.java b/selcukes-core/src/main/java/io/github/selcukes/core/page/ui/Dropdown.java index 67fec7f4e..c4f3d7f79 100644 --- a/selcukes-core/src/main/java/io/github/selcukes/core/page/ui/Dropdown.java +++ b/selcukes-core/src/main/java/io/github/selcukes/core/page/ui/Dropdown.java @@ -48,15 +48,14 @@ public Object selected(Select select, String optionLocator) { return type.getRetriever().apply(select); } + @Getter @AllArgsConstructor enum SelectionType { LABEL(Select::selectByVisibleText, select -> select.getFirstSelectedOption().getText()), VALUE(Select::selectByValue, select -> select.getFirstSelectedOption().getAttribute(ATTRIBUTE)), INDEX((select, value) -> select.selectByIndex(parseInt(value)), select -> select.getOptions().indexOf(select.getFirstSelectedOption())); - @Getter private final BiConsumer selector; - @Getter private final Function retriever; } diff --git a/selcukes-extent-reports/src/main/java/io/github/selcukes/extent/report/ExtentService.java b/selcukes-extent-reports/src/main/java/io/github/selcukes/extent/report/ExtentService.java index 2ba88f19a..829de5950 100644 --- a/selcukes-extent-reports/src/main/java/io/github/selcukes/extent/report/ExtentService.java +++ b/selcukes-extent-reports/src/main/java/io/github/selcukes/extent/report/ExtentService.java @@ -28,6 +28,7 @@ import io.github.selcukes.collections.Resources; import io.github.selcukes.collections.StringHelper; import io.github.selcukes.databind.properties.PropertiesMapper; +import lombok.Getter; import java.io.IOException; import java.util.Arrays; @@ -51,6 +52,7 @@ public class ExtentService { private static final String VIEW_ORDER_SPARK_KEY = "extent.reporter.spark.vieworder"; private static final String REPORT_NAME = "Automation Report"; private static final String REPORT_TITLE = "Automation Report"; + @Getter private final ExtentReports extentReports; private final Map propertiesMap; @@ -63,10 +65,6 @@ public ExtentService(ExtentReports extentReports) { addSystemInfo(); } - public ExtentReports getExtentReports() { - return extentReports; - } - private Map initProperties() { try { return PropertiesMapper.parse(Resources.ofTest("extent.properties")); diff --git a/selcukes-notifier/src/main/java/io/github/selcukes/notifier/enums/NotifierEnum.java b/selcukes-notifier/src/main/java/io/github/selcukes/notifier/enums/NotifierEnum.java index 8d351466c..9de80ce7a 100644 --- a/selcukes-notifier/src/main/java/io/github/selcukes/notifier/enums/NotifierEnum.java +++ b/selcukes-notifier/src/main/java/io/github/selcukes/notifier/enums/NotifierEnum.java @@ -21,6 +21,7 @@ import lombok.AllArgsConstructor; import lombok.Getter; +@Getter @AllArgsConstructor public enum NotifierEnum { PRETEXT("Selcukes Automation Report"), @@ -37,6 +38,5 @@ public enum NotifierEnum { MESSAGE_CARD("MessageCard"), TIME_STAMP("Time Stamp"), EXCEPTION("Exception"); - @Getter final String value; } From e847fce51ad7b87da5580e0e666be1957f01afed Mon Sep 17 00:00:00 2001 From: Ramesh Babu Prudhvi Date: Wed, 17 Jan 2024 12:13:46 +0530 Subject: [PATCH 11/16] [All] Code cleanup --- .../selcukes/collections/Resources.java | 29 +++---------------- .../selcukes/collections/TextTable.java | 10 +++---- .../io/github/selcukes/collections/Try.java | 8 +---- .../github/selcukes/commons/exec/Shell.java | 15 +++++----- .../selcukes/commons/helper/FileHelper.java | 4 +-- .../selcukes/commons/http/WebClient.java | 2 +- .../extent/report/TestSourcesModel.java | 1 + 7 files changed, 21 insertions(+), 48 deletions(-) diff --git a/selcukes-collections/src/main/java/io/github/selcukes/collections/Resources.java b/selcukes-collections/src/main/java/io/github/selcukes/collections/Resources.java index 375379548..6708a0863 100644 --- a/selcukes-collections/src/main/java/io/github/selcukes/collections/Resources.java +++ b/selcukes-collections/src/main/java/io/github/selcukes/collections/Resources.java @@ -266,11 +266,8 @@ public static OutputStream newOutputStream(Path filePath) { * be parsed */ public URL toURL(String urlStr) { - try { - return new URL(urlStr); - } catch (MalformedURLException e) { - throw new IllegalArgumentException("Invalid URL string: " + urlStr, e); - } + return tryURL(urlStr) + .orElseThrow(() -> new IllegalArgumentException("Invalid URL string: " + urlStr)); } /** @@ -284,27 +281,9 @@ public URL toURL(String urlStr) { */ public Optional tryURL(String urlStr) { try { - return Optional.of(new URL(urlStr)); - } catch (MalformedURLException e) { + return Optional.of(new URI(urlStr).toURL()); + } catch (MalformedURLException | URISyntaxException e) { return Optional.empty(); } } - - /** - * Returns a new URI object by parsing the given URI string. - * - * @param uriStr the URI string to be parsed into a URI - * object - * @return the URI object representing the parsed - * URI string - * @throws IllegalArgumentException if the URI string is invalid and cannot - * be parsed - */ - public URI toURI(String uriStr) { - try { - return new URI(uriStr); - } catch (URISyntaxException e) { - throw new IllegalArgumentException("Invalid URI string: " + uriStr, e); - } - } } diff --git a/selcukes-collections/src/main/java/io/github/selcukes/collections/TextTable.java b/selcukes-collections/src/main/java/io/github/selcukes/collections/TextTable.java index c760033d7..9663a4c49 100644 --- a/selcukes-collections/src/main/java/io/github/selcukes/collections/TextTable.java +++ b/selcukes-collections/src/main/java/io/github/selcukes/collections/TextTable.java @@ -96,13 +96,11 @@ public String printHtmlTable() { private static class HtmlTable { public String buildTable(DataTable table) { - StringBuilder sb = new StringBuilder(); - sb.append("\n"); - sb.append(buildHeaderRow(table.getFirst())); - sb.append(buildDataRows(table)); - sb.append("
"); - return sb.toString(); + return "\n" + + buildHeaderRow(table.getFirst()) + + buildDataRows(table) + + "
"; } private String buildHeaderRow(Map row) { diff --git a/selcukes-collections/src/main/java/io/github/selcukes/collections/Try.java b/selcukes-collections/src/main/java/io/github/selcukes/collections/Try.java index 8b8d5fb20..f40cf4c51 100644 --- a/selcukes-collections/src/main/java/io/github/selcukes/collections/Try.java +++ b/selcukes-collections/src/main/java/io/github/selcukes/collections/Try.java @@ -32,13 +32,7 @@ */ public class Try { private final T result; - /** - * -- GETTER -- - * Returns the cause of the failure represented by this Try object. - * - * @return the cause of the failure represented by this Try object, or null - * if this Try object represents a success - */ + @Getter private final Exception exception; diff --git a/selcukes-commons/src/main/java/io/github/selcukes/commons/exec/Shell.java b/selcukes-commons/src/main/java/io/github/selcukes/commons/exec/Shell.java index de2321c1e..b2819ea85 100644 --- a/selcukes-commons/src/main/java/io/github/selcukes/commons/exec/Shell.java +++ b/selcukes-commons/src/main/java/io/github/selcukes/commons/exec/Shell.java @@ -76,13 +76,14 @@ private ExecResults interactWithProcess(final Process process) { } var output = new StreamGuzzler(process.getInputStream()); var error = new StreamGuzzler(process.getErrorStream()); - var executors = Executors.newFixedThreadPool(2); - executors.submit(error); - executors.submit(output); - executors.shutdown(); - while (!executors.isTerminated()) { - // Wait for all the tasks to complete. - Await.until(1); + try (var executors = Executors.newFixedThreadPool(2)) { + executors.submit(error); + executors.submit(output); + executors.shutdown(); + while (!executors.isTerminated()) { + // Wait for all the tasks to complete. + Await.until(1); + } } return new ExecResults(output.getContent(), error.getContent(), process.exitValue()); diff --git a/selcukes-commons/src/main/java/io/github/selcukes/commons/helper/FileHelper.java b/selcukes-commons/src/main/java/io/github/selcukes/commons/helper/FileHelper.java index 0f3eb75ce..88a3ac021 100644 --- a/selcukes-commons/src/main/java/io/github/selcukes/commons/helper/FileHelper.java +++ b/selcukes-commons/src/main/java/io/github/selcukes/commons/helper/FileHelper.java @@ -369,8 +369,8 @@ public String downloadToUsersFolder(final String url) { * @return The content of the file. */ public String readContent(final String filePath) { - try { - return new String(loadResourceFromJar(filePath).readAllBytes(), StandardCharsets.UTF_8); + try (var inputStream = loadResourceFromJar(filePath)) { + return new String(inputStream.readAllBytes(), StandardCharsets.UTF_8); } catch (Exception e) { throw new ConfigurationException(String.format("Cannot load [%s] from classpath", filePath)); } diff --git a/selcukes-commons/src/main/java/io/github/selcukes/commons/http/WebClient.java b/selcukes-commons/src/main/java/io/github/selcukes/commons/http/WebClient.java index de22f848b..c1a128117 100644 --- a/selcukes-commons/src/main/java/io/github/selcukes/commons/http/WebClient.java +++ b/selcukes-commons/src/main/java/io/github/selcukes/commons/http/WebClient.java @@ -315,6 +315,6 @@ private String encode(String value) { private URI buildUri() { var joiner = new StringJoiner("&"); queryParams.forEach((key, value) -> joiner.add(encode(key) + "=" + encode(value))); - return Resources.toURI(baseUri + endpoint + (baseUri.contains("?") ? "&" : "?") + joiner); + return URI.create(baseUri + endpoint + (baseUri.contains("?") ? "&" : "?") + joiner); } } diff --git a/selcukes-extent-reports/src/main/java/io/github/selcukes/extent/report/TestSourcesModel.java b/selcukes-extent-reports/src/main/java/io/github/selcukes/extent/report/TestSourcesModel.java index 66aa1ad0e..366ed3c41 100644 --- a/selcukes-extent-reports/src/main/java/io/github/selcukes/extent/report/TestSourcesModel.java +++ b/selcukes-extent-reports/src/main/java/io/github/selcukes/extent/report/TestSourcesModel.java @@ -41,6 +41,7 @@ import java.util.Optional; import java.util.stream.Stream; +@SuppressWarnings("OptionalGetWithoutIsPresent") public final class TestSourcesModel { private final Map pathToReadEventMap = new HashMap<>(); From 6f0e49189b2d41f2034fd09b4d949377448916e5 Mon Sep 17 00:00:00 2001 From: Ramesh Babu Prudhvi Date: Wed, 17 Jan 2024 12:20:53 +0530 Subject: [PATCH 12/16] [All] Revert changes --- .../io/github/selcukes/commons/exec/Shell.java | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/selcukes-commons/src/main/java/io/github/selcukes/commons/exec/Shell.java b/selcukes-commons/src/main/java/io/github/selcukes/commons/exec/Shell.java index b2819ea85..bdb418e61 100644 --- a/selcukes-commons/src/main/java/io/github/selcukes/commons/exec/Shell.java +++ b/selcukes-commons/src/main/java/io/github/selcukes/commons/exec/Shell.java @@ -76,16 +76,14 @@ private ExecResults interactWithProcess(final Process process) { } var output = new StreamGuzzler(process.getInputStream()); var error = new StreamGuzzler(process.getErrorStream()); - try (var executors = Executors.newFixedThreadPool(2)) { - executors.submit(error); - executors.submit(output); - executors.shutdown(); - while (!executors.isTerminated()) { - // Wait for all the tasks to complete. - Await.until(1); - } + var executors = Executors.newFixedThreadPool(2); + executors.submit(error); + executors.submit(output); + executors.shutdown(); + while (!executors.isTerminated()) { + // Wait for all the tasks to complete. + Await.until(1); } - return new ExecResults(output.getContent(), error.getContent(), process.exitValue()); } From 1f277a75abfa0d01d1544075d41ee8768ca16c87 Mon Sep 17 00:00:00 2001 From: Ramesh Babu Prudhvi Date: Wed, 17 Jan 2024 12:34:32 +0530 Subject: [PATCH 13/16] [All] Fixed URI issue --- .../main/java/io/github/selcukes/collections/Resources.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/selcukes-collections/src/main/java/io/github/selcukes/collections/Resources.java b/selcukes-collections/src/main/java/io/github/selcukes/collections/Resources.java index 6708a0863..46afb78b1 100644 --- a/selcukes-collections/src/main/java/io/github/selcukes/collections/Resources.java +++ b/selcukes-collections/src/main/java/io/github/selcukes/collections/Resources.java @@ -25,9 +25,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; -import java.net.MalformedURLException; import java.net.URI; -import java.net.URISyntaxException; import java.net.URL; import java.nio.file.Files; import java.nio.file.Path; @@ -282,7 +280,7 @@ public URL toURL(String urlStr) { public Optional tryURL(String urlStr) { try { return Optional.of(new URI(urlStr).toURL()); - } catch (MalformedURLException | URISyntaxException e) { + } catch (Exception e) { return Optional.empty(); } } From 6d113f200fe69cce954abb38d67cc00ba829ca9f Mon Sep 17 00:00:00 2001 From: Ramesh Babu Prudhvi Date: Wed, 17 Jan 2024 13:10:15 +0530 Subject: [PATCH 14/16] Updated Tests --- .../main/java/io/github/selcukes/commons/Await.java | 3 --- .../io/github/selcukes/databind/tests/CsvTest.java | 12 +++++++++--- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/selcukes-commons/src/main/java/io/github/selcukes/commons/Await.java b/selcukes-commons/src/main/java/io/github/selcukes/commons/Await.java index cab66b9c6..318828886 100644 --- a/selcukes-commons/src/main/java/io/github/selcukes/commons/Await.java +++ b/selcukes-commons/src/main/java/io/github/selcukes/commons/Await.java @@ -83,14 +83,11 @@ public boolean until(Callable conditionEvaluator) { stopwatch += pollTimeout; } catch (InterruptedException e) { Thread.currentThread().interrupt(); - logger.error(e, () -> "Interrupted while waiting for condition"); return false; } catch (Exception e) { - logger.error(e, () -> "Error while evaluating condition"); return false; } } - logger.error(() -> "Condition not met within the given time"); return false; } } diff --git a/selcukes-databind/src/test/java/io/github/selcukes/databind/tests/CsvTest.java b/selcukes-databind/src/test/java/io/github/selcukes/databind/tests/CsvTest.java index b456c559c..5f6f074a7 100644 --- a/selcukes-databind/src/test/java/io/github/selcukes/databind/tests/CsvTest.java +++ b/selcukes-databind/src/test/java/io/github/selcukes/databind/tests/CsvTest.java @@ -23,6 +23,9 @@ import java.util.Map; import static io.github.selcukes.databind.csv.CsvMapper.CSV_STRIP_REGEX; +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertFalse; +import static org.testng.Assert.assertTrue; public class CsvTest { @@ -30,9 +33,11 @@ public class CsvTest { public void csvDataReaderTest() { var filePath = Resources.ofTest("employee.csv"); var table = CsvMapper.parse(filePath, CSV_STRIP_REGEX); + assertFalse(table.isEmpty(), "Table should not be empty"); Map keyMapping = Map.of("Name", "FullName"); table.renameColumn(keyMapping); + assertTrue(table.getColumns().contains("FullName"), "Column 'FullName' should be present"); table.updateRows(row -> { // Update ID Column values @@ -41,12 +46,14 @@ public void csvDataReaderTest() { } return row; }); - table.forEach(System.out::println); + assertFalse(table.getColumnEntries("ID").stream().anyMatch(String::isEmpty), "ID column should not be empty"); var filePath1 = Resources.ofTest("employee1.csv"); CsvMapper.write(filePath1, table); var table1 = CsvMapper.parse(filePath1, CSV_STRIP_REGEX); - table1.forEach(System.out::println); + + assertFalse(table1.isEmpty(), "Written table should not be empty"); + assertEquals(table1, table, "Written table should be the same as the original table"); } private String updateID(String phone, String country) { @@ -54,5 +61,4 @@ private String updateID(String phone, String country) { var lastFourDigits = phone.substring(phone.length() - 4); return countryCode + "_DDA_" + lastFourDigits; } - } From a3b4acd95f51d3d01e6a40576c44f70acb549f31 Mon Sep 17 00:00:00 2001 From: Ramesh Babu Prudhvi Date: Wed, 17 Jan 2024 13:24:23 +0530 Subject: [PATCH 15/16] Fixed Deprecated Changes --- .../io/github/selcukes/commons/helper/FileHelper.java | 3 ++- .../io/github/selcukes/core/grid/SeleniumService.java | 11 +++++------ .../io/github/selcukes/wdb/core/ChromeBinary.java | 6 +++--- .../java/io/github/selcukes/wdb/core/EdgeBinary.java | 6 +++--- .../io/github/selcukes/wdb/core/FirefoxBinary.java | 6 +++--- .../io/github/selcukes/wdb/core/IExplorerBinary.java | 6 +++--- .../java/io/github/selcukes/wdb/core/OperaBinary.java | 6 +++--- 7 files changed, 22 insertions(+), 22 deletions(-) diff --git a/selcukes-commons/src/main/java/io/github/selcukes/commons/helper/FileHelper.java b/selcukes-commons/src/main/java/io/github/selcukes/commons/helper/FileHelper.java index 88a3ac021..cf67d52d5 100644 --- a/selcukes-commons/src/main/java/io/github/selcukes/commons/helper/FileHelper.java +++ b/selcukes-commons/src/main/java/io/github/selcukes/commons/helper/FileHelper.java @@ -16,6 +16,7 @@ package io.github.selcukes.commons.helper; +import io.github.selcukes.collections.Resources; import io.github.selcukes.commons.exception.ConfigurationException; import lombok.SneakyThrows; import lombok.experimental.UtilityClass; @@ -358,7 +359,7 @@ public String downloadToUsersFolder(final String url) { if (file.exists()) { return file.getPath(); } - download(new URL(url), file); + download(Resources.toURL(url), file); return file.getPath(); } diff --git a/selcukes-core/src/main/java/io/github/selcukes/core/grid/SeleniumService.java b/selcukes-core/src/main/java/io/github/selcukes/core/grid/SeleniumService.java index cccb0fe7c..9270ad12b 100644 --- a/selcukes-core/src/main/java/io/github/selcukes/core/grid/SeleniumService.java +++ b/selcukes-core/src/main/java/io/github/selcukes/core/grid/SeleniumService.java @@ -29,7 +29,6 @@ import org.openqa.selenium.os.ExternalProcess; import java.io.File; -import java.net.MalformedURLException; import java.net.URL; import java.util.LinkedList; import java.util.List; @@ -89,7 +88,7 @@ public SeleniumService start(String mode, String... extraFlags) { process = builder.start(); try { - var url = new URL(baseUrl + "/status"); + var url = Resources.toURL(baseUrl + "/status"); new UrlChecker().waitUntilAvailable(10, SECONDS, url); logger.info(() -> "Selenium Server is ready..."); } catch (UrlChecker.TimeoutException e) { @@ -97,7 +96,7 @@ public SeleniumService start(String mode, String... extraFlags) { process.shutdown(); process = null; throw new DriverConnectionException(e); - } catch (MalformedURLException e) { + } catch (Exception e) { throw new DriverConnectionException(e); } @@ -135,10 +134,10 @@ public URL getServiceUrl() { @SneakyThrows public String getServerJar() { var seleniumServerJar = ConfigFactory.getConfig().getWeb().getServerJar(); - var serverJarUrl = new URL(seleniumServerJar); - var reportsPath = ofNullable(ConfigFactory.getConfig().getReports()) + var serverJarUrl = Resources.toURL(seleniumServerJar); + var reportPath = ofNullable(ConfigFactory.getConfig().getReports()) .map(reports -> reports.get("path")).orElse("target"); - var serverJarPath = Resources.of(reportsPath + "/selenium-server.jar"); + var serverJarPath = Resources.of(reportPath + "/selenium-server.jar"); FileHelper.download(serverJarUrl, serverJarPath.toFile()); return serverJarPath.toAbsolutePath().toString(); } diff --git a/webdriver-binaries/src/main/java/io/github/selcukes/wdb/core/ChromeBinary.java b/webdriver-binaries/src/main/java/io/github/selcukes/wdb/core/ChromeBinary.java index 7ec81dc11..101b496ad 100644 --- a/webdriver-binaries/src/main/java/io/github/selcukes/wdb/core/ChromeBinary.java +++ b/webdriver-binaries/src/main/java/io/github/selcukes/wdb/core/ChromeBinary.java @@ -16,6 +16,7 @@ package io.github.selcukes.wdb.core; +import io.github.selcukes.collections.Resources; import io.github.selcukes.commons.exception.WebDriverBinaryException; import io.github.selcukes.commons.os.Architecture; import io.github.selcukes.commons.os.OsType; @@ -23,7 +24,6 @@ import io.github.selcukes.wdb.enums.DriverType; import io.github.selcukes.wdb.util.UrlHelper; -import java.net.MalformedURLException; import java.net.URL; import java.util.Objects; @@ -35,14 +35,14 @@ public class ChromeBinary extends AbstractBinary { @Override public URL getDownloadURL() { try { - return new URL(String.format( + return Resources.toURL(String.format( BINARY_DOWNLOAD_URL_PATTERN, UrlHelper.CHROMEDRIVER_URL, getBinaryVersion(), getBinaryEnvironment().getOsNameAndArch(), getBinaryEnvironment().getOsNameAndArch())); - } catch (MalformedURLException e) { + } catch (Exception e) { throw new WebDriverBinaryException(e); } } diff --git a/webdriver-binaries/src/main/java/io/github/selcukes/wdb/core/EdgeBinary.java b/webdriver-binaries/src/main/java/io/github/selcukes/wdb/core/EdgeBinary.java index 74e16d795..830b367b6 100644 --- a/webdriver-binaries/src/main/java/io/github/selcukes/wdb/core/EdgeBinary.java +++ b/webdriver-binaries/src/main/java/io/github/selcukes/wdb/core/EdgeBinary.java @@ -16,11 +16,11 @@ package io.github.selcukes.wdb.core; +import io.github.selcukes.collections.Resources; import io.github.selcukes.commons.exception.WebDriverBinaryException; import io.github.selcukes.wdb.enums.DriverType; import io.github.selcukes.wdb.util.UrlHelper; -import java.net.MalformedURLException; import java.net.URL; public class EdgeBinary extends AbstractBinary { @@ -29,13 +29,13 @@ public class EdgeBinary extends AbstractBinary { @Override public URL getDownloadURL() { try { - return new URL(String.format( + return Resources.toURL(String.format( BINARY_DOWNLOAD_URL_PATTERN, UrlHelper.EDGE_DRIVER_URL, getBinaryVersion(), getBinaryEnvironment().getOsNameAndArch())); - } catch (MalformedURLException e) { + } catch (Exception e) { throw new WebDriverBinaryException(e); } } diff --git a/webdriver-binaries/src/main/java/io/github/selcukes/wdb/core/FirefoxBinary.java b/webdriver-binaries/src/main/java/io/github/selcukes/wdb/core/FirefoxBinary.java index 92ef2d2a8..6557a6158 100644 --- a/webdriver-binaries/src/main/java/io/github/selcukes/wdb/core/FirefoxBinary.java +++ b/webdriver-binaries/src/main/java/io/github/selcukes/wdb/core/FirefoxBinary.java @@ -16,13 +16,13 @@ package io.github.selcukes.wdb.core; +import io.github.selcukes.collections.Resources; import io.github.selcukes.commons.exception.WebDriverBinaryException; import io.github.selcukes.commons.os.OsType; import io.github.selcukes.wdb.enums.DownloaderType; import io.github.selcukes.wdb.enums.DriverType; import io.github.selcukes.wdb.util.UrlHelper; -import java.net.MalformedURLException; import java.net.URL; import java.util.Objects; @@ -32,7 +32,7 @@ public class FirefoxBinary extends AbstractBinary { @Override public URL getDownloadURL() { try { - return new URL(String.format( + return Resources.toURL(String.format( BINARY_DOWNLOAD_URL_PATTERN, UrlHelper.GECKODRIVER_URL, getBinaryVersion(), @@ -40,7 +40,7 @@ public URL getDownloadURL() { getBinaryEnvironment().getOsNameAndArch(), getCompressedBinaryType().getName())); - } catch (MalformedURLException e) { + } catch (Exception e) { throw new WebDriverBinaryException(e); } } diff --git a/webdriver-binaries/src/main/java/io/github/selcukes/wdb/core/IExplorerBinary.java b/webdriver-binaries/src/main/java/io/github/selcukes/wdb/core/IExplorerBinary.java index f67791b70..a380c4493 100644 --- a/webdriver-binaries/src/main/java/io/github/selcukes/wdb/core/IExplorerBinary.java +++ b/webdriver-binaries/src/main/java/io/github/selcukes/wdb/core/IExplorerBinary.java @@ -16,13 +16,13 @@ package io.github.selcukes.wdb.core; +import io.github.selcukes.collections.Resources; import io.github.selcukes.commons.exception.WebDriverBinaryException; import io.github.selcukes.wdb.enums.DriverType; import io.github.selcukes.wdb.util.UrlHelper; import io.github.selcukes.wdb.util.VersionHelper; import io.github.selcukes.wdb.version.VersionComparator; -import java.net.MalformedURLException; import java.net.URL; import java.util.Map; import java.util.Optional; @@ -33,9 +33,9 @@ public class IExplorerBinary extends AbstractBinary { @Override public URL getDownloadURL() { try { - return new URL(UrlHelper.IEDRIVER_URL + "/" + latestVersionUrl); + return Resources.toURL(UrlHelper.IEDRIVER_URL + "/" + latestVersionUrl); - } catch (MalformedURLException e) { + } catch (Exception e) { throw new WebDriverBinaryException(e); } } diff --git a/webdriver-binaries/src/main/java/io/github/selcukes/wdb/core/OperaBinary.java b/webdriver-binaries/src/main/java/io/github/selcukes/wdb/core/OperaBinary.java index 4239f26b9..738c47d31 100644 --- a/webdriver-binaries/src/main/java/io/github/selcukes/wdb/core/OperaBinary.java +++ b/webdriver-binaries/src/main/java/io/github/selcukes/wdb/core/OperaBinary.java @@ -16,11 +16,11 @@ package io.github.selcukes.wdb.core; +import io.github.selcukes.collections.Resources; import io.github.selcukes.commons.exception.WebDriverBinaryException; import io.github.selcukes.wdb.enums.DriverType; import io.github.selcukes.wdb.util.UrlHelper; -import java.net.MalformedURLException; import java.net.URL; public class OperaBinary extends AbstractBinary { @@ -29,13 +29,13 @@ public class OperaBinary extends AbstractBinary { @Override public URL getDownloadURL() { try { - return new URL(String.format( + return Resources.toURL(String.format( BINARY_DOWNLOAD_URL_PATTERN, UrlHelper.OPERA_DRIVER_URL, getBinaryVersion(), getBinaryEnvironment().getOsNameAndArch(), getCompressedBinaryType().getName())); - } catch (MalformedURLException e) { + } catch (Exception e) { throw new WebDriverBinaryException(e); } } From b3201700a89ec4749ad0dad5da20a3091af416d5 Mon Sep 17 00:00:00 2001 From: Ramesh Babu Prudhvi Date: Wed, 17 Jan 2024 13:40:07 +0530 Subject: [PATCH 16/16] Fixed Deprecated Changes --- .../main/java/io/github/selcukes/core/grid/SeleniumService.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/selcukes-core/src/main/java/io/github/selcukes/core/grid/SeleniumService.java b/selcukes-core/src/main/java/io/github/selcukes/core/grid/SeleniumService.java index 9270ad12b..61c2fbf9c 100644 --- a/selcukes-core/src/main/java/io/github/selcukes/core/grid/SeleniumService.java +++ b/selcukes-core/src/main/java/io/github/selcukes/core/grid/SeleniumService.java @@ -122,7 +122,7 @@ public void stop() { */ @SneakyThrows public URL getServiceUrl() { - return new URL(baseUrl); + return Resources.toURL(baseUrl); } /**