Skip to content

Commit

Permalink
Merge pull request #88 from banane-io/feature/unit_tests_action_service
Browse files Browse the repository at this point in the history
Feature/unit tests action service
  • Loading branch information
mrheaumeproulx committed May 27, 2020
2 parents 95cde4c + 815eabc commit f41c35c
Show file tree
Hide file tree
Showing 36 changed files with 263 additions and 25 deletions.
13 changes: 13 additions & 0 deletions pom.xml
Expand Up @@ -114,11 +114,24 @@
<artifactId>kotlin-stdlib-jdk8</artifactId>
<version>${kotlin.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.mockk</groupId>
<artifactId>mockk</artifactId>
<version>1.10.0</version>
<scope>test</scope>
</dependency>

</dependencies>


<build>
<sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
<testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Expand Up @@ -3,14 +3,13 @@ package banane.io.pdb.model
import javax.persistence.*

@Entity
class MapPoint {
data class MapPoint(
@get:GeneratedValue(strategy = GenerationType.IDENTITY)
@get:Id
var id: Long? = null
var x = 0
var y = 0
var zone: String? = null
var id: Long,
var x: Int,
var y: Int,
var zone: String?,
@Enumerated(EnumType.STRING)
var terrain: Terrain? = null

}
var terrain: Terrain?
)
File renamed without changes.
File renamed without changes.
Expand Up @@ -9,41 +9,36 @@ import org.springframework.stereotype.Service
import java.util.*

@Service
open class ActionServiceImpl : ActionService {
@Autowired
private val securityService: SecurityService? = null
@Autowired
private val heroRepository: HeroRepository? = null
@Autowired
private val baseRepository: BaseRepository? = null
open class ActionServiceImpl(private val securityService: SecurityService, private val heroRepository: HeroRepository, private val baseRepository: BaseRepository ) : ActionService {


override fun executeAction(action: Action): Boolean {
if (action == Action.LOGGING) {
val hero = heroFromSession
if (hero != null) {
hero.wood = hero.wood?.plus(10)
heroRepository.save<Hero>(hero)
return true
}
heroRepository!!.save<Hero>(hero)
return true
} else if (action == Action.MINE) {
val hero = heroFromSession
if (hero != null) {
hero?.stone = hero.stone?.plus(10)
heroRepository.save<Hero>(hero)
return true
}
heroRepository!!.save<Hero>(hero)
return true
} else if (action == Action.CREATE_BASE) {
val hero = heroFromSession
if (hero?.base == null && hero?.wood!! >= 50 && hero?.stone!! >= 50) {
if (hero != null && hero.base == null && hero.wood!! >= 50 && hero.stone!! >= 50) {
val newBase = Base()
newBase.location = hero.currentZone
newBase.owner = hero
newBase.stone = 0
newBase.wood = 0
baseRepository!!.save(newBase)
baseRepository.save(newBase)
hero.stone = hero.stone!! - 50
hero.wood = hero.wood!! - 50
heroRepository!!.save<Hero>(hero)
heroRepository.save<Hero>(hero)
return true
}
}
Expand All @@ -59,11 +54,11 @@ open class ActionServiceImpl : ActionService {
actions.add(Action.LOGGING)
actions.add(Action.MINE)
} else if (Terrain.PLAIN == terrain) {
val loggedInUser = securityService!!.findLoggedInUser()
val loggedInUser = securityService.findLoggedInUser()
if (loggedInUser != null) {
if (loggedInUser.hero?.base == null) {
actions.add(Action.CREATE_BASE)
} else if (loggedInUser != null) {
} else {
if (loggedInUser.hero?.base!!.location == mapPoint) {
actions.add(Action.VISIT_BASE)
}
Expand All @@ -75,7 +70,7 @@ open class ActionServiceImpl : ActionService {

private val heroFromSession: Hero?
private get() {
val findLoggedInUser = securityService!!.findLoggedInUser()
val findLoggedInUser = securityService.findLoggedInUser()
if (findLoggedInUser != null) {
return findLoggedInUser.hero
}
Expand Down
231 changes: 231 additions & 0 deletions src/test/kotlin/banane/io/pdb/service/ActionServiceImplTest.kt
@@ -0,0 +1,231 @@
package banane.io.pdb.service

import banane.io.pdb.model.*
import banane.io.pdb.repository.BaseRepository
import banane.io.pdb.repository.HeroRepository
import banane.io.pdb.security.SecurityService
import io.mockk.every
import io.mockk.mockk
import io.mockk.slot
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import kotlin.test.assertEquals
import kotlin.test.assertFalse

internal class ActionServiceImplTest {

lateinit var serviceToTest: ActionServiceImpl
lateinit var securityService: SecurityService
lateinit var heroRepository: HeroRepository
lateinit var baseRepository: BaseRepository

@BeforeEach
fun setup() {
securityService = mockk<SecurityService>()
heroRepository = mockk<HeroRepository>()
baseRepository = mockk<BaseRepository>()

serviceToTest = ActionServiceImpl(securityService, heroRepository, baseRepository)
}

private fun createMapPointWith(terrain: Terrain): MapPoint {
return MapPoint(1, 1, 1, "This is a description", terrain)
}

@Test
fun `Verify that Mountain MapPoint should return Mine action`() {
val mountainPoint = createMapPointWith(Terrain.MOUNTAIN)

val availableActionsFromMapPoint = serviceToTest.getAvailablesActionsFromMapPoint(mountainPoint)

assertEquals(1, availableActionsFromMapPoint.size, "There's should only be one action available")
assert(availableActionsFromMapPoint.contains(Action.MINE))
}

@Test
fun `Verify that Forest MapPoint should return both Logging and Mining actions`() {
val forestMapPoint = createMapPointWith(Terrain.FOREST)

val availableActionsFromMapPoint = serviceToTest.getAvailablesActionsFromMapPoint(forestMapPoint)

assertEquals(2, availableActionsFromMapPoint.size, "There's should only be two actions availables")
assert(availableActionsFromMapPoint.contains(Action.MINE))
assert(availableActionsFromMapPoint.contains(Action.LOGGING))
}

@Test
fun `Verify that on Plain MapPoint when User is null should return no action`() {
every { securityService.findLoggedInUser()} returns null

val plainMapPoint = createMapPointWith(Terrain.PLAIN)

val availableActionsFromMapPoint = serviceToTest.getAvailablesActionsFromMapPoint(plainMapPoint)
assert(availableActionsFromMapPoint.isEmpty())
}

@Test
fun `Verify that Create base action is return if user has a hero but no base`() {
var user = User()
user.hero = Hero()
user.hero?.base = null
every { securityService.findLoggedInUser()} returns user

val plainMapPoint = createMapPointWith(Terrain.PLAIN)

val availableActionsFromMapPoint = serviceToTest.getAvailablesActionsFromMapPoint(plainMapPoint)
assertEquals(1, availableActionsFromMapPoint.size, "There's should only be one action available")
assert(availableActionsFromMapPoint.contains(Action.CREATE_BASE))
}

@Test
fun `Verify that User with a base but on different MapPoint return no action`() {
var user = User()
user.hero = Hero()
var baseOfUser = Base()
var otherMapPoint = createMapPointWith(Terrain.PLAIN)
otherMapPoint.x = 2
baseOfUser.location = otherMapPoint
user.hero?.base = baseOfUser
every { securityService.findLoggedInUser()} returns user

val plainMapPoint = createMapPointWith(Terrain.PLAIN)

val availableActionsFromMapPoint = serviceToTest.getAvailablesActionsFromMapPoint(plainMapPoint)
assert(availableActionsFromMapPoint.isEmpty())
}

@Test
fun `Verify that User with a base on same MapPoint return VisitBase action`() {
var user = User()
user.hero = Hero()
var baseOfUser = Base()
var sameMapPoint = createMapPointWith(Terrain.PLAIN)
baseOfUser.location = sameMapPoint
user.hero?.base = baseOfUser
every { securityService.findLoggedInUser()} returns user

val plainMapPoint = createMapPointWith(Terrain.PLAIN)

val availableActionsFromMapPoint = serviceToTest.getAvailablesActionsFromMapPoint(plainMapPoint)
assertEquals(1, availableActionsFromMapPoint.size, "There's should only be one action available")
assert(availableActionsFromMapPoint.contains(Action.VISIT_BASE))
}

@Test
fun `Execute Action return false if the action is not present in the else if`() {
assertFalse(serviceToTest.executeAction(Action.VISIT_BASE))
}

@Test
fun `Executing logging action returns false if there is no hero`() {
var user = User()
every { securityService.findLoggedInUser()} returns user

assertFalse(serviceToTest.executeAction(Action.LOGGING))
}

@Test
fun `Executing logging action add 10 wood to the hero`() {
var user = User()
user.hero = Hero()
user.hero?.wood = 0
every { securityService.findLoggedInUser()} returns user

val slot = slot<Hero>()
every { heroRepository.save<Hero>(capture(slot)) } answers { slot.captured }

assert(serviceToTest.executeAction(Action.LOGGING))
assertEquals(10, slot.captured.wood, "Hero should have 10 wood in his inventory")
}

@Test
fun `Executing mining action add 10 stone to the hero`() {
var user = User()
user.hero = Hero()
user.hero?.stone = 0
every { securityService.findLoggedInUser()} returns user

val slot = slot<Hero>()
every { heroRepository.save<Hero>(capture(slot)) } answers { slot.captured }

assert(serviceToTest.executeAction(Action.MINE))
assertEquals(10, slot.captured.stone, "Hero should have 10 wood in his inventory")
}

@Test
fun `Executing mining action returns false if there is no hero`() {
var user = User()
every { securityService.findLoggedInUser()} returns user

assertFalse(serviceToTest.executeAction(Action.MINE))
}

@Test
fun `Executing CREATE_BASE action returns false if there is no hero`() {
var user = User()
every { securityService.findLoggedInUser()} returns user

assertFalse(serviceToTest.executeAction(Action.CREATE_BASE))
}

@Test
fun `Executing CREATE_BASE action returns false if there a base already exist`() {
var user = User()
user.hero = Hero()
user.hero?.base = Base()
every { securityService.findLoggedInUser()} returns user

assertFalse(serviceToTest.executeAction(Action.CREATE_BASE))
}

@Test
fun `Executing CREATE_BASE action returns false if the hero doesn't have enough wood`() {
var user = User()
user.hero = Hero()
user.hero?.wood = 0
user.hero?.stone = 50
every { securityService.findLoggedInUser()} returns user

assertFalse(serviceToTest.executeAction(Action.CREATE_BASE))
}

@Test
fun `Executing CREATE_BASE action returns false if the hero doesn't have enough stone`() {
var user = User()
user.hero = Hero()
user.hero?.wood = 50
user.hero?.stone = 0
every { securityService.findLoggedInUser()} returns user

assertFalse(serviceToTest.executeAction(Action.CREATE_BASE))
}

@Test
fun `Executing CREATE_BASE action returns true if there the hero have enough resources`() {
var user = User()
user.hero = Hero()
user.hero?.wood = 50
user.hero?.stone = 50
user.hero?.currentZone = createMapPointWith(Terrain.PLAIN)
every { securityService.findLoggedInUser()} returns user

val slot = slot<Hero>()
every { heroRepository.save<Hero>(capture(slot)) } answers { slot.captured }

val slotBase = slot<Base>()
every { baseRepository.save(capture(slotBase))} answers { slotBase.captured }

assert(serviceToTest.executeAction(Action.CREATE_BASE))

val hero = slot.captured
assertEquals(0, hero.wood, "Hero has no wood anymore")
assertEquals(0, hero.stone, "Hero has no stone anymore")

val newBase = slotBase.captured

assertEquals(0, newBase.stone, "Initial value of stone")
assertEquals(0, newBase.wood, "Initial value of wood")
assertEquals(user.hero?.currentZone, newBase.location, "Location should be at the currentZone of Hero")
assertEquals(user.hero, newBase.owner, "The owner of the base is the hero of the user")
}
}

0 comments on commit f41c35c

Please sign in to comment.