...
 
Commits (26)
......@@ -3,24 +3,20 @@ package ru.terrakok.gitlabclient.entity.app.target
/**
* Created by Konstantin Tskhovrebov (aka @terrakok) on 26.12.17.
*/
sealed class TargetBadge(
val target: AppTarget?,
val targetId: Long?,
val internal: TargetInternal?
) {
class Text(
val text: String,
target: AppTarget? = null,
targetId: Long? = null,
internal: TargetInternal? = null
) : TargetBadge(target, targetId, internal)
sealed class TargetBadge {
data class Text(
val text: String,
val target: AppTarget? = null,
val targetId: Long? = null,
val internal: TargetInternal? = null
) : TargetBadge()
class Icon(
val icon: TargetBadgeIcon,
val count: Int
) : TargetBadge(null, null, null)
data class Icon(
val icon: TargetBadgeIcon,
val count: Int
) : TargetBadge()
class Status(
val status: TargetBadgeStatus
) : TargetBadge(null, null, null)
data class Status(
val status: TargetBadgeStatus
) : TargetBadge()
}
\ No newline at end of file
......@@ -2,8 +2,6 @@ package ru.terrakok.gitlabclient
import org.junit.Assert
import org.junit.Test
import ru.terrakok.gitlabclient.entity.Project
import ru.terrakok.gitlabclient.entity.Visibility
import ru.terrakok.gitlabclient.model.data.server.MarkDownUrlResolver
/**
......@@ -12,12 +10,7 @@ import ru.terrakok.gitlabclient.model.data.server.MarkDownUrlResolver
class MarkDownUrlResolverTest {
private val markDownUrlResolver = MarkDownUrlResolver()
private val project = Project(
1, null, "", Visibility.PUBLIC, "", "", "", null, null, "GitFox", "", "",
"terrakok/gitlab-client", true, 1, true, true, true, true, true, null, null, 1, null, null, false, null, true,
1, 1, null, true, null, true, true, true, null
)
private val project = TestData.getProject(1)
@Test
fun check_markdown_without_image() {
......
This diff is collapsed.
......@@ -4,8 +4,11 @@ import io.reactivex.Single
import io.reactivex.observers.TestObserver
import org.junit.Test
import org.mockito.Mockito.*
import ru.terrakok.gitlabclient.TestData
import ru.terrakok.gitlabclient.TestSchedulers
import ru.terrakok.gitlabclient.entity.*
import ru.terrakok.gitlabclient.entity.Branch
import ru.terrakok.gitlabclient.entity.OrderBy
import ru.terrakok.gitlabclient.entity.RepositoryTreeNodeType
import ru.terrakok.gitlabclient.entity.app.ProjectFile
import ru.terrakok.gitlabclient.model.repository.project.ProjectRepository
import ru.terrakok.gitlabclient.model.repository.tools.Base64Tools
......@@ -23,45 +26,7 @@ class ProjectInteractorTest {
private val testError = RuntimeException("test error")
private val testPage = 13
private val testProject = Project(
id = 42L,
description = null,
defaultBranch = "test_br",
visibility = Visibility.PUBLIC,
sshUrlToRepo = null,
httpUrlToRepo = null,
webUrl = "https://gitlab.com/terrakok/gitlab-client",
tagList = null,
owner = null,
name = "",
nameWithNamespace = "",
path = "test path",
pathWithNamespace = "",
issuesEnabled = false,
openIssuesCount = 0L,
mergeRequestsEnabled = false,
jobsEnabled = false,
wikiEnabled = false,
snippetsEnabled = false,
containerRegistryEnabled = false,
createdAt = null,
lastActivityAt = null,
creatorId = 0L,
namespace = null,
permissions = null,
archived = false,
avatarUrl = null,
sharedRunnersEnabled = false,
forksCount = 0L,
starCount = 0L,
runnersToken = null,
publicJobs = false,
sharedWithGroups = null,
onlyAllowMergeIfPipelineSucceeds = false,
onlyAllowMergeIfAllDiscussionsAreResolved = false,
requestAccessEnabled = false,
readmeUrl = "https://gitlab.com/terrakok/gitlab-client/blob/test_br/README.md"
)
private val testProject = TestData.getProject(42L)
private val testProjectsList = listOf(testProject)
@Test
......@@ -185,16 +150,7 @@ class ProjectInteractorTest {
fun get_project_raw_file() {
val raw = "lorem ipsum"
val testFileContent = "bG9yZW0gaXBzdW0=" //base64 for raw
val testFile = File(
"",
"",
0L,
"",
testFileContent,
testProject.defaultBranch!!,
"",
"",
"")
val testFile = TestData.getFile(testFileContent, testProject.defaultBranch!!)
`when`(repo.getProjectFile(anyLong(), anyString(), anyString())).thenReturn(Single.just(testFile))
`when`(base64Tools.decode(anyString())).thenReturn(raw)
......@@ -217,16 +173,7 @@ class ProjectInteractorTest {
fun get_project_readme() {
val raw = "lorem ipsum"
val testFileContent = "bG9yZW0gaXBzdW0=" //base64 for raw
val testFile = File(
"",
"",
0L,
"",
testFileContent,
testProject.defaultBranch!!,
"",
"",
"")
val testFile =TestData.getFile(testFileContent, testProject.defaultBranch!!)
`when`(repo.getProjectFile(anyLong(), anyString(), anyString())).thenReturn(Single.just(testFile))
`when`(base64Tools.decode(anyString())).thenReturn(raw)
......
......@@ -9,8 +9,8 @@ import org.junit.Test
import org.mockito.ArgumentMatchers
import org.mockito.Mockito.`when`
import org.mockito.Mockito.verify
import ru.terrakok.gitlabclient.TestData
import ru.terrakok.gitlabclient.entity.app.session.OAuthParams
import ru.terrakok.gitlabclient.entity.app.session.UserAccount
import ru.terrakok.gitlabclient.model.data.cache.ProjectCache
import ru.terrakok.gitlabclient.model.repository.session.SessionRepository
......@@ -29,14 +29,8 @@ class SessionInteractorTest {
private val OAUTH_PARAMS =
OAuthParams("appId", "appKey", "redirect_url")
private val testAccount = UserAccount(
13L,
"token",
"user_server_path",
"user_avatar_url",
"user_name",
true)
private val testAccount = TestData.getUserAccount()
@Before
fun setUp() {
repository = mock()
......
package ru.terrakok.gitlabclient.model.repository.app
import io.reactivex.Single
import org.junit.Assert.assertEquals
import org.junit.Test
import org.mockito.Mockito.*
import ru.terrakok.gitlabclient.TestSchedulers
import ru.terrakok.gitlabclient.entity.app.develop.AppInfo
import ru.terrakok.gitlabclient.entity.app.develop.AppLibrary
import ru.terrakok.gitlabclient.entity.app.develop.LicenseType
import ru.terrakok.gitlabclient.model.data.storage.Prefs
import ru.terrakok.gitlabclient.model.data.storage.RawAppData
/**
* @author Vitaliy Belyaev on 21.05.2019.
*/
class AppInfoRepositoryTest {
private val appInfo = AppInfo(
"1.5.1",
13,
"test description",
"wer342frvr3",
"https://gitlab.com/terrakok/gitlab-client",
"https://gitlab.com/terrakok/gitlab-client/issues"
)
private val testAppLibraries = listOf(AppLibrary("name", "url", LicenseType.MIT))
private val rawAppData = mock(RawAppData::class.java)
private val prefs = mock(Prefs::class.java)
private val repository = AppInfoRepository(rawAppData, appInfo, prefs, TestSchedulers())
@Test
fun `get timestamp success`() {
val timestamp = 1234545454L
`when`(prefs.firstLaunchTimeStamp).thenReturn(timestamp)
val result = repository.firstLaunchTimeStamp
verify(prefs, times(1)).firstLaunchTimeStamp
verifyNoMoreInteractions(prefs)
assertEquals(timestamp, result)
}
@Test
fun `set timestamp success`() {
val timestamp = 1234545454L
repository.firstLaunchTimeStamp = timestamp
verify(prefs, times(1)).firstLaunchTimeStamp = timestamp
verifyNoMoreInteractions(prefs)
}
@Test
fun `get app info success`() {
val testObserver = repository.getAppInfo().test()
testObserver
.assertNoErrors()
.assertValue(appInfo)
.assertComplete()
}
@Test
fun `get app libraries success`() {
`when`(rawAppData.getAppLibraries()).thenReturn(Single.just(testAppLibraries))
val testObserver = repository.getAppLibraries().test()
verify(rawAppData, times(1)).getAppLibraries()
verifyNoMoreInteractions(rawAppData)
testObserver
.assertNoErrors()
.assertValue(testAppLibraries)
.assertComplete()
}
@Test
fun `get empty libraries list on error`() {
`when`(rawAppData.getAppLibraries()).thenReturn(Single.error(RuntimeException()))
val testObserver = repository.getAppLibraries().test()
verify(rawAppData, times(1)).getAppLibraries()
verifyNoMoreInteractions(rawAppData)
testObserver
.assertNoErrors()
.assertValue(emptyList())
.assertComplete()
}
}
\ No newline at end of file
package ru.terrakok.gitlabclient.model.repository.label
import com.nhaarman.mockitokotlin2.anyOrNull
import io.reactivex.Completable
import io.reactivex.Single
import org.junit.Test
import org.mockito.ArgumentMatchers.anyInt
import org.mockito.ArgumentMatchers.anyLong
import org.mockito.BDDMockito.*
import org.mockito.Mockito.mock
import ru.terrakok.gitlabclient.TestData
import ru.terrakok.gitlabclient.TestSchedulers
import ru.terrakok.gitlabclient.di.PrimitiveWrapper
import ru.terrakok.gitlabclient.model.data.server.GitlabApi
/**
* @author Vitaliy Belyaev on 26.05.2019.
*/
class LabelRepositoryTest {
private val defaultPageSize = 2
private val projectId = 123L
private val testLabel = TestData.getLabel()
private val api = mock(GitlabApi::class.java)
private val repository = LabelRepository(
api,
PrimitiveWrapper(defaultPageSize),
TestSchedulers())
@Test
fun `get label list should succeed with valid input`() {
// GIVEN
val testPage = 1
given(api.getProjectLabels(
anyLong(),
anyInt(),
anyInt())).willReturn(Single.just(listOf(testLabel)))
// WHEN
val testObserver = repository.getLabelList(projectId, testPage).test()
testObserver.awaitTerminalEvent()
// THEN
then(api)
.should(times(1))
.getProjectLabels(projectId, testPage, defaultPageSize)
then(api).shouldHaveNoMoreInteractions()
testObserver.assertResult(listOf(testLabel))
}
@Test
fun `create label should succeed with valid input`() {
// GIVEN
val labelName = "label name"
val color = "color"
val description = "description"
val priority = 1
given(api.createLabel(
anyLong(),
anyString(),
anyString(),
anyOrNull(),
anyOrNull())).willReturn(Single.just(testLabel))
// WHEN
val testObserver = repository
.createLabel(projectId, labelName, color, description, priority)
.test()
testObserver.awaitTerminalEvent()
// THEN
then(api)
.should(times(1))
.createLabel(projectId, labelName, color, description, priority)
then(api).shouldHaveNoMoreInteractions()
testObserver.assertResult(testLabel)
}
@Test
fun `delete label should succeed with valid input`() {
// GIVEN
val labelName = "label name"
given(api.deleteLabel(
anyLong(),
anyString())).willReturn(Completable.complete())
// WHEN
val testObserver = repository.deleteLabel(projectId, labelName).test()
testObserver.awaitTerminalEvent()
// THEN
then(api)
.should(times(1))
.deleteLabel(projectId, labelName)
then(api).shouldHaveNoMoreInteractions()
testObserver
.assertNoErrors()
.assertComplete()
}
@Test
fun `subscribe to label should succeed with valid input`() {
// GIVEN
val labelId = 343L
given(api.subscribeToLabel(
anyLong(),
anyLong())).willReturn(Single.just(testLabel))
// WHEN
val testObserver = repository.subscribeToLabel(projectId, labelId).test()
testObserver.awaitTerminalEvent()
// THEN
then(api)
.should(times(1))
.subscribeToLabel(projectId, labelId)
then(api).shouldHaveNoMoreInteractions()
testObserver.assertResult(testLabel)
}
@Test
fun `unsubscribe from label should succeed with valid input`() {
// GIVEN
val labelId = 343L
given(api.unsubscribeFromLabel(
anyLong(),
anyLong())).willReturn(Single.just(testLabel))
// WHEN
val testObserver = repository.unsubscribeFromLabel(projectId, labelId).test()
testObserver.awaitTerminalEvent()
// THEN
then(api)
.should(times(1))
.unsubscribeFromLabel(projectId, labelId)
then(api).shouldHaveNoMoreInteractions()
testObserver.assertResult(testLabel)
}
}
\ No newline at end of file
package ru.terrakok.gitlabclient.model.repository.milestone
import com.nhaarman.mockitokotlin2.anyOrNull
import io.reactivex.Completable
import io.reactivex.Single
import org.junit.Test
import org.mockito.ArgumentMatchers.*
import org.mockito.BDDMockito.given
import org.mockito.BDDMockito.then
import org.mockito.Mockito
import org.mockito.Mockito.times
import ru.terrakok.gitlabclient.TestData
import ru.terrakok.gitlabclient.TestSchedulers
import ru.terrakok.gitlabclient.di.PrimitiveWrapper
import ru.terrakok.gitlabclient.model.data.server.GitlabApi
/**
* @author Vitaliy Belyaev on 03.06.2019.
*/
class MilestoneRepositoryTest {
private val defaultPageSize = 1
private val testPage = 2
private val testMilestone = TestData.getMilestone()
private val api = Mockito.mock(GitlabApi::class.java)
private val repository = MilestoneRepository(
api,
TestSchedulers(),
PrimitiveWrapper(defaultPageSize))
@Test
fun `get milestones should succeed with valid api response`() {
// GIVEN
given(api.getMilestones(
anyLong(),
anyOrNull(),
anyInt(),
anyInt())).willReturn(Single.just(listOf(testMilestone)))
// WHEN
val testObserver = repository
.getMilestones(projectId = testMilestone.projectId, page = testPage).test()
testObserver.awaitTerminalEvent()
// THEN
then(api)
.should(times(1))
.getMilestones(
testMilestone.projectId,
null,
testPage,
defaultPageSize)
then(api).shouldHaveNoMoreInteractions()
testObserver.assertResult(listOf(testMilestone))
}
@Test
fun `get milestone should succeed with valid api response`() {
// GIVEN
given(api.getMilestone(
anyLong(),
anyLong())).willReturn(Single.just(testMilestone))
// WHEN
val testObserver = repository
.getMilestone(testMilestone.projectId, testMilestone.id).test()
testObserver.awaitTerminalEvent()
// THEN
then(api)
.should(times(1))
.getMilestone(testMilestone.projectId, testMilestone.id)
then(api).shouldHaveNoMoreInteractions()
testObserver.assertResult(testMilestone)
}
@Test
fun `create milestone should succeed with valid api response`() {
// GIVEN
given(api.createMileStone(
anyLong(),
anyString(),
anyOrNull(),
anyOrNull(),
anyOrNull())).willReturn(Single.just(testMilestone))
// WHEN
val testObserver = repository
.createMilestone(
testMilestone.projectId,
testMilestone.title!!,
null,
null,
null).test()
testObserver.awaitTerminalEvent()
// THEN
then(api)
.should(times(1))
.createMileStone(
testMilestone.projectId,
testMilestone.title!!,
null,
null,
null)
then(api).shouldHaveNoMoreInteractions()
testObserver.assertResult(testMilestone)
}
@Test
fun `update milestone should succeed with valid api response`() {
// GIVEN
given(api.updateMileStone(
anyLong(),
anyLong(),
anyOrNull(),
anyOrNull(),
anyOrNull(),
anyOrNull())).willReturn(Single.just(testMilestone))
// WHEN
val testObserver = repository
.updateMilestone(
testMilestone.projectId,
testMilestone.id,
null,
null,
null,
null).test()
testObserver.awaitTerminalEvent()
// THEN
then(api)
.should(times(1))
.updateMileStone(
testMilestone.projectId,
testMilestone.id,
null,
null,
null,
null)
then(api).shouldHaveNoMoreInteractions()
testObserver.assertResult(testMilestone)
}
@Test
fun `delete milestone should succeed with valid api response`() {
// GIVEN
given(api.deleteMilestone(
anyLong(),
anyLong())).willReturn(Completable.complete())
// WHEN
val testObserver = repository
.deleteMilestone(testMilestone.projectId, testMilestone.id).test()
testObserver.awaitTerminalEvent()
// THEN
then(api)
.should(times(1))
.deleteMilestone(testMilestone.projectId, testMilestone.id)
then(api).shouldHaveNoMoreInteractions()
testObserver
.assertNoErrors()
.assertNoValues()
.assertComplete()
}
}
\ No newline at end of file
......@@ -7,7 +7,7 @@ import org.junit.Assert
import org.junit.Test
import org.mockito.Mockito.`when`
import org.mockito.Mockito.verify
import org.threeten.bp.LocalDateTime
import ru.terrakok.gitlabclient.TestData
import ru.terrakok.gitlabclient.TestSchedulers
import ru.terrakok.gitlabclient.entity.User
import ru.terrakok.gitlabclient.model.data.server.GitlabApi
......@@ -23,35 +23,7 @@ class ProfileRepositoryTest {
private val testServer = "Test server"
private val testError = RuntimeException("test error")
private val testDate = LocalDateTime.of(2018, 1, 1, 0, 0)
private val testUser = User(
id = 1L,
username = "",
email = null,
name = "",
state = null,
avatarUrl = null,
webUrl = null,
createdAt = testDate,
isAdmin = false,
bio = null,
location = null,
skype = null,
linkedin = null,
twitter = null,
websiteUrl = null,
organization = null,
lastSignInAt = testDate,
confirmedAt = testDate,
colorSchemeId = 0L,
projectsLimit = 0L,
currentSignInAt = testDate,
identities = null,
canCreateGroup = false,
canCreateProject = false,
twoFactorEnabled = false,
external = false
)
private val testUser = TestData.getUser()
private val api: GitlabApi = mock()
private val profileRepo = ProfileRepository(testServer, api, TestSchedulers())
......
package ru.terrakok.gitlabclient.model.repository.project
import com.nhaarman.mockitokotlin2.anyOrNull
import io.reactivex.Single
import org.junit.Test
import org.mockito.ArgumentMatchers.*
import org.mockito.BDDMockito.given
import org.mockito.BDDMockito.then
import org.mockito.Mockito
import org.mockito.Mockito.times
import ru.terrakok.gitlabclient.TestData
import ru.terrakok.gitlabclient.TestSchedulers
import ru.terrakok.gitlabclient.di.PrimitiveWrapper
import ru.terrakok.gitlabclient.entity.app.ProjectFile
import ru.terrakok.gitlabclient.model.data.server.GitlabApi
/**
* @author Vitaliy Belyaev on 03.06.2019.
*/
class ProjectRepositoryTest {
private val defaultPageSize = 1
private val testPage = 2
private val testProject = TestData.getProject(123L)
private val api = Mockito.mock(GitlabApi::class.java)
private val repository = ProjectRepository(
api,
TestSchedulers(),
PrimitiveWrapper(defaultPageSize))
@Test
fun `get projects should succeed with valid api response`() {
// GIVEN
given(api.getProjects(
anyOrNull(), anyOrNull(), anyOrNull(), anyOrNull(), anyOrNull(),
anyOrNull(), anyOrNull(), anyOrNull(), anyOrNull(), anyInt(), anyInt()))
.willReturn(Single.just(listOf(testProject)))
// WHEN
val testObserver = repository.getProjectsList(page = testPage).test()
testObserver.awaitTerminalEvent()
// THEN
then(api)
.should(times(1))
.getProjects(
null, null, null, null, null,
null, null, null, null,
testPage, defaultPageSize)
then(api).shouldHaveNoMoreInteractions()
testObserver.assertResult(listOf(testProject))
}
@Test
fun `get project should succeed with valid api response`() {
// GIVEN
given(api.getProject(anyLong(), anyBoolean())).willReturn(Single.just(testProject))
// WHEN
val testObserver = repository.getProject(testProject.id).test()
testObserver.awaitTerminalEvent()
// THEN
then(api)
.should(times(1))
.getProject(testProject.id, true)
then(api).shouldHaveNoMoreInteractions()
testObserver.assertResult(testProject)
}
@Test
fun `get project file should succeed with valid api response`() {
// GIVEN
val fileReference = "file reference"
val testFile = TestData.getFile()
given(api.getFile(anyLong(), anyString(), anyString())).willReturn(Single.just(testFile))
// WHEN
val testObserver = repository.getProjectFile(
testProject.id, testFile.path, fileReference).test()
testObserver.awaitTerminalEvent()
// THEN
then(api)
.should(times(1))
.getFile(testProject.id, testFile.path, fileReference)
then(api).shouldHaveNoMoreInteractions()
testObserver.assertResult(testFile)
}
@Test
fun `get project files should map tree nodes to projects files`() {
// GIVEN
val path = "some path"
val branchName = "some branch name"
val testTree = TestData.getRepositoryTreeNode()
val testProjectFile = ProjectFile(testTree.id, testTree.name, testTree.type)
given(api.getRepositoryTree(
anyLong(), anyOrNull(), anyOrNull(), anyOrNull(), anyInt(), anyInt()))
.willReturn(Single.just(listOf(testTree)))
// WHEN
val testObserver = repository.getProjectFiles(
projectId = testProject.id,
path = path,
branchName = branchName,
page = testPage).test()
testObserver.awaitTerminalEvent()
// THEN
then(api)
.should(times(1))
.getRepositoryTree(
testProject.id, path, branchName, null, testPage, defaultPageSize)
then(api).shouldHaveNoMoreInteractions()
testObserver.assertResult(listOf(testProjectFile))
}
@Test
fun `get project branches should succeed with valid api response`() {
// GIVEN
val testBranch = TestData.getBranch()
given(api.getRepositoryBranches(anyLong())).willReturn(Single.just(listOf(testBranch)))
// WHEN
val testObserver = repository.getProjectBranches(testProject.id).test()
testObserver.awaitTerminalEvent()
// THEN
then(api)
.should(times(1))
.getRepositoryBranches(testProject.id)
then(api).shouldHaveNoMoreInteractions()
testObserver.assertResult(listOf(testBranch))
}
}
\ No newline at end of file
package ru.terrakok.gitlabclient.model.repository.session
import com.google.gson.Gson
import org.junit.Assert
import org.junit.Test
import org.mockito.BDDMockito.given
import org.mockito.BDDMockito.then
import org.mockito.Mockito.mock
import org.mockito.Mockito.times
import ru.terrakok.gitlabclient.TestData
import ru.terrakok.gitlabclient.TestSchedulers
import ru.terrakok.gitlabclient.model.data.storage.Prefs
/**
* @author Vitaliy Belyaev on 08.06.2019.
*/
class SessionRepositoryTest {
private val defaultServerPath = "https://someurl.com"
private val testAccount = TestData.getUserAccount()
private val prefs = mock(Prefs::class.java)
private val gson = mock(Gson::class.java)
private val repository = SessionRepository(
defaultServerPath,
prefs,
gson,
TestSchedulers())
@Test
fun `get current account should return null when there is no selected account`() {
// GIVEN
given(prefs.selectedAccount).willReturn(null)
// WHEN
val result = repository.getCurrentUserAccount()
// THEN
then(prefs).should(times(1)).selectedAccount
then(prefs).shouldHaveNoMoreInteractions()
Assert.assertEquals(null, result)
}
@Test
fun `get current account should return stored selected account`() {
// GIVEN
given(prefs.selectedAccount).willReturn(testAccount.id)
given(prefs.accounts).willReturn(listOf(testAccount, testAccount.copy(userId = 43)))
// WHEN
val result = repository.getCurrentUserAccount()
// THEN
then(prefs).should(times(1)).selectedAccount
then(prefs).should(times(1)).accounts
then(prefs).shouldHaveNoMoreInteractions()
Assert.assertEquals(testAccount, result)
}
@Test
fun `set current account should return null when not find account with such id`() {
// GIVEN
given(prefs.accounts).willReturn(listOf(testAccount.copy(userId = 43)))
// WHEN
val result = repository.setCurrentUserAccount(testAccount.id)
// THEN
then(prefs).should(times(1)).accounts
then(prefs).should(times(1)).selectedAccount = null
then(prefs).shouldHaveNoMoreInteractions()
Assert.assertEquals(null, result)
}
@Test
fun `set current account should return stored account and set selected account`() {
// GIVEN
given(prefs.accounts).willReturn(listOf(testAccount))
// WHEN
val result = repository.setCurrentUserAccount(testAccount.id)
// THEN
then(prefs).should(times(1)).accounts
then(prefs).should(times(1)).selectedAccount = testAccount.id
then(prefs).shouldHaveNoMoreInteractions()
Assert.assertEquals(testAccount, result)
}
@Test
fun `get user accounts should return stored user accounts`() {
// GIVEN
val storedAccounts = listOf(testAccount, testAccount.copy(userId = 43))
given(prefs.accounts).willReturn(storedAccounts)
// WHEN
val result = repository.getUserAccounts()
// THEN
then(prefs).should(times(1)).accounts
then(prefs).shouldHaveNoMoreInteractions()
Assert.assertEquals(storedAccounts, result)
}
@Test
fun `logout should remove account from stored user accounts`() {
// GIVEN
val storedAccounts = listOf(testAccount, testAccount.copy(userId = 43))
val expectedAccounts = listOf(testAccount.copy(userId = 43))
given(prefs.accounts).willReturn(storedAccounts)
// WHEN
repository.logout(testAccount.id)
// THEN
then(prefs).should(times(1)).accounts
then(prefs).should(times(1)).accounts = expectedAccounts
}
@Test
fun `logout should return first account from new stored accounts when ids matches`() {
// GIVEN
val storedAccounts = listOf(testAccount, testAccount.copy(userId = 43))
val expectedAccounts = listOf(testAccount.copy(userId = 43))
val newAccount = testAccount.copy(userId = 43)
given(prefs.accounts).willReturn(storedAccounts)
given(prefs.selectedAccount).willReturn(testAccount.id)
// WHEN
val result = repository.logout(testAccount.id)
// THEN
then(prefs).should(times(1)).accounts
then(prefs).should(times(1)).accounts = expectedAccounts
then(prefs).should(times(1)).selectedAccount
then(prefs).should(times(1)).selectedAccount = newAccount.id
then(prefs).shouldHaveNoMoreInteractions()
Assert.assertEquals(newAccount, result)
}
@Test
fun `logout should return selected account from new stored accounts when ids not matches`() {
// GIVEN
val storedAccounts = listOf(testAccount, testAccount.copy(userId = 43))
val expectedAccounts = listOf(testAccount.copy(userId = 43))
val selectedAccount = testAccount.copy(userId = 43)
given(prefs.accounts).willReturn(storedAccounts)
given(prefs.selectedAccount).willReturn(selectedAccount.id)
// WHEN
val result = repository.logout(testAccount.id)
// THEN
then(prefs).should(times(1)).accounts
then(prefs).should(times(1)).accounts = expectedAccounts
then(prefs).should(times(1)).selectedAccount
then(prefs).shouldHaveNoMoreInteractions()
Assert.assertEquals(selectedAccount, result)
}
}
\ No newline at end of file
package ru.terrakok.gitlabclient.model.repository.todo
import com.nhaarman.mockitokotlin2.anyOrNull
import io.reactivex.Completable
import io.reactivex.Single
import org.junit.Test
import org.mockito.ArgumentMatchers.anyInt
import org.mockito.BDDMockito.given
import org.mockito.BDDMockito.then
import org.mockito.Mockito
import org.mockito.Mockito.times
import ru.terrakok.gitlabclient.TestData
import ru.terrakok.gitlabclient.TestSchedulers
import ru.terrakok.gitlabclient.di.PrimitiveWrapper
import ru.terrakok.gitlabclient.entity.todo.TodoState
import ru.terrakok.gitlabclient.model.data.server.GitlabApi
/**
* Created by Vitaliy Belyaev on 11.06.2019.
*/
class TodoRepositoryTest {
private val defaultPageSize = 1
private val testPage = 2
private val testTodo = TestData.getTodo()
private val testUser = TestData.getUser()
private val api = Mockito.mock(GitlabApi::class.java)
private val repository = TodoRepository(
api,
TestSchedulers(),
PrimitiveWrapper(defaultPageSize))
@Test
fun `get todos should succeed with valid api response`() {
// GIVEN
val expectedTargetHeader = TestData.getTargetHeaderForTodo(testTodo, testUser)
given(api.getTodos(
anyOrNull(), anyOrNull(), anyOrNull(), anyOrNull(),
anyOrNull(), anyInt(), anyInt())).willReturn(Single.just(listOf(testTodo)))
// WHEN
val testObserver = repository.getTodos(currentUser = testUser, page = testPage).test()
testObserver.awaitTerminalEvent()
// THEN
then(api)
.should(times(1))
.getTodos(null, null, null, null,
null, testPage, defaultPageSize)
then(api).shouldHaveNoMoreInteractions()
testObserver.assertResult(listOf(expectedTargetHeader))
}
@Test
fun `mark pending todo done should return todo with done state`() {
// GIVEN
val doneTodo = testTodo.copy(state = TodoState.DONE)
given(api.markPendingTodoAsDone(anyInt())).willReturn(Single.just(doneTodo))
// WHEN
val testObserver = repository.markPendingTodoAsDone(testTodo.id.toInt()).test()
testObserver.awaitTerminalEvent()
// THEN
then(api)
.should(times(1))
.markPendingTodoAsDone(testTodo.id.toInt())
then(api).shouldHaveNoMoreInteractions()
testObserver.assertResult(doneTodo)
}
@Test
fun `mark all pending todos done should return complete with valid api response`() {
// GIVEN
given(api.markAllPendingTodosAsDone()).willReturn(Completable.complete())
// WHEN
val testObserver = repository.markAllPendingTodosAsDone().test()
testObserver.awaitTerminalEvent()
// THEN
then(api)
.should(times(1))
.markAllPendingTodosAsDone()
then(api).shouldHaveNoMoreInteractions()
testObserver.assertNoErrors()
.assertNoValues()
.assertComplete()
}
}
\ No newline at end of file
......@@ -3,9 +3,8 @@ package ru.terrakok.gitlabclient.presentation.project.labels
import com.nhaarman.mockitokotlin2.*
import io.reactivex.Single
import org.junit.Test
import ru.terrakok.gitlabclient.TestData
import ru.terrakok.gitlabclient.di.PrimitiveWrapper
import ru.terrakok.gitlabclient.entity.Color
import ru.terrakok.gitlabclient.entity.Label
import ru.terrakok.gitlabclient.model.interactor.label.LabelInteractor
import ru.terrakok.gitlabclient.model.system.flow.FlowRouter
import ru.terrakok.gitlabclient.presentation.global.ErrorHandler
......@@ -126,17 +125,5 @@ class ProjectLabelsPresenterTest {
}
}
private fun label(id: Long, subscribed: Boolean = false): Label {
return Label(
id = id,
name = "name$id",
color = Color("green", 12),
description = null,
openIssuesCount = 0,
closedIssuesCount = 0,
openMergeRequestsCount = 0,
subscribed = subscribed,
priority = null
)
}
}
\ No newline at end of file
private fun label(id: Long, subscribed: Boolean = false) = TestData.getLabel(id, subscribed)
}