Please disable your adblock and script blockers to view this page

Search this blog

Showing posts with label ADF BC. Show all posts
Showing posts with label ADF BC. Show all posts

Thursday 18 April 2019

Populate Oracle JET table on top of Oracle ADF BC using REST Web Service

 Previously I have posted about creating REST Web Service from ADF Application Module and in this post, I am going to use the same REST service for populating data in a JET table. Here we'll populate JET table on top of Oracle ADF BC (Business Components).

Here I am taking the same sample application that I have discussed in my first post Getting started with Oracle JET and JDeveloper

The URL of ADF BC based web service is http://127.0.0.1:7101/RestWebServApp-RESTWebService-context-root/rest/Jdev12.2.1/Department and it returns a list of Departments in nested JSON format



Let’s see the implementation part

Add an oj-table component in the dashboard.html page and define the required number of columns, this web service returns a list of Departments so I have taken 4 columns for Department Id, Department Name, Manager Id, and Location Id

  1. <div class="oj-hybrid-padding">
  2. <h1>Dashboard Content Area</h1>
  3. <div id="div1">
  4. <oj-table id='table' aria-label='Departments Table'
  5. data='[[datasource]]'
  6. selection-mode='{"row": "multiple", "column": "multiple"}'
  7. dnd='{"reorder": {"columns": "enabled"}}'
  8. scroll-policy='loadMoreOnScroll'
  9. scroll-policy-options='{"fetchSize": 10}'
  10. columns='[{"headerText": "Department Id",
  11. "field": "dept_id",
  12. "resizable": "enabled"},
  13. {"headerText": "Department Name",
  14. "field": "dept_name",
  15. "resizable": "enabled"},
  16. {"headerText": "Manager Id",
  17. "field": "mgr_id",
  18. "resizable": "enabled"},
  19. {"headerText": "Location Id",
  20. "field": "loc_id",
  21. "resizable": "enabled"}
  22. ]'
  23. style='width: 100%; height: 400px;'>
  24. </oj-table>
  25. </div>
  26. </div>

