In this chapter we are going to create service layer which exposes backend functionality via REST services with JSON transport.
All we need to do is creating the class org.example.app.task.service.TaskService
@Path("/task")
public class TaskService {
@Inject
private UcFindTaskList ucFindTaskList;
// ...
@GET
@Path("/list/{id}")
public TaskListEto findTaskList(@PathParam("id") Long id) {
TaskListEto task = this.ucFindTaskList.findById(id);
if (task == null) {
throw new NotFoundException("TaskList with id " + id + " does not exist.");
}
return task;
}
// ...
}
Now create all endpoints required for implementing the todo list application as expected by the frontend. As we’re following a design-first approach you must implement the REST endpoints as defined in the OpenAPI specification, which is available in the backend at src/main/resources/META-INF/openapi.yaml
.
You can view the documentation of our pre-defined API using Swagger UI reachable at http://localhost:8080/q/swagger-ui and use it for testing it manually.
To properly test our REST service, we define a tests in the class org.example.app.task.service.TaskServiceTest
.
You can write tests which integrate with all below layers:
@QuarkusTest
public class TaskServiceTest extends Assertions {
/** Test of {@link TaskService#findTaskList(Long)}. */
@Test
public void testFindTaskList() {
given().when().get("/task/list/1").then().statusCode(200)
.body(is("{\"id\":1,\"version\":0,\"title\":\"Shopping List\"}"));
}
// ... test other service operations here ...
}
Or write unit tests which mock the logic layer:
import static net.javacrumbs.jsonunit.JsonMatchers.jsonEquals;
@QuarkusTest
public class TaskServiceTest {
@InjectMock
UcFindTaskList findTaskList;
@Test
void testFindTaskListSuccessCase() {
given(TaskServiceTest.this.findTaskList.findById(anyLong())).willReturn(aTaskList());
// aTaskList() is a helper method which creates a pre-populated TaskListEto
given()
.when()
.get("/task/list/123")
.then()
.statusCode(200)
.body(jsonEquals("{\"id\":123,\"version\":1,\"title\":\"Shopping List\"}"));
}
}