Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 
geert-janklaps
Active Contributor

Up until now, we’ve already covered creating a project through the lobby, we’ve added a basic data model and service to our project including some local testing data. We’ve seen that SAP Joule takes our data model into consideration when generating sample data, as the sum of our item prices is reflected in our sales order header entity as a total amount.

In this blog post we’re going to extend our existing project with application logic to automatically calculate the total amount based on the item prices. And since application logic needs to be tested, we’ll also create a unit test to automatically test our new application logic.

Adding application logic to our project

Fire up the SAP Joule plugin again and choose the “Application Logic” step to get started. Adding application logic takes a few steps, as a first step we’ll need to open up the CDS Graphical Modeler and click the entity we’d like to extend with application logic. Once you’ve selected the entity you’ll notice a small menu appears next to the entity with some options. In this case be sure to click the “Add Logic” button as shown below.

geertjanklaps_12-1711462247570.png

In the “add application logic” screen you’ll need to provide some details about the logic you’d like to implement. In this case we’re aiming to calculate the total value of the order when creating / updating an order, which means we’re going to apply our logic to the service entity. The service entity should already be preselected for you, if not please manually select the SalesOrders entity.

Next to that you’ll need to provide a name for the logic you’d like to implement, in this case I’ve used “calculateTotalAmount”. You can also add an optional description to the logic. Creating the empty application logic handler is just a matter of clicking the “Add” button as shown below.

geertjanklaps_13-1711462274713.png

As a next step define when the application logic should be executed, in this case we’ll use the “on” phase of the CAP handlers and choose the “UPDATE” and “CREATE” events to make sure our total amount is calculated on each update or create of a sales order.

Once you’ve selected the phase and events, open up the code editor with the “Open Code Editor” button, so we can use SAP Joule to generate some logic for us.

geertjanklaps_14-1711462298097.png

After opening the application logic code editor, open SAP Joule through the “Implement Application Logic” step and provide the requirement so SAP Joule can create a proposal implementation. In this case I used the following requirement: “Calculate the total amount based on the item total price”.

If you’re happy with the proposed implementation, accept it through the button in the top right corner of the proposal to add it to the application logic.

geertjanklaps_15-1711462325458.png

We’ve now successfully implemented our application logic to calculate the total amount of the order. In the next step we’ll create a unit test, to automatically validate the implementation during e.g. our build / deployment pipelines.

Adding unit tests to our project

To add a unit test to our project, we have to follow a similar approach as we did to add application logic. In the application logic screen, select the previously created application logic and choose “Open Code Editor”. Now choose the option “Unit test” instead of “Application Logic”.

geertjanklaps_16-1711462371663.png

Now you can use SAP Joule again to generate the unit test. In this case I’ve used following requirement: “Generate a unit test to validate if the total amount is equal to the sum of the total price of the order items”.

After SAP Joule generates a proposal, you’ll even see that you get an explanation of what the test should do below the proposal. If you’re fine with the proposal, you can accept the proposal again using the button in the top right of the proposal to add it to the unit test logic.

geertjanklaps_17-1711462395188.png

We can now test our application through the unit test, by opening a terminal (menu -> terminal -> new terminal) and executing command: “npm run test”. Below I’ve highlighted each step in the unit test and the corresponding result in the terminal.

geertjanklaps_18-1711462415039.png

Running and previewing our backing service

To run the application, simply open the run configurations menu and open the generated run configuration. By clicking the run button, the application will be started with the test data we created earlier.

geertjanklaps_19-1711462448512.png

When you open the running application, you’ll be greeted by a new beautiful overview screen listing both UI applications and backend services. In this case we only have a backend service for now. To preview the service you have two options, with the table view you’ll get a Fiori Elements (list report) preview with the code preview you’ll just see the resulting JSON data.

geertjanklaps_20-1711462468126.png

When opening up the table preview, you’ll notice that when SAP Joule generated our data model it already included the necessary list report annotations and labels.

geertjanklaps_21-1711462489854.png

geertjanklaps_22-1711462519002.png

Opening up the code preview just gives a JSON response with our test data:

geertjanklaps_23-1711462534357.png

We’ve now successfully created a fully functional backend service with application logic, corresponding unit test and are able to run the application in preview mode. In the next blog we’ll see how we can add a SAP Fiori Elements template and deploy it to SAP Business Technology Platform.

Blog series navigation

Labels in this area