Skip to content

Commit c606fc8

Browse files
authored
Bael 7066 (#15515)
* BAEL-7066: Happy path test * BAEL-7066: Fix the test * BAEL-7066: Change assume to assert * BAEL-7066: Ignore Nulls test * BAEL-7066: Combined tests * BAEL-7066: Fix a typo and removed smoke tests * BAEL-7066: Fix formatting * BAEL-7066: Renamed to Employees * BAEL-7066: Extract a base test class * BAEL-7066: Added a skeleton for non absent test * BAEL-7066: Added absent employee tests * BAEL-7066: Fix formatting * BAEL-7066: Empty values test * BAEL-7066: Moved controllers * BAEL-7066: Renamed methods * BAEL-7066: Refactor nonNull tests * BAEL-7066: Refactor nonEmpty tests * BAEL-7066: Refactor nonAbsent tests * BAEL-7066: Renamed the base test * BAEL-7066: cleanup * BAEL-7066: Fixed indents
1 parent 2096eac commit c606fc8

14 files changed

Lines changed: 674 additions & 0 deletions
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.baeldung.jsonignore;
2+
3+
import org.springframework.boot.autoconfigure.SpringBootApplication;
4+
5+
@SpringBootApplication
6+
public class Application {
7+
8+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.baeldung.jsonignore.absentfields;
2+
3+
import com.fasterxml.jackson.annotation.JsonInclude;
4+
import com.fasterxml.jackson.annotation.JsonInclude.Include;
5+
import java.util.Optional;
6+
7+
@JsonInclude(Include.NON_ABSENT)
8+
public class Employee {
9+
10+
private String lastName;
11+
private String firstName;
12+
private long id;
13+
private Optional<Salary> salary;
14+
15+
16+
public Employee() {
17+
}
18+
19+
public Employee(final long id, final String lastName, final String firstName, final Optional<Salary> salary) {
20+
this.id = id;
21+
this.lastName = lastName;
22+
this.firstName = firstName;
23+
this.salary = salary;
24+
}
25+
26+
public String getLastName() {
27+
return lastName;
28+
}
29+
30+
public String getFirstName() {
31+
return firstName;
32+
}
33+
34+
public long getId() {
35+
return id;
36+
}
37+
38+
public void setLastName(final String lastName) {
39+
this.lastName = lastName;
40+
}
41+
42+
public void setFirstName(final String firstName) {
43+
this.firstName = firstName;
44+
}
45+
46+
public void setId(final long id) {
47+
this.id = id;
48+
}
49+
50+
public Optional<Salary> getSalary() {
51+
return salary;
52+
}
53+
54+
public void setSalary(final Optional<Salary> salary) {
55+
this.salary = salary;
56+
}
57+
58+
@Override
59+
public String toString() {
60+
return "Employee{" +
61+
"lastName='" + lastName + '\'' +
62+
", firstName='" + firstName + '\'' +
63+
", id=" + id +
64+
", salary=" + salary +
65+
'}';
66+
}
67+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.baeldung.jsonignore.absentfields;
2+
3+
import java.math.BigDecimal;
4+
5+
public class Salary {
6+
7+
private BigDecimal hourlyRate;
8+
9+
public Salary() {
10+
}
11+
12+
public Salary(BigDecimal hourlyRate) {
13+
this.hourlyRate = hourlyRate;
14+
}
15+
16+
public BigDecimal getHourlyRate() {
17+
return hourlyRate;
18+
}
19+
20+
public void setHourlyRate(final BigDecimal hourlyRate) {
21+
this.hourlyRate = hourlyRate;
22+
}
23+
24+
@Override
25+
public String toString() {
26+
return "Salary{" +
27+
"hourlyRate=" + hourlyRate +
28+
'}';
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.baeldung.jsonignore.controller;
2+
3+
import com.baeldung.jsonignore.absentfields.Employee;
4+
import org.springframework.web.bind.annotation.PostMapping;
5+
import org.springframework.web.bind.annotation.RequestBody;
6+
import org.springframework.web.bind.annotation.RequestMapping;
7+
import org.springframework.web.bind.annotation.RestController;
8+
9+
@RestController
10+
@RequestMapping(AbsentEmployeeEchoController.USERS)
11+
public class AbsentEmployeeEchoController {
12+
13+
public static final String USERS = "/absent_employees";
14+
15+
@PostMapping
16+
public Employee echoUser(@RequestBody Employee employee) {
17+
return employee;
18+
}
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.baeldung.jsonignore.controller;
2+
3+
import com.baeldung.jsonignore.nullfields.Employee;
4+
import org.springframework.web.bind.annotation.PostMapping;
5+
import org.springframework.web.bind.annotation.RequestBody;
6+
import org.springframework.web.bind.annotation.RequestMapping;
7+
import org.springframework.web.bind.annotation.RestController;
8+
9+
@RestController
10+
@RequestMapping(EmployeeEchoController.USERS)
11+
public class EmployeeEchoController {
12+
13+
public static final String USERS = "/employees";
14+
15+
@PostMapping
16+
public Employee echoUser(@RequestBody Employee employee) {
17+
return employee;
18+
}
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.baeldung.jsonignore.controller;
2+
3+
import com.baeldung.jsonignore.emptyfields.Employee;
4+
import org.springframework.web.bind.annotation.PostMapping;
5+
import org.springframework.web.bind.annotation.RequestBody;
6+
import org.springframework.web.bind.annotation.RequestMapping;
7+
import org.springframework.web.bind.annotation.RestController;
8+
9+
@RestController
10+
@RequestMapping(EmptyEmployeeEchoController.USERS)
11+
public class EmptyEmployeeEchoController {
12+
13+
public static final String USERS = "/empty_employees";
14+
15+
@PostMapping
16+
public Employee echoUser(@RequestBody Employee employee) {
17+
return employee;
18+
}
19+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package com.baeldung.jsonignore.emptyfields;
2+
3+
import com.fasterxml.jackson.annotation.JsonInclude;
4+
import com.fasterxml.jackson.annotation.JsonInclude.Include;
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
import java.util.Optional;
8+
9+
@JsonInclude(Include.NON_EMPTY)
10+
public class Employee {
11+
12+
private String lastName;
13+
private String firstName;
14+
private long id;
15+
private Optional<Salary> salary;
16+
17+
private String phoneticName = "";
18+
19+
private List<PhoneNumber> phoneNumbers = new ArrayList<>();
20+
21+
public Employee() {
22+
}
23+
24+
public Employee(final long id, final String lastName, final String firstName,
25+
final Optional<Salary> salary, final String phoneticName, final List<PhoneNumber> phoneNumbers) {
26+
this.id = id;
27+
this.lastName = lastName;
28+
this.firstName = firstName;
29+
this.salary = salary;
30+
this.phoneticName = phoneticName != null ? phoneticName : "";
31+
this.phoneNumbers.addAll(phoneNumbers);
32+
}
33+
34+
public String getLastName() {
35+
return lastName;
36+
}
37+
38+
public void setLastName(final String lastName) {
39+
this.lastName = lastName;
40+
}
41+
42+
public String getFirstName() {
43+
return firstName;
44+
}
45+
46+
public void setFirstName(final String firstName) {
47+
this.firstName = firstName;
48+
}
49+
50+
public long getId() {
51+
return id;
52+
}
53+
54+
public void setId(final long id) {
55+
this.id = id;
56+
}
57+
58+
public Optional<Salary> getSalary() {
59+
return salary;
60+
}
61+
62+
public void setSalary(final Optional<Salary> salary) {
63+
this.salary = salary;
64+
}
65+
66+
public String getPhoneticName() {
67+
return phoneticName;
68+
}
69+
70+
public void setPhoneticName(final String phoneticName) {
71+
this.phoneticName = phoneticName;
72+
}
73+
74+
public List<PhoneNumber> getPhoneNumbers() {
75+
return phoneNumbers;
76+
}
77+
78+
public void setPhoneNumbers(final List<PhoneNumber> phoneNumbers) {
79+
this.phoneNumbers = phoneNumbers;
80+
}
81+
82+
@Override
83+
public String toString() {
84+
return "Employee{" +
85+
"lastName='" + lastName + '\'' +
86+
", firstName='" + firstName + '\'' +
87+
", id=" + id +
88+
", salary=" + salary +
89+
", phoneticName='" + phoneticName + '\'' +
90+
", phoneNumbers=" + phoneNumbers +
91+
'}';
92+
}
93+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.baeldung.jsonignore.emptyfields;
2+
3+
public class PhoneNumber {
4+
5+
private String number;
6+
7+
public PhoneNumber() {
8+
}
9+
10+
public PhoneNumber(final String number) {
11+
this.number = number;
12+
}
13+
14+
public String getNumber() {
15+
return number;
16+
}
17+
18+
public void setNumber(final String number) {
19+
this.number = number;
20+
}
21+
22+
@Override
23+
public String toString() {
24+
return "PhoneNumber{" +
25+
"number='" + number + '\'' +
26+
'}';
27+
}
28+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.baeldung.jsonignore.emptyfields;
2+
3+
import java.math.BigDecimal;
4+
5+
public class Salary {
6+
7+
private BigDecimal hourlyRate;
8+
9+
public Salary() {
10+
}
11+
12+
public Salary(BigDecimal hourlyRate) {
13+
this.hourlyRate = hourlyRate;
14+
}
15+
16+
public BigDecimal getHourlyRate() {
17+
return hourlyRate;
18+
}
19+
20+
public void setHourlyRate(final BigDecimal hourlyRate) {
21+
this.hourlyRate = hourlyRate;
22+
}
23+
24+
@Override
25+
public String toString() {
26+
return "Salary{" +
27+
"hourlyRate=" + hourlyRate +
28+
'}';
29+
}
30+
}

0 commit comments

Comments
 (0)