Introduction

This is in continuation to the earlier post wherein I explained how can we deploye a custom Micronaut app written in Groovy to SAP Cloud Foundry BTP. Please read the earlier post here.

In this part I will explain how we can use SAP HANA cloud as a persistence layer for the application. and how we can perform unit testing of the app from local workstation.

Objective

  • Use SAP HANA cloud as persistence layer.
  • Ability to perform unit testing in local IDE.

 

Pre-Requisite

  • A HANA cloud instance is running in BTP.

There should be 2 instances running in BTP for the HANA cloud database.

How to perform unit testing?

There are basically 2 ways of performing unit testing of this app before deploying in BTP. The first approach requires that we whitelist certain IP which will be allowed to access the HANA database remotely. For this I can set this configuration during setup of the database itself.

Then I need to bind the database with the SFLIGHT app so that the keys are generated. Looking closely at the service key, we have the URL, Username, password already availble. We can use these credentials to perform CRUDQ operations on the table remotely. However, it is a very bad practise to hardcode these credentials in the application itself. Use this option at your discretion.

The second one is a more elegant and preferred option which implements seamless unit testing and deployment.

I will use 2 database, H2 for local testing and HANA for production deployment. I can configure this using many ways in Micronaut. Micronaut supports providing these application properties in multiple places:

  • In CMD line while invoking app locally.
  • In application itself.
  • In separate environment  – application.yml file.

I will be using option 2 as it is the easiest one. I have configured the different databse in the application itself.

package com.sap

import io.micronaut.context.env.PropertySource
import io.micronaut.runtime.Micronaut
import groovy.transform.CompileStatic
import io.pivotal.cfenv.core.CfEnv
import io.pivotal.cfenv.core.CfService

@CompileStatic
class Application {
    static void main(String[] args) {
//        Micronaut.run(Application, args)
        String userName, passWord, url
        CfService cfService = new CfEnv().findServiceByLabel('hana')
        Micronaut micronaut = Micronaut.build(args)
        if (cfService != null) {
            passWord = cfService.getCredentials().getPassword()
            userName = cfService.getCredentials().getUsername()
            url = cfService.getCredentials().getMap().get('url')
            micronaut.propertySources(PropertySource.of(['datasources.default.url'            : url,
                                                         'datasources.default.driverClassName': 'com.sap.db.jdbc.Driver',
                                                         'datasources.default.username'       : userName,
                                                         'datasources.default.password'       : passWord,
                                                         'datasources.default.schema-generate': 'CREATE_DROP'] as Map<String, Object>)).start()
        } else {
            micronaut.propertySources(PropertySource.of(['datasources.default.url'            : 'jdbc:h2:mem:devDb;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE',
                                                         'datasources.default.driverClassName': 'org.h2.Driver',
                                                         'datasources.default.username'       : 'sa',
                                                         'datasources.default.password'       : '',
                                                         'datasources.default.schema-generate': 'CREATE_DROP',
                                                         'datasources.default.dialect'        : 'H2'] as Map<String, Object>)).start()
        }
    }
}

If you have a look at the above code, I have no where mentioned the credentials of the HANA cloud database, yet when the app is actually deployed in BTP, it will read all those details from the service key. I dont worry if the local credentials are exposed, as they are only for testing and H2 is an in memory database.

Run the application in BTP and I can see that the SFLIGHT table is generated and is also having entries.

Conclusion

I have deployed a custom Micronaut app written in groovy that is protected by XSUAA, has different roles and uses HANA cloud as the persistence layer.

Happy BTP development.

Github for this boilerplate.

 

Sara Sampaio

Sara Sampaio

Author Since: March 10, 2022

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x