Skip to content

Commit

Permalink
Add integration tests for hateoas controller
Browse files Browse the repository at this point in the history
  • Loading branch information
serpro69 committed Jun 25, 2024
1 parent 87d80b9 commit 387c62d
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 47 deletions.
92 changes: 45 additions & 47 deletions rest/src/main/java/payroll/Employee.java
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 + '\'' + '}';
}
}
78 changes: 78 additions & 0 deletions rest/src/test/java/EmployeeControllerIT.java
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);
}
}

0 comments on commit 387c62d

Please sign in to comment.