Friday, 8 May 2020

Setup WireMock project in Eclipse

Step 1: Create new maven project ‘employee-rest-client’ in Eclipse.

 

Step 2: Update pom.xml with dependencies.

 

Since I am using Spring WebClient to communicate with Employee Rest Service, I need webflux dependency.

 

<dependency>

         <groupId>org.springframework.boot</groupId>

         <artifactId>spring-boot-starter-webflux</artifactId>

</dependency>

 

Similarly, I need junit4 and wiremock to test ‘employee-rest-client’ project.

 

<dependency>

         <groupId>com.github.tomakehurst</groupId>

         <artifactId>wiremock-jre8-standalone</artifactId>

         <version>2.26.3</version>

         <scope>test</scope>

</dependency>

 

<dependency>

         <groupId>junit</groupId>

         <artifactId>junit</artifactId>

         <scope>test</scope>

</dependency>

 

pom.xml
<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.sample.app</groupId>
	<artifactId>employee-rest-client</artifactId>
	<version>1</version>

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


	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-webflux</artifactId>
		</dependency>

		<dependency>
			<groupId>com.github.tomakehurst</groupId>
			<artifactId>wiremock-jre8-standalone</artifactId>
			<version>2.26.3</version>
			<scope>test</scope>
		</dependency>

		<!-- https://mvnrepository.com/artifact/junit/junit -->
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<scope>test</scope>
		</dependency>


	</dependencies>
</project>

Step 3: Create a package ‘com.sample.app.model’ and define Employee POJO to represent employee information.

 

Employee.java
package com.sample.app.model;

public class Employee {

	private int id;
	private String firstName;
	private String lastName;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	@Override
	public String toString() {
		return "Employee [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + "]";
	}

}

Step 4: Create a package ‘com.sample.app.service’ and define EmployeeService interface like below.

 

EmployeeService.java

package com.sample.app.service;

import java.util.List;

import com.sample.app.model.Employee;

public interface EmployeeService {

	public List<Employee> emps();

	public Employee byId(int id);

	public List<Employee> containsName(String name);

	public Employee addEmployee(Employee emp);

	public Employee updateEmployee(int id, Employee emp);

	public Employee deleteEmployee(int id);

}

Step 5: Create a package ‘com.sample.app.service.impl’ and define ‘EmployeeRestClient’ like below.

 

EmployeeRestClient.java
package com.sample.app.service.impl;

import java.util.List;

import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.util.UriComponentsBuilder;

import com.sample.app.model.Employee;
import com.sample.app.service.EmployeeService;

public class EmployeeRestClient implements EmployeeService {

	private WebClient webClient;

	public EmployeeRestClient(WebClient webClient) {
		this.webClient = webClient;
	}

	@Override
	public List<Employee> emps() {

		return webClient.get().uri("api/v1/employees").retrieve().bodyToFlux(Employee.class).collectList().block();
	}

	@Override
	public Employee byId(int id) {
		return webClient.get().uri("api/v1/employees/" + id).retrieve().bodyToMono(Employee.class).block();
	}

	@Override
	public List<Employee> containsName(String name) {
		String uriToHit = UriComponentsBuilder.fromUriString("api/v1/employees/by-name/").queryParam("empName", name)
				.buildAndExpand().toString();

		return webClient.get().uri(uriToHit).retrieve().bodyToFlux(Employee.class).collectList().block();
	}

	@Override
	public Employee addEmployee(Employee emp) {
		return webClient.post().uri("api/v1/employees").syncBody(emp).retrieve().bodyToMono(Employee.class).block();
	}

	@Override
	public Employee updateEmployee(int id, Employee emp) {
		return webClient.put().uri("api/v1/employees/" + id).syncBody(emp).retrieve().bodyToMono(Employee.class)
				.block();
	}

	@Override
	public Employee deleteEmployee(int id) {
		return webClient.delete().uri("api/v1/employees/" + id).retrieve().bodyToMono(Employee.class).block();
	}

}
That’s it you are done with basic client project. Total project structure looks like below.


You can download complete project from this link.


Previous                                                    Next                                                    Home

No comments:

Post a Comment