Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added filePDF.pdf
Empty file.
32 changes: 32 additions & 0 deletions src/main/java/utils/FileUtilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
* <p>
* 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.
* </p>
*
* @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.
Expand Down
29 changes: 29 additions & 0 deletions src/test/java/AppTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -154,6 +156,33 @@ public void getPDFFileTextTest() throws IOException {
printer.success("The getPDFFileTextTest() test passed!" + pdfText);
}

@Test
public void downloadPDFFileTextTest() throws IOException {
try {
Response<ResponseBody> 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";
Expand Down
15 changes: 15 additions & 0 deletions src/test/java/petstore/PetStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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<Pet> getPetsByStatus(PetStoreServices.PetStatus status){
log.info("Getting pets by status: " + highlighted(StringUtilities.Color.BLUE, status.name()));
Call<List<Pet>> petByStatusCall = petStoreServices.getPet(status);
Expand All @@ -33,4 +40,12 @@ public Pet getPetById(Long petId){
Call<Pet> petByIdCall = petStoreServices.getPetById(petId);
return getResponseForCode(30, 200, petByIdCall, true).body();
}

public class GetPdf {
public static Response<ResponseBody> getPdf() {
log.info("Getting a subscription invoice");
Call<ResponseBody> getPdfResponse = getPdf.getPdf();
return getResponse(getPdfResponse, true, true);
}
}
}
7 changes: 7 additions & 0 deletions src/test/java/petstore/PetStoreServices.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package petstore;

import okhttp3.ResponseBody;
import petstore.models.Pet;
import retrofit2.Call;
import retrofit2.http.*;
Expand All @@ -24,4 +25,10 @@ enum PetStatus {

@POST("pet")
Call<Pet> postPet(@Body Pet pet);

interface GetPdf{
String BASE_URL = "https://sandbox.mabl.com/";
@GET("downloads/mabl_dash.pdf")
Call<ResponseBody> getPdf();
}
}