Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.gabrielsantanaa.todoapi.domain.entities
- import java.time.LocalDateTime
- data class TaskEntity(
- var id: Long,
- var title: String,
- var description: String,
- var createAt: LocalDateTime,
- var status: TaskStatusEntity
- ) {
- private val subtasks = mutableListOf<TaskEntity>()
- fun addSubtask(task: TaskEntity) {
- if (subtasks.find { it.id == task.id } != null) throw IllegalArgumentException()
- subtasks.add(task)
- }
- val titleIsValid: Boolean
- get() {
- return title.length in 5..29
- }
- val descriptionIsValid: Boolean
- get() {
- return description.length in 10..200
- }
- val statusIsValid: Boolean
- get() {
- return (status as? TaskStatusEntity.Done)?.let {
- !it.doneAt.isBefore(createAt)
- } ?: run { false }
- }
- }
- package com.gabrielsantanaa.todoapi.domain.entities
- import org.assertj.core.api.Assertions.assertThat
- import org.junit.jupiter.api.Test
- import java.time.LocalDateTime
- class TaskEntityTest {
- @Test
- fun `title length less than 5 should be invalid`() {
- val taskEntity = TaskEntity(0, "papa", "", LocalDateTime.now(), TaskStatusEntity.ToDo)
- val result = taskEntity.titleIsValid
- assertThat(result).isFalse
- }
- @Test
- fun `title length bigger than 30 should be invalid`() {
- val taskEntity = TaskEntity(0, "aasasasasaasasasasasasasasasasasasasasaasasasasasasasasa", "", LocalDateTime.now(), TaskStatusEntity.ToDo)
- val result = taskEntity.titleIsValid
- assertThat(result).isFalse
- }
- @Test
- fun `title length between 5 and 30 should be valid`() {
- val taskEntity = TaskEntity(0, "aasasasasaasasasasasasa", "", LocalDateTime.now(), TaskStatusEntity.ToDo)
- val result = taskEntity.titleIsValid
- assertThat(result).isTrue
- }
- @Test
- fun `description length less than 10 should be invalid`() {
- val taskEntity = TaskEntity(0, "", "aa", LocalDateTime.now(), TaskStatusEntity.ToDo)
- val result = taskEntity.descriptionIsValid
- assertThat(result).isFalse
- }
- @Test
- fun `description length bigger than 200 should be invalid`() {
- val taskEntity = TaskEntity(0, "", "", LocalDateTime.now(), TaskStatusEntity.ToDo)
- var description = ""
- for (x in 1..201) {
- description += x.toShort()
- }
- taskEntity.description = description
- val result = taskEntity.descriptionIsValid
- assertThat(result).isFalse
- }
- @Test
- fun `description length between 10 and 200 should be valid`() {
- val taskEntity = TaskEntity(0, "", "aasasasasaasasasasasasa", LocalDateTime.now(), TaskStatusEntity.ToDo)
- val result = taskEntity.descriptionIsValid
- assertThat(result).isTrue
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment