8000 Rest api and rest assured test case · venkata-qa/rest-assured-tutorial@8f2c7cc · GitHub
[go: up one dir, main page]

Skip to content

Commit 8f2c7cc

Browse files
committed
Rest api and rest assured test case
1 parent 59f793e commit 8f2c7cc

File tree

17 files changed

+867
-0
lines changed

17 files changed

+867
-0
lines changed

pom.xml

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<parent>
7+
<groupId>org.springframework.boot</groupId>
8+
<artifactId>spring-boot-starter-parent</artifactId>
9+
<version>2.1.4.RELEASE</version>
10+
<relativePath/> <!-- lookup parent from repository -->
11+
</parent>
12+
<groupId>org.example</groupId>
13+
<artifactId>rest-assured-tutorial</artifactId>
14+
<version>1.0-SNAPSHOT</version>
15+
16+
<dependencies>
17+
<dependency>
18+
<groupId>org.springframework.boot</groupId>
19+
<artifactId>spring-boot-starter-web</artifactId>
20+
</dependency>
21+
22+
<dependency>
23+
<groupId>org.springframework.boot</groupId>
24+
<artifactId>spring-boot-devtools</artifactId>
25+
<scope>runtime</scope>
26+
</dependency>
27+
<dependency>
28+
<groupId>org.springframework.boot</groupId>
29+
<artifactId>spring-boot-starter-test</artifactId>
30+
<scope>test</scope>
31+
</dependency>
32+
33+
<dependency>
34+
<groupId>org.modelmapper</groupId>
35+
<artifactId>modelmapper</artifactId>
36+
<version>2.1.1</version>
37+
</dependency>
38+
<dependency>
39+
<groupId>org.springframework.boot</groupId>
40+
<artifactId>spring-boot-starter-data-jpa</artifactId>
41+
</dependency>
42+
43+
<dependency>
44+
<groupId>com.h2database</groupId>
45+
<artifactId>h2</artifactId>
46+
<scope>runtime</scope>
47+
</dependency>
48+
49+
<!--<dependency>
50+
<groupId>org.springframework.boot</groupId>
51+
<artifactId>spring-boot-starter-security</artifactId>
52+
</dependency>
53+
<dependency>
54+
<groupId>org.springframework.security</groupId>
55+
<artifactId>spring-security-jwt</artifactId>
56+
<version>1.0.7.RELEASE</version>
57+
</dependency>
58+
<dependency>
59+
<groupId>org.springframework.security.oauth</groupId>
60+
<artifactId>spring-security-oauth2</artifactId>
61+
<version>2.1.0.RELEASE</version>
62+
</dependency>-->
63+
64+
<dependency>
65+
<groupId>io.rest-assured</groupId>
66+
<artifactId>rest-assured</artifactId>
67+
<version>4.3.0</version>
68+
<scope>test</scope>
69+
</dependency>
70+
<dependency>
71+
<groupId>org.testng</groupId>
72+
<artifactId>testng</artifactId>
73+
<version>7.1.0</version>
74+
<scope>test</scope>
75+
</dependency>
76+
<dependency>
77+
<groupId>io.rest-assured</groupId>
78+
<artifactId>json-schema-validator</artifactId>
79+
<version>3.3.0</version>
80+
<scope>test</scope>
81+
</dependency>
82+
</dependencies>
83+
84+
<build>
85+
<plugins>
86+
<plugin>
87+
<groupId>org.springframework.boot</groupId>
88+
<artifactId>spring-boot-maven-plugin</artifactId>
89+
</plugin>
90+
</plugins>
91+
</build>
92+
</project>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.onlyfullstack.springrestexample;
2+
3+
import org.modelmapper.ModelMapper;
4+
import org.springframework.boot.SpringApplication;
5+
import org.springframework.boot.autoconfigure.SpringBootApplication;
6+
import org.springframework.context.annotation.Bean;
7+
8+
@SpringBootApplication
9+
public class RestAssuredApplication {
10+
11+
@Bean
12+
public ModelMapper getModelMapper() {
13+
return new ModelMapper();
14+
}
15+
16+
public static void main(String[] args) {
17+
SpringApplication.run(RestAssuredApplication.class, args);
18+
19+
}
20+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package com.onlyfullstack.springrestexample.controller;
2+
3+
4+
import com.onlyfullstack.springrestexample.datatransferobject.EmployeeDTO;
5+
import com.onlyfullstack.springrestexample.domainobject.EmployeeDO;
6+
import com.onlyfullstack.springrestexample.exception.EntityNotFoundException;
7+
import com.onlyfullstack.springrestexample.service.EmployeeService;
8+
import org.modelmapper.ModelMapper;
9+
import org.springframework.beans.factory.annotation.Autowired;
10+
import org.springframework.http.HttpStatus;
11+
import org.springframework.web.bind.annotation.DeleteMapping;
12+
import org.springframework.web.bind.annotation.GetMapping;
13+
import org.springframework.web.bind.annotation.PathVariable;
14+
import org.springframework.web.bind.annotation.PostMapping;
15+
import org.springframework.web.bind.annotation.PutMapping;
16+
import org.springframework.web.bind.annotation.RequestBody;
17+
import org.springframework.web.bind.annotation.RequestMapping;
18+
import org.springframework.web.bind.annotation.ResponseStatus;
19+
import org.springframework.web.bind.annotation.RestController;
20+
21+
import javax.validation.Valid;
22+
import java.util.List;
23+
import java.util.stream.Collectors;
24+
25+
@RestController
26+
@RequestMapping("employees")
27+
public class EmployeeController {
28+
29+
@Autowired
30+
private EmployeeService service;
31+
32+
@Autowired
33+
private ModelMapper mapper;
34+
35+
@GetMapping
36+
public List<EmployeeDTO> getAllEmployees() {
37+
List<EmployeeDO> employeeDOList = service.getAllEmployees();
38+
List<EmployeeDTO> employeeDTOList = employeeDOList.stream()
39+
.map(employee -> mapper.map(employee, EmployeeDTO.class))
40+
.collect(Collectors.toList());
41+
42+
return employeeDTOList;
43+
}
44+
45+
@GetMapping(value = "{employeeI F438 d}")
46+
public EmployeeDTO getEmployee(@PathVariable(name = "employeeId") Long empId) throws EntityNotFoundException {
47+
return mapper.map(service.getEmployee(empId), EmployeeDTO.class);
48+
}
49+
50+
@PostMapping
51+
@ResponseStatus(HttpStatus.CREATED)
52+
public EmployeeDTO addEmployee(@Valid @RequestBody EmployeeDTO employeeDTO) {
53+
System.out.println("Entered in addEmployee with : " + employeeDTO);
54+
EmployeeDO employeeDO = mapper.map(employeeDTO, EmployeeDO.class);
55+
EmployeeDTO savedEmployee = mapper.map(service.addEmployee(employeeDO), EmployeeDTO.class);
56+
System.out.println("Employee saved into database");
57+
System.out.println("Exited from addEmployee");
58+
return savedEmployee;
59+
}
60+
61+
@DeleteMapping(value = "{employeeId}")
62+
public void deleteEmployee(@PathVariable Long employeeId) throws EntityNotFoundException {
63+
service.deleteEmployee(employeeId);
64+
}
65+
66+
@PutMapping(value = "{employeeId}")
67+
public EmployeeDTO updateEmployee(@Valid @RequestBody EmployeeDTO employeeDTO, @PathVariable(name = "employeeId") Long empId) throws EntityNotFoundException {
68+
System.out.println("Entered in updateEmployee with : " + employeeDTO);
69+
EmployeeDO employeeDO = mapper.map(employeeDTO, EmployeeDO.class);
70+
EmployeeDTO savedEmployee = mapper.map(service.updateEmployee(employeeDO, empId), EmployeeDTO.class);
71+
System.out.println("Employee update into database");
72+
System.out.println("Exited from updateEmployee");
73+
return savedEmployee;
74+
}
75+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.onlyfullstack.springrestexample.datatransferobject;
2+
3+
import javax.validation.constraints.Email;
4+
import javax.validation.constraints.NotNull;
5+
import javax.validation.constraints.Positive;
6+
7+
public class EmployeeDTO {
8+
9+
private Long id;
10+
11+
@NotNull(message = "First name must not be empty")
12+
private String firstName;
13+
14+
@NotNull(message = "Last name must not be empty")
15+
private String lastName;
16+
17+
@Positive(message = "Salary must be positive")
18+
private Long salary;
19+
20+
@Email(message = "email must be valid")
21+
private String email;
22+
23+
public Long getId() {
24+
return id;
25+
}
26+
27+
public void setId(Long id) {
28+
this.id = id;
29+
}
30+
31+
public String getFirstName() {
32+
return firstName;
33+
}
34+
35+
public void setFirstName(String firstName) {
36+
this.firstName = firstName;
37+
}
38+
39+
public String getLastName() {
40+
return lastName;
41+
}
42+
43+
public void setLastName(String lastName) {
44+
this.lastName = lastName;
45+
}
46+
47+
public Long getSalary() {
48+
return salary;
49+
}
50+
51+
public void setSalary(Long salary) {
52+
this.salary = salary;
53+
}
54+
55+
public String getEmail() {
56+
return email;
57+
}
58+
59+
public void setEmail(String email) {
60+
this.email = email;
61+
}
62+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.onlyfullstack.springrestexample.datatransferobject;
2+
3+
import java.util.Date;
4+
5+
public class ErrorDetails {
6+
7+
private Date timestamp;
8+
9+
private String message;
10+
11+
private String details;
12+
13+
public ErrorDetails(Date timestamp, String message, String details) {
14+
super();
15+
this.timestamp = timestamp;
16+
this.message = message;
17+
this.details = details;
18+
}
19+
20+
public Date getTimestamp() {
21+
return timestamp;
22+
}
23+
24+
public void setTimestamp(Date timestamp) {
25+
this.timestamp = timestamp;
26+
}
27+
28+
public String getMessage() {
29+
return message;
30+
}
31+
32+
public void setMessage(String message) {
33+
this.message = message;
34+
}
35+
36+
public String getDetails() {
37+
return details;
38+
}
39+
40+
public void setDetails(String details) {
41+
this.details = details;
42+
}
43+
44+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package com.onlyfullstack.springrestexample.domainobject;
2+
3+
import javax.persistence.Column;
4+
import javax.persistence.Entity;
5+
import javax.persistence.GeneratedValue;
6+
import javax.persistence.GenerationType;
7+
import javax.persistence.Id;
8+
import javax.persistence.SequenceGenerator;
9+
import javax.persistence.Table;
10+
11+
@Entity
12+
@Table(name = "employee_data")
13+
@SequenceGenerator(name = "employee_id_seq", initialValue = 5, allocationSize = 100)
14+
public class EmployeeDO {
15+
16+
@Id
17+
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "employee_id_seq")
18+
private Long id;
19+
20+
@Column(name = "first_name")
21+
private String firstName;
22+
23+
@Column(name = "last_name")
24+
private String lastName;
25+
26+
@Column(name = "salary")
27+
private Long salary;
28+
29+
@Column(name = "email")
30+
private String email;
31+
32+
public Long getId() {
33+
return id;
34+
}
35+
36+
public void setId(Long id) {
37+
this.id = id;
38+
}
39+
40+
public String getFirstName() {
41+
return firstName;
42+
}
43+
44+
public void setFirstName(String firstName) {
45+
this.firstName = firstName;
46+
}
47+
48+
public String getLastName() {
49+
return lastName;
50+
}
51+
52+
public void setLastName(String lastName) {
53+
this.lastName = lastName;
54+
}
55+
56+
public Long getSalary() {
57+
return salary;
58+
}
59+
60+
public void setSalary(Long salary) {
61+
this.salary = salary;
62+
}
63+
64+
public String getEmail() {
65+
return email;
66+
}
67+
68+
public void setEmail(String email) {
69+
this.email = email;
70+
}
71+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.onlyfullstack.springrestexample.exception;
2+
3+
import org.springframework.http.HttpStatus;
4+
import org.springframework.web.bind.annotation.ResponseStatus;
5+
6+
@ResponseStatus(value = HttpStatus.BAD_REQUEST)
7+
public class EntityNotFoundException extends Exception {
8+
9+
static final long serialVersionUID = 1L;
10+
11+
public EntityNotFoundException(String resourceName, String fieldName, Object fieldValue) {
12+
super(String.format("%s not found with %s : '%s'", resourceName, fieldName, fieldValue));
13+
}
14+
15+
}

0 commit comments

Comments
 (0)
0