Testing rest services by using the client - java

I have been an old school type of programmer and didn't really use tests in my projects. I want to change that now but since it's not an industrial project but my own I would like to ask:
Is there any problem with using the client to test my Rest service?
Why would I need to write a unit test to test the rest backend and then write a client when I can just extend the client?
Is there any convention that the testing language should be the same as the implementing language ? I am talking about Java rest and JS client.
I think this will make my approach much more agile and fast.

Why would I need to write a unit test to test the rest backend and then write a client when I can just extend the client?
Basically in Test Cases you test everything while a client may use only a subset of the interface and will never hit a possible bug.
Is there any convention that the testing language should be the same as the implementing language ?
No. But there are very good tools (even automatic generation) that snaps into the Java IDE, Selecting java Automation testing tools for web application .

Related

Is it possible to use more than one framework at the backend(Spring boot + Django)?

tl;dr: Is Spring + Django back-end possible?
When I was new to industry and was still working my way around the office, I got interested in Django and created a very small, basic-level application using the framework. When I got to meet my team after a few weeks, they said to go for Spring framework. After spending half a year on the framework and the main proj, I finally started to get time to start working off-hours. But, I don't want to lose both the skills - My teammate(when we were still in office ;) ) once told me that they worked on a project that started with python code, and then later added features using Java. And I am unable to find any helpful google searches(mostly showing Spring vs Django).
How should I go about it? Is it too much to ask for? Is it worthwhile? Will I learn some new concepts of application architecture a noob like me would have missed. Please provide me with some insight.
Are there resources(docs) I can go through?
P.S. I'm not a diehard fan of either of the frameworks right now, just another coder testing waters.
You can't write java in python.
You can extend Python with C/C++ which is quite common: Extending Python with C or C++
And about the part that they told that they added features with java:
It's common to create different parts of a project using different languages and tools. Microservice architecture is a common architecture for these kinds of use cases. You basically code different parts of the project in a language you want and then you connect all the parts using different methods like REST APIs, gRPC and etc.
Imagine you are creating a website like youtube that lets others upload videos. There is a form that users upload their files and you store them in your storage and then you have to encode the video file for different qualities. You can code the form handler using Python and Django to store the files in your storage. Then you can code another service using java that handles the encoding part which is a heavy process. When an upload is completed, you send the file or file path to your java service using an internal REST API and tell the service to start encoding the video and notify the Django service and then the Django service will publish the video on the feed that can itself be written in another language.
I would say go for 1 framework and stick with it. For example Django if you want to code in python, and spring if you want to code in java. Learning both frameworks however brings a lot of value, because you can compare their benefits (eg. spring forces you to write clean code, django has build-in and simpler database management)
I like Django's build-in tooling a lot, you only need to know python for it to work. Spring requires a bit more knowledge of eg. hibernate for database management. However I predict Django will outgrow spring at some point, because of cloud valuing fast iteration over code and quick startup time (auto-scaling apps) over large overhead apps and long boot times. Hoever, if you like java, I can recommend JHipster for java/spring webapp development to get up to speed very fast and learning the ways of REST CRUD api fast.
To combine 2 programs: write your main logic in one app, and write a small service in the second language, making sure its independent of the first app (no back and forth communication and complicated logic, but simple independent request/response, as if the main app was never there). Add a REST api to the second app and use eg. http requests to communicate.
What's possible in terms of combining languages:
connect different applications with each other: by letting them communicate through their APIs. For example a python api developed with flask or django can send requests to a java api developed with spring, as long as they have a way to communicate (eg over http, or via some queue like rabbitmq)
connect a webapp to 2 different backends: by using a shared authentication system: For example a keycloak authentication server to handle tokens, that your backend applications know about.
What's not possible (and also not preferable):
combining java with python code in the same program: there are some hacky ways to get it to work, but its asking for trouble and not readable.

Calling Python machine learning service from Java as an OS process or micro service via REST

I am building a web application for business management as a side project and i face an architecture problem.
My application will be an Angular web client communicating with a kotlin/java spring back-end via REST. One of the modules of the application will be time series forecasting of sales/orders, and because of the ease of development and the piles of documentation that exists online, i want to build that module in python. That module will generate dynamic html plots that i want to serve to the client. So my question is, it would be better if i do the python part as a stand alone rest micro service with Flask or it's ok to just put the python code inside the java project and call it using ProcessBuilder? Thanks in advance :)
My view will be to create a microservice with Python Flask with certain REST endpoints for obtaining the results and let java application interact with python based microservice for machine learning. The main advantage with this approach will be the separation of concern and deployment will be independent and you can leverage the benefits of microservice architecture.
Although it is also possible to bundle python code inside java or you can use jython, any changes in either in java code or in python code will add extra overhead for deployment. Bundling additional non java code will be another overhead for different OS systems like Windows, Mac and Linux.
You can check the following links for reference.
https://www.datacamp.com/community/tutorials/machine-learning-models-api-python

What is the advantage of using Direct Web Remoting over a REST web services design?

I have an enterprise Java web application deployed in tomcat.
I am looking for ways to integrate a HTML & JS UI with the mid tier java code. So far, from looking around I have read that there are two ways to do this which fit my requirements :
1) DWR - Direct web remoting
2) Use REST web services.
I am not sure which approach to use.
Edit: What is the advantage of using DWR over a rest WS design?
Is there a better approach available? Some people asked me to have a look at GWT, but I am not sure if these are scalable.
Edit 2: I will most likely be using dojo for the UI development(Works well with REST/DWR??), and I want to keep the java code chages minimal. I understand that by using web services, I would have to write an additional WS client layer on top of the existing java code.
Also, the usual enterprise needs, performance, scalability, etc.
Ok so you're building a web client frontend to your java backend, there are a few ways to go about this:
You want to use java standards as much as possible, preferably living in the java EE world: JSF. You will need some knowledge of HTML, XML and perhaps even javascript (though limited). Personally I have had a pretty buggy experience with JSF+richfaces to the extent that I don't use it anymore.
You are very well acquainted with java desktop application development but don't know much about javascript: vaadin: it allows you to write plain java using desktop application terminology which will be compiled to javascript. Currently I am working on a big vaadin project and it is very nice to be working with pure java in eclipse, the downside is however that you are far removed from the actual frontend, so tweaking can be tricky. Additionally everything is stored in sessions (afaik) and scales poorly.
You are primarily a frontend person with unrivaled javascript skills: use a REST interface in java and a pure js/html/css frontend. Personally I think this is the cleanest design and I have designed a few of my own applications like this. The downside is that managing large javascript projects tends to be hard because...well...javascript sucks. The upside is that this will always be the fastest most lightweight option available to you.
Ideologically I would definitely suggest the last approach but this can be hard for large projects. If you have the hardware to throw at it, vaadin is a nice option. My JSF 2 experience was a bit disappointing due to the bugs in (necessary) third party libraries like richfaces.
PS: I have never heard of DWR but the last stable release seems 2 years old and all it seems to do is expose java code as javascript methods which can be better handled with a REST interface.
Open interface standards like REST and SOAP make it easy to build code to consume these services if you are using frameworks to build a REST Client and a SOAP client code respectively. But the point is that you need to have this client code to make calls to these services.
DWR on the other hand, generates this client code. Your java classes are ready to be called (like you would do in Java).

Automated Testing Framework

I am trying build an automated testing framework for some of our internal web services (java) at work. Each service has a set of APIs (3-5), although this could be relatively easy to achieve, the problem comes with some APIs which do not behave as pure functions, ex: something like persistX, this could store something in a database and returns an exception incase of failure.
There is no easy way to validate since there is no output here.
So I was wondering if this could a a bit generalized, say while testing the API, the user could provide a simple plugin or script to the framework of some sort that could validate the test. This is just one idea would be great if someone can tell me some better ones or any resources about the same.
Thanks
I recommend the robot framework. It is a keyword driven framework written in python. Because of that, you can run it in the JVM with jython which means you can extend it with java code (or python, of course). I've sucessfully used it to call APIs, then verify the result by peeking into a database, or querying the file system.
It also works on the .NET platform, has a selenium module for testing the front end, a jenkins plugin, and several other tools. It's very extensible and flexible.
What you are looking at is the application of black-box and white-box testing and the tools that support both.
For the web services that return a proper response you can perform black-box testing by verifying the data in returned response. SoapUI is the best tool for this.
For the APIs which do not behave as pure functions, you do white-box testing by verifying its side-effects like persistence, event generation, logging etc. For this you like programmable tools and SoapUI may or may not be the correct option.
We do both at our work and after evaluating multiple tools/frameworks (SoapUI, RSSPec, Robotframework), I chose Spock. Why spock?
It allows you to write intention revealing tests in BDD style
We are Java shop and we want to use the same familiar language for automation as well but with simplified syntatic sugar. And spock is all groovy based.
Excellent Webdriver/Selenium 2 support (including PageFactory) with Geb
It is built on top of JUNIT so all the plugins of JUNIT can be leveraged (code coverage, hudson/jenkins integration, etc)
Lot of webservice APIs and XML DSLs (no need to work with XPATH for simple scenarios)
Simplified setup (unlike robotframework it doesnt require python, jython setup)
etc....

How to invoke a gwt rpc servlet from c or c++ client using libcurl?

I have a google app engine application with a GWT client. Most server functionality is accessible through normal GWT RPC calls as usual. I am writing a data mining application in C++ which needs to communicate with the appengine application. Problem is, there are just too many gwt servlets implemented so a rewrite of server code is out of the question.
Any ideas?
If you were using java on the client you could use GWT SyncProxy. Although main usecase of this library was to test remote gwt rpc service, but it can be effectively used for any purpose. As you are developing your client in C++ this might not be an option for you.
If you control the GWT application (that is you can recompile it), you can probably do something like this:
Export RPC related functions into javascript. That is make specific functions of your GWT code made available to be called as normal Javascript functions from within the host page. See this article for a tutorial
Use a embedded browser engine like webkit and load your GWT module script in it.
Call the exported GWT functions as normal java functions.
But it will probably prove to be too much work, so you might be better off refactoring the servlets to expose another JSON/XML based interface in addition to RPC.
Solved, the best way to go is as Tahir suggested. Trying to connect to GWT rpcs directly from C++ is just too much work. The easiest way was to write thin wrappers on the server side as normal servlets and use http and curl from C++. One issue remains about object serialization to JSON or XML. I elected to write a custom annotation on the object fields which I wanted to serialize and then read these annotations at runtime in order to serialize them.

Categories