-
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.
added first scenario with creation task process
- Loading branch information
Showing
11 changed files
with
512 additions
and
0 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
src/main/java/rocks/byivo/todolist/builders/TaskBuilder.java
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,49 @@ | ||
package rocks.byivo.todolist.builders; | ||
|
||
import java.util.Date; | ||
|
||
import rocks.byivo.todolist.model.Task; | ||
import rocks.byivo.todolist.model.TaskStatus; | ||
|
||
public class TaskBuilder { | ||
|
||
private Task task; | ||
|
||
public TaskBuilder() { | ||
task = new Task(); | ||
} | ||
|
||
public TaskBuilder withIdTask(Long idTask) { | ||
task.setIdTask(idTask); | ||
return this; | ||
} | ||
|
||
public TaskBuilder withTitle(String title) { | ||
task.setTitle(title); | ||
return this; | ||
} | ||
|
||
public TaskBuilder withDescription(String description) { | ||
task.setDescription(description); | ||
return this; | ||
} | ||
|
||
public TaskBuilder withStatus(TaskStatus status) { | ||
task.setStatus(status); | ||
return this; | ||
} | ||
|
||
public TaskBuilder withCreatedAt(Date createdAt) { | ||
task.setCreatedAt(createdAt); | ||
return this; | ||
} | ||
|
||
public TaskBuilder withUpdatedAt(Date updatedAt) { | ||
task.setUpdatedAt(updatedAt); | ||
return this; | ||
} | ||
|
||
public Task build() { | ||
return task; | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
src/main/java/rocks/byivo/todolist/controllers/TaskController.java
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,31 @@ | ||
package rocks.byivo.todolist.controllers; | ||
|
||
import static org.springframework.web.bind.annotation.RequestMethod.POST; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import rocks.byivo.todolist.dto.TaskDTO; | ||
import rocks.byivo.todolist.model.Task; | ||
import rocks.byivo.todolist.services.TaskService; | ||
|
||
@RestController("tasks") | ||
public class TaskController { | ||
|
||
private TaskService taskService; | ||
|
||
@Autowired | ||
public TaskController(TaskService taskService) { | ||
this.taskService = taskService; | ||
} | ||
|
||
@RequestMapping(method=POST) | ||
ResponseEntity<TaskDTO> insert(@RequestBody TaskDTO newTaskFromClient) { | ||
Task createdTask = taskService.createTaskFrom(newTaskFromClient); | ||
return new ResponseEntity<TaskDTO>(createdTask.toTransferObject(), HttpStatus.CREATED); | ||
} | ||
} |
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,59 @@ | ||
package rocks.byivo.todolist.dto; | ||
|
||
import java.io.Serializable; | ||
|
||
public class TaskDTO implements Serializable { | ||
|
||
private static final long serialVersionUID = 1L; | ||
|
||
private Long idTask; | ||
|
||
private String title; | ||
|
||
private String description; | ||
|
||
private String status; | ||
|
||
private String readableStatus; | ||
|
||
public Long getIdTask() { | ||
return idTask; | ||
} | ||
|
||
public void setIdTask(Long idTask) { | ||
this.idTask = idTask; | ||
} | ||
|
||
public String getTitle() { | ||
return title; | ||
} | ||
|
||
public void setTitle(String title) { | ||
this.title = title; | ||
} | ||
|
||
public String getDescription() { | ||
return description; | ||
} | ||
|
||
public void setDescription(String description) { | ||
this.description = description; | ||
} | ||
|
||
public String getStatus() { | ||
return status; | ||
} | ||
|
||
public void setStatus(String status) { | ||
this.status = status; | ||
} | ||
|
||
public String getReadableStatus() { | ||
return readableStatus; | ||
} | ||
|
||
public void setReadableStatus(String readableStatus) { | ||
this.readableStatus = readableStatus; | ||
} | ||
|
||
} |
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,84 @@ | ||
package rocks.byivo.todolist.model; | ||
|
||
|
||
import java.util.Date; | ||
|
||
import rocks.byivo.todolist.dto.TaskDTO; | ||
|
||
public class Task { | ||
|
||
private Long idTask; | ||
|
||
private String title; | ||
|
||
private String description; | ||
|
||
private TaskStatus status; | ||
|
||
private Date createdAt; | ||
|
||
private Date updatedAt; | ||
|
||
public Long getIdTask() { | ||
return idTask; | ||
} | ||
|
||
public void setIdTask(Long idTask) { | ||
this.idTask = idTask; | ||
} | ||
|
||
public String getTitle() { | ||
return title; | ||
} | ||
|
||
public void setTitle(String title) { | ||
this.title = title; | ||
} | ||
|
||
public String getDescription() { | ||
return description; | ||
} | ||
|
||
public void setDescription(String description) { | ||
this.description = description; | ||
} | ||
|
||
public TaskStatus getStatus() { | ||
return status; | ||
} | ||
|
||
public void setStatus(TaskStatus status) { | ||
this.status = status; | ||
} | ||
|
||
public Date getCreatedAt() { | ||
return createdAt; | ||
} | ||
|
||
public void setCreatedAt(Date createdAt) { | ||
this.createdAt = createdAt; | ||
} | ||
|
||
public Date getUpdatedAt() { | ||
return updatedAt; | ||
} | ||
|
||
public void setUpdatedAt(Date updatedAt) { | ||
this.updatedAt = updatedAt; | ||
} | ||
|
||
public TaskDTO toTransferObject() { | ||
TaskDTO taskDTO = new TaskDTO(); | ||
taskDTO.setIdTask(this.getIdTask()); | ||
taskDTO.setTitle(this.getTitle()); | ||
taskDTO.setDescription(this.getDescription()); | ||
|
||
if(status != null) { | ||
taskDTO.setStatus(status.name()); | ||
taskDTO.setReadableStatus(status.toString()); | ||
} | ||
|
||
return taskDTO; | ||
} | ||
|
||
} |
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,23 @@ | ||
package rocks.byivo.todolist.model; | ||
|
||
import com.fasterxml.jackson.annotation.JsonValue; | ||
|
||
public enum TaskStatus { | ||
|
||
TO_DO("To Do"), | ||
DOING("Doing"), | ||
DONE("Done"), | ||
; | ||
|
||
private String humanReadableName; | ||
|
||
private TaskStatus(String humanReadableName) { | ||
this.humanReadableName = humanReadableName; | ||
} | ||
|
||
@Override | ||
@JsonValue | ||
public String toString() { | ||
return humanReadableName; | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
src/main/java/rocks/byivo/todolist/repositories/TaskRepository.java
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,8 @@ | ||
package rocks.byivo.todolist.repositories; | ||
|
||
import rocks.byivo.todolist.model.Task; | ||
|
||
public interface TaskRepository { | ||
|
||
Task create(Task newTask); | ||
} |
17 changes: 17 additions & 0 deletions
17
src/main/java/rocks/byivo/todolist/repositories/impl/FakeRepository.java
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,17 @@ | ||
package rocks.byivo.todolist.repositories.impl; | ||
|
||
import org.springframework.stereotype.Repository; | ||
|
||
import rocks.byivo.todolist.model.Task; | ||
import rocks.byivo.todolist.repositories.TaskRepository; | ||
|
||
@Repository | ||
public class FakeRepository implements TaskRepository { | ||
|
||
@Override | ||
public Task create(Task newTask) { | ||
newTask.setIdTask(1L); | ||
return newTask; | ||
} | ||
|
||
} |
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,9 @@ | ||
package rocks.byivo.todolist.services; | ||
|
||
import rocks.byivo.todolist.dto.TaskDTO; | ||
import rocks.byivo.todolist.model.Task; | ||
|
||
public interface TaskService { | ||
|
||
Task createTaskFrom(TaskDTO taskFromClient); | ||
} |
43 changes: 43 additions & 0 deletions
43
src/main/java/rocks/byivo/todolist/services/impl/TaskServiceImpl.java
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,43 @@ | ||
package rocks.byivo.todolist.services.impl; | ||
|
||
import java.util.Date; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Service; | ||
|
||
import rocks.byivo.todolist.builders.TaskBuilder; | ||
import rocks.byivo.todolist.dto.TaskDTO; | ||
import rocks.byivo.todolist.model.Task; | ||
import rocks.byivo.todolist.model.TaskStatus; | ||
import rocks.byivo.todolist.repositories.TaskRepository; | ||
import rocks.byivo.todolist.services.TaskService; | ||
|
||
@Service | ||
public class TaskServiceImpl implements TaskService { | ||
|
||
private TaskRepository taskRepository; | ||
|
||
@Autowired | ||
public TaskServiceImpl(TaskRepository taskRepository) { | ||
this.taskRepository = taskRepository; | ||
} | ||
|
||
@Override | ||
public Task createTaskFrom(TaskDTO taskFromClient) { | ||
Task taskToBeCreated = createTaskWithInfoFrom(taskFromClient); | ||
return taskRepository.create(taskToBeCreated); | ||
} | ||
|
||
private Task createTaskWithInfoFrom(TaskDTO taskFromClient) { | ||
Date creationDate = new Date(); | ||
|
||
return new TaskBuilder() | ||
.withTitle(taskFromClient.getTitle()) | ||
.withDescription(taskFromClient.getDescription()) | ||
.withStatus(TaskStatus.TO_DO) | ||
.withCreatedAt(creationDate) | ||
.withUpdatedAt(creationDate) | ||
.build(); | ||
} | ||
|
||
} |
60 changes: 60 additions & 0 deletions
60
src/test/java/rocks/byivo/todolist/controllers/TaskControllerIT.java
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,60 @@ | ||
package rocks.byivo.todolist.controllers; | ||
|
||
import static io.restassured.RestAssured.given; | ||
import static org.hamcrest.Matchers.equalTo; | ||
import static rocks.byivo.todolist.ContextPathConfig.BASE_PATH; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.boot.web.server.LocalServerPort; | ||
import org.springframework.test.context.junit4.SpringRunner; | ||
|
||
import io.restassured.RestAssured; | ||
import io.restassured.http.ContentType; | ||
import rocks.byivo.todolist.builders.TaskBuilder; | ||
import rocks.byivo.todolist.dto.TaskDTO; | ||
import rocks.byivo.todolist.model.TaskStatus; | ||
|
||
@RunWith(SpringRunner.class) | ||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) | ||
public class TaskControllerIT { | ||
|
||
private static final String RANDOM_DESCRIPTION = "A random description"; | ||
private static final String FAKE_TITLE = "Fake Title"; | ||
private static final String TASKS_PATH = BASE_PATH +"/tasks"; | ||
|
||
@LocalServerPort | ||
int port; | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
RestAssured.port = port; | ||
} | ||
|
||
@Test | ||
public void | ||
should_create_a_TODO_task_with_provided_title_and_description() { | ||
TaskDTO newTaskFromClient = new TaskBuilder() | ||
.withTitle(FAKE_TITLE) | ||
.withDescription(RANDOM_DESCRIPTION) | ||
.build() | ||
.toTransferObject(); | ||
|
||
given() | ||
.basePath(TASKS_PATH) | ||
.contentType(ContentType.JSON) | ||
.body(newTaskFromClient) | ||
.when() | ||
.post() | ||
.then() | ||
.statusCode(201) | ||
.body("idTask", equalTo(1)) | ||
.body("title", equalTo(FAKE_TITLE)) | ||
.body("description", equalTo(RANDOM_DESCRIPTION)) | ||
.body("status", equalTo(TaskStatus.TO_DO.name())) | ||
.body("readableStatus", equalTo(TaskStatus.TO_DO.toString())); | ||
} | ||
|
||
} |
Oops, something went wrong.