Showing posts with label Spring. Show all posts
Showing posts with label Spring. Show all posts

Thursday, March 5, 2020

Passing spring application parameters in EAP 7.1

In this post I will demonstrate how to pass Spring application properties in Spring Boot spring boot application running on JBoss EAP. I will use code base of my previous post "SpringBott change default port"(Initial code base)

Prerequisites 


  1. You should have install java 1.8 or above.
  2. You should have Eclipse installed in your PC.
  3. Your PC should setup Maven installed and configured.
  4. Your PC should have installed MySQL and Server need to have user who have access to the configured database and user should able to log in to the database by user name/password. 


Enable existing project to run on EAP

When Spring boot application developed it's have its embedded tomcat. So we have to remove that embedded tomcat in order to run the same project in EAP. Also I have make the project packaging as war file so we can deploy the project in to EAP.


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.nirmal.springbootrest</groupId>
  <artifactId>RestApiSpringBoot</artifactId>
  <version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.5.RELEASE</version>
    </parent>
<dependencies>
<!-- Compile -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<!-- Provided -->
  <dependency>
   <groupId>org.apache.tomcat.embed</groupId>
   <artifactId>tomcat-embed-jasper</artifactId>
   <scope>provided</scope>
  </dependency>
<!-- Runtime -->
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>6.1.0.jre8</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

Then lets remove some required parameters from application.properties file as show in below. I have highlighted the commented part.

#==== connect to mysql ======#
spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:sqlserver://localhost;databaseName=TestDB
#spring.datasource.username=nirmal
#spring.datasource.password=Test123_
spring.datasource.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.jpa.database-platform=org.hibernate.dialect.SQLServer2012Dialect
server.port = 8080

spring.jpa.properties.hibernate.show_sql=true
spring.jpa.properties.hibernate.use_sql_comments=false
spring.jpa.properties.hibernate.format_sql=true

#==== Logging configurations ======#
logging.level.root=WARN,INFO,ERROR
logging.level.com.baeldung=TRACE


Perform maven install

I will demonstrate it from IntelliJ as mention below. First perform maven install command by double click on "install" under maven in InelliJ as show below.




Then after successful installation you can see generated war file in target folder. 


Add application properties in EAP

First lets run the JBoss EAP. For that go to the JBOSS directory and navigate to "bin" folder. Then run the "standalone.bat" in windows. (In linux run the standalone.sh file). Once EAP started you can go to Admin console in following URL. http://localhost:9990/console/App.html

Enter the admin username and password when accessing the URL. Then you should be able to see EAP web application. Then click on Configuration tab and then click on system properties. Then you should be able to see similar UI to below screen. Then click on view button.




Then you should be able to see similar image to below figure. 



In order to add parameters click on Add button. Then add following details 

namevalue
spring.datasource.usernamenirmal
spring.datasource.passwordTest123_


After add those you can see similar to below figure.




Then lets deploy the war file. For that click on Back button on left top corner and then click on deployments. Then click "Add" button. Then you should be able to see similar figure to below.



Then click next button and then Click choose file and select the generated war file from target folder in the project. Then click next and then click finish. Once installation completed you should be able to see similar output on command line.



Now server started on 8090 and you should be able to access same web service from following like.






Also you can perform any action which I mentioned in my previous blog post.

Project source can be downloaded from following GIT HUB URL.



Saturday, February 29, 2020

Spring Boot + Cucumber + Selenium

In this post I will demonstrate how to configure Spring Boot application in to Behavioral testing with Cucumber using selenium.


Prerequisites 
  1. You should have install java 1.8 or above.
  2. You should have Eclipse installed in your PC.
  3. Your PC should setup Maven installed and configured.


Lets create project 

Create maven project with following dependencies. I have highlighted the selenium and cucumber dependencies.


<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.nirmal.springbootcucumber</groupId>
<artifactId>bdd_cucumber</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>bdd_cucumber</name>
<description>BDD tests with Cucumber, Selenium and Spring Boot</description>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.1.RELEASE</version>
<relativePath />
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-server</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-spring</artifactId>
<version>1.2.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-junit</artifactId>
<version>1.2.5</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
            </plugin>
</plugins>
</build>
</project>


For maven installation purposes I will add Spring Boot initializer class as show in below. 


package com.nirmal.springbootcucumber;


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class AppRunner {

public static void main(String[] args) {
SpringApplication.run(AppRunner.class, args);
}
}


Now application and perform maven install command.



Configure cucumber on Tests 

First we have to add test initializer class which do initialization part of test framework. I have created the package com.nirmal.springbootcucumber under test folder.


package com.nirmal.springbootcucumber;

import org.junit.runner.RunWith;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;

@RunWith(Cucumber.class)
@CucumberOptions(features="Features",glue="com.nirmal.springbootcucumber.StepDefinition"})
public class Runner
{

}

Note that in here I user two annotations which have following responibilites

@RunWith(Cucumber.class)
This will specifies this class need consider as test runner. Also we have provided cucumber as parameter, which says this should run as cucumber test


