Category Archives: Spring

Spring

Setting of JBPM 6.4 (latest) process environment

I had a great vacation where I took opportunity to learn JBPM and how to setup JBPM in Wildfly. Below are the steps to setup a fully functional process designer where we can develop a process with dynamic UI and deploy it in a RESTful service and call the application using an angular sample and invoke a human workflow. For some reason, the Jboss does not start when you are in a company firewall.

  • Have java7 or java8 in your machine, Download and unzip wildfly-8.2.0.Final.zip into a folder
  • Go to <WILDFLYHOME>/bin directory and run add-user.bat, create 2 users,
    1. Management user: admin (pass)/ with no role
    2. Application user: kieserver /kieserver1!/ with kie-server as the role
    3. Application user: user4/pass/ with admin as the role
  • Go to <WILDFLYHOME>/standalone/configuration and rename standalone.xml as standalone1.xml and and rename standalone-full.xml to standalone.xml and replace below code under security-domains block in the xml file.


<security-domain name="other" cache-type="default">
<authentication>
<login-module code="Remoting" flag="optional">
<module-option name="password-stacking" value="useFirstPass"/>
</login-module>
<login-module code="RealmUsersRoles" flag="required">
<module-option name="usersProperties" value="${jboss.server.config.dir}/application-users.properties"/>
<module-option name="rolesProperties" value="${jboss.server.config.dir}/application-roles.properties"/>
<module-option name="realm" value="ApplicationRealm"/>
<module-option name="password-stacking" value="useFirstPass"/>
</login-module>
</authentication>
</security-domain>
<security-domain name="jboss-web-policy" cache-type="default">
<authorization>
<policy-module code="Delegating" flag="required"/>
</authorization>
</security-domain>
<security-domain name="jboss-ejb-policy" cache-type="default">
<authorization>
<policy-module code="Delegating" flag="required"/>
</authorization>
</security-domain>

 

Let me know your feedback. Also try various processes like HR, Evaluation, and other processes in Kie-workbench.

Advertisements

Writing Medium Complex Web Application using Spring Boot – Part 2

For folks in hurry, get the latest code from Github and run the application as per the instructions in README file.

This Blog explains how we setup our development environment for building this application.

Setting up Postgres Database with schema:

The application schema file is present under folder called sqlscripts/schema.sql . Install Postgres database and start the database using below command:

# Create a database with user as "postgres" and password as "password"
#than run this command
<POSTGRES_HOME>/bin/postgres -D ..\data

Open the pgAdmin windows application and create a database called “pg” and open the above script and run it.

First let us look at the POM file.

This POM file is configured to work with reverse engineering of Table to JPA components. We are using “org.apache.openjpa.jdbc.meta.ReverseMappingTool” as below:

             <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.2.1</version>
                <configuration>
                    <mainClass>org.apache.openjpa.jdbc.meta.ReverseMappingTool</mainClass>
                    <arguments>
                        <argument>-directory</argument>
                        <argument>target/generated-sources/java</argument> <!-- or target/generated/model -->
                        <argument>-accessType</argument>
                        <argument>fields</argument>
                        <argument>-useGenericCollections</argument>
                        <argument>true</argument>
                        <argument>-package</argument>
                        <argument>org.hcl.test.model</argument>
                        <argument>-innerIdentityClasses</argument>
                        <argument>false</argument>
                        <argument>-useBuiltinIdentityClass</argument>
                        <argument>false</argument>
                        <argument>-primaryKeyOnJoin</argument>
                        <argument>false</argument>
                        <argument>-annotations</argument>
                        <argument>true</argument>
                        <argument>-p</argument>
                        <argument>src/main/resources/META-INF/reverse-persistence.xml</argument>
                        <argument>-nullableAsObject</argument>
                        <argument>false</argument>
                    </arguments>
                    <includePluginDependencies>true</includePluginDependencies>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>javax.validation</groupId>
                        <artifactId>validation-api</artifactId>
                        <version>1.1.0.Final</version>
                    </dependency>
                    <dependency>
                        <groupId>org.apache.openjpa</groupId>
                        <artifactId>openjpa-all</artifactId>
                        <version>2.4.0</version>
                    </dependency>
                    <dependency>
                        <groupId>org.postgresql</groupId>
                        <artifactId>postgresql</artifactId>
                        <version>9.3-1100-jdbc4</version>
                    </dependency>
                </dependencies>
            </plugin>

