Skip to content

Commit

Permalink
✨ Add method to find all environments linked to project
Browse files Browse the repository at this point in the history
  • Loading branch information
Lysoun committed Apr 30, 2024
1 parent 6442e36 commit 11e602a
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package dao.environment

import environment.*
import environment.Environment
import environment.EnvironmentCreationRequest
import environment.EnvironmentDaoInterface
import environment.EnvironmentNameUpdateRequest
import generated.domain.tables.references.DM_ENVIRONMENT
import org.jooq.DSLContext
import org.jooq.impl.DSL.defaultValue
Expand Down Expand Up @@ -35,4 +38,9 @@ class EnvironmentDao(val dslContext: DSLContext): EnvironmentDaoInterface {
override fun findOneById(id: UUID): Environment? =
dslContext.fetchOne(DM_ENVIRONMENT, DM_ENVIRONMENT.ID.eq(id))
?.into(Environment::class.java)

override fun findAllForProject(projectRef: UUID): List<Environment> = dslContext.select()
.from(DM_ENVIRONMENT)
.where(DM_ENVIRONMENT.FK_PROJECT_REF.eq(projectRef))
.fetchInto(Environment::class.java)
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import generated.domain.tables.references.DM_ENVIRONMENT
import org.junit.jupiter.api.BeforeAll
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
import strikt.api.expect
import strikt.api.expectThat
import strikt.assertions.containsExactlyInAnyOrder
import strikt.assertions.isEqualTo
import strikt.assertions.isNotNull
import strikt.assertions.isNull
Expand Down Expand Up @@ -222,4 +224,50 @@ internal class EnvironmentDaoTest: AbstractDaoTest() {
}
}

@Nested
inner class TestFindAllForProject {
@Test
fun `should return all environments linked to project`() {
// Given
val environment1 = environmentDao.insert(buildEnvironmentCreationRequest(
name = "findAllEnvironment1",
smallName = "findAllSmallNameEnvironment1",
fkProjectRef = projectId
))
val environment2 = environmentDao.insert(buildEnvironmentCreationRequest(
name = "findAllEnvironment2",
smallName = "findAllSmallNameEnvironment2",
fkProjectRef = projectId
))

// When
val environments = environmentDao.findAllForProject(projectId)

// Then
expectThat(environments).containsExactlyInAnyOrder(environment1, environment2)
}

@Test
fun `should only return environments linked to project`() {
// Given
val anotherProject = projectDao.insert(buildProjectCreationRequest())

environmentDao.insert(buildEnvironmentCreationRequest(
name = "findAllEnvironment1",
smallName = "findAllSmallNameEnvironment1",
fkProjectRef = anotherProject.id
))
val environment2 = environmentDao.insert(buildEnvironmentCreationRequest(
name = "findAllEnvironment2",
smallName = "findAllSmallNameEnvironment2",
fkProjectRef = projectId
))

// When
val environments = environmentDao.findAllForProject(projectId)

// Then
expectThat(environments).containsExactlyInAnyOrder(environment2)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,30 @@ open class Environment(
open val name: String,
open val fkProjectRef: UUID,
open val smallName: String
)
) {
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false

other as Environment

if (id != other.id) return false
if (name != other.name) return false
if (fkProjectRef != other.fkProjectRef) return false
if (smallName != other.smallName) return false

return true
}

override fun hashCode(): Int {
var result = id.hashCode()
result = 31 * result + name.hashCode()
result = 31 * result + fkProjectRef.hashCode()
result = 31 * result + smallName.hashCode()
return result
}

override fun toString(): String {
return "Environment(id=$id, name='$name', fkProjectRef=$fkProjectRef, smallName='$smallName')"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ interface EnvironmentDaoInterface {
fun updateEnvironmentName(environmentId: UUID, updateRequest: EnvironmentNameUpdateRequest): Environment?
fun delete(id: UUID)
fun findOneById(id: UUID): Environment?
fun findAllForProject(projectRef: UUID): List<Environment>
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ class EnvironmentService(private val environmentDaoInterface: EnvironmentDaoInte
fun delete(id: UUID) = environmentDaoInterface.delete(id)

fun findOneById(id: UUID): Environment? = environmentDaoInterface.findOneById(id)
fun findAllForProject(projectRef: UUID): List<Environment> = environmentDaoInterface.findAllForProject(projectRef)
}

0 comments on commit 11e602a

Please sign in to comment.