Skip to content

Commit

Permalink
removed mockito utilities
Browse files Browse the repository at this point in the history
Bug-Url: #136
Signed-off-by: Laszlo Hornyak <[email protected]>
  • Loading branch information
K0zka committed Sep 23, 2016
1 parent 5310fa3 commit 5094b53
Show file tree
Hide file tree
Showing 26 changed files with 241 additions and 305 deletions.
4 changes: 2 additions & 2 deletions src/main/kotlin/com/github/K0zka/kerub/model/Range.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ import java.io.Serializable
*/
data class Range<T>(
@JsonProperty("min")
final val min: T,
val min: T,
@JsonProperty("max")
final val max: T) : Serializable
val max: T) : Serializable
31 changes: 0 additions & 31 deletions src/test/kotlin/com/github/K0zka/kerub/MockitoUtils.kt

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import com.github.K0zka.kerub.model.AddEntry
import com.github.K0zka.kerub.model.DeleteEntry
import com.github.K0zka.kerub.model.UpdateEntry
import com.github.K0zka.kerub.testVm
import com.github.K0zka.kerub.verify
import com.nhaarman.mockito_kotlin.any
import com.nhaarman.mockito_kotlin.doReturn
import com.nhaarman.mockito_kotlin.mock
import com.nhaarman.mockito_kotlin.spy
import com.nhaarman.mockito_kotlin.verify
import com.nhaarman.mockito_kotlin.whenever
import org.junit.Test

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package com.github.K0zka.kerub.controller.impl

import com.github.K0zka.kerub.controller.HostAssignedMessage
import com.github.K0zka.kerub.data.HostDao
import com.github.K0zka.kerub.eq
import com.github.K0zka.kerub.host.HostManager
import com.github.K0zka.kerub.model.Host
import com.github.K0zka.kerub.on
import com.github.K0zka.kerub.verify
import com.nhaarman.mockito_kotlin.eq
import com.nhaarman.mockito_kotlin.mock
import com.nhaarman.mockito_kotlin.verify
import com.nhaarman.mockito_kotlin.whenever
import org.junit.Before
import org.junit.Test
import java.util.UUID
Expand Down Expand Up @@ -38,10 +38,10 @@ class InterControllerListenerTest {
dedicated = true,
publicKey = "TEST"
)
on(hostDao.get( eq(hostId) )).thenReturn(
whenever(hostDao.get( eq(hostId) )).thenReturn(
host
)
on(message.`object`).thenReturn(HostAssignedMessage(hostId, controllerId))
whenever(message.`object`).thenReturn(HostAssignedMessage(hostId, controllerId))
impl!!.onMessage(message)

verify(hostManager).connectHost(eq(host))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
package com.github.K0zka.kerub.host

import com.github.K0zka.kerub.anyString
import com.github.K0zka.kerub.controller.HostAssignedMessage
import com.github.K0zka.kerub.controller.InterController
import com.github.K0zka.kerub.data.AssignmentDao
import com.github.K0zka.kerub.data.ControllerDao
import com.github.K0zka.kerub.data.dynamic.ControllerDynamicDao
import com.github.K0zka.kerub.matchAny
import com.github.K0zka.kerub.model.Host
import com.github.K0zka.kerub.model.controller.Assignment
import com.github.K0zka.kerub.model.controller.AssignmentType
import com.github.K0zka.kerub.model.dynamic.ControllerDynamic
import com.github.K0zka.kerub.times
import com.github.K0zka.kerub.verify
import com.github.k0zka.finder4j.backtrack.BacktrackService
import com.nhaarman.mockito_kotlin.any
import com.nhaarman.mockito_kotlin.mock
import com.nhaarman.mockito_kotlin.times
import com.nhaarman.mockito_kotlin.verify
import org.junit.After
import org.junit.Before
import org.junit.Test
Expand All @@ -24,34 +23,34 @@ import org.mockito.runners.MockitoJUnitRunner
import java.util.UUID

@RunWith(MockitoJUnitRunner::class) class ControllerAssignerImplTest {
var backtrack : BacktrackService? = null
var backtrack: BacktrackService? = null

val controllerDao : ControllerDao = mock()
val controllerDao: ControllerDao = mock()

val controllerDynamicDao : ControllerDynamicDao = mock()
val controllerDynamicDao: ControllerDynamicDao = mock()

var hostAssignmentDao : AssignmentDao = mock()
var hostAssignmentDao: AssignmentDao = mock()

var interController : InterController = mock()
var interController: InterController = mock()

@Before
fun setup() {
backtrack = BacktrackService(4)
Mockito.`when`(controllerDynamicDao.listAll()).thenReturn(listOf(
ControllerDynamic(
controllerId = "ctrl-1",
addresses = listOf(),
maxHosts = 64,
totalHosts = 0
),
addresses = listOf(),
maxHosts = 64,
totalHosts = 0
),
ControllerDynamic(
controllerId = "ctrl-2",
addresses = listOf(),
maxHosts = 64,
totalHosts = 10
)
)

))
))
}

@After
Expand All @@ -76,17 +75,10 @@ import java.util.UUID
controller = "",
entityId = UUID.randomUUID(),
type = AssignmentType.host
)
verify(hostAssignmentDao, times(2)).add( matchAny(Assignment::class.java,
assignment
) )
)
verify(hostAssignmentDao, times(2)).add(any<Assignment>())

verify(interController, times(2)).sendToController( anyString(), matchAny(HostAssignedMessage::class.java,
HostAssignedMessage(
hostId = UUID.randomUUID(),
conrollerId = ""
)
) )
verify(interController, times(2)).sendToController(any(), any<HostAssignedMessage>())

}
}
Original file line number Diff line number Diff line change
@@ -1,26 +1,23 @@
package com.github.K0zka.kerub.host

import com.github.K0zka.kerub.data.dynamic.ControllerDynamicDao
import com.github.K0zka.kerub.matchAny
import com.github.K0zka.kerub.model.dynamic.ControllerDynamic
import com.nhaarman.mockito_kotlin.any
import com.nhaarman.mockito_kotlin.mock
import org.hamcrest.CoreMatchers
import org.infinispan.manager.EmbeddedCacheManager
import org.infinispan.remoting.transport.Address
import org.junit.Assert
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mock
import org.mockito.Mockito
import org.mockito.runners.MockitoJUnitRunner

class ControllerManagerImplTest {

val dao : ControllerDynamicDao = mock()
val dao: ControllerDynamicDao = mock()

val cacheManager : EmbeddedCacheManager = mock()
val cacheManager: EmbeddedCacheManager = mock()

var address : Address = mock()
var address: Address = mock()

@Test
fun getControllerId() {
Expand All @@ -33,13 +30,13 @@ class ControllerManagerImplTest {
fun start() {
Mockito.`when`(cacheManager.address).thenReturn(address)
val notNullInstance = ControllerDynamic("", 1, 0, listOf())
Mockito.`when`(dao.add(matchAny(ControllerDynamic::class.java, notNullInstance)))
.then {
val controllerDynamic = it.arguments[0]!! as ControllerDynamic
//checks
""
}
Mockito.`when`(dao.add(any<ControllerDynamic>()))
.then {
val controllerDynamic = it.arguments[0]!! as ControllerDynamic
//checks
""
}
ControllerManagerImpl(dao, cacheManager).start()
Mockito.verify(dao)?.add(matchAny(ControllerDynamic::class.java, notNullInstance))
Mockito.verify(dao)?.add(any<ControllerDynamic>())
}
}
46 changes: 22 additions & 24 deletions src/test/kotlin/com/github/K0zka/kerub/host/HostManagerImplTest.kt
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package com.github.K0zka.kerub.host

import com.github.K0zka.kerub.anyString
import com.github.K0zka.kerub.data.AssignmentDao
import com.github.K0zka.kerub.data.HostDao
import com.github.K0zka.kerub.data.VirtualStorageDeviceDao
import com.github.K0zka.kerub.data.config.HostConfigurationDao
import com.github.K0zka.kerub.data.dynamic.HostDynamicDao
import com.github.K0zka.kerub.data.dynamic.VirtualMachineDynamicDao
import com.github.K0zka.kerub.data.dynamic.VirtualStorageDeviceDynamicDao
import com.github.K0zka.kerub.eq
import com.github.K0zka.kerub.exc.HostAddressException
import com.github.K0zka.kerub.expect
import com.github.K0zka.kerub.getTestKey
Expand All @@ -17,12 +15,12 @@ import com.github.K0zka.kerub.hypervisor.Hypervisor
import com.github.K0zka.kerub.model.Host
import com.github.K0zka.kerub.model.controller.Assignment
import com.github.K0zka.kerub.model.controller.AssignmentType
import com.github.K0zka.kerub.never
import com.github.K0zka.kerub.on
import com.github.K0zka.kerub.verify
import com.nhaarman.mockito_kotlin.any
import com.nhaarman.mockito_kotlin.eq
import com.nhaarman.mockito_kotlin.mock
import com.nhaarman.mockito_kotlin.never
import com.nhaarman.mockito_kotlin.spy
import com.nhaarman.mockito_kotlin.verify
import com.nhaarman.mockito_kotlin.whenever
import org.apache.sshd.client.session.ClientSession
import org.apache.sshd.server.Command
Expand Down Expand Up @@ -166,10 +164,10 @@ class HostManagerImplTest {
whenever(address.isLinkLocalAddress).thenReturn(false)
whenever(address.isAnyLocalAddress).thenReturn(false)
whenever(hostManager!!.resolve(any())).thenReturn(address)
on(sshClientService.loginWithPublicKey(
address = anyString(),
hostPublicKey = anyString(),
userName = anyString())).thenReturn(clientSession)
whenever(sshClientService.loginWithPublicKey(
address = any(),
hostPublicKey = any(),
userName = any())).thenReturn(clientSession)
hostManager!!.connectHost(host)
Thread.sleep(1000)
}
Expand All @@ -182,43 +180,43 @@ class HostManagerImplTest {
dedicated = false,
publicKey = "")
whenever(hostManager!!.resolve(any())).then { throw UnknownHostException("TEST") }
on(sshClientService.loginWithPublicKey(
address = anyString(),
hostPublicKey = anyString(),
userName = anyString())).thenReturn(clientSession)
whenever(sshClientService.loginWithPublicKey(
address = any(),
hostPublicKey = any(),
userName = any())).thenReturn(clientSession)
expect(HostAddressException::class) {
hostManager!!.connectHost(host)
}
verify(sshClientService, never).loginWithPublicKey(any(), any(), any())
verify(sshClientService, never()).loginWithPublicKey(any(), any(), any())
}

@Test
fun start() {
val controllerId = "test controller id"
val hostId = UUID.randomUUID()
val host = Host(id = hostId, address = "host.example.com", dedicated = true, publicKey = "testkey")
on(controllerManager.getControllerId()).thenReturn(controllerId)
on(hostAssignmentDao.listByControllerAndType(eq(controllerId), eq(AssignmentType.host))).thenReturn(
whenever(controllerManager.getControllerId()).thenReturn(controllerId)
whenever(hostAssignmentDao.listByControllerAndType(eq(controllerId), eq(AssignmentType.host))).thenReturn(
listOf(Assignment(controller = controllerId, entityId = hostId, type = AssignmentType.host))
)
on(hostDao[hostId]).thenReturn(host)
on(sshClientService.createSession(anyString(), anyString())).thenReturn(clientSession)
whenever(hostDao[hostId]).thenReturn(host)
whenever(sshClientService.createSession(any(), any())).thenReturn(clientSession)
val address = mock<InetAddress>()
whenever(address.isLoopbackAddress).thenReturn(false)
whenever(address.isLinkLocalAddress).thenReturn(false)
whenever(address.isAnyLocalAddress).thenReturn(false)
on(hostManager!!.resolve(any())).thenReturn(address)
on(sshClientService.loginWithPublicKey(any(), any(), any())).thenReturn(clientSession)
whenever(hostManager!!.resolve(any())).thenReturn(address)
whenever(sshClientService.loginWithPublicKey(any(), any(), any())).thenReturn(clientSession)

hostManager!!.start()

verify(sshClientService)!!.loginWithPublicKey(eq(host.address), anyString(), eq(host.publicKey))
verify(sshClientService).loginWithPublicKey(eq(host.address), any(), eq(host.publicKey))

}

@Test
fun dataConnection() {
on(sshClientService.loginWithPublicKey(anyString(), anyString(), anyString())).thenReturn(clientSession)
whenever(sshClientService.loginWithPublicKey(any(), any(), any())).thenReturn(clientSession)

val hostId = UUID.randomUUID()
val host = Host(id = hostId, address = "host.example.com", dedicated = true, publicKey = "testkey")
Expand All @@ -230,7 +228,7 @@ class HostManagerImplTest {
})

assertTrue(called)
verify(sshClientService).loginWithPublicKey(anyString(), anyString(), anyString())
verify(sshClientService).loginWithPublicKey(any(), any(), any())
}

@Test
Expand All @@ -239,7 +237,7 @@ class HostManagerImplTest {
val hostId = UUID.randomUUID()
val host = Host(id = hostId, address = "host.example.com", dedicated = true, publicKey = "testkey")
whenever(discoverer.detectDistro(eq(clientSession))).thenReturn(distro)
whenever(sshClientService.loginWithPublicKey(eq(host.address), anyString(), eq(host.publicKey))).thenReturn(clientSession)
whenever(sshClientService.loginWithPublicKey(eq(host.address), any(), eq(host.publicKey))).thenReturn(clientSession)

hostManager!!.connectHost(host)
val result = hostManager!!.execute(host) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package com.github.K0zka.kerub.host

import com.github.K0zka.kerub.eq
import com.github.K0zka.kerub.expect
import com.github.K0zka.kerub.getTestKey
import com.github.K0zka.kerub.verify
import com.nhaarman.mockito_kotlin.any
import com.nhaarman.mockito_kotlin.eq
import com.nhaarman.mockito_kotlin.mock
import com.nhaarman.mockito_kotlin.verify
import org.apache.sshd.client.SshClient
import org.apache.sshd.client.session.ClientSession
import org.apache.sshd.client.subsystem.sftp.SftpClient
Expand Down Expand Up @@ -45,13 +46,13 @@ class SshClientServiceImplTest {
fun installPublicKey() {
Mockito.`when`(session.createSftpClient()).thenReturn(sftClient)
Mockito.`when`(sftClient.stat(eq(".ssh"))).thenReturn(SftpClient.Attributes())
Mockito.`when`(sftClient.open(eq(".ssh/authorized_keys"), Matchers.any<SftpClient.OpenMode>())).thenReturn(handle)
Mockito.`when`(sftClient.open(eq(".ssh/authorized_keys"), Matchers.any<EnumSet<SftpClient.OpenMode>>())).thenReturn(handle)
Mockito.`when`(sftClient.open(eq(".ssh/authorized_keys"), any<SftpClient.OpenMode>())).thenReturn(handle)
Mockito.`when`(sftClient.open(eq(".ssh/authorized_keys"), any<EnumSet<SftpClient.OpenMode>>())).thenReturn(handle)
Mockito.`when`(sftClient.stat(eq(".ssh/authorized_keys"))) .thenReturn(SftpClient.Attributes())
Mockito.`when`(sftClient.stat(eq(handle))).thenReturn(SftpClient.Attributes())
service!!.installPublicKey(session)

verify(sftClient)!!.close(eq(handle))
verify(sftClient).close(eq(handle))
}

@Test
Expand Down
Loading

0 comments on commit 5094b53

Please sign in to comment.