In my article “Oracle API Platform Cloud Service: Design-First approach and using Oracle Apiary”, I talked about using Oracle Apiary and interacting with its Mock Server by using code examples.
[https://technology.amis.nl/2018/01/31/oracle-api-platform-cloud-service-design-first-approach-using-oracle-apiary/]
One of the examples for the “HumanResourceService” API is about the action “Get all employees” in combination with “Java” as language. With that example, also the dependencies to be used in a Maven pom.xml (java6+) are given. This example triggered me to try out the Oracle JDeveloper, Maven and Java combination.
In this article the focus will there for be on calling the Oracle Apiary Mock Server from Java code, with the help of Oracle JDeveloper (in combination with JUnit and Maven).
For more information about using Maven, see: https://maven.apache.org
So this time I needed a recent version of Oracle JDeveloper and fortunately I already had a pre-built Oracle VM VirtualBox appliance, named: “Oracle SOA Suite VirtualBox Appliance [May 2016]”, available which contains the following:
- Oracle Linux (64-bit) EL 6 Update 7
- Oracle Database, Enterprise Edition 12.1.0.2
- Oracle SOA Suite 12.2.1 (includes Service Bus, B2B, Oracle Enterprise Scheduler (ESS) and Business Activity Monitoring)
- NEW: Oracle Real-Time Integration Business Insight 12.2.1
- Oracle Managed File Transfer 12.2.1
- Oracle Stream Explorer 12.2.1
- Oracle JDeveloper 12.2.1
- Java JDK 1.8.0_51-b16 (64-bit)
For this appliance see:
http://www.oracle.com/technetwork/middleware/soasuite/learnmore/soa-vm-2870913.html
and
http://www.oracle.com/technetwork/middleware/soasuite/learnmore/soasuite1221vbox-readme-2870943.pdf
For a newer version of an appliance with Oracle JDeveloper 12.2.1.1.0 and Java JDK 1.8.0_92(64-bit) [July 2016], take a look at for example:
http://www.oracle.com/technetwork/middleware/soasuite/learnmore/prebuiltvm-soasuite122110-3070567.html
or
http://www.oracle.com/technetwork/community/developer-vm/index.html
For the latest version of Oracle JDeveloper see:
http://www.oracle.com/technetwork/developer-tools/jdev/downloads/index.html
After importing (Integration_12.2.1_OTN.ova) and starting the appliance in VirtualBox, the desktop looks like:
Before starting the appliance I included a SharedFolder (visible on the desktop as sf_MySharedFolder).
Interacting with the Mock Server by using code examples
In Oracle Apiary, for the “HumanResourceService” API, one of the code examples for interacting with its Mock Server is about the action “Get all employees” in combination with “Java” as language.
// Maven : Add these dependecies to your pom.xml (java6+) // <dependency> // <groupId>org.glassfish.jersey.core</groupId> // <artifactId>jersey-client</artifactId> // <version>2.8</version> // </dependency> // <dependency> // <groupId>org.glassfish.jersey.media</groupId> // <artifactId>jersey-media-json-jackson</artifactId> // <version>2.8</version> // </dependency> import javax.ws.rs.client.Client; import javax.ws.rs.client.ClientBuilder; import javax.ws.rs.client.Entity; import javax.ws.rs.core.Response; import javax.ws.rs.core.MediaType; Client client = ClientBuilder.newClient(); Response response = client.target("https://private-b4874b1-humanresourceservice.apiary-mock.com/employees") .request(MediaType.TEXT_PLAIN_TYPE) .get(); System.out.println("status: " + response.getStatus()); System.out.println("headers: " + response.getHeaders()); System.out.println("body:" + response.readEntity(String.class));
With that example, also the dependencies to be used in a Maven pom.xml (java6+) are given.
Maven implementation in Oracle JDeveloper
I started Oracle JDeveloper and first I took a closer look at the Maven implementation.
Via menu Tools | Preferences.. | Maven you can manage the Maven implementation.
The first time, click on button “Load Extension”.
Then you can see that the JDeveloper Embedded Version (3.2.5) is used. Next I took a look at the other Maven preferences.
Logging/Error:
Phases/Goals:
The “Default” Goal Profile is used with the following phases:
- clean (project cleaning)
- compile (compile the source code of the project)
- test (test the compiled source code using a suitable unit testing framework. These tests should not require the code be packaged or deployed)
- package (take the compiled code and package it in its distributable format, such as a JAR)
- install (install the package into the local repository, for use as a dependency in other projects locally)
- site (project site documentation)
For more information about Maven build lifecycles and phases, see:
https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html
Repositories:
Here you can see which repositories are used.
A Maven repository holds build artifacts and dependencies of varying types.
The local repository refers to a copy on your own installation that is a cache of the remote downloads. It also contains the temporary build artifacts that you have not yet released.
By default the local repository is located at ${user.home}/.m2/repository/. You can change the path and repository name by clicking the Edit button
[Oracle JDeveloper Help, Maven: Repositories Dialog]
The “Maven Central” repository is a remote repository. The URL for the Maven central repository is http://repo1.Maven.org/Maven2
[Oracle JDeveloper Help, Maven: Repositories Dialog]
For more information about Maven repositories, see:
https://maven.apache.org/guides/introduction/introduction-to-repositories.html
Settings:
Here you can specify the location of the Maven settings.xml file and specify command line options for Maven.
The content of file /home/oracle/.m2/setting.xml is:
<?xml version="1.0" encoding="UTF-8"?> <settings xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.1.0 http://maven.apache.org/xsd/settings-1.1.0.xsd" xmlns="http://maven.apache.org/SETTINGS/1.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <proxies> <proxy> <port>80</port> <host>www-proxy.us.oracle.com</host> <nonProxyHosts>localhost|127.0.0.0/8|integration|integration.oracle.com|*.us.oracle.com|localhost.localdomain|127.0.0.1|[::1]|127.0.0.1</nonProxyHosts> </proxy> </proxies> </settings>
For more information about Maven Settings, see:
https://maven.apache.org/settings.html
Maven standard directory layout
Having a common directory layout would allow for users familiar with one Maven project to immediately feel at home in another Maven project. The advantages are analogous to adopting a site-wide look-and-feel.
[https://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html]
For calling the Oracle Apiary Mock Server from Java code, I used the following maven standard directory layout:
- src/main/java (Application/Library sources)
- src/main/resources (Application/Library resources)
- src/test/java (Test sources)
- src/test/resources (Test resources)
- target (All output of the build)
To match the default maven structure in JDeveloper I created a Custom Application.
Be aware that there are other options in JDeveloper to work with Maven projects. For example by using Maven Archetypes.
[https://docs.oracle.com/middleware/1221/jdev/user-guide/GUID-609458B4-79BD-4F5E-8ABE-C5C616576E7D.htm#OJDUG6528%5d]
Creating a Custom Application
Via menu File | New | Application.. | Custom Application an application can be created that matches the default maven structure (maven standard directory layout).
[https://docs.oracle.com/middleware/1221/jdev/user-guide/GUID-609458B4-79BD-4F5E-8ABE-C5C616576E7D.htm#OJDUG6874]
In the wizard, I created application “ServicesApplication” with Application Package Prefix “nl.xyz.services”.
After clicking on the button “Next >” I filled in the Project Name “HumanResourceServiceProject” and as Project Features, selected “Maven”.
In the next screen, I changed the default Group ID from nl.xyz.services to nl.xyz.services.humanresourceservice.
Also the checkbox was checked, to modify the normal project structure to match the default maven structure.
In the next screen, “Use Maven” was chosen.
After finishing the wizard, the Applications window for the “ServicesApplication” looked like:
The Project level Maven POM file overview screen looks like:
The “HumanResourceServiceProject”, Project Properties look like:
In the “Project Source Paths” screen, I changed the Default Package from nl.xyz.services to nl.xyz.services.humanresourceservice.
Here you can see that the Project Source Path settings are set in the Project level Maven POM file. The Java Source Path (matching the default maven structure) is set to:
/u02/oracle/developer/mywork/ServicesApplication/HumanResourceServiceProject/src/main/java
After finishing the wizard, the Application level pom.xml content is:
<?xml version="1.0" encoding="UTF-8" ?> <project xmlns="http://maven.apache.org/POM/4.0.0"> <modelVersion>4.0.0</modelVersion> <groupId>nl.xyz.services</groupId> <artifactId>ServicesApplication</artifactId> <version>1.0-SNAPSHOT</version> <description>Super POM for ServicesApplication</description> <modules> <module>HumanResourceServiceProject</module> </modules> <packaging>pom</packaging> <dependencies> <dependency> <groupId>nl.xyz.services.humanresourceservice</groupId> <artifactId>HumanResourceServiceProject</artifactId> <version>1.0-SNAPSHOT</version> <type>jar</type> </dependency> </dependencies> </project>
After finishing the wizard, the Project level pom.xml content is:
<?xml version="1.0" encoding="UTF-8" ?> <project xmlns="http://maven.apache.org/POM/4.0.0"> <modelVersion>4.0.0</modelVersion> <groupId>nl.xyz.services.humanresourceservice</groupId> <artifactId>HumanResourceServiceProject</artifactId> <version>1.0-SNAPSHOT</version> <description>Project for HumanResourceServiceProject</description> <build> <resources> <resource> <directory>${basedir}</directory> <includes> <include>*</include> </includes> </resource> <resource> <directory>src/main/resources/</directory> <includes> <include>*</include> </includes> </resource> </resources> </build> </project>
Creating Java classes “Employee” and “Department”
As described in my previous article, the “Human Resource Service” API contains actions for “Employees” and “Departments”.
So there for I decided to create two Java classes corresponding with them.
Via menu File | New | Java Class…, I created a Java class called “Employee” and in the wizard, I unchecked “Implement Abstract Methods”.
package nl.xyz.services.humanresourceservice; public class Employee { public Employee() { super(); } }
In the same way I created a Java class called “Department”.
package nl.xyz.services.humanresourceservice; public class Department { public Department() { super(); } }
After creating the Java classes, the Applications window for the “ServicesApplication” looked like:
The two Java classes (with extension .java) where created in directory:
/u02/oracle/developer/mywork/ServicesApplication/HumanResourceServiceProject/src/main/java/nl/xyz/services/humanresourceservice
Apache Maven – compile
So far so good. In JDeveIoper, I created an Application, Project and two Java classes. Time to put Maven to work.
Via a right-click on the pom.xml | Run Maven, the Phases/Goals you can choose from, become visible:
After choosing “compile”, the following popped-up:
Here I clicked the button “Yes”. After this, the content of file /home/oracle/.m2/setting.xml is:
<?xml version="1.0" encoding="UTF-8"?> <settings xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.1.0 http://maven.apache.org/xsd/settings-1.1.0.xsd" xmlns="http://maven.apache.org/SETTINGS/1.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <proxies/> </settings>
Remark:
The JDeveloper proxy settings (see Tools | Preferences | Web Browser and Proxy) were set on: Use System Default Proxy Settings.
With this option, the server uses the default proxy settings on your machine. These are taken from the operation system (in the case of Windows and MacOS) or from the window manager (in the case of Linux).
[Oracle JDeveloper Help, Preferences Dialog – Web Browser and Proxy Page – Proxy Settings]
The content of Apache Maven – compile – Log is:
[INFO] Compiling 2 source files to /u02/oracle/developer/mywork/ServicesApplication/HumanResourceServiceProject/target/classes
[INFO] ————————————————————————
[INFO] BUILD SUCCESS
[INFO] ————————————————————————
[INFO] Total time: 7.375 s
[INFO] Finished at: 2018-02-06T13:23:45-08:00
[INFO] Final Memory: 14M/133M
[INFO] ————————————————————————
Process exited with exit code 0.
The two compiled Java classes (with extension .class) where created in directory:
/u02/oracle/developer/mywork/ServicesApplication/HumanResourceServiceProject/target/classes/nl/xyz/services/humanresourceservice
Java class “Employee” interacting with the Mock Server
In Oracle Apiary, for the “HumanResourceService” API, one of the code examples for interacting with its Mock Server is about the action “Get all employees”.
There for I changed the code for Java class “Employee” and used the Java code example from Oracle Apiary.
package nl.xyz.services.humanresourceservice; import javax.ws.rs.client.Client; import javax.ws.rs.client.ClientBuilder; import javax.ws.rs.client.Entity; import javax.ws.rs.core.Response; import javax.ws.rs.core.MediaType; public class Employee { public Employee() { super(); } public Response getAllEmployees() { Client client = ClientBuilder.newClient(); Response response = client.target("https://private-b4874b1-humanresourceservice.apiary-mock.com/employees") .request(MediaType.TEXT_PLAIN_TYPE) .get(); System.out.println("status: " + response.getStatus()); System.out.println("headers: " + response.getHeaders()); System.out.println("body:" + response.readEntity(String.class)); return response; } }
Of course then there were several issues found.
In line with the comment (// Maven : Add these dependecies to your pom.xml (java6+)) in the Java code example from Oracle Apiary, dependencies had to be added in the Project level Maven POM file.
I added the dependency in the Project level Maven POM file overview screen, tab “Dependencies” (by using the icon +):
After doing that, the Project level pom.xml content was:
<?xml version="1.0" encoding="UTF-8" ?> <project xmlns="http://maven.apache.org/POM/4.0.0"> <modelVersion>4.0.0</modelVersion> <groupId>nl.xyz.services.humanresourceservice</groupId> <artifactId>HumanResourceServiceProject</artifactId> <version>1.0-SNAPSHOT</version> <description>Project for HumanResourceServiceProject</description> <build> <resources> <resource> <directory>${basedir}</directory> <includes> <include>*</include> </includes> </resource> <resource> <directory>src/main/resources/</directory> <includes> <include>*</include> </includes> </resource> </resources> </build> <dependencies> <dependency> <groupId>org.glassfish.jersey.core</groupId> <artifactId>jersey-client</artifactId> <version>2.8</version> </dependency> </dependencies> </project>
The other comment part I copied / pasted in the tab Source of the pom.xml:
<?xml version="1.0" encoding="UTF-8" ?> <project xmlns="http://maven.apache.org/POM/4.0.0"> <modelVersion>4.0.0</modelVersion> <groupId>nl.xyz.services.humanresourceservice</groupId> <artifactId>HumanResourceServiceProject</artifactId> <version>1.0-SNAPSHOT</version> <description>Project for HumanResourceServiceProject</description> <build> <resources> <resource> <directory>${basedir}</directory> <includes> <include>*</include> </includes> </resource> <resource> <directory>src/main/resources/</directory> <includes> <include>*</include> </includes> </resource> </resources> </build> <dependencies> <dependency> <groupId>org.glassfish.jersey.core</groupId> <artifactId>jersey-client</artifactId> <version>2.8</version> </dependency> <dependency> <groupId>org.glassfish.jersey.media</groupId> <artifactId>jersey-media-json-jackson</artifactId> <version>2.8</version> </dependency> </dependencies> </project>
After saving the pom.xml, the dependencies where being resolved.
The Maven local repository was updated, with among others, the following two jar files:
- /home/oracle/.m2/repository/org/glassfish/jersey/core/jersey-client/2.8/jersey-client-2.8.jar
- /home/oracle/.m2/repository/org/glassfish/jersey/media/jersey-media-json-jackson/2.8/ jersey-media-json-jackson-2.8.jar
See also:
http://repo1.maven.org/maven2/org/glassfish/jersey/core/jersey-client/2.8/
respectively:
http://repo1.maven.org/maven2/org/glassfish/jersey/media/jersey-media-json-jackson/2.8/
JUnit
In order to invoke the Oracle Apiary Mock Server for the action “Get all employees” from the Java class “Employee”, I decided to use a JUnit TestCase class.
For this I had to install an Oracle Extension for Oracle JDeveloper. Via menu Help | Check for Updates…, from the list of Available Updates, I choose “JUnit Integration” and installed it.
After the install, a JDeveloper restart was done.
For more information about JUnit, see:
https://junit.org/junit4
Creating Java class “EmployeeTest”
In the Applications window for the “ServicesApplication”, after a right-click on package nl.xyz.services.humanresourceservice, via the menu File | New | From gallery… | General | Unit Tests | Test Case, a wizard was started.
In the dropdown box I selected the Java class “Employee”, checked the check box for method getAllEmployees() and clicked on button “Next >”.
After clicking on Next and then Finish (in the screen above) a Java class “EmployeeTest” was created.
There were several issues found. The needed JUnit library seemed not to be part of the classpath.
A right-click on Java class “EmployeeTest” followed by Make and then followed by Reformat fixed the problem and the issues disappeared.
After creating the JUnit TestCase class, the Project level pom.xml content was:
<?xml version="1.0" encoding="UTF-8" ?> <project xmlns="http://maven.apache.org/POM/4.0.0"> <modelVersion>4.0.0</modelVersion> <groupId>nl.xyz.services.humanresourceservice</groupId> <artifactId>HumanResourceServiceProject</artifactId> <version>1.0-SNAPSHOT</version> <description>Project for HumanResourceServiceProject</description> <build> <resources> <resource> <directory>${basedir}</directory> <includes> <include>*</include> </includes> </resource> <resource> <directory>src/main/resources/</directory> <includes> <include>*</include> </includes> </resource> </resources> </build> <dependencies> <dependency> <groupId>org.glassfish.jersey.core</groupId> <artifactId>jersey-client</artifactId> <version>2.8</version> <type>jar</type> <scope>compile</scope> </dependency> <dependency> <groupId>org.glassfish.jersey.media</groupId> <artifactId>jersey-media-json-jackson</artifactId> <version>2.8</version> <type>jar</type> <scope>compile</scope> </dependency> <dependency> <groupId>com.oracle.adf.library</groupId> <artifactId>JUnit-4-Runtime</artifactId> <version>12.2.1-0-0</version> <type>pom</type> <scope>provided</scope> </dependency> </dependencies> </project>
Notice that a dependency for JUnit was automatically added to the Project level POM file.
After creating the JUnit TestCase class, the Applications window for the “ServicesApplication” looked like:
Remark:
Of course you can also create a dedicated project in the application for testing. See also the tab “Test Settings” in the Project level Maven POM file overview screen.
Apache Maven – test
After a right-click on the pom.xml | Run Maven | test, the content of Apache Maven – test – Log is:
…
INFO] — maven-surefire-plugin:2.12.4:test (default-test) @ HumanResourceServiceProject —
[INFO] No tests to run.
[INFO] ————————————————————————
[INFO] BUILD SUCCESS
[INFO] ————————————————————————
[INFO] Total time: 1.160 s
[INFO] Finished at: 2018-02-08T12:10:12-08:00
[INFO] Final Memory: 9M/191M
[INFO] ————————————————————————
Process exited with exit code 0.
There were no test classes found! This is because the Java class “EmployeeTest” is not in the correct default maven structure (maven standard directory layout).
To fix this, the JUnit TestCase class has to be in the right subdirectory under the src/test/java (Test sources) directory. This can be done in several ways, but I used File | Save As… .
In the Save As screen, via repeated use of the icon “Create new subdirectory”, I created the following subdirectory: src/test/java/nl/xyz/services/humanresourceservice and then clicked on button “Save”.
In the “Save Warning” pop-up I clicked on button “Yes”.
In the “Add to Project Content” pop-up, I selected “Java Paths” and clicked on button “OK”.
Then the Java class “EmployeeTest” was deleted, via right-click | Delete.
In the “Confirm Delete” pop-up, I clicked on button “OK”.
After a right-click on the pom.xml | Run Maven | test, the content of Apache Maven – test – Log is:
[INFO] — maven-surefire-plugin:2.12.4:test (default-test) @ HumanResourceServiceProject —
[INFO] Surefire report directory: /u02/oracle/developer/mywork/ServicesApplication/HumanResourceServiceProject/target/surefire-reports
——————————————————-
T E S T S
——————————————————-
Running nl.xyz.services.humanresourceservice.EmployeeTest
Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 0.013 sec <<< FAILURE!
nl.xyz.services.humanresourceservice.EmployeeTest.testGetAllEmployees() Time elapsed: 0.008 sec <<< FAILURE!
java.lang.AssertionError: Unimplemented
at org.junit.Assert.fail(Assert.java:88)
at nl.xyz.services.humanresourceservice.EmployeeTest.testGetAllEmployees(EmployeeTest.java:15)
Results :
Failed tests: nl.xyz.services.humanresourceservice.EmployeeTest.testGetAllEmployees(): Unimplemented
Tests run: 1, Failures: 1, Errors: 0, Skipped: 0
[INFO] ————————————————————————
[INFO] BUILD FAILURE
[INFO] ————————————————————————
[INFO] Total time: 2.473 s
[INFO] Finished at: 2018-02-08T12:42:57-08:00
[INFO] Final Memory: 15M/200M
[INFO] ————————————————————————
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12.4:test (default-test) on project HumanResourceServiceProject: There are test failures.
[ERROR]
[ERROR] Please refer to /u02/oracle/developer/mywork/ServicesApplication/HumanResourceServiceProject/target/surefire-reports for the individual test results.
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
Process exited with exit code 1.
There was a test class found, but it failed. This was an expected outcome, because of the org.junit.Assert.fail(String message) method, which fails a test with the given message.
[https://junit.org/junit4/javadoc/4.12/org/junit/Assert.html#fail(java.lang.String)]
package nl.xyz.services.humanresourceservice; import static org.junit.Assert.*; import org.junit.Test; public class EmployeeTest { public EmployeeTest() { } /** * @see Employee#getAllEmployees() */ @Test public void testGetAllEmployees() { fail("Unimplemented"); } }
Making Java class “EmployeeTest” visible in the Applications window
In order to make the Java class “EmployeeTest” visible in the Applications window for the “ServicesApplication”, the Project level POM file had to be modified.
I added a resource directory in the Project level Maven POM file overview screen, tab “Source Paths”, part “Resources | Resource Directories” (by using the icon +):
Where I selected the src/test/java subdirectory.
After adding the resource directory, the Project level pom.xml content is:
<?xml version="1.0" encoding="UTF-8" ?> <project xmlns="http://maven.apache.org/POM/4.0.0"> <modelVersion>4.0.0</modelVersion> <groupId>nl.xyz.services.humanresourceservice</groupId> <artifactId>HumanResourceServiceProject</artifactId> <version>1.0-SNAPSHOT</version> <description>Project for HumanResourceServiceProject</description> <build> <resources> <resource> <directory>${basedir}</directory> <includes> <include>*</include> </includes> </resource> <resource> <directory>src/main/resources/</directory> <includes> <include>*</include> </includes> </resource> <resource> <directory>src/test/java/</directory> </resource> </resources> </build> <dependencies> <dependency> <groupId>org.glassfish.jersey.core</groupId> <artifactId>jersey-client</artifactId> <version>2.8</version> <type>jar</type> <scope>compile</scope> </dependency> <dependency> <groupId>org.glassfish.jersey.media</groupId> <artifactId>jersey-media-json-jackson</artifactId> <version>2.8</version> <type>jar</type> <scope>compile</scope> </dependency> <dependency> <groupId>com.oracle.adf.library</groupId> <artifactId>JUnit-4-Runtime</artifactId> <version>12.2.1-0-0</version> <type>pom</type> <scope>provided</scope> </dependency> </dependencies> </project>
After adding the resource directory, the Applications window for the “ServicesApplication” looked like:
Modifying Java class “EmployeeTest”
Then I modified the Java class “EmployeeTest” in order the make it call the Java class “Employee”:
package nl.xyz.services.humanresourceservice; import javax.ws.rs.core.Response; import static org.junit.Assert.*; import org.junit.Test; public class EmployeeTest { public EmployeeTest() { } private Employee employee = new Employee(); /** * @see Employee#getAllEmployees() */ @Test public void testGetAllEmployees() { Response response = employee.getAllEmployees(); } }
After a right-click on the pom.xml | Run Maven | test, the content of Apache Maven – test – Log:
(Remark: I shortened the response list of employees)
——————————————————-
T E S T S
——————————————————-
Running nl.xyz.services.humanresourceservice.EmployeeTest
status: 200
headers: {Server=[Cowboy], Access-Control-Allow-Origin=[*], Access-Control-Allow-Methods=[OPTIONS,GET,HEAD,POST,PUT,DELETE,TRACE,CONNECT], Connection=[keep-alive], X-Apiary-Ratelimit-Limit=[120], Date=[Thu, 08 Feb 2018 21:29:12 GMT], Via=[1.1 vegur], X-Apiary-Transaction-Id=[5a7cc1282efcd107002bce05], Content-Length=[15519], Access-Control-Max-Age=[10], X-Apiary-Ratelimit-Remaining=[119], Content-Type=[application/json]}
body:{
“items”: [
{
“employee_id”: 100,
“first_name”: “Steven”,
“last_name”: “King”,
“email”: “SKING”,
“phone_number”: “515.123.4567”,
“hire_date”: “1987-06-17T04:00:00Z”,
“job_id”: “AD_PRES”,
“salary”: 24000,
“commission_pct”: null,
“manager_id”: null,
“department_id”: 90,
“links”: [
{
“rel”: “self”,
“href”: “http://localhost:9090/ords/hr/employees/100”
}
]
},
{
“employee_id”: 101,
“first_name”: “Neena”,
“last_name”: “Kochhar”,
“email”: “NKOCHHAR”,
“phone_number”: “515.123.4568”,
“hire_date”: “1989-09-21T04:00:00Z”,
“job_id”: “AD_VP”,
“salary”: 17000,
“commission_pct”: null,
“manager_id”: 100,
“department_id”: 90,
“links”: [
{
“rel”: “self”,
“href”: “http://localhost:9090/ords/hr/employees/101”
}
]
},
…
{
“employee_id”: 123,
“first_name”: “Shanta”,
“last_name”: “Vollman”,
“email”: “SVOLLMAN”,
“phone_number”: “650.123.4234”,
“hire_date”: “1997-10-10T04:00:00Z”,
“job_id”: “ST_MAN”,
“salary”: 6500,
“commission_pct”: null,
“manager_id”: 100,
“department_id”: 50,
“links”: [
{
“rel”: “self”,
“href”: “http://localhost:9090/ords/hr/employees/123”
}
]
},
{
“employee_id”: 124,
“first_name”: “Kevin”,
“last_name”: “Mourgos”,
“email”: “KMOURGOS”,
“phone_number”: “650.123.5234”,
“hire_date”: “1999-11-16T05:00:00Z”,
“job_id”: “ST_MAN”,
“salary”: 5800,
“commission_pct”: null,
“manager_id”: 100,
“department_id”: 50,
“links”: [
{
“rel”: “self”,
“href”: “http://localhost:9090/ords/hr/employees/124”
}
]
}
],
“hasMore”: true,
“limit”: 25,
“offset”: 0,
“count”: 25,
“links”: [
{
“rel”: “self”,
“href”: “http://localhost:9090/ords/hr/employees/”
},
{
“rel”: “edit”,
“href”: “http://localhost:9090/ords/hr/employees/”
},
{
“rel”: “describedby”,
“href”: “http://localhost:9090/ords/hr/metadata-catalog/employees/”
},
{
“rel”: “first”,
“href”: “http://localhost:9090/ords/hr/employees/”
},
{
“rel”: “next”,
“href”: “http://localhost:9090/ords/hr/employees/?offset=25”
}
]
}
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 2.147 sec
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] ————————————————————————
[INFO] BUILD SUCCESS
[INFO] ————————————————————————
[INFO] Total time: 4.570 s
[INFO] Finished at: 2018-02-08T13:33:14-08:00
[INFO] Final Memory: 16M/246M
[INFO] ————————————————————————
Process exited with exit code 0.
Looking at this output it was obvious that interacting with the Oracle Apiary MockServer action “Get all employees” from Java class “Employee” via the Java class “EmployeeTest” worked correctly.
API Inspector
I then started Oracle Apiary, via: https://apiary.io/ , where I signed in and looked at the API Inspector.
Each request and response from the Mock Server is logged in the API Inspector, which can be found by clicking “Inspector” in the Apiary header. There you will see each request received, each response given, and any validation errors that were found.
[https://help.apiary.io/tools/api-inspector/]
There I could see, the request that was made from the Java class “EmployeeTest”:
For that request (by clicking on it) more details are available:
Completing the HumanResourceServiceProject
So now a successful call to the Oracle Apiary MockServer action “Get all employees” was made, from Java class “Employee” via the Java class “EmployeeTest”.
There were other actions that had to be implemented, and also in the Java class “Department”. So, in the same way as Java class “EmployeeTest”, I created Java class “Department Test”.
After adding the Java class, the Applications window for the “ServicesApplication” looks like:
Below you can find the code for the Java classes.
Remark:
All the Java code in this article is not suitable for a production environment, but meant as simple examples.
Java class “Employee”:
package nl.xyz.services.humanresourceservice; import javax.ws.rs.client.Client; import javax.ws.rs.client.ClientBuilder; import javax.ws.rs.client.Entity; import javax.ws.rs.core.Response; import javax.ws.rs.core.MediaType; public class Employee { private String mockServerURI; public Employee(String mockServerURI) { this.mockServerURI = mockServerURI; } public Response getAllEmployees() { Client client = ClientBuilder.newClient(); String requestURI = this.mockServerURI + "/employees"; System.out.println("requestURI: " + requestURI); Response response = client.target(requestURI).request(MediaType.TEXT_PLAIN_TYPE).get(); System.out.println("status: " + response.getStatus()); System.out.println("headers: " + response.getHeaders()); System.out.println("body:" + response.readEntity(String.class)); return response; } public Response getEmployee(int id) { Client client = ClientBuilder.newClient(); String requestURI = this.mockServerURI + "/employees/" + id; System.out.println("requestURI: " + requestURI); Response response = client.target(requestURI).request(MediaType.TEXT_PLAIN_TYPE).get(); System.out.println("status: " + response.getStatus()); System.out.println("headers: " + response.getHeaders()); System.out.println("body:" + response.readEntity(String.class)); return response; } public Response createEmployee() { Client client = ClientBuilder.newClient(); String requestURI = this.mockServerURI + "/employees"; System.out.println("requestURI: " + requestURI); Entity payload = Entity.json("{ 'LAST_NAME': 'TESTINSERT', 'FIRST_NAME': 'TESTFIRST', 'EMAIL': 'TESTMAIL', 'HIRE_DATE': '25-JUN-15', 'JOB_ID': 'IT_PROG', 'SALARY': 6000, 'MANAGER_ID': 103, 'DEPARTMENT_ID': 60}"); Response response = client.target(requestURI).request(MediaType.APPLICATION_JSON_TYPE).post(payload); System.out.println("status: " + response.getStatus()); System.out.println("headers: " + response.getHeaders()); System.out.println("body:" + response.readEntity(String.class)); return response; } public Response updateEmployee(int id) { Client client = ClientBuilder.newClient(); String requestURI = this.mockServerURI + "/employees/" + id; System.out.println("requestURI: " + requestURI); Entity payload = Entity.json("{ 'LAST_NAME': 'TESTUPDATE', 'JOB_ID': 'SA_REP', 'SALARY': 8000, 'DEPARTMENT_ID': 80}"); Response response = client.target(requestURI).request(MediaType.APPLICATION_JSON_TYPE).put(payload); System.out.println("status: " + response.getStatus()); System.out.println("headers: " + response.getHeaders()); System.out.println("body:" + response.readEntity(String.class)); return response; } }
Java class “EmployeeTest”:
package nl.xyz.services.humanresourceservice; import javax.ws.rs.core.Response; import static org.junit.Assert.*; import org.junit.Test; public class EmployeeTest { private static final String HTTP_STATUS_CODE = "HTTP status code"; private static final int HTTP_STATUS_CODE_OK = 200; private static final int HTTP_STATUS_CODE_CREATED = 201; private Employee employee = new Employee("https://private-b4874b1-humanresourceservice.apiary-mock.com"); public EmployeeTest() { } /** * @see Employee#getAllEmployees() */ @Test public void testGetAllEmployees() { Response response = employee.getAllEmployees(); assertEquals(HTTP_STATUS_CODE, HTTP_STATUS_CODE_OK, response.getStatus()); } /** * @see Employee#getEmployee() */ @Test public void testGetEmployee() { int id = 100; Response response = employee.getEmployee(id); assertEquals(HTTP_STATUS_CODE, HTTP_STATUS_CODE_OK, response.getStatus()); } /** * @see Employee#createEmployee() */ @Test public void testCreateEmployee() { Response response = employee.createEmployee(); assertEquals(HTTP_STATUS_CODE, HTTP_STATUS_CODE_CREATED, response.getStatus()); } /** * @see Employee#updateEmployee() */ @Test public void testUpdateEmployee() { int id = 220; Response response = employee.updateEmployee(id); assertEquals(HTTP_STATUS_CODE, HTTP_STATUS_CODE_OK, response.getStatus()); } }
Java class “Department”:
package nl.xyz.services.humanresourceservice; import javax.ws.rs.client.Client; import javax.ws.rs.client.ClientBuilder; import javax.ws.rs.core.Response; import javax.ws.rs.core.MediaType; public class Department { private String mockServerURI; public Department(String mockServerURI) { this.mockServerURI = mockServerURI; } public Response getDepartment(int id) { Client client = ClientBuilder.newClient(); String requestURI = this.mockServerURI + "/department/" + id; System.out.println("requestURI: " + requestURI); Response response = client.target(requestURI).request(MediaType.TEXT_PLAIN_TYPE).get(); System.out.println("status: " + response.getStatus()); System.out.println("headers: " + response.getHeaders()); System.out.println("body:" + response.readEntity(String.class)); return response; } public Response getDepartmentEmployee(int department_id, int employee_id) { Client client = ClientBuilder.newClient(); String requestURI = this.mockServerURI + "/departments/" + department_id + "/employees/" + employee_id; System.out.println("requestURI: " + requestURI); Response response = client.target(requestURI).request(MediaType.TEXT_PLAIN_TYPE).get(); System.out.println("status: " + response.getStatus()); System.out.println("headers: " + response.getHeaders()); System.out.println("body:" + response.readEntity(String.class)); return response; } }
Java class “DepartmentTest”:
package nl.xyz.services.humanresourceservice; import javax.ws.rs.core.Response; import static org.junit.Assert.*; import org.junit.Test; public class DepartmentTest { private static final String HTTP_STATUS_CODE = "HTTP status code"; private static final int HTTP_STATUS_CODE_OK = 200; private Department department = new Department("https://private-b4874b1-humanresourceservice.apiary-mock.com"); public DepartmentTest() { } /** * @see Department#getDepartment(int) */ @Test public void testGetDepartment() { int id = 30; Response response = department.getDepartment(id); assertEquals(HTTP_STATUS_CODE, HTTP_STATUS_CODE_OK, response.getStatus()); } /** * @see Department#getDepartmentEmployee(int,int) */ @Test public void testGetDepartmentEmployee() { int department_id = 30; int employee_id = 119; Response response = department.getDepartmentEmployee(department_id, employee_id); assertEquals(HTTP_STATUS_CODE, HTTP_STATUS_CODE_OK, response.getStatus()); } }
After a right-click on the pom.xml | Run Maven | test, the content of Apache Maven – test – Log:
(Remark: I shortened the response list of employees)
…
[INFO] — maven-surefire-plugin:2.12.4:test (default-test) @ HumanResourceServiceProject —
[INFO] Surefire report directory: /u02/oracle/developer/mywork/ServicesApplication/HumanResourceServiceProject/target/surefire-reports
——————————————————-
T E S T S
——————————————————-
Running nl.xyz.services.humanresourceservice.DepartmentTest
requestURI: https://private-b4874b1-humanresourceservice.apiary-mock.com/department/30
status: 200
headers: {Server=[Cowboy], Access-Control-Allow-Origin=[*], Access-Control-Allow-Methods=[OPTIONS,GET,HEAD,POST,PUT,DELETE,TRACE,CONNECT], Connection=[keep-alive], X-Apiary-Ratelimit-Limit=[120], Date=[Fri, 09 Feb 2018 20:10:55 GMT], Via=[1.1 vegur], X-Apiary-Transaction-Id=[5a7e004fc5c2ab07003caec1], Content-Length=[112], Access-Control-Max-Age=[10], X-Apiary-Ratelimit-Remaining=[119], Content-Type=[application/json]}
body:{
“department_id”: 30,
“department_name”: “Purchasing”,
“manager_id”: 114,
“location_id”: 1700
}
requestURI: https://private-b4874b1-humanresourceservice.apiary-mock.com/departments/30/employees/119
status: 200
headers: {Server=[Cowboy], Access-Control-Allow-Origin=[*], Access-Control-Allow-Methods=[OPTIONS,GET,HEAD,POST,PUT,DELETE,TRACE,CONNECT], Connection=[keep-alive], X-Apiary-Ratelimit-Limit=[120], Date=[Fri, 09 Feb 2018 20:10:56 GMT], Via=[1.1 vegur], X-Apiary-Transaction-Id=[5a7e005099b117070044215c], Content-Length=[146], Access-Control-Max-Age=[10], X-Apiary-Ratelimit-Remaining=[118], Content-Type=[application/json]}
body:{
“department_id”: 30,
“department_name”: “Purchasing”,
“employee_id”: 119,
“first_name”: “Karen”,
“last_name”: “Colmenares”
}
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 2.909 sec
Running nl.xyz.services.humanresourceservice.EmployeeTest
requestURI: https://private-b4874b1-humanresourceservice.apiary-mock.com/employees
status: 201
headers: {Server=[Cowboy], Access-Control-Allow-Origin=[*], Access-Control-Allow-Methods=[OPTIONS,GET,HEAD,POST,PUT,DELETE,TRACE,CONNECT], Connection=[keep-alive], X-Apiary-Ratelimit-Limit=[120], Date=[Fri, 09 Feb 2018 20:10:57 GMT], Via=[1.1 vegur], X-Apiary-Transaction-Id=[5a7e005179e3cd0700eedbb3], Content-Length=[301], Access-Control-Max-Age=[10], X-Apiary-Ratelimit-Remaining=[117], Content-Type=[application/json]}
body:{
“employee_id”: 220,
“first_name”: “TESTFIRST”,
“last_name”: “TESTINSERT”,
“email”: “TESTMAIL”,
“phone_number”: null,
“hire_date”: “2015-06-25T04:00:00Z”,
“job_id”: “IT_PROG”,
“salary”: 6000,
“commission_pct”: null,
“manager_id”: 103,
“department_id”: 60
}
requestURI: https://private-b4874b1-humanresourceservice.apiary-mock.com/employees/220
status: 200
headers: {Server=[Cowboy], Access-Control-Allow-Origin=[*], Access-Control-Allow-Methods=[OPTIONS,GET,HEAD,POST,PUT,DELETE,TRACE,CONNECT], Connection=[keep-alive], X-Apiary-Ratelimit-Limit=[120], Date=[Fri, 09 Feb 2018 20:10:58 GMT], Via=[1.1 vegur], X-Apiary-Transaction-Id=[5a7e00520fb27c07004e4008], Content-Length=[300], Access-Control-Max-Age=[10], X-Apiary-Ratelimit-Remaining=[116], Content-Type=[application/json]}
body:{
“employee_id”: 220,
“first_name”: “TESTFIRST”,
“last_name”: “TESTUPDATE”,
“email”: “TESTMAIL”,
“phone_number”: null,
“hire_date”: “2015-06-25T04:00:00Z”,
“job_id”: “SA_REP”,
“salary”: 8000,
“commission_pct”: null,
“manager_id”: 103,
“department_id”: 80
}
requestURI: https://private-b4874b1-humanresourceservice.apiary-mock.com/employees/100
status: 200
headers: {Server=[Cowboy], Access-Control-Allow-Origin=[*], Access-Control-Allow-Methods=[OPTIONS,GET,HEAD,POST,PUT,DELETE,TRACE,CONNECT], Connection=[keep-alive], X-Apiary-Ratelimit-Limit=[120], Date=[Fri, 09 Feb 2018 20:10:59 GMT], Via=[1.1 vegur], X-Apiary-Transaction-Id=[5a7e005399b117070044215f], Content-Length=[806], Access-Control-Max-Age=[10], X-Apiary-Ratelimit-Remaining=[115], Content-Type=[application/json]}
body:{
“employee_id”: 100,
“first_name”: “Steven”,
“last_name”: “King”,
“email”: “SKING”,
“phone_number”: “515.123.4567”,
“hire_date”: “1987-06-17T04:00:00Z”,
“job_id”: “AD_PRES”,
“salary”: 24000,
“commission_pct”: null,
“manager_id”: null,
“department_id”: 90,
“links”: [
{
“rel”: “self”,
“href”: “http://localhost:9090/ords/hr/employees/100”
},
{
“rel”: “edit”,
“href”: “http://localhost:9090/ords/hr/employees/100”
},
{
“rel”: “describedby”,
“href”: “http://localhost:9090/ords/hr/metadata-catalog/employees/item”
},
{
“rel”: “collection”,
“href”: “http://localhost:9090/ords/hr/employees/”
}
]
}
requestURI: https://private-b4874b1-humanresourceservice.apiary-mock.com/employees
status: 200
headers: {Server=[Cowboy], Access-Control-Allow-Origin=[*], Access-Control-Allow-Methods=[OPTIONS,GET,HEAD,POST,PUT,DELETE,TRACE,CONNECT], Connection=[keep-alive], X-Apiary-Ratelimit-Limit=[120], Date=[Fri, 09 Feb 2018 20:11:00 GMT], Via=[1.1 vegur], X-Apiary-Transaction-Id=[5a7e00540fb27c07004e400b], Content-Length=[15519], Access-Control-Max-Age=[10], X-Apiary-Ratelimit-Remaining=[119], Content-Type=[application/json]}
body:{
“items”: [
{
“employee_id”: 100,
“first_name”: “Steven”,
“last_name”: “King”,
“email”: “SKING”,
“phone_number”: “515.123.4567”,
“hire_date”: “1987-06-17T04:00:00Z”,
“job_id”: “AD_PRES”,
“salary”: 24000,
“commission_pct”: null,
“manager_id”: null,
“department_id”: 90,
“links”: [
{
“rel”: “self”,
“href”: “http://localhost:9090/ords/hr/employees/100”
}
]
},
{
“employee_id”: 101,
“first_name”: “Neena”,
“last_name”: “Kochhar”,
“email”: “NKOCHHAR”,
“phone_number”: “515.123.4568”,
“hire_date”: “1989-09-21T04:00:00Z”,
“job_id”: “AD_VP”,
“salary”: 17000,
“commission_pct”: null,
“manager_id”: 100,
“department_id”: 90,
“links”: [
{
“rel”: “self”,
“href”: “http://localhost:9090/ords/hr/employees/101”
}
]
},
…
{
“employee_id”: 123,
“first_name”: “Shanta”,
“last_name”: “Vollman”,
“email”: “SVOLLMAN”,
“phone_number”: “650.123.4234”,
“hire_date”: “1997-10-10T04:00:00Z”,
“job_id”: “ST_MAN”,
“salary”: 6500,
“commission_pct”: null,
“manager_id”: 100,
“department_id”: 50,
“links”: [
{
“rel”: “self”,
“href”: “http://localhost:9090/ords/hr/employees/123”
}
]
},
{
“employee_id”: 124,
“first_name”: “Kevin”,
“last_name”: “Mourgos”,
“email”: “KMOURGOS”,
“phone_number”: “650.123.5234”,
“hire_date”: “1999-11-16T05:00:00Z”,
“job_id”: “ST_MAN”,
“salary”: 5800,
“commission_pct”: null,
“manager_id”: 100,
“department_id”: 50,
“links”: [
{
“rel”: “self”,
“href”: “http://localhost:9090/ords/hr/employees/124”
}
]
}
],
“hasMore”: true,
“limit”: 25,
“offset”: 0,
“count”: 25,
“links”: [
{
“rel”: “self”,
“href”: “http://localhost:9090/ords/hr/employees/”
},
{
“rel”: “edit”,
“href”: “http://localhost:9090/ords/hr/employees/”
},
{
“rel”: “describedby”,
“href”: “http://localhost:9090/ords/hr/metadata-catalog/employees/”
},
{
“rel”: “first”,
“href”: “http://localhost:9090/ords/hr/employees/”
},
{
“rel”: “next”,
“href”: “http://localhost:9090/ords/hr/employees/?offset=25”
}
]
}
Tests run: 4, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 3.594 sec
Results :
Tests run: 6, Failures: 0, Errors: 0, Skipped: 0
[INFO] ————————————————————————
[INFO] BUILD SUCCESS
[INFO] ————————————————————————
[INFO] Total time: 8.834 s
[INFO] Finished at: 2018-02-09T12:15:04-08:00
[INFO] Final Memory: 15M/198M
[INFO] ————————————————————————
Process exited with exit code 0.
In the Oracle Apiary API Inspector, I could see, the 6 request that were made in total from the Java classes “EmployeeTest” and “DepartementTest” during the last Maven test run:
Apache Maven – clean
After a right-click on the pom.xml | Run Maven | clean, the content of Apache Maven – clean – Log is:
INFO] — maven-clean-plugin:2.5:clean (default-clean) @ HumanResourceServiceProject —
[INFO] Deleting /u02/oracle/developer/mywork/ServicesApplication/HumanResourceServiceProject/target
[INFO] ————————————————————————
[INFO] BUILD SUCCESS
[INFO] ————————————————————————
[INFO] Total time: 0.316 s
[INFO] Finished at: 2018-02-09T07:51:02-08:00
[INFO] Final Memory: 8M/151M
[INFO] ————————————————————————
Process exited with exit code 0.
This results in subdirectory target being deleted from the file system.
Apache Maven – package
——————————————————-
T E S T S
…
Results :
Tests run: 6, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] — maven-jar-plugin:2.4:jar (default-jar) @ HumanResourceServiceProject —
[INFO] Building jar: /u02/oracle/developer/mywork/ServicesApplication/HumanResourceServiceProject/target/HumanResourceServiceProject-1.0-SNAPSHOT.jar
[INFO] ————————————————————————
[INFO] BUILD SUCCESS
[INFO] ————————————————————————
[INFO] Total time: 8.963 s
[INFO] Finished at: 2018-02-09T12:36:23-08:00
[INFO] Final Memory: 17M/201M
[INFO] ————————————————————————
Process exited with exit code 0.
In the log above, you can see that after compiling code and running the tests, the following jar file is made:
/u02/oracle/developer/mywork/ServicesApplication/HumanResourceServiceProject/target/HumanResourceServiceProject-1.0-SNAPSHOT.jar
Summary
As a follow up from my previous article, in this article the focus is on calling the Oracle Apiary Mock Server from Java code.
One of the examples for the “HumanResourceService” API is about the action “Get all employees” in combination with “Java” as language. With that example, also the dependencies to be used in a Maven pom.xml (java6+) are given. This example triggered me to try out the Oracle JDeveloper, Maven and Java combination.
This article shows you how, with the help of Oracle JDeveloper 12.2.1 in combination with JUnit and Maven, a successful call to the Oracle Apiary MockServer action “Get all employees” from a Java class was made, and also to other actions for “Employees” and “Departments”.