diff --git a/filePDF.pdf b/filePDF.pdf new file mode 100644 index 0000000..e69de29 diff --git a/src/main/java/utils/FileUtilities.java b/src/main/java/utils/FileUtilities.java index 044204a..775d4b3 100644 --- a/src/main/java/utils/FileUtilities.java +++ b/src/main/java/utils/FileUtilities.java @@ -153,6 +153,38 @@ public static void saveFile(String content, String directory){ catch (Exception gamma){Assert.fail(String.valueOf(gamma));} } + /** + * Downloads a PDF file from the given InputStream and saves it with the specified file name. + *

+ * This method reads data from the provided InputStream in chunks and writes it to a local file. + * Once the download is complete, both the InputStream and the FileOutputStream are closed to + * free up resources. + *

+ * + * @param inputStream The InputStream containing the PDF data to be downloaded. + * @param fileName The name (including path, if necessary) under which the PDF should be saved. + * @throws IOException If an I/O error occurs during reading or writing. + */ + public static void downloadPdf(InputStream inputStream, String fileDestinationPath, String fileName) throws IOException { + + try (InputStream in = inputStream) { + Files.copy(in, Paths.get(fileDestinationPath + fileName), StandardCopyOption.REPLACE_EXISTING); + } catch (IOException exception) { + log.error(exception.getMessage(), exception); + } + + File file = new File(fileName); + FileOutputStream outputStream = new FileOutputStream(file); + byte[] buffer = new byte[2048]; + int bytesRead; + while ((bytesRead = inputStream.read(buffer)) != -1) { + outputStream.write(buffer, 0, bytesRead); + } + outputStream.close(); + inputStream.close(); + log.success("PDF downloaded successfully: " + fileName); + } + /** * Writes a string to a file. diff --git a/src/test/java/AppTest.java b/src/test/java/AppTest.java index 84b17a2..e6e2803 100644 --- a/src/test/java/AppTest.java +++ b/src/test/java/AppTest.java @@ -3,12 +3,14 @@ import com.google.gson.JsonObject; import context.ContextStore; import enums.ZoneIds; +import okhttp3.ResponseBody; import org.junit.Assert; import org.junit.Before; import petstore.PetStore; import petstore.PetStoreServices; import petstore.models.Pet; import org.junit.Test; +import retrofit2.Response; import utils.*; import utils.arrays.ArrayUtilities; import utils.email.EmailUtilities; @@ -154,6 +156,33 @@ public void getPDFFileTextTest() throws IOException { printer.success("The getPDFFileTextTest() test passed!" + pdfText); } + @Test + public void downloadPDFFileTextTest() throws IOException { + try { + Response getPdfResponse = PetStore.GetPdf.getPdf(); + + if (getPdfResponse.isSuccessful() && getPdfResponse.body() != null) { + FileUtilities.downloadPdf(getPdfResponse.body().byteStream(),"src/test/resources/", "filePDF.pdf"); + } else { + printer.info("Failed to download PDF! HTTP Code: " + getPdfResponse.code()); + } + } catch (IOException e) { + printer.info("Exception: " + e.getMessage()); + } + FileUtilities.verifyFilePresence("src/test/resources/filePDF.pdf"); + printer.success("The pdffile is present!"); + + String pdfText = FileUtilities.parsePDFFileToText("src/test/resources/filePDF.pdf"); + System.out.println(pdfText); + + assert pdfText != null; + Assert.assertTrue( + "PDF text does not contain the expected value!", + pdfText.contains("Run Settings") + ); + printer.success("The getPDFFileTextTest() test passed!" + pdfText); + } + @Test public void getSimpleDateStringFromTest() { String offsetDateTimeString = "2024-01-25T14:00:00+01:00"; diff --git a/src/test/java/petstore/PetStore.java b/src/test/java/petstore/PetStore.java index 0e8c496..5c27217 100644 --- a/src/test/java/petstore/PetStore.java +++ b/src/test/java/petstore/PetStore.java @@ -2,8 +2,10 @@ import api_assured.ApiUtilities; import api_assured.ServiceGenerator; +import okhttp3.ResponseBody; import petstore.models.Pet; import retrofit2.Call; +import retrofit2.Response; import utils.StringUtilities; import java.util.List; @@ -16,6 +18,11 @@ public class PetStore extends ApiUtilities { .printHeaders(true) .generate(PetStoreServices.class); + static PetStoreServices.GetPdf getPdf = new ServiceGenerator() + .setRequestLogging(true) + .printHeaders(true) + .generate(PetStoreServices.GetPdf.class); + public List getPetsByStatus(PetStoreServices.PetStatus status){ log.info("Getting pets by status: " + highlighted(StringUtilities.Color.BLUE, status.name())); Call> petByStatusCall = petStoreServices.getPet(status); @@ -33,4 +40,12 @@ public Pet getPetById(Long petId){ Call petByIdCall = petStoreServices.getPetById(petId); return getResponseForCode(30, 200, petByIdCall, true).body(); } + + public class GetPdf { + public static Response getPdf() { + log.info("Getting a subscription invoice"); + Call getPdfResponse = getPdf.getPdf(); + return getResponse(getPdfResponse, true, true); + } + } } diff --git a/src/test/java/petstore/PetStoreServices.java b/src/test/java/petstore/PetStoreServices.java index 6c7989c..75c471d 100644 --- a/src/test/java/petstore/PetStoreServices.java +++ b/src/test/java/petstore/PetStoreServices.java @@ -1,5 +1,6 @@ package petstore; +import okhttp3.ResponseBody; import petstore.models.Pet; import retrofit2.Call; import retrofit2.http.*; @@ -24,4 +25,10 @@ enum PetStatus { @POST("pet") Call postPet(@Body Pet pet); + + interface GetPdf{ + String BASE_URL = "https://sandbox.mabl.com/"; + @GET("downloads/mabl_dash.pdf") + Call getPdf(); + } }