The reverse-persistence.xml file looks like this,

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
    <persistence-unit name="openjpa">
        <provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>        
            <properties>
            <property name="openjpa.ConnectionUserName" value="postgres"/>
            <property name="openjpa.ConnectionPassword" value="password"/>
            <property name="openjpa.ConnectionURL" value="jdbc:postgresql://localhost:5432/pg"/>  
            <property name="openjpa.ConnectionDriverName"  value="org.postgresql.Driver"/>
            <property name="openjpa.jdbc.DBDictionary" value="postgres" />
            <property name="openjpa.Log" value="SQL=TRACE"/>
        </properties>
    </persistence-unit>
</persistence>

Once we configure the above steps, running below command will generate all the JPA POJO’s

mvn clean exec:java test

There is also a why to create dynamic schema without the need for postgres. This can be an exercise for you

Writing Medium Complex Web Application using Spring Boot – Part 1

Introduction

The Pivotal team has done a great job of coming with Spring Boot microservice application stack. In the next few blogs, I will demonstrate the capabilities of Spring Boot to write a Medium Complex application (Not a Todo application) using Industry leading polyglot stack like, Angular.js, Spring JPA, Spring Boot and Postgres Database in the backend. I will also demonstrate how to run this application in Heroku.

Design consideration

  • With respect to Spring Boot
    • Takes off lot of xml headaches of Spring MVC and follows a strong convention over configuration
    • Works like Ruby, with lot of starter jar dependencies like spring-boot-starter-web or like spring-boot-starter-data-jpa, which helps in quickly setup all the dependencies. There are whole bunch of them
    • Encourages the concept of Microservices, where we can build low foot print self contained services, which we can deploy in PaaS like Heroku
  • In addition to these goodies, this sample application also imposes few of its own,
    • Minimum Boilerplate coding. Minimum amount of Handcrafted POJOs. Almost no xml configuration.
    • Design is more based on Test Driven Development (TDD) including Java and Javascript side

The Architecture

Untitled

The Application

What it does?

This is a sample application that helps user to register himself and purchases books online. It has roughly 10 screens and 2 actors the user and the admin. It will have below capabilities,

  • User can do following actions
    • Register himself
    • Login to the system
    • View the books, and add to the shopping card
    • Checkout the books
  • Currently Admin is hardcoded in the system and admin can do the following,
    • Approve the order to be shipped

Technology Stack

Frontend:

Angular.js, Bootstrap

Serverside:

Spring Boot: spring-boot-starter-web, spring-boot-starter-jpa, spring-boot-starter-test

Database:

Postgres for the application and H2 for Junit testing.

Modules:

The blog is divided into multiple section:

  • Setup the environment for development
  • JUnit testing of DAO layer
  • JUnit testing of Service layer
  • JUnit testing of Controller
  • JUnit testing of Javascript and integrating with Build system

Integrating Kickstrap with Spring MVC application

Get the latest code from Github,

In continuation of my earlier blogs on Introduction to Spring MVC, in this blog I will talk about Kickstrap, Less and how Kickstrap can be integrated with our Spring MVC based bookstore application.

If you go to Kickstrap website they mention that it is Twitter Bootstrap on Steroids. It is heavily based on Less which is a dynamic stylesheet language. You can download Kickstrap from here.

Advantages of using Kickstrap is,

As a 1st step we need to copy the Kickstrap in the application folder as below,

kickstrap bookstore-app

kickstrap bookstore-app

The tiles definition in tiles.xml in Spring MVC is as below,

