Unit testing app engine image upload in Java - java

I am doing image uploads as outlined in the documentation for the App Engine Images API, using getServingUrl() to generate an upload url, and then forwarding the request to my own handler. I am wondering if there is a good way of unit testing the image handling logic in my handler. If I instantiate my handler in a unit test and pass on a request to it, it fails with a "Must be called from a blob upload callback request". Can I somehow mock it?

Write a mock object that returns a known value from the getServingUrl method and then make sure that your class correctly reads that value and uses it to make the upload request. You have to assume that the GAE code is going to do the right thing in this case. The only logic you should concern yourself with is that which reads that url from GAE and subsequently posts to it with your blob data.

Related

How to get the response of an intermediate API call?

I am currently trying developing some API tests using rest-assured. More specifically, I am trying to test Stripe's payment gateway (3DS). The way it works is that I subscribe a user using our API endpoint, which has a response including an external link to Stripe's website where the user fills out a form for their payment details.
This part is fine and dandy.
The problem lies ahead. I need to be able to send a POST call to this form with the payload of the form. However, the POST payload that Stripe is expecting includes other elements that are not part of the form such as muid, guid, sid, key, and payment-user-agent. After doing some research, I see that these elements are given when the user first loads the form. However, they are given are given at an intermediate API call during the loading of the page. Meaning, if I try to call a GET on the form page, I will not retrieve these elements. The loading of the page calls other endpoints sequentially, where at some point, it makes a call to another endpoint with an encrypted request payload, which I cannot reproduce for my automated tests, which returns the muid, guid, and others.
I was wondering if it were possible to get the response of the intermediate API call somehow?
I tried doing research of methods that can possibly achieve this, but I have yet to come up with something that is API testing related.
I cannot do a Selenium script to push the form for me, as I need it to be API testing related, and with no GUI browser included. I could potentially run Selenium in headless maybe, but I was wondering if there was anything more lightweight or a simple way to retrieve the response of the intermediate api call before I proceed with that solution.

How do I upload a file(picture) from android and save to the blobstore using GAE and google endpoints?

Sorry if this is a little general. Hopfully I can get pointed in the correct direction.
I have an android app that includes instant messaging. I want to add the functionality to send photos in messages as well. My backend is built on GAE and cloud endpoints and is written in Java.
So far, I've looked into google cloud storage, the blobstore, java servlet pages, etc. But a clear solution (or example using endpoints) has been impossible to find.
So, as the question states, how can I send and serve images from the blobstore using android and GAE endpoint backend?
Edit: This question is only regarding the back end. In android, I can do the http post easily enough. I'm just lost when it comes to doing this in endpoints
I suggest the following approach to implement what you require:
Pass the image data as base64 encoded text from your client.
On the server side, you can extract out the image content and then use one of many options that are available, which include Blobstore, Google Cloud Storage, etc. I suggest that you go with Google Cloud Storage because that is the recommended approach. If you prefer the Datastore, keep in mind that the data is limited to 1 MB in size, so you might hit that limit depending on the size of images that you are dealing with.
A SO question and answer here contains lot of relevant code that you could utilize: Sending images as a base64 string to a google cloud endpoint from cms
Why not using a simple HTTP POST to handler for image upload?
When I use the blobstore I typically have a pair of handlers one for uploading and one for serving images.
So, in your case I'll rather do a post to a URL sending the image file and some parameter than can link it to the proper place in the datastore (i.e. user ID).
Then, using endpoints I'll have the same linking key and display the image using the serving handler with the given parameter.
If you are already using endpoints you can follow #Romin 's suggestion and send the images as Base64 or upload your images in 2 steps using the blobstore/GCS service, first get the Upload URL (via endpoints) and then post your image to that URL.

Is it possible to call Pubsub's subscription pull directly in javascript instead of calling java method to fetch the data?

I've been working in pubsub where I can successfully pull the data from a particular topic under a particular project in java. If I've to show these data in html, first I've to call the servlet method, then the servlet will call pubsub api to get the data, then I've to include that data in the response.
Since it involves an additional one layer (java) to access the data, is it possible to directly fecth the data in javascript call by skipping the java call..? Is there any api's available in google pubsub to serve for that purpose?
The api can be used in javascript (with ajax) since its just https calls,
https://cloud.google.com/pubsub/reference/rest/
however its a bad idea because you would need to expose your server account access token and the client could then abuse it.

Simulate form post using http client in Android app?

So, I'm currently developing an app for a service which has a json-based (unfortunately) read only API. Retrieving content is no problem at all, however the only way to post content is using a form on their site which location is a PHP script. The service is open source so I know which fields the form expects, but whatever I send, it always results in a BAD REQUEST.
I captured the network traffic inside my browser and as far as I can see, the browser constructs a multipart form request, however when I copy the request and invoke it again using a REST client, a BAD REQUEST gets returned.
Is there a way to construct a http request in Android that simulates a form post?
If it's readonly I think you wouldn't be able to make requests with POST (it's assume for editing or adding things).
If you let me make you an advise, I recommend you using this project as a Library.
https://github.com/matessoftwaresolutions/AndroidHttpRestService
It makes you easy deal with apis, control network problems etc.
You can find a sample of use there.
You only have to:
Build your URL
Tell the component to execute in POST mode
Build your JSON
As I told you, I don't know even if it will work.
I hope it helps!!!

Using RestTemplate with local json file

Background
I'm writing a web service that makes calls to an external api. This api has not yet been put into production. As such, when I make a call to my web service in my dev environment, I want to stub out the responses that it returns. Note: this is not a unit testing question.
My Solution So Far
The api calls are made using RestTemplate from the lovely Spring people, the url of which is held in application.properties. This has allowed me to set different urls for different environments using Active Profiles. So, for example, application-dev.properties holds a different url.
The dev url is ideally a pointer to a json file under resources/.
My Issue
I can't seem to get RestTemplate to pick up the local json file. The url I'm using is:
url = "file://staticJson.json"
However that comes up with a
Object of class [sun.net.www.protocol.ftp.FtpURLConnection] must be an instance of class java.net.HttpURLConnection
And now I'm unsure of how to proceed, or if this is even possible without extending RestTemplate.
Any directions to try or solutions would be fantastic.
If any more information is required I'll do my best to provide it asap.

Categories

Resources