Now see the code of dashboard.js, Read comments to understand the javascript code, Here getJSON() method of jQuery is used to call REST Web Service as jQuery is a part of Oracle JET libraries.

  1. /*
  2. * Your dashboard ViewModel code goes here
  3. */
  4. define(['ojs/ojcore', 'knockout', 'jquery', 'ojs/ojinputtext', 'ojs/ojtable', 'ojs/ojarraytabledatasource'],
  5. function (oj, ko, $) {
  6. function DashboardViewModel() {
  7. var self = this;
  8. //Define a Knockout observable array and name it tabData
  9. self.tabData = ko.observableArray();
  10. //Use jQuery method to call REST Web Service
  11. $.getJSON("http://127.0.0.1:7101/RestWebServApp-RESTWebService-context-root/rest/Jdev12.2.1/Department").
  12. then(function (departments) {
  13. //JSON response is nested that's why 'items' is used to access records
  14. $.each(departments.items, function () {
  15. //Push data in Observable array
  16. self.tabData.push( {
  17. dept_id : this.DepartmentId,
  18. dept_name : this.DepartmentName,
  19. mgr_id : this.ManagerId,
  20. loc_id : this.LocationId
  21. });
  22. });
  23. });
  24. //Pass observable array in utility class to show data in table
  25. self.datasource = new oj.ArrayTableDataSource(self.tabData,
  26. {
  27. idAttribute : 'dept_id'
  28. });
  29. .
  30. .
  31. .

tabData is a knockout observable array that is defined to hold returned value and you can see that
self.tabData.push is used to push web service data into this array and at last a JET utility class
ArrayTableDataSource is used to convert this observable array into tabular format data.

Now run the application and you can see that a JET table is populated with Departments data



Cheers 🙂 Happy Learning

Monday 21 January 2019

Unit testing of ADF Application using JUnit

 JUnit is a unit testing package for Java language and can be used to test Oracle ADF applications as ADF is built on top of the J2EE framework. Unit testing is basically is a process to verify the smallest testable module against some defined test criteria. Here I am going to illustrate how can we set up and use JUnit in JDeveloper 12.2.1.3 to test the ADF application.

JDeveloper 12.2.1.3 comes with a JUnit extension so no need to install it separately. Let's start by creating a Fusion Web Application in JDeveloper IDE. Here I am taking Departments table of default HR Schema to prepare the model for ADF Application.




The next step is to create a new project to hold unit tests so that the whole application doesn't look ambiguous. Right-click on the Application name and select New–> From Gallery–> General–> Projects --> Java Project 



Put a name for that project and click on the Finish button.



Now next step is to create a Test Suite for business components and before that, we should know some terminology that is used in unit testing.

Test Suite- A group of Test Cases

Test Fixture- A class to handle long-running test cases and keep the state of multiple test cases.

Assertation- To check the result of a test case against the expected result.

Now to open the test suite wizard, Right-click on new project and select New–> From Gallery–> General–> Unit Tests --> ADF Business Components Test Suite



Click on the OK button and configure the test suite. You can see that here I have selected the Model project and DeptAm application module to test. You need to select Configuration for database connection too and here I have selected DeptAMLocal.



Click on the Next button and see that this wizard will generate a Test Suite class and a Test Fixture class. This wizard will also generate separate unit test classes for each view object in the application.



Now click on Finish button and you can under new project all files are created.

DeptAmFixture.java- Test Fixture Class

AllDeptAMTests.java- Test Suite Class

DepartmentsVO1VOTest.java- Unit Test Class for Departments ViewObject



Now open DepartmentsVO1VOTest.java class and look at the default test case that checks that the Department View Object should not be null.

You can see here @Test annotation, this indicates that this java method is a unit test and after performing test assert is used to verify the test result.

  1. @Test
  2. public void testAccess() {
  3. ViewObject view = fixture1.getApplicationModule().findViewObject("DepartmentsVO1");
  4. assertNotNull(view);
  5. }
  6. @Before
  7. public void setUp() {
  8. }
  9. @After
  10. public void tearDown() {
  11. }

To check this default test case, Right-click on the test suite class and select run. You can see that the unit test execute successfully.



The next step is to create some of the own unit tests, I have created this unit test that checks that the Department Id should not be null in a newly created row.

  1. @Test
  2. public void checkDeptIdNotNull() {
  3. ViewObject deptVo = fixture1.getApplicationModule().findViewObject("DepartmentsVO1");
  4. Row newRow = deptVo.createRow();
  5. newRow.setAttribute("DepartmentId", 222);
  6. newRow.setAttribute("DepartmentName", "Testing");
  7. assertNotNull("DepartmentId should not be null", newRow.getAttribute("DepartmentId"));
  8. }

So in the above code, I have created a new row in Department's view object and set 222 in Department Id. Now run this test case.



You can see here that the test is passed successfully because Department Id is not null, That's great. Now comment this line in the code

//newRow.setAttribute("DepartmentId", 222);

and run the test again



See that test is failed with AssertionError as Department Id is null this time. This is how we can write our own unit tests to check.

Here I am writing one more test case to check whether the department is in the database or not. I am passing 1990 as department id that is not in the database.

  1. @Test
  2. public void findDepartment() {
  3. ViewObject deptVo = fixture1.getApplicationModule().findViewObject("DepartmentsVO1");
  4. int deptId = 1990;
  5. Row row[] = deptVo.findByKey(new Key(new Object[] { deptId }), 1);
  6. Integer count = row.length;
  7. //assertTrue fails when second parameter evaluates to "false"
  8. assertTrue("Department Not Found", count.compareTo(0) == 1);
  9. }

Let's see the result



This is how we configure and use JUnit in the Oracle ADF Application for Unit Testing.

Cheers 🙂 Happy Learning