Guest User

Untitled

a guest
Oct 7th, 2022
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 2.89 KB | None | 0 0
  1. package com.gabrielsantanaa.todoapi.domain.entities
  2.  
  3. import java.time.LocalDateTime
  4.  
  5. data class TaskEntity(
  6.     var id: Long,
  7.     var title: String,
  8.     var description: String,
  9.     var createAt: LocalDateTime,
  10.     var status: TaskStatusEntity
  11. ) {
  12.  
  13.     private val subtasks = mutableListOf<TaskEntity>()
  14.  
  15.     fun addSubtask(task: TaskEntity) {
  16.         if (subtasks.find { it.id == task.id } != null) throw IllegalArgumentException()
  17.         subtasks.add(task)
  18.     }
  19.  
  20.     val titleIsValid: Boolean
  21.         get() {
  22.             return title.length in 5..29
  23.         }
  24.  
  25.     val descriptionIsValid: Boolean
  26.         get() {
  27.             return description.length in 10..200
  28.         }
  29.  
  30.     val statusIsValid: Boolean
  31.         get() {
  32.             return (status as? TaskStatusEntity.Done)?.let {
  33.                 !it.doneAt.isBefore(createAt)
  34.             } ?: run { false }
  35.         }
  36.  
  37. }
  38.  
  39. package com.gabrielsantanaa.todoapi.domain.entities
  40.  
  41. import org.assertj.core.api.Assertions.assertThat
  42. import org.junit.jupiter.api.Test
  43. import java.time.LocalDateTime
  44.  
  45. class TaskEntityTest {
  46.  
  47.     @Test
  48.     fun `title length less than 5 should be invalid`() {
  49.         val taskEntity = TaskEntity(0, "papa", "", LocalDateTime.now(), TaskStatusEntity.ToDo)
  50.  
  51.         val result = taskEntity.titleIsValid
  52.  
  53.         assertThat(result).isFalse
  54.     }
  55.  
  56.     @Test
  57.     fun `title length bigger than 30 should be invalid`() {
  58.         val taskEntity = TaskEntity(0, "aasasasasaasasasasasasasasasasasasasasaasasasasasasasasa", "", LocalDateTime.now(), TaskStatusEntity.ToDo)
  59.  
  60.         val result = taskEntity.titleIsValid
  61.  
  62.         assertThat(result).isFalse
  63.     }
  64.  
  65.     @Test
  66.     fun `title length between 5 and 30 should be valid`() {
  67.         val taskEntity = TaskEntity(0, "aasasasasaasasasasasasa", "", LocalDateTime.now(), TaskStatusEntity.ToDo)
  68.  
  69.         val result = taskEntity.titleIsValid
  70.  
  71.         assertThat(result).isTrue
  72.     }
  73.  
  74.     @Test
  75.     fun `description length less than 10 should be invalid`() {
  76.         val taskEntity = TaskEntity(0, "", "aa", LocalDateTime.now(), TaskStatusEntity.ToDo)
  77.  
  78.         val result = taskEntity.descriptionIsValid
  79.  
  80.         assertThat(result).isFalse
  81.     }
  82.  
  83.     @Test
  84.     fun `description length bigger than 200 should be invalid`() {
  85.         val taskEntity = TaskEntity(0, "", "", LocalDateTime.now(), TaskStatusEntity.ToDo)
  86.         var description = ""
  87.         for (x in 1..201) {
  88.             description += x.toShort()
  89.         }
  90.         taskEntity.description = description
  91.  
  92.         val result = taskEntity.descriptionIsValid
  93.  
  94.         assertThat(result).isFalse
  95.     }
  96.  
  97.     @Test
  98.     fun `description length between 10 and 200 should be valid`() {
  99.         val taskEntity = TaskEntity(0, "", "aasasasasaasasasasasasa", LocalDateTime.now(), TaskStatusEntity.ToDo)
  100.  
  101.         val result = taskEntity.descriptionIsValid
  102.  
  103.         assertThat(result).isTrue
  104.     }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment