Commit d5a9ad07 authored by Konstantin Tskhovrebov's avatar Konstantin Tskhovrebov 🤖

Merge branch 'feature/todo-repo-tests' into 'feature/repositories_unit_tests'

Add tests for TodoRepository and refactor some test classes to use TestData

See merge request !211
parents d5798e37 a6937d0c
......@@ -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() {
......
package ru.terrakok.gitlabclient
import com.google.gson.GsonBuilder
import org.threeten.bp.LocalDateTime
import org.threeten.bp.Month
import ru.terrakok.gitlabclient.entity.*
......@@ -11,12 +12,95 @@ import ru.terrakok.gitlabclient.entity.mergerequest.MergeRequest
import ru.terrakok.gitlabclient.entity.mergerequest.MergeRequestState
import ru.terrakok.gitlabclient.entity.milestone.Milestone
import ru.terrakok.gitlabclient.entity.milestone.MilestoneState
import ru.terrakok.gitlabclient.entity.target.TargetState
import ru.terrakok.gitlabclient.entity.target.TargetType
import ru.terrakok.gitlabclient.entity.todo.Todo
import ru.terrakok.gitlabclient.entity.todo.TodoAction
import ru.terrakok.gitlabclient.model.data.server.deserializer.LocalDateTimeDeserializer
import ru.terrakok.gitlabclient.model.data.server.deserializer.TodoDeserializer
/**
* @author Vitaliy Belyaev on 01.06.2019.
*/
object TestData {
val todoJson =
"""
{
"id": 102,
"project": {
"id": 2,
"name": "Gitlab Ce",
"name_with_namespace": "Gitlab Org / Gitlab Ce",
"path": "gitlab-ce",
"path_with_namespace": "gitlab-org/gitlab-ce"
},
"author": {
"name": "Administrator",
"username": "root",
"id": 1,
"state": "active",
"avatar_url": "http://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80&d=identicon",
"web_url": "https://gitlab.example.com/root"
},
"action_name": "marked",
"target_type": "MergeRequest",
"target": {
"id": 34,
"iid": 7,
"project_id": 2,
"title": "Dolores in voluptatem tenetur praesentium omnis repellendus voluptatem quaerat.",
"description": "Et ea et omnis illum cupiditate. Dolor aspernatur tenetur ducimus facilis est nihil. Quo esse cupiditate molestiae illo corrupti qui quidem dolor.",
"state": "opened",
"created_at": "2016-06-17T07:49:24.419Z",
"updated_at": "2016-06-17T07:52:43.484Z",
"target_branch": "tutorials_git_tricks",
"source_branch": "DNSBL_docs",
"upvotes": 0,
"downvotes": 0,
"author": {
"name": "Maxie Medhurst",
"username": "craig_rutherford",
"id": 12,
"state": "active",
"avatar_url": "http://www.gravatar.com/avatar/a0d477b3ea21970ce6ffcbb817b0b435?s=80&d=identicon",
"web_url": "https://gitlab.example.com/craig_rutherford"
},
"assignee": {
"name": "Administrator",
"username": "root",
"id": 1,
"state": "active",
"avatar_url": "http://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80&d=identicon",
"web_url": "https://gitlab.example.com/root"
},
"source_project_id": 2,
"target_project_id": 2,
"labels": [],
"work_in_progress": false,
"milestone": {
"id": 32,
"iid": 2,
"project_id": 2,
"title": "v1.0",
"description": "Assumenda placeat ea voluptatem voluptate qui.",
"state": "active",
"created_at": "2016-06-17T07:47:34.163Z",
"updated_at": "2016-06-17T07:47:34.163Z",
"due_date": null
},
"merge_when_pipeline_succeeds": false,
"merge_status": "cannot_be_merged",
"subscribed": true,
"user_notes_count": 7
},
"target_url": "https://gitlab.example.com/gitlab-org/gitlab-ce/merge_requests/7",
"body": "Dolores in voluptatem tenetur praesentium omnis repellendus voluptatem quaerat.",
"state": "pending",
"created_at": "2016-06-17T07:52:35.225Z"
}
"""
fun getCommit() = Commit(
"dsf233fef2fes34",
"dsf233",
......@@ -53,10 +137,10 @@ object TestData {
webUrl = "https://gitlab.com/terrakok/gitlab-client",
tagList = null,
owner = null,
name = "",
name = "GitFox",
nameWithNamespace = "",
path = "test path",
pathWithNamespace = "",
pathWithNamespace = "terrakok/gitlab-client",
issuesEnabled = false,
openIssuesCount = 0L,
mergeRequestsEnabled = false,
......@@ -156,13 +240,13 @@ object TestData {
)
}
fun getFile() = File(
fun getFile(content: String = "file content", branch: String = "test_br") = File(
"file name",
"file path",
500L,
"encoding",
"file content",
"file branch",
content,
branch,
"blob id",
"commit id",
"last commit id"
......@@ -199,6 +283,18 @@ object TestData {
"url of milestone"
)
fun getLabel(id: Long = 555L, subscribed: Boolean = false) = Label(
id = id,
name = "name$id",
color = Color("green", 12),
description = "description",
openIssuesCount = 0,
closedIssuesCount = 0,
openMergeRequestsCount = 0,
subscribed = subscribed,
priority = null
)
fun getUserAccount() = UserAccount(
13L,
"token",
......@@ -207,4 +303,75 @@ object TestData {
"user_name",
true
)
fun getTodo() = GsonBuilder()
.registerTypeAdapter(LocalDateTime::class.java, LocalDateTimeDeserializer())
.registerTypeAdapter(Todo::class.java, TodoDeserializer())
.create().fromJson(todoJson, Todo::class.java)
fun getUser() = User(
4321L, "username", "email@mail.com", "Name", null,
null, null,
LocalDateTime.of(2007, Month.DECEMBER, 14, 11, 0),
false, null, null, null, null, null,
null, null,
LocalDateTime.of(2009, Month.DECEMBER, 14, 11, 0),
LocalDateTime.of(2008, Month.DECEMBER, 14, 11, 0),
2424L, 32L,
LocalDateTime.of(2008, Month.DECEMBER, 14, 11, 0),
null, canCreateGroup = false, canCreateProject = false,
twoFactorEnabled = true, external = false
)
fun getTargetHeaderForTodo(todo: Todo, currentUser: User): TargetHeader {
val target = todo.target
val assignee = if (todo.actionName != TodoAction.MARKED) {
currentUser.let {
Assignee(it.id, it.state, it.name, it.webUrl, it.avatarUrl, it.username)
}
} else {
null
}
val appTarget = when (todo.targetType) {
TargetType.MERGE_REQUEST -> AppTarget.MERGE_REQUEST
TargetType.ISSUE -> AppTarget.ISSUE
}
val targetName = when (todo.targetType) {
TargetType.MERGE_REQUEST -> "${AppTarget.MERGE_REQUEST} !${target.iid}"
TargetType.ISSUE -> "${AppTarget.ISSUE} #${target.iid}"
}
val badges = mutableListOf<TargetBadge>()
badges.add(
TargetBadge.Status(
when (target.state) {
TargetState.OPENED -> TargetBadgeStatus.OPENED
TargetState.CLOSED -> TargetBadgeStatus.CLOSED
TargetState.MERGED -> TargetBadgeStatus.MERGED
}
)
)
badges.add(TargetBadge.Text(todo.author.username, AppTarget.USER, todo.author.id))
badges.add(TargetBadge.Text(todo.project.name, AppTarget.PROJECT, todo.project.id))
return TargetHeader.Public(
todo.author,
TargetHeaderIcon.NONE,
TargetHeaderTitle.Todo(
todo.author.name,
assignee?.name,
todo.actionName,
targetName,
todo.project.nameWithNamespace,
todo.author.id == currentUser.id,
assignee?.id == currentUser.id
),
todo.body,
todo.createdAt,
appTarget,
target.id,
TargetInternal(target.projectId, target.iid),
badges,
TargetAction.Undefined
)
}
}
\ No newline at end of file
......@@ -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()
......
......@@ -9,9 +9,12 @@ import org.mockito.BDDMockito.then
import org.mockito.Mockito.*
import org.threeten.bp.LocalDateTime
import org.threeten.bp.Month
import ru.terrakok.gitlabclient.TestData
import ru.terrakok.gitlabclient.TestSchedulers
import ru.terrakok.gitlabclient.di.PrimitiveWrapper
import ru.terrakok.gitlabclient.entity.*
import ru.terrakok.gitlabclient.entity.Author
import ru.terrakok.gitlabclient.entity.OrderBy
import ru.terrakok.gitlabclient.entity.Sort
import ru.terrakok.gitlabclient.entity.app.target.*
import ru.terrakok.gitlabclient.entity.event.Event
import ru.terrakok.gitlabclient.entity.event.EventAction
......@@ -26,7 +29,7 @@ class EventRepositoryTest {
private val defaultPageSize = 1
private val testPage = 2
private val testEvent = getTestEvent()
private val testProject = getTestProject(testEvent.projectId)
private val testProject = TestData.getProject(testEvent.projectId)
private val api = mock(GitlabApi::class.java)
private val markDownUrlResolver = mock(MarkDownUrlResolver::class.java)
......@@ -429,46 +432,6 @@ class EventRepositoryTest {
.assertResult(listOf(expectedTargetHeader))
}
private fun getTestProject(projectId: Long) = Project(
id = projectId,
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 fun getTestEvent() = Event(
123L,
EventAction.CREATED,
......@@ -481,20 +444,9 @@ class EventRepositoryTest {
Author(1L, "", "", "", "", ""),
"author",
null,
getTestNote()
TestData.getNote()
)
private fun getTestNote() = Note(
13L,
"note test body",
Author(1L, "", "", "", "", ""),
LocalDateTime.of(2007, Month.DECEMBER, 14, 11, 0),
null,
false,
435L,
EventTargetType.ISSUE,
333L)
private fun getExpectedTargetHeaderForIssue(event: Event): TargetHeader {
return TargetHeader.Public(
event.author,
......
......@@ -9,12 +9,12 @@ import org.mockito.Mockito.mock
import org.mockito.Mockito.times
import org.threeten.bp.LocalDateTime
import org.threeten.bp.Month
import ru.terrakok.gitlabclient.TestData
import ru.terrakok.gitlabclient.TestSchedulers
import ru.terrakok.gitlabclient.di.PrimitiveWrapper
import ru.terrakok.gitlabclient.entity.Author
import ru.terrakok.gitlabclient.entity.Note
import ru.terrakok.gitlabclient.entity.Project
import ru.terrakok.gitlabclient.entity.Visibility
import ru.terrakok.gitlabclient.entity.app.target.*
import ru.terrakok.gitlabclient.entity.event.EventAction
import ru.terrakok.gitlabclient.entity.event.EventTargetType
......@@ -30,7 +30,7 @@ class IssueRepositoryTest {
private val defaultPageSize = 1
private val testPage = 2
private val testIssue = getTestIssue()
private val testProject = getTestProject(testIssue.projectId)
private val testProject = TestData.getProject(testIssue.projectId)
private val api = mock(GitlabApi::class.java)
private val markDownUrlResolver = mock(MarkDownUrlResolver::class.java)
......@@ -443,7 +443,6 @@ class IssueRepositoryTest {
testObserver.awaitTerminalEvent()
// THEN
then(api)
.should(times(1))
.getMilestoneIssues(
......@@ -457,46 +456,6 @@ class IssueRepositoryTest {
testObserver.assertResult(listOf(testIssue))
}
private fun getTestProject(projectId: Long) = Project(
id = projectId,
description = null,
defaultBranch = "test_br",
visibility = Visibility.PUBLIC,
sshUrlToRepo = null,
httpUrlToRepo = null,
webUrl = "https://gitlab.com/terrakok/gitlab-client",
tagList = null,
owner = null,
name = "testProjectName",
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 fun getTestIssue() = Issue(
123L,
3342424L,
......
......@@ -8,10 +8,9 @@ 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.entity.Color
import ru.terrakok.gitlabclient.entity.Label
import ru.terrakok.gitlabclient.model.data.server.GitlabApi
/**
......@@ -20,17 +19,7 @@ import ru.terrakok.gitlabclient.model.data.server.GitlabApi
class LabelRepositoryTest {
private val defaultPageSize = 2
private val projectId = 123L
private val testLabel = Label(
234L,
"test label",
Color("black", 123),
"description",
2,
3,
1,
false,
1
)
private val testLabel = TestData.getLabel()
private val api = mock(GitlabApi::class.java)
private val repository = LabelRepository(
......
......@@ -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.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()