Merge pull request #2694 from earth001/master

Update Person class to use property "id" like identifier. (BAEL-1121)
This commit is contained in:
Carsten Gräf 2017-10-01 08:35:42 +02:00 committed by GitHub
commit 29cc370083
4 changed files with 29 additions and 42 deletions

View File

@ -10,6 +10,7 @@ import javax.json.bind.annotation.JsonbTransient;
public class Person { public class Person {
private int id;
@JsonbProperty("person-name") @JsonbProperty("person-name")
private String name; private String name;
@JsonbProperty(nillable = true) @JsonbProperty(nillable = true)
@ -23,8 +24,9 @@ public class Person {
public Person() { public Person() {
} }
public Person(String name, String email, int age, LocalDate registeredDate, BigDecimal salary) { public Person(int id, String name, String email, int age, LocalDate registeredDate, BigDecimal salary) {
super(); super();
this.id = id;
this.name = name; this.name = name;
this.email = email; this.email = email;
this.age = age; this.age = age;
@ -32,6 +34,14 @@ public class Person {
this.salary = salary; this.salary = salary;
} }
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getAge() { public int getAge() {
return age; return age;
} }
@ -76,7 +86,9 @@ public class Person {
@Override @Override
public String toString() { public String toString() {
StringBuilder builder = new StringBuilder(); StringBuilder builder = new StringBuilder();
builder.append("Person [name="); builder.append("Person [id=");
builder.append(id);
builder.append(", name=");
builder.append(name); builder.append(name);
builder.append(", email="); builder.append(", email=");
builder.append(email); builder.append(email);
@ -94,11 +106,7 @@ public class Person {
public int hashCode() { public int hashCode() {
final int prime = 31; final int prime = 31;
int result = 1; int result = 1;
result = prime * result + age; result = prime * result + id;
result = prime * result + ((email == null) ? 0 : email.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((registeredDate == null) ? 0 : registeredDate.hashCode());
result = prime * result + ((salary == null) ? 0 : salary.hashCode());
return result; return result;
} }
@ -111,27 +119,7 @@ public class Person {
if (getClass() != obj.getClass()) if (getClass() != obj.getClass())
return false; return false;
Person other = (Person) obj; Person other = (Person) obj;
if (age != other.age) if (id != other.id)
return false;
if (email == null) {
if (other.email != null)
return false;
} else if (!email.equals(other.email))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (registeredDate == null) {
if (other.registeredDate != null)
return false;
} else if (!registeredDate.equals(other.registeredDate))
return false;
if (salary == null) {
if (other.salary != null)
return false;
} else if (!salary.equals(other.salary))
return false; return false;
return true; return true;
} }

View File

@ -26,12 +26,12 @@ public class PersonController {
public void init() { public void init() {
// @formatter:off // @formatter:off
personRepository = new ArrayList<>(Arrays.asList( personRepository = new ArrayList<>(Arrays.asList(
new Person("Jhon", "jhon@test.com", 20, LocalDate.of(2019, 9, 9), BigDecimal.valueOf(1000)), new Person(1, "Jhon", "jhon@test.com", 20, LocalDate.of(2019, 9, 9), BigDecimal.valueOf(1000)),
new Person("Jhon", "jhon1@test.com", 20, LocalDate.of(2019, 9, 9), BigDecimal.valueOf(1500)), new Person(2, "Jhon", "jhon1@test.com", 20, LocalDate.of(2019, 9, 9), BigDecimal.valueOf(1500)),
new Person("Jhon", null, 20, LocalDate.of(2019, 9, 9), BigDecimal.valueOf(1000)), new Person(3, "Jhon", null, 20, LocalDate.of(2019, 9, 9), BigDecimal.valueOf(1000)),
new Person("Tom", "tom@test.com", 21, LocalDate.of(2019, 9, 9), BigDecimal.valueOf(1500)), new Person(4, "Tom", "tom@test.com", 21, LocalDate.of(2019, 9, 9), BigDecimal.valueOf(1500)),
new Person("Mark", "mark@test.com", 21, LocalDate.of(2019, 9, 9), BigDecimal.valueOf(1200)), new Person(5, "Mark", "mark@test.com", 21, LocalDate.of(2019, 9, 9), BigDecimal.valueOf(1200)),
new Person("Julia", "jhon@test.com", 20, LocalDate.of(2019, 9, 9), BigDecimal.valueOf(1000)))); new Person(6, "Julia", "jhon@test.com", 20, LocalDate.of(2019, 9, 9), BigDecimal.valueOf(1000))));
// @formatter:on // @formatter:on
} }

View File

@ -30,15 +30,14 @@ public class JsonbIntegrationTest {
ResponseEntity<Person> response = template.withBasicAuth(username, password) ResponseEntity<Person> response = template.withBasicAuth(username, password)
.getForEntity("/person/1", Person.class); .getForEntity("/person/1", Person.class);
Person person = response.getBody(); Person person = response.getBody();
assertTrue(person.equals(new Person("Jhon", "jhon1@test.com", 0, LocalDate.of(2019, 9, 9), BigDecimal.valueOf(1500.0)))); assertTrue(person.equals(new Person(2, "Jhon", "jhon1@test.com", 0, LocalDate.of(2019, 9, 9), BigDecimal.valueOf(1500.0))));
} }
@Test @Test
public void whenSendPostAPerson_thenGetOkStatus() { public void whenSendPostAPerson_thenGetOkStatus() {
ResponseEntity<Boolean> response = template.withBasicAuth(username, password) ResponseEntity<Boolean> response = template.withBasicAuth(username, password)
.postForEntity("/person", "{\"birthDate\":\"07-09-2017\",\"email\":\"jhon1@test.com\",\"person-name\":\"Jhon\"}", Boolean.class); .postForEntity("/person", "{\"birthDate\":\"07-09-2017\",\"email\":\"jhon1@test.com\",\"person-name\":\"Jhon\",\"id\":10}", Boolean.class);
boolean value = response.getBody(); assertTrue(response.getBody());
assertTrue(true == value);
} }
} }

View File

@ -22,15 +22,15 @@ public class JsonbTest {
@Test @Test
public void givenPersonObject_whenSerializeWithJsonb_thenGetPersonJson() { public void givenPersonObject_whenSerializeWithJsonb_thenGetPersonJson() {
Person person = new Person("Jhon", "jhon@test.com", 20, LocalDate.of(2019, 9, 7), BigDecimal.valueOf(1000)); Person person = new Person(1, "Jhon", "jhon@test.com", 20, LocalDate.of(2019, 9, 7), BigDecimal.valueOf(1000));
String jsonPerson = jsonb.toJson(person); String jsonPerson = jsonb.toJson(person);
assertTrue("{\"email\":\"jhon@test.com\",\"person-name\":\"Jhon\",\"registeredDate\":\"07-09-2019\",\"salary\":\"1000.0\"}".equals(jsonPerson)); assertTrue("{\"email\":\"jhon@test.com\",\"id\":1,\"person-name\":\"Jhon\",\"registeredDate\":\"07-09-2019\",\"salary\":\"1000.0\"}".equals(jsonPerson));
} }
@Test @Test
public void givenPersonJson_whenDeserializeWithJsonb_thenGetPersonObject() { public void givenPersonJson_whenDeserializeWithJsonb_thenGetPersonObject() {
Person person = new Person("Jhon", "jhon@test.com", 0, LocalDate.of(2019, 9, 7), BigDecimal.valueOf(1000.0)); Person person = new Person(1, "Jhon", "jhon@test.com", 0, LocalDate.of(2019, 9, 7), BigDecimal.valueOf(1000.0));
String jsonPerson = "{\"email\":\"jhon@test.com\",\"person-name\":\"Jhon\",\"registeredDate\":\"07-09-2019\",\"salary\":\"1000.0\"}"; String jsonPerson = "{\"email\":\"jhon@test.com\",\"id\":1,\"person-name\":\"Jhon\",\"registeredDate\":\"07-09-2019\",\"salary\":\"1000.0\"}";
assertTrue(jsonb.fromJson(jsonPerson, Person.class) assertTrue(jsonb.fromJson(jsonPerson, Person.class)
.equals(person)); .equals(person));
} }