Junit error after lib upgrade from Junit 4 to Junit 5

Recently, I did the Junit libs upgrade in my spring boot project but got the build failure.

All of spring boot projects has been used Junit 4 and followed the test class template mentioned below. The class was used the annotation @ExtendWit(SpringRunner.class). It is mandatory if not needed to load spring boot context completely.

Continue reading “Junit error after lib upgrade from Junit 4 to Junit 5”

Elasticsearch Failover policy in log4j xml

Many java developers use log4j for logging for java applications. the logs aggregator would be crucial if there are multiple application in multiple places. It helps the developers to access the all logs of all applications in central location. ELK stack is the one of famous solution.

Continue reading “Elasticsearch Failover policy in log4j xml”

Dotnet CoreCLI Commands to Create Project

.NET Framework was developed by Microsoft but it did not support Linux. When open source community is getting bigger and bigger, Microsoft also change their direction to open source track. Dotnet Core is the solution from Microsoft to enter open source world which is based on .NET Framework. Dotnet Core is open source and cross platform development framework. It support Linux too.

This article explains the basic command to setup dotnet core project. I have used dotnet core 2.2 on Ubuntu.

Continue reading “Dotnet CoreCLI Commands to Create Project”

Microservice & Container

Microservices is the one of main architecture to building could-based solution. Here, it consists of combination of small distributed applications to build entire system. The application is a component which is isolated and well-developed. All dependencies of relevant application should be in independent from other component and belongs to particular component. The component and its dependencies are packaged in single container. Finally the container is deployed on platform infrastructure.

Scaling is the important factor on cloud based solution. The container can do major role for scaling as per resource demand. It is very difficult and complex operation to managing containers manually. But it is very important to manage financial cost less. There are some tools such docker swarm, mesophere and kunernetes for automate container orchestration. These tools can manage resources at scaling as per given configuration.

Continue reading “Microservice & Container”

Yarn upgrade to the latest

Yarn is a package manager for JavaScript created by Facebook. 

Find current version

$ yarn –version

Current version on my machine is 0.27

I followed below steps to complete upgrade process.

Remove yarn setup
$ sudo apt autoremove yarn

Add repository with latest version
$ curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
$ echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list

Install yarn
$ sudo apt-get update
$ sudo apt-get install yarn

Continue reading “Yarn upgrade to the latest”

Spring JPA – Saving list items in entity

Spring JPA provides easy way to saving entity in database than traditional SQL operation. Here, developer can populate entity object and trigger save function in relevant repository. In entity population, developer must be aware on list operation.

Scenario:

@Entity
@Table(name="hotelinfo")
public class HotelInfoEntity {

 @Id
 @GeneratedValue(strategy=GenerationType.IDENTITY)
 private Long id;
 
 private String name;
 
 private String address;
 
 @ManyToOne(fetch=FetchType.LAZY)
 @JoinColumn(name = "countryid")
 private CountryEntity country;
 
 @OneToMany(mappedBy = "hotelInfo", cascade = CascadeType.ALL, orphanRemoval=true)
 private List rooms = new ArrayList<>();

 public List getRooms() {
  return rooms;
 }

 public void setRooms(List rooms) {
  this.rooms = rooms;
 }

 ... getter and setter methods of rest 
}

Incorrect implementation:

Most of developer do mistake when set list parameters ( Eg: setRooms(List rooms) ). They create the array and call to setter function to set list attribute to object.

It is wrong. It could cause to following error, specially in update operation.

Error:

This object keeps the reference to room list. When developer crates new array and assigned using setter method, reference to room list in hotel entity might be lost. Hence following error could happen.

2017-11-08 10:38:37.557 ERROR 3756 --- [nio-8080-exec-7] r.r.h.p.setup.service.PromotionService : IN SAVE 
org.springframework.orm.jpa.JpaSystemException: A collection with cascade="all-delete-orphan" was no longer referenced by the owning entity instance: com.example.samplespringboot.entity.RoomEntity; nested exception is org.hibernate.HibernateException: A collection with cascade="all-delete-orphan" was no longer referenced by the owning entity instance: com.example.samplespringboot.entity.RoomEntity
 at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:333)
 at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:244)
 at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:521)
 at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:761)

Solution:

To add list items

Stop creating new array and assigned to list directly.
Do add element to list.
Eg:

hotelInfoEnt.getRooms().add(e);

To clear list

Clear list
Eg:

hotelInfoEnt.getRooms().clear();

Java 8 – Lambda – List Operatoins

Java 8 feature, Lambda, provides features to process List easily.

Model class:

public class User {
 private Long id;
 private String firstName;
 private String lastName;
 private int age;
 private String city;
 private LocalDate dateOfBirth;

 // Getter and setter methods
}

List to Map

Key: id of user object
Value: user obeject

Option 1

Map<Long, User> userIdMap = userList.stream().collect(Collectors.toMap(User::getId, x -> x));

Option 2

Map<Long, User> userIdMap = userList.stream().collect(Collectors.toMap(x -> x.getId(), x -> x));

List to Map (Object parameters)

Key: id of user object
Value: first name of user object

Option 1

Map<Long, String> userIdMap = userList.stream().collect(Collectors.toMap(x -> x.getId(), x -> x.getFirstName()));

Option 2

Map<Long, String> userIdMap = userList.stream().collect(Collectors.toMap(User::getId, User::getFirstName));

Key: id of user object
Value: name concatenating first and and last names

Map<Long, String> userFullNameMap = userList.stream().collect(Collectors.toMap(x -> x.getId(), x -> (x.getFirstName() + " " + x.getLastName()) ));

Filter

Filter user object by age

 List<User> result = userList.stream()
                          .filter(x -> x.getAge() > 29)
                          .collect(Collectors.toList());

Filtered output as List

 List<User> result = userList.stream()
                          .filter(x -> x.getAge() > 29)
                          .collect(Collectors.toCollection(ArrayList::new));

Filtered output as Set

 Set<User> result = userList.stream()
                          .filter(x -> x.getAge() > 29)
                          .collect(Collectors.toCollection(HashSet::new));