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.
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.
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.
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.
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.
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.
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”.
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.
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.
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.
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.
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.
Opening up the code preview just gives a JSON response with our test data:
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
10 | |
10 | |
7 | |
7 | |
6 | |
6 | |
6 | |
5 | |
4 | |
3 |