Automate File Upload Test case to AWS S3 storage using RestAssured

Introduction

Nowadays, cloud computing is trending in the market. Most of the new products are getting shaped using cloud services as the underlying architecture. When we talk about the cloud, AWS (Amazon Web Service) is one of the leading cloud providers. Coming back to the apps, many of the apps use the media (images/video/pdf/doc), e.g. Social media apps like Twitter, Facebook, etc. use images/videos in the post, Ecommerce Apps use products’ images and the list is growing. Let’s gear our focus on AWS cloud services and first understand how the media upload to AWS S3 bucket works. Later, you’ll see how you can automate media upload test case using Rest Assured + Java.

Overview of serverless — media uploading to S3

When you upload any file directly to an S3 bucket, you must first request a signed URL from the Amazon S3 service. You can then upload the file directly using the signed URL. This is the two-step process for your application front end:

Let’s try to upload the jpeg image file to the AWS S3 bucket using postman.

  1. Call an Amazon API Gateway endpoint, which invokes the getSignedURL Lambda function. This gets a signed URL from the S3 bucket.
Get the signed URL from AWS S3 bucket.

As you can see the “uploadURL” field has the signed url in the response.

2. Hit this signed url along with the file to be uploaded from the application to the S3 bucket.

Upload Image file using the signed URL.

Yeah…The file is uploaded successfully to AWS S3 storage. Isn’t it simple????

Let’s see how you can automate above 2 steps procedure using Rest Assured and java.

Automating Media Upload Testcase using RestAssured + Java

Knowing the underlying AWS serverless architecture and upload file to AWS s3 bucket using postman (manually), the next step is to automate these 2 steps sequence which even simpler yet a bit tricky :)

Let’s automate the image/video upload to AWS S3 bucket using RestAssured (RestAPI) with Java. As the journey kick-off, the challenges start coming in the way. No worry at all, if you want to automate upload media API test cases. Let’s discuss the complete solution in detail.

Step 1: Login using AWS Cognito user credentials and getToken.

import com.amazonaws.auth.SystemPropertiesCredentialsProvider;

import com.amazonaws.regions.Regions;

import com.amazonaws.services.cognitoidp.AWSCognitoIdentityProvider;

import com.amazonaws.services.cognitoidp.AWSCognitoIdentityProviderClientBuilder;

import com.amazonaws.services.cognitoidp.model.AdminInitiateAuthRequest;

import com.amazonaws.services.cognitoidp.model.AdminInitiateAuthResult;

import com.amazonaws.services.cognitoidp.model.AuthFlowType;

import com.api.setup.RequestBuilder;

import com.core.Config;

import org.json.simple.parser.ParseException;

import java.io.IOException;

import java.util.HashMap;

import java.util.Map;

import static com.amazonaws.SDKGlobalConfiguration.ACCESS_KEY_SYSTEM_PROPERTY;

import static com.amazonaws.SDKGlobalConfiguration.SECRET_KEY_SYSTEM_PROPERTY;

public class AWSCognitoAccess {

public static String getIdToken(String username, String password) throws IOException, ParseException {

Regions regions = Regions.EU_WEST_1;

String accessKey = “<Replace your accessKey>”;

String secretKey = “<Replace your secretKey>”;

String clientId = “<Replace your clientId>”;

String poolId = “<Replace your poolId>”;

System.setProperty(ACCESS_KEY_SYSTEM_PROPERTY, accessKey); System.setProperty(SECRET_KEY_SYSTEM_PROPERTY, secretKey);

AWSCognitoIdentityProvider provider = AWSCognitoIdentityProviderClientBuilder.standard()

.withRegion(regions).withCredentials(new SystemPropertiesCredentialsProvider()).build();

Map<String, String> authParams = new HashMap<>();

authParams.put(“USERNAME”, username);

authParams.put(“PASSWORD”, password);

AdminInitiateAuthRequest adminInitiateAuthRequest = new AdminInitiateAuthRequest().withClientId(clientId)

.withUserPoolId(poolId).withAuthFlow(AuthFlowType.ADMIN_NO_SRP_AUTH).withAuthParameters(authParams);

AdminInitiateAuthResult result = provider.adminInitiateAuth(adminInitiateAuthRequest);

return result.getAuthenticationResult().getIdToken();

}

}