<tiles-definitions>
<definition name="template" template="/WEB-INF/templates/template.jsp">
<put-attribute name="header" value="/WEB-INF/templates/header.jsp"/>
<put-attribute name="footer" value="/WEB-INF/templates/footer.jsp"/>
</definition>
...
</tiles-definitions>

In the template.jsp you need to add Kickstrap.less and less-1.3.0.min.js as below,

<link rel="stylesheet/less" type="text/css" href="/bookstore-example-with-mvc/resources/css/kickstrap.less">
<script src="/bookstore-example-with-mvc/resources/css/Kickstrap/js/less-1.3.0.min.js"></script>

If you see the template.jsp, we use some of the css class of Kickstrap like “container”, “row-fluid” and others. row-fluid helps in responsive web design.

Changing to different theme

In Kickstrap changing to different theme is easy. Open the theme.less, currently we are using cerulean theme, as below

@import "Kickstrap/themes/cerulean/variables.less";
@import "Kickstrap/themes/cerulean/bootswatch.less";

Now run the below command and open the browser and type http://localhost:8080/bookstore-example-with-mvc,

mvn clean tomcat7:run

The theme looks as below,

cerulean-theme

cerulean-theme

Below are the themes when you drilldown Kickstrap folder as shown below,

themes-bookstore-app

themes-bookstore-app

If you want to change it to, let us say cyborg theme, you need to change kickstrap.less as below,

@import "Kickstrap/themes/cyborg/variables.less";
@import "Kickstrap/themes/cyborg/bootswatch.less";

Now run the below command and open the browser and type http://localhost:8080/bookstore-example-with-mvc, you will see the change in theme,

mvn clean tomcat7:run

The theme is changed and looks as below,

cyborg-theme

cyborg-theme

I hope this blog helped you.

Reference:

Pro Spring MVC: With Web Flow by by Marten Deinum, Koen Serneels

JUnit testing of Spring MVC application – Introduction

For people in hurry, here is the Github code and the steps to run the sample.

Spring MVC is one of the leading Java Web application frameworks along with Struts. In the next few blogs, I will take an use case and demonstrate how to develop a good quality web application in an enterprise Java world. I will also demonstrate latest capabilities of Spring like Annotation based configurations and its advantages over XML based configurations. Prerequisite for this application is Java 7.x, Tomcat 7.x, Maven 3.x and STS IDE.

The use case I will be talking about is a Bookstore application, where in user register to this application and purchase books. There is also an administration task like creating the book catalog.

Spring MVC application architecture

Source Java9s: Spring MVC Architecture

Source Java9s: Spring MVC Architecture

For this sample application we extensively use the Latest Spring MVC annotation capability. The major advantage with this is we will not be depending on any xml configuration including web.xml. I personally like XML configuration with namespace, because it is readable. The advantage of annotation is, since everything is Java, if we use STS IDE, any typo can be caught at compilation time,  and we can write a good quality code and even measure the code coverage. We also used Kickstrap as the css engine for this application. Here is a blog on Integrating Kickstrap with Spring MVC application.

What is code coverage

Code coverage is a measure of how well you unit tested your code. If you use a good IDE like STS IDE we can install ecobertura using the update site. Once you setup ecobertura, you can start developing your application. When you run your JUnit test in “Cover as” mode, it will show the code coverage of your JUnit tests as below,

eCobertura STS-IDE view

eCobertura STS-IDE view

In the subsequent blogs I will use test driven development (TDD) to build each layer and gradually increase the code coverage and write better quality code. The layers I will be developing in TDD in the order are,

Reference:

Pro Spring MVC: With Web Flow by by Marten Deinum, Koen Serneels

JUnit testing of Spring MVC application: Testing Spring Webflow

For people in hurry get the latest code from Github and run the below command,


mvn clean test -Dtest=com.example.bookstore.web.controller.CreateOrderTest

