forked from spring-guides/tut-rest
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add integration tests for hateoas controller
Reproduce spring-projects/spring-hateoas#2160
- Loading branch information
Showing
2 changed files
with
123 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,67 @@ | ||
package payroll; | ||
|
||
import java.util.Objects; | ||
|
||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.Id; | ||
import java.util.Objects; | ||
|
||
@Entity | ||
class Employee { | ||
public class Employee { | ||
|
||
private @Id @GeneratedValue Long id; | ||
private String name; | ||
private String role; | ||
private @Id @GeneratedValue Long id; | ||
private String name; | ||
private String role; | ||
|
||
Employee() {} | ||
public Employee() {} | ||
|
||
Employee(String name, String role) { | ||
Employee(String name, String role) { | ||
|
||
this.name = name; | ||
this.role = role; | ||
} | ||
this.name = name; | ||
this.role = role; | ||
} | ||
|
||
public Long getId() { | ||
return this.id; | ||
} | ||
public Long getId() { | ||
return this.id; | ||
} | ||
|
||
public String getName() { | ||
return this.name; | ||
} | ||
public String getName() { | ||
return this.name; | ||
} | ||
|
||
public String getRole() { | ||
return this.role; | ||
} | ||
public String getRole() { | ||
return this.role; | ||
} | ||
|
||
public void setId(Long id) { | ||
this.id = id; | ||
} | ||
public void setId(Long id) { | ||
this.id = id; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public void setRole(String role) { | ||
this.role = role; | ||
} | ||
public void setRole(String role) { | ||
this.role = role; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
@Override | ||
public boolean equals(Object o) { | ||
|
||
if (this == o) | ||
return true; | ||
if (!(o instanceof Employee)) | ||
return false; | ||
Employee employee = (Employee) o; | ||
return Objects.equals(this.id, employee.id) && Objects.equals(this.name, employee.name) | ||
&& Objects.equals(this.role, employee.role); | ||
} | ||
if (this == o) return true; | ||
if (!(o instanceof Employee)) return false; | ||
Employee employee = (Employee) o; | ||
return Objects.equals(this.id, employee.id) | ||
&& Objects.equals(this.name, employee.name) | ||
&& Objects.equals(this.role, employee.role); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(this.id, this.name, this.role); | ||
} | ||
@Override | ||
public int hashCode() { | ||
return Objects.hash(this.id, this.name, this.role); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Employee{" + "id=" + this.id + ", name='" + this.name + '\'' + ", role='" + this.role + '\'' + '}'; | ||
} | ||
@Override | ||
public String toString() { | ||
return "Employee{" + "id=" + this.id + ", name='" + this.name + '\'' + ", role='" + this.role + '\'' + '}'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.boot.test.web.client.TestRestTemplate; | ||
import org.springframework.boot.web.client.RestTemplateBuilder; | ||
import org.springframework.core.ParameterizedTypeReference; | ||
import org.springframework.hateoas.CollectionModel; | ||
import org.springframework.hateoas.EntityModel; | ||
import org.springframework.hateoas.MediaTypes; | ||
import org.springframework.http.HttpEntity; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.HttpMethod; | ||
import payroll.Employee; | ||
import payroll.PayrollApplication; | ||
|
||
import static org.assertj.core.api.AssertionsForClassTypes.assertThat; | ||
|
||
@SpringBootTest(classes = PayrollApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) | ||
public class EmployeeControllerIT { | ||
|
||
@Value(value = "${local.server.port}") | ||
private int port; | ||
|
||
private TestRestTemplate rest; | ||
|
||
@BeforeEach | ||
public void setup() { | ||
rest = new TestRestTemplate(new RestTemplateBuilder().rootUri("http://localhost:" + port)); | ||
} | ||
|
||
@Test | ||
public void testGettingAllEmployeesWithAcceptHalFormsHeader() { | ||
final var headers = new HttpHeaders(); | ||
headers.add("Accept", MediaTypes.HAL_FORMS_JSON_VALUE); | ||
var httpEntity = new HttpEntity<>(headers); | ||
var response = rest.exchange( | ||
"/employees", | ||
HttpMethod.GET, | ||
httpEntity, | ||
new ParameterizedTypeReference<CollectionModel<EntityModel<Employee>>>() {}); | ||
var resources = response.getBody(); | ||
assertThat(resources.getContent()).isNotNull(); | ||
var employees = resources.getContent(); | ||
System.out.println(resources); | ||
assertThat(employees).isNotNull(); | ||
assertThat(employees.size()).isEqualTo(2); | ||
} | ||
|
||
@Test | ||
public void testGettingAllEmployeesWithoutAcceptHalFormsHeader() { | ||
var response = rest.exchange( | ||
"/employees", | ||
HttpMethod.GET, | ||
new HttpEntity<>(null, null), | ||
new ParameterizedTypeReference<CollectionModel<EntityModel<Employee>>>() {}); | ||
var resources = response.getBody(); | ||
assertThat(resources.getContent()).isNotNull(); | ||
var employees = resources.getContent(); | ||
// NB! resources returns empty content in collection model | ||
System.out.println(resources); | ||
assertThat(employees).isNotNull(); | ||
assertThat(employees.size()).isEqualTo(2); | ||
} | ||
|
||
@Test | ||
public void testGettingAllEmployeesWithStringResponse() { | ||
var response = rest.exchange( | ||
"/employees", | ||
HttpMethod.GET, | ||
new HttpEntity<>(null, null), | ||
String.class); // Deserialize to String to check that body is not empty | ||
var resources = response.getBody(); | ||
assertThat(resources).isNotNull(); | ||
// NB! when deserializing to String, the response body is not empty | ||
System.out.println(resources); | ||
} | ||
} |