Security Notes: I strongly recommend keeping your AWS accessKey/secretKey/clientId/poolId secured. You may keep it in the separate config file and add that config file in the .gitignore file to skip it pushing to the git repository. If you are using any CI/CD pipeline, please use the best practices provided to keep the above-secured information.

Step 2: Call AWS API gateway to get a signed URL from S3.

============Create Media Model Class=============

package Models;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

import com.fasterxml.jackson.annotation.JsonInclude;

import lombok.AllArgsConstructor;

import lombok.Builder;

import lombok.Data;

import lombok.NoArgsConstructor;

@Data

@Builder

@NoArgsConstructor

@AllArgsConstructor

@JsonIgnoreProperties(ignoreUnknown = true)

@JsonInclude(JsonInclude.Include.NON_NULL)

public class Media {

private String mediaId;

private String filePath;

private String mimeType;

private String contentType;

private String uploadURL;

private String imageId;

}

==============Call AWS API gateway===========

public Media callAWSAPIGateway(String contentType){

String apiEndPoint = “https://<replace your domainName>/uploadImage”;

public Media buildMediaRequest(String contentType){

Media media = Media.builder().build();

media.setContentType(contentType);

return media;

}

response = RestAssured.given().body(buildMediaRequest(contentType)).post(apiEndPoint);

}

======Get signed url from response and save it into map========

public static void get_media_url(String mediaUrlKey, String imageIdKey){

Media media = response.as(Media.class);

map.put(mediaUrlKey,media.getUploadURL());

map.put(imageIdKey,media.getImageId());

}

Step 3: Finally, Call uploadMedia method from your calling function / main method.

public void uploadMedia(String username, String password, String contentType, String mediaName, String mediaUrlKey, String imageIdKey) throws IOException, ParseException {

//This is the media file location.

File mediaFile = new File(System.getProperty(“user.dir”)+”/src/test/resources/testResource/”+mediaName);

//Below highlighted lines do the magic. You may get an error if you don’t set urlEncodingEnable to false and set config for CharSet.

RestAssured.urlEncodingEnabled = false;

callAWSAPIGateway(contentType);

get_media_url(mediaUrlKey,imageIdKey);

URL mediaUrl = new URL(map.get(mediaUrlKey));

RestAssured.given().config(RestAssured.config().encoderConfig(encoderConfig().appendDefaultContentCharsetToContentTypeIfUndefined(false)))

.header(“Content-Type”, contentType)

.body(mediaFile)

.put(mediaUrl);

RestAssured.urlEncodingEnabled = true;

}

Let’s call uploadMedia from main class.

public class TestFileUpload {
public static void main(String[] args) {

String username = “maulikpatel”;

String password = “HappyTest@123”;
String contentType = “video/mp4”;
String mediaName = “testVideo.mp4”;

String mediaUrlKey = “videoUploadURLKey”;

String imageIdKey = “videoImageIdKey”;

uploadMedia(username, String password, String contentType, mediaName, mediaUrlKey, imageIdKey)
}
}

That’s it. Your file is now uploaded to the S3 bucket successfully.

Happy Testing…. Very Happy Automated Testing…. :)

#############################

Sincerely,

Maulikkumar Patel
An Automation and Functional QA Evangelist

--

--

--

Automation and Functional QA Evangelist

Love podcasts or audiobooks? Learn on the go with our new app.

Cisco is hiring for Project Interns

5 Signs your Network is overdue for an Upgrade

#hk #jewelry #rantai http://ift.tt/2jxuvP4

Reduce Virtual Meeting Fatigue using Power Automate

Restarting HTTPD Service is not idempotence in nature and also consume more resources suggest a…

Over the past 48 hours I have proposed this idea to the CEOs

Upgrading Calico CNI Plugin for Kubernetes Cluster

Which is the best for Web development between Python with Django and PHP with Laravel

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Maulikkumar Patel

Maulikkumar Patel

Automation and Functional QA Evangelist

More from Medium

5GC deployment on AWS Cloud

The trend of deploying core network on cloud

Automation mastery: the next frontier for quality SAP cloud deployments?

Creating an AWS AppConfig Application using Terraform

DevOps Using AWS Tooling