In continuation of my earlier blogs on Introduction to Spring MVC and Testing Frontend using Selenium in Spring MVC, in this blog I will demonstrate how to test Web layer in Spring MVC and Spring Web Flow. If you want to measure the quality of JUnit testing of your Spring Web Flow application refer Measuring navigation flow JUnit test in Spring Web flow. There is another blog I have written on JUnit testing Spring Web Flow refer JUnit testing with Spring Webflow.

In this blog I will demo how to build a Spring Web Flow based application using Test driven development (TDD). The use case I will be talking of is a web flow where in user drills down thru few categories and selects few books and select delivery options and place an order.

There is a good introduction of Spring Web Flow here. But just to give a brief introduction about Spring Web Flow, it is composed of a set of states. A state is a point in the flow where some event happens: for instance showing books list for a category or adding the books to the shopping cart. Each state has one or more transitions that are used to move to another state. A transition is triggered by an event.

As a first step, please refer this createOrders-flow.xml, if you notice carefully OrderForm is stored in the flowScope. And this object carries the state between each step. Let us see the bootstrapping process in JUnit test as below,

public class CreateOrderTest extends AbstractXmlFlowExecutionTests {
private OrderController orderController;

protected void setUp() {
orderController = Mockito.mock(OrderController.class);
}

@Override
protected void configureFlowBuilderContext(MockFlowBuilderContext builderContext) {
builderContext.registerBean("orderController", orderController);
}

@Override
protected FlowDefinitionResource getResource(FlowDefinitionResourceFactory resourceFactory) {
return resourceFactory.createFileResource("src/main/webapp/WEB-INF/view/public/createOrders/createOrders-flow.xml");
}

//JUnit tests below
}

If you notice this class is subclassed from a class AbstractXmlFlowExecutionTests, it has 3 overridden methods, setup, configureFlowBuilderContext, getResource. getResource method is the one where we set the createOrders-flow.xml and is returned for testing. If you also notice we have used Mockito to mock OrderController and register the bean in the flow XML.

Let us start building the JUnit test to test each node (view-state) of the flow. For starting the flow you need to do as below,

public void testStartCreateOrderFlow() {

MutableAttributeMap input = new LocalAttributeMap();
MockExternalContext context = new MockExternalContext();
startFlow(input, context);

Mockito.verify(orderController, VerificationModeFactory.times(1)).initializeForm();
Mockito.verify(orderController, VerificationModeFactory.times(1)).initializeSelectableCategories();

assertCurrentStateEquals("selectCategory");
assertResponseWrittenEquals("selectCategory", context);
}

If you notice carefully in the flow, we are asking the controller to call the initializeForm at on-start event and call initializeSelectableCategories at on-render of selectCategory view-state.

Using Mockito we are verifying if the initializeForm and initializeSelectableCategories called once only. And finally we assert for the next state.

Similarly we test other view states.

Finally we test calling a subflow by mocking the subflow, refer createMockBookingSubflow below you will get the idea. If you want to understand more refer Mocking a subflow.


public void testSelectDeliveryOptions_AddAndNext() {

setCurrentState("selectDeliveryOptions");
 getFlowScope().put("orderForm", createTestOrderForm());
 getFlowDefinitionRegistry().registerFlowDefinition(createMockBookingSubflow());

MockExternalContext context = new MockExternalContext();
 context.setEventId("finish");
 resumeFlow(context);
 }

private Flow createMockBookingSubflow() {
 Flow mockBookingFlow = new Flow("secured/placeOrders");
 mockBookingFlow.setInputMapper(new Mapper() {
 public MappingResults map(Object source, Object target) {
 // assert that 1L was passed in as input
 return null;
 }
 });
 // immediately return the bookingConfirmed outcome so the caller can respond
 new EndState(mockBookingFlow, "endOrderOk");
 return mockBookingFlow;
 }

I hope this blog helped you. In my next blog I will be taking about integrating Kickstrap with Spring MVC application.

Reference:

Pro Spring MVC: With Web Flow by by Marten Deinum, Koen Serneels