@CucumberOptions(features="Features",glue="com.nirmal.springbootcucumber.StepDefinition"})
This annotation provide basic configuration details related to cucumber
The first parameter, called features, provides the location of the feature file. Similarly, the second parameter, called glue, provides the path of the step definition class.




Create feature file and define cucumber steps on Tests 


Lets create new folder call "Feature" under our project folder and put the feature file that I'm going to demonstrate in this post.



Feature: Google page loading and Searching

Scenario: Google page loading and Searching

Given Open the Firefox and launch the application

When Click on clickable item "//*[@id='tsf']/div[2]/div[1]/div[1]/div/div[2]/input"

Then Enter the value on "//*[@id='tsf']/div[2]/div[1]/div[1]/div/div[2]/input" as "Beautiful Sri Lanka" and press enter

Then click on item with xpath "//*[@id='gb']/div/div[1]/a"

Then Enter the value on "//*[@id='identifierId']" as "validemailid" and press enter

Then Enter the value on "//*[@id='password']/div[1]/div/div[1]/input" as "test" and press enter

Then go back to previous URL

Then go back to previous URL

Then click on item with xpath "//*[@id='logo']/img"


Then lets download the chrome driver for this example. You can latest web driver from following URL. Then extract in to you local folder. I will extracted in in to "C:\driver\chromedriver_win32"

Then lets create the steps for this feature file. For that I'll create new class class com.nirmal.springbootcucumber.StepDefinition.Steps under the test.

package com.nirmal.springbootcucumber.StepDefinition;

import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

import java.util.concurrent.TimeUnit;

public class Steps {

    WebDriver driver;
   
    @Given("^Open the Firefox and launch the application$")
    public void open_the_Firefox_and_launch_the_application() throws Throwable
    {
 System.setProperty("webdriver.chrome.driver", "C:\\driver\\chromedriver_win32\\chromedriver.exe");
       driver= new ChromeDriver();

       driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
       
       driver.manage().window().maximize();
       driver.get("http://www.google.com/");
    }
    
    @When("^click on item with selector \\\"([^\\\"]*)\\\"")
    public void  click_on_item_with_selector(String selector) throws Throwable
    {
       driver.findElement(By.cssSelector(selector)).click();;
    }

    @When("^click on item with xpath \\\"([^\\\"]*)\\\"")
    public void  click_on_item_with_xpath(String xpathExpression) throws Throwable
    {
       driver.findElement(By.xpath(xpathExpression)).click();
    }
    
    @Then("^go back to previous URL")
    public void go_back_to_previous_URL() throws Throwable {
    driver.navigate().back();
    }


    @When("^div vivible and clickable with xpath \\\"([^\\\"]*)\\\"")
    public void  div_vivible_and_clickable(String xpathExpression) throws Throwable
    {
       driver.findElement(By.xpath(xpathExpression)).isDisplayed();
       driver.findElement(By.xpath(xpathExpression)).isEnabled();
    }



    @Then("^Enter the value on \"([^\"]*)\" as \"([^\"]*)\" and press enter")
    public void enter_the_value_on_and_pressenterFor(String xpathExpression, String value) throws Throwable
    {
        driver.findElement(By.xpath(xpathExpression)).sendKeys(value);
        driver.findElement(By.xpath(xpathExpression)).sendKeys(Keys.ENTER);
    }


    @When("^Click on clickable item \\\"([^\\\"]*)\\\"")
    public void Click_on_clickable_item(String xpathExpression) throws Throwable
    {
        driver.findElement(By.xpath(xpathExpression)).click();
    }

}


Make sure to configure your chrome driver path (Which you have downloaded in previous step) in open_the_Firefox_and_launch_the_application method as show in above class.


Lets run cucumber Test 

Then double click on run debug configuration button as show in below figure

click on run debug configuration button

Then double click on JUnit under then select the main test class as "com.nirmal.springbootcucumber.Runner" then click ok.



Then you can run the configuration buy click on run button



Then selenium will load the Chrome instance in your local PC and follow the instruction we have given in the feature file. After successful test run you should be able to see similar output as below.

Test result after Successful execution of feature steps 


You can access the updated code base from following GitHub URL.

You ca

Friday, February 28, 2020

Spring Boot property encryption using Jasypt

In this post I will demonstrate how to encrypt data on Spring Boot property file using Jasypt (Java Simplified Encryption). For this I will use code base of my previous post "Spring Boot REST API CRUD operations with MySQL with Spring Data" you can download the code base on github.

Prerequisites 
  1. You should have install java 1.8 or above.
  2. You should have Eclipse installed in your PC.
  3. Your PC should setup Maven installed and configured.
  4. MYSQL server need to be installed. 

Install Jasypt (Java Simplified Encryption) 

In this example we have to use Jasypt to encript our passwords. So first we can download the Jasypt from official web site on http://www.jasypt.org/download.html

Extract in to folder and navigate to bin folder and you can run encrypt.bat/encrypt.sh with relevant parameters. 

encrypt.bat input="This is my message to be encrypted" password=MYPASSWORD_SECRET 

input
This is the password that we going to use in the application

password
This is the secret to decrypt the password

Then you can see similar output as figure below.

Sample encrypt command 
Make sure to use user password for input parameter and generate the encrypted string



Dependency Configure

I will use following project https://github.com/NirmalBalasooriya/RestApiSpringBoot for the demonstration of this post. Check out the project in to you IDE.  First lets add Jasypt  dependency in to pom.

<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot</artifactId>
<version>3.0.2</version>

</dependency>


Update the project pom file as show below 


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.nirmal.springbootrest</groupId>
  <artifactId>RestApiSpringBoot</artifactId>
  <version>0.0.1-SNAPSHOT</version>

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.5.RELEASE</version>
    </parent>
<dependencies>
<!-- Compile -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<!-- Provided -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<!-- Runtime -->
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>6.1.0.jre8</version>
</dependency>

<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot</artifactId>
<version>3.0.2</version>
</dependency>

</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>



Application Configuration

First we have to add following configurations in to Spring boot configurations. For this we have two options either we can add the configuration detail in to default application.property file or we can add separate property file to hold jasypt configurations. I will add these configurations in to existing property file.

jasypt.encryptor.iv-generator-classname=org.jasypt.iv.NoIvGenerator
jasypt.encryptor.algorithm=PBEWithMD5AndDES

Then we can change the spring.datasource.password field. we can use encrypted script there. Make sure to us following format

ENC(generated encrypted string)

EG:
ENC(Qh5TpYelwS//T13si7t218U6t42iM4B2)

So our final application property file would be something similar to this.

#==== connect to mysql ======#
spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:sqlserver://localhost;databaseName=TestDB
spring.datasource.username=nirmal
spring.datasource.password=ENC(Qh5TpYelwS//T13si7t218U6t42iM4B2)
spring.datasource.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.jpa.database-platform=org.hibernate.dialect.SQLServer2012Dialect

spring.jpa.properties.hibernate.show_sql=true
spring.jpa.properties.hibernate.use_sql_comments=false
spring.jpa.properties.hibernate.format_sql=true

#==== Logging configurations ======#
logging.level.root=WARN,INFO,ERROR
logging.level.com.baeldung=TRACE

jasypt.encryptor.iv-generator-classname=org.jasypt.iv.NoIvGenerator
jasypt.encryptor.algorithm=PBEWithMD5AndDES

#jasypt.encryptor.password=MYPASSWORD_SECRET




Enable Application Configuration

Lets enable the encrypted configuration by adding following annotation in our spring boot initialization class  we have to use @EnableEncryptableProperties configuration annotaion. So final class would be something similar below.


package com.nirmal.springbootrest;

import javax.sql.DataSource;

import com.ulisesbocchio.jasyptspringboot.annotation.EnableEncryptableProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

/**
 * Spring Boot initialization class of the ResrApiSpringBoot project
 * 
 * @author Nirmal Balasooriya
 *
 */

@ComponentScan({ "com.nirmal.springbootrest", "com.nirmal.springbootres.controller" })
@EnableJpaRepositories("com.nirmal.springbootrest")
@SpringBootApplication(scanBasePackages = { "com.nirmal.springbootres.controller" })
@EnableEncryptableProperties
//@PropertySource(name="EncryptedProperties", value = "classpath:encrypted.properties")
public class AppInitializer extends SpringBootServletInitializer {

@Autowired
DataSource dataSource;

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(AppInitializer.class);
}

public static void main(String[] args) {
SpringApplication.run(AppInitializer.class, args);
}

}


Note-
If you need to use separate property file for encrypted properties you can use following

@PropertySource(name="EncryptedProperties", value = "classpath:encrypted.properties")



Run the application


In order to run first we have to perform maven install command and then we can run the application

in command line we can use following command to run the application

mvn -Djasypt.encryptor.password=MYPASSWORD_SECRET spring-boot:run

Here we are passing the secret which we used in password encryption process. it is possible to configure the secrent in property file as well. For that you can use following property.(Which I commented on my example since I'm passing it as command line argument)

jasypt.encryptor.password=MYPASSWORD_SECRET

One successfully started the application you should be able to see similar result as below. 




Also you should be see table has been created on your configured Database and you can access the web service from following URL. 

With following out put on web browser.



Lets Test the application 


Then lets add new Book in to API


curl -H "Accept: application/json" -H "Content-type: application/json" -X POST -d "{ \"isbmNumber\":\"9999\", \"name\":\"How to develop API\", \"description\":\"sample book\", \"auther\":\"Nirmal Balasooriya \" }" http://localhost:8080/saveOrUpdate

  
For this one following output will return


{"code":"1","desc":"Book save successful","t":"9999"}


If you check the database you can see data has been inserted to the Database.


Also we can access data through API as well http://localhost:8080/findBook/9999



You can access the updated code base from following GitHub URL.