Skip to content

Commit

Permalink
Add domain layer unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
abelgardep committed Feb 4, 2020
1 parent edcd2d4 commit 444892b
Show file tree
Hide file tree
Showing 10 changed files with 468 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class OCAnonymousServerRepository(
return ServerInfo(
ownCloudVersion = pairRemoteStatus.first.version,
baseUrl = normalizeProtocolPrefix(trimWebdavSuffix(path), pairRemoteStatus.second),
authenticationMethods = authenticationMethod,
authenticationMethod = authenticationMethod,
isSecureConnection = pairRemoteStatus.second
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ package com.owncloud.android.domain.server.model
data class ServerInfo(
val ownCloudVersion: String,
val baseUrl: String,
val authenticationMethods: AuthenticationMethod,
val authenticationMethod: AuthenticationMethod,
val isSecureConnection: Boolean
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/**
* ownCloud Android client application
*
* @author Abel García de Prada
* Copyright (C) 2020 ownCloud GmbH.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.owncloud.android.domain.authentication.usecases

import com.owncloud.android.domain.authentication.AuthenticationRepository
import io.mockk.every
import io.mockk.spyk
import io.mockk.verify
import org.junit.Assert
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertNull
import org.junit.Assert.assertTrue
import org.junit.Test

class LoginAsyncUseCaseTest {
private val authRepository: AuthenticationRepository = spyk()
private val useCase = LoginAsyncUseCase(authRepository)
private val useCaseParams = LoginAsyncUseCase.Params(
serverPath = "http://demo.owncloud.com",
username = "test",
password = "test"
)

@Test
fun invalidParams() {
var invalidUseCaseParams = useCaseParams.copy(serverPath = "")
var useCaseResult = useCase.execute(invalidUseCaseParams)

assertTrue(useCaseResult.isError)
assertTrue(useCaseResult.getThrowableOrNull() is IllegalArgumentException)

invalidUseCaseParams = useCaseParams.copy(username = "")
useCaseResult = useCase.execute(invalidUseCaseParams)

assertTrue(useCaseResult.isError)
assertTrue(useCaseResult.getThrowableOrNull() is IllegalArgumentException)

invalidUseCaseParams = useCaseParams.copy(password = "")
useCaseResult = useCase.execute(invalidUseCaseParams)

assertTrue(useCaseResult.isError)
assertTrue(useCaseResult.getThrowableOrNull() is IllegalArgumentException)

verify(exactly = 0) { authRepository.login(any(), any(), any()) }
}

@Test
fun loginSuccess() {
every { authRepository.login(any(), any(), any()) } returns Unit
val useCaseResult = useCase.execute(useCaseParams)

assertTrue(useCaseResult.isSuccess)
assertFalse(useCaseResult.isError)

assertNull(useCaseResult.getThrowableOrNull())
assertEquals(Unit, useCaseResult.getDataOrNull())

verify(exactly = 1) { authRepository.login(any(), any(), any()) }
}

@Test
fun getServerInfoWithException() {
every { authRepository.login(any(), any(), any()) } throws Exception()

val useCaseResult = useCase.execute(useCaseParams)

assertFalse(useCaseResult.isSuccess)
assertTrue(useCaseResult.isError)

assertNull(useCaseResult.getDataOrNull())
assertTrue(useCaseResult.getThrowableOrNull() is Exception)

verify(exactly = 1) { authRepository.login(any(), any(), any()) }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/**
* ownCloud Android client application
*
* @author Abel García de Prada
* Copyright (C) 2020 ownCloud GmbH.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.owncloud.android.domain.server.model

import com.owncloud.android.domain.server.model.AuthenticationMethod.BASIC_HTTP_AUTH
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Test

class ServerInfoTest {

@Test
fun testConstructor() {
val item = ServerInfo(
"10.3.2.1",
"https://demo.owncloud.com",
BASIC_HTTP_AUTH,
false
)

assertEquals(BASIC_HTTP_AUTH, item.authenticationMethod)
assertEquals("https://demo.owncloud.com", item.baseUrl)
assertEquals("10.3.2.1", item.ownCloudVersion)
assertEquals(false, item.isSecureConnection)
}

@Test
fun testEqualsOk() {
val item1 = ServerInfo(
authenticationMethod = BASIC_HTTP_AUTH,
baseUrl = "https://demo.owncloud.com",
ownCloudVersion = "10.3.2.1",
isSecureConnection = false
)

val item2 = ServerInfo(
"10.3.2.1",
"https://demo.owncloud.com",
BASIC_HTTP_AUTH,
false
)

assertTrue(item1 == item2)
assertFalse(item1 === item2)
}

@Test
fun testEqualsKo() {
val item1 = ServerInfo(
authenticationMethod = BASIC_HTTP_AUTH,
baseUrl = "https://demo.owncloud.com",
ownCloudVersion = "10.3.2.1",
isSecureConnection = false
)

val item2 = ServerInfo(
"10.0.0.0",
"https://demo.owncloud.com",
BASIC_HTTP_AUTH,
false
)

assertFalse(item1 == item2)
assertFalse(item1 === item2)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/**
* ownCloud Android client application
*
* @author Abel García de Prada
* Copyright (C) 2020 ownCloud GmbH.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.owncloud.android.domain.server.usecases

import com.owncloud.android.domain.server.ServerRepository
import io.mockk.every
import io.mockk.spyk
import io.mockk.verify
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertNull
import org.junit.Assert.assertTrue
import org.junit.Test

class CheckPathExistenceUseCaseTest {
private val serverRepository: ServerRepository = spyk()
private val useCase = CheckPathExistenceUseCase(serverRepository)
private val useCaseParams = CheckPathExistenceUseCase.Params(
remotePath = "http://demo.owncloud.com",
isUserLogged = false
)

@Test
fun getServerInfoSuccess() {
every { serverRepository.checkPathExistence(any(), any()) } returns true
val useCaseResult = useCase.execute(useCaseParams)

assertTrue(useCaseResult.isSuccess)
assertFalse(useCaseResult.isError)

assertNull(useCaseResult.getThrowableOrNull())
assertEquals(true, useCaseResult.getDataOrNull())

verify(exactly = 1) { serverRepository.checkPathExistence(any(), any()) }
}

@Test
fun getServerInfoWithException() {
every { serverRepository.checkPathExistence(any(), any()) } throws Exception()

val useCaseResult = useCase.execute(useCaseParams)

assertFalse(useCaseResult.isSuccess)
assertTrue(useCaseResult.isError)

assertNull(useCaseResult.getDataOrNull())
assertTrue(useCaseResult.getThrowableOrNull() is Exception)

verify(exactly = 1) { serverRepository.checkPathExistence(any(), any()) }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/**
* ownCloud Android client application
*
* @author Abel García de Prada
* Copyright (C) 2020 ownCloud GmbH.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.owncloud.android.domain.server.usecases

import com.owncloud.android.domain.server.AnonymousServerRepository
import com.owncloud.android.testutil.OCServerInfo
import io.mockk.every
import io.mockk.spyk
import io.mockk.verify
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertNull
import org.junit.Assert.assertTrue
import org.junit.Test

class GetServerInfoUseCaseTest {
private val anonymousServerRepository: AnonymousServerRepository = spyk()
private val useCase = GetServerInfoUseCase((anonymousServerRepository))
private val useCaseParams = GetServerInfoUseCase.Params(serverPath = "http://demo.owncloud.com")

@Test
fun getServerInfoSuccess() {
every { anonymousServerRepository.getServerInfo(useCaseParams.serverPath) } returns OCServerInfo
val useCaseResult = useCase.execute(useCaseParams)

assertTrue(useCaseResult.isSuccess)
assertFalse(useCaseResult.isError)

assertNull(useCaseResult.getThrowableOrNull())
assertEquals(OCServerInfo, useCaseResult.getDataOrNull())

verify(exactly = 1) { anonymousServerRepository.getServerInfo(useCaseParams.serverPath) }
}

@Test
fun getServerInfoWithException() {
every { anonymousServerRepository.getServerInfo(useCaseParams.serverPath) } throws Exception()

val useCaseResult = useCase.execute(useCaseParams)

assertFalse(useCaseResult.isSuccess)
assertTrue(useCaseResult.isError)

assertNull(useCaseResult.getDataOrNull())
assertTrue(useCaseResult.getThrowableOrNull() is Exception)

verify(exactly = 1) { anonymousServerRepository.getServerInfo(useCaseParams.serverPath)}
}
}
Loading

0 comments on commit 444892b

Please sign in to comment.