Skip to content

Commit

Permalink
test: add test for application services
Browse files Browse the repository at this point in the history
  • Loading branch information
andrea-acampora committed Feb 26, 2023
1 parent e4b8f7b commit 4a6e62e
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
48 changes: 48 additions & 0 deletions src/test/kotlin/application/service/TestApplicationService.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright (c) 2023. Smart Operating Block
*
* Use of this source code is governed by an MIT-style
* license that can be found in the LICENSE file or at
* https://opensource.org/licenses/MIT.
*/

package application.service

import application.controller.UserController
import application.service.mock.MockDBManager
import entity.user.User
import io.kotest.core.spec.style.StringSpec
import io.kotest.matchers.shouldBe
import io.kotest.matchers.shouldNotBe

class TestApplicationService : StringSpec({

var userController = UserController(MockDBManager())
val mockUser = User("00000001", "User1999!")

beforeEach {
userController = UserController(MockDBManager())
}

"The application service should be able to create a User!" {
userController.createUser(mockUser).also {
it shouldNotBe null
userController.getUser(mockUser.userId) shouldBe mockUser
}
}

"The application service should be able to get a User!" {
userController.createUser(mockUser)
userController.getUser(mockUser.userId).also {
it shouldNotBe null
it shouldBe mockUser
}
}

"The application service should be able to delete a User!" {
userController.createUser(mockUser)
userController.deleteUser(mockUser.userId).also {
it shouldBe true
}
}
})
42 changes: 42 additions & 0 deletions src/test/kotlin/application/service/mock/MockDBManager.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright (c) 2023. Smart Operating Block
*
* Use of this source code is governed by an MIT-style
* license that can be found in the LICENSE file or at
* https://opensource.org/licenses/MIT.
*/

package application.service.mock

import application.controller.manager.HealthProfessionalDatabaseManager
import application.controller.manager.UserDatabaseManager
import entity.healthprofessional.HealthProfessionalData.HealthProfessional
import entity.user.User

class MockDBManager : UserDatabaseManager, HealthProfessionalDatabaseManager {

private val users = mutableSetOf<User>()
private val healthProfessionals = mutableSetOf<HealthProfessional>()

override fun createHealthProfessional(healthProfessional: HealthProfessional): HealthProfessional? {
healthProfessionals.add(healthProfessional)
return getHealthProfessional(healthProfessional.healthProfessionalId)
}

override fun deleteHealthProfessional(healthProfessionalId: String): Boolean =
healthProfessionals.removeIf { it.healthProfessionalId == healthProfessionalId }

override fun getHealthProfessional(healthProfessionalId: String): HealthProfessional? =
healthProfessionals.find { it.healthProfessionalId == healthProfessionalId }

override fun createUser(user: User): User? {
users.add(user)
return getUser(user.userId)
}

override fun deleteUser(userId: String): Boolean =
users.removeIf { it.userId == userId }

override fun getUser(userId: String): User? =
users.find { it.userId == userId }
}

0 comments on commit 4a6e62e

Please sign in to comment.