Java code

profileSul63n
TestingRESTWebServer.pdf

Testing REST Server Unit Testing When testing the rest server, first create test classes in the Netbeans project. You can test all the rest classes in a test harness before deploying in a Web Server. You should do this, because you can create repeatable tests, which are easy to run.

For instance when testing the customer rest method you can create a class similar to the one below showing tests for GET and POST methods.

// TESTING CUSTOMERS GET METHOD package test;

import rest.CustomersResource;

public class TestCustomerGet { public static void main(String[] args) { CustomersResource cr = new CustomersResource(); System.out.println( cr.getText() ); } }

// TESTING CUSTOMERS POST METHOD package test;

import rest.CustomersResource;

public class TestCustomerPost { public static void main(String[] args) { String testMessage = "{\"CustomerReference\":\"234gy\",\"Phone\":\"0858037364\"," + "\"CreditLimit\":100.0,\"CustomerId\":1,\"CustomerName\":\"john Doe\"}"; CustomersResource cr = new CustomersResource(); System.out.println( cr.postText(testMessage) ); } }

With this approach you can test all the inputs and outputs of the rest methods before deploying them. If these tests are successful you can have a high degree of confidence that the rest calls are coded correctly, and subsequent errors deployment rather than code issues.

Integration Testing To test the full functionality you will need to deploy the code in a Web server. There are two options we have used in the lab - Glassfish and Wildfly.

Testing in Glassfish If you were successful in installing Glassfish, then you can deploy to Glassfish using Netbeans. Right click on the project and select Deploy. If deployed then right click on project again and select ‘Test Restful Web Services’. You should see a screen like the one below, which will allow you to test all methods.

Testing in Wildfly You can use any java rest server which supports the Jersey (JAX-RS) plugin. Wildfly can be a better alternative to Glassfish, because Glashfish can be difficult to run on some environments. Wildfly is a light weight, free version of JBoss server, and runs in a standalone mode, and is robust.

Download Wildfly from: https://wildfly.org/downloads/ Get the full version zip file and unzip into a directory of your choice.

Make a new ‘web’ project in Netbeans, selecting Wildfly and the server. Then copy the packages from your existing project to the new Wildfly project. Add the mysql-connect and json-simple jar files to the library directory. Make sure you use version 8 of mysql-connect. Correct versions of both jar files are now on Moodle.

You should be able to start the Wildfly server from Netbeans services tab under servers. If not, open a cmd window and navigate to the bin directory under the Wildfly installation. From the command line, Type >standalone.

To deploy your application to the Wildfly server, you should be able to right click on your project and select deploy. Sometimes Netbeans has a problem deploying the app. If so dot he following: • Right click on project and select ‘clean and build’ in Netbeans. • In netbeans, in the services tab, find the Wildfly server, right click and select ‘View Admin

Console’ (alternatively type into your browser url: localhost:9990 ). • In the Wildfly browser console, select ‘Deployments’ in the menu bar • In deployment page, click on button on top left and select ‘Upload Deployment’ • Click on ‘choose a file’ and navigate to your java project’s ‘dist’ directory. • You should find a file named the same as your project, with .war extension, select this. • Select next, and then finish.

To check your server has deployed do the following: • In the wildfly console (in browser), select to ‘Runtime’ in menu. • In runtime page, select your server on the left • under monitor, select JAX-RS. • Under Rest resource you should be able to see your endpoints. • Select on an endoint and you should be able to select your GET method, and see the

response.

To fully test all methods, you will need a front end to test with, to make calls in HTTP. There are a number available on the web for free. For example, you can download a good http rest testing application from:

https://www.getpostman.com/downloads/

Below is a screenshot of Postman:

I have also put an Angular based test tool on Moodle, which you can use, in case you don’t want to use a third party app.

CORS If you test using the Angular test tool, you will likely get a CORS error in your browser. CORS stands for Cross Origin Resource Sharing, since browsers block the calling code from a url other than that of the url of the page calling the code. We will cover this later in the course, when we connect to Angular. To overcome this error, follow the instructions below: • Go to Netbeans, and right click in project. • If you see an option called: ‘Cross-Orgin Resource sharing filter’, select it. If you do not see

the option, then select other: In the popup, select ‘Web Services’ in category, and choose ‘Cross-origin resource sharing filter’ on the right.

• Rebuild and redeploy the project, as described above.