Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
NTV-386: Migrate SurveyResponse.java to kotlin and deprecate Autoparc…
…el (#1564) * Rename .java to .kt * Migrate SurveyResponse.java and SurveyResponseFactory.java to kotlin and deprecate Autoparcel * Add unit test * unit test * add unit test * add unit test * add unit test * fix unit test
- Loading branch information
Showing
5 changed files
with
234 additions
and
96 deletions.
There are no files selected for viewing
30 changes: 0 additions & 30 deletions
30
app/src/main/java/com/kickstarter/mock/factories/SurveyResponseFactory.java
This file was deleted.
Oops, something went wrong.
27 changes: 27 additions & 0 deletions
27
app/src/main/java/com/kickstarter/mock/factories/SurveyResponseFactory.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.kickstarter.mock.factories | ||
|
||
import com.kickstarter.mock.factories.ProjectFactory.allTheWayProject | ||
import com.kickstarter.models.SurveyResponse | ||
import org.joda.time.DateTime | ||
|
||
object SurveyResponseFactory { | ||
@JvmStatic | ||
fun surveyResponse(): SurveyResponse { | ||
val surveyUrl = "https://www.kickstarter.com/surveys/" + IdFactory.id() | ||
|
||
val web = SurveyResponse.Urls.Web.builder() | ||
.survey(surveyUrl) | ||
.build() | ||
|
||
val urlsEnvelope = SurveyResponse.Urls.builder() | ||
.web(web) | ||
.build() | ||
|
||
return SurveyResponse.builder() | ||
.answeredAt(DateTime().minusDays(10)) | ||
.id(IdFactory.id().toLong()) | ||
.project(allTheWayProject()) | ||
.urls(urlsEnvelope) | ||
.build() | ||
} | ||
} |
66 changes: 0 additions & 66 deletions
66
app/src/main/java/com/kickstarter/models/SurveyResponse.java
This file was deleted.
Oops, something went wrong.
131 changes: 131 additions & 0 deletions
131
app/src/main/java/com/kickstarter/models/SurveyResponse.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
package com.kickstarter.models | ||
|
||
import android.os.Parcelable | ||
import kotlinx.parcelize.Parcelize | ||
import org.joda.time.DateTime | ||
|
||
@Parcelize | ||
class SurveyResponse private constructor( | ||
private val answeredAt: DateTime?, | ||
private val id: Long, | ||
private val project: Project?, | ||
private val urls: Urls? | ||
) : Parcelable { | ||
|
||
fun answeredAt() = this.answeredAt | ||
fun id() = this.id | ||
fun project() = this.project | ||
fun urls() = this.urls | ||
|
||
@Parcelize | ||
data class Builder( | ||
private var answeredAt: DateTime? = DateTime.now(), | ||
private var id: Long = 0L, | ||
private var project: Project? = null, | ||
private var urls: Urls? = null | ||
) : Parcelable { | ||
|
||
fun answeredAt(answeredAt: DateTime?) = apply { answeredAt?.let { this.answeredAt = it } } | ||
fun id(id: Long) = apply { this.id = id ?: 0L } | ||
fun project(project: Project?) = apply { this.project = project } | ||
fun urls(urls: Urls?) = apply { urls?.let { this.urls = it } } | ||
fun build() = SurveyResponse( | ||
answeredAt = this.answeredAt, | ||
id = id, | ||
project = project, | ||
urls = this.urls | ||
) | ||
} | ||
|
||
@Parcelize | ||
class Urls private constructor( | ||
private val web: Web | ||
) : Parcelable { | ||
fun web() = this.web | ||
|
||
@Parcelize | ||
data class Builder( | ||
private var web: Web = Web.builder().build() | ||
) : Parcelable { | ||
fun web(web: Web?) = apply { this.web = web ?: Web.builder().build() } | ||
|
||
fun build() = Urls( | ||
web = web | ||
) | ||
} | ||
|
||
fun toBuilder() = Builder( | ||
web = web, | ||
) | ||
|
||
companion object { | ||
@JvmStatic | ||
fun builder() = Builder() | ||
} | ||
|
||
override fun equals(obj: Any?): Boolean { | ||
var equals = super.equals(obj) | ||
if (obj is Urls) { | ||
equals = web() == obj.web() | ||
} | ||
return equals | ||
} | ||
|
||
@Parcelize | ||
class Web private constructor( | ||
private val survey: String? | ||
) : Parcelable { | ||
fun survey() = this.survey | ||
|
||
@Parcelize | ||
data class Builder( | ||
private var survey: String? = null | ||
) : Parcelable { | ||
fun survey(survey: String?) = apply { this.survey = survey } | ||
fun build() = Web( | ||
survey = survey | ||
) | ||
} | ||
|
||
fun toBuilder() = Builder( | ||
survey = survey | ||
) | ||
|
||
companion object { | ||
@JvmStatic | ||
fun builder() = Builder() | ||
} | ||
|
||
override fun equals(obj: Any?): Boolean { | ||
var equals = super.equals(obj) | ||
if (obj is Web) { | ||
equals = survey() == obj.survey() | ||
} | ||
return equals | ||
} | ||
} | ||
} | ||
|
||
fun toBuilder() = Builder( | ||
answeredAt = answeredAt, | ||
id = id, | ||
project = project, | ||
urls = urls | ||
) | ||
|
||
override fun equals(obj: Any?): Boolean { | ||
var equals = super.equals(obj) | ||
if (obj is SurveyResponse) { | ||
equals = answeredAt() == obj.answeredAt() && | ||
id() == obj.id() && | ||
project() == obj.project() && | ||
urls() == obj.urls() | ||
} | ||
return equals | ||
} | ||
|
||
companion object { | ||
@JvmStatic | ||
fun builder() = Builder() | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
app/src/test/java/com/kickstarter/models/SurveyResponseTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package com.kickstarter.models | ||
|
||
import com.kickstarter.KSRobolectricTestCase | ||
import com.kickstarter.mock.factories.IdFactory | ||
import com.kickstarter.mock.factories.ProjectFactory | ||
import org.joda.time.DateTime | ||
import org.junit.Test | ||
|
||
class SurveyResponseTest : KSRobolectricTestCase() { | ||
|
||
@Test | ||
fun testDefaultInit() { | ||
val dateTime: DateTime = DateTime.now().plusMillis(300) | ||
|
||
val surveyUrl = "https://www.kickstarter.com/surveys/" + IdFactory.id() | ||
|
||
val project = ProjectFactory.allTheWayProject() | ||
|
||
val web = SurveyResponse.Urls.Web.builder() | ||
.survey(surveyUrl) | ||
.build() | ||
|
||
val urlsEnvelope = SurveyResponse.Urls.builder() | ||
.web(web) | ||
.build() | ||
|
||
val survey = SurveyResponse.builder() | ||
.id(1234L) | ||
.answeredAt(dateTime) | ||
.project(project) | ||
.urls(urlsEnvelope).build() | ||
|
||
assertEquals(survey.id(), 1234L) | ||
assertEquals(survey.answeredAt(), dateTime) | ||
assertEquals(survey.project(), project) | ||
assertEquals(survey.urls(), urlsEnvelope) | ||
assertEquals(survey.urls()?.web(), web) | ||
assertEquals(survey.urls()?.web()?.survey(), surveyUrl) | ||
assertEquals(web.survey(), surveyUrl) | ||
assertEquals(urlsEnvelope.web(), web) | ||
} | ||
|
||
@Test | ||
fun testDefaultWebInit() { | ||
val surveyUrl = "https://www.kickstarter.com/surveys/" + IdFactory.id() | ||
|
||
val web = SurveyResponse.Urls.Web.builder().build().toBuilder().survey(surveyUrl).build() | ||
assertEquals(web.survey(), surveyUrl) | ||
|
||
val urlsEnvelope = SurveyResponse.Urls.builder().build().toBuilder().web(web).build() | ||
assertEquals(urlsEnvelope.web(), web) | ||
} | ||
|
||
@Test | ||
fun testSurvey_equalFalse() { | ||
val survey = SurveyResponse.builder().build() | ||
val survey2 = SurveyResponse.builder().project(ProjectFactory.backedProject()).build() | ||
val survey3 = SurveyResponse.builder().project(ProjectFactory.allTheWayProject()).id(5678L).build() | ||
val survey4 = SurveyResponse.builder().project(ProjectFactory.allTheWayProject()).build() | ||
|
||
assertFalse(survey == survey2) | ||
assertFalse(survey == survey3) | ||
assertFalse(survey == survey4) | ||
|
||
assertFalse(survey3 == survey2) | ||
assertFalse(survey3 == survey4) | ||
} | ||
|
||
@Test | ||
fun testSurvey_equalTrue() { | ||
val survey1 = SurveyResponse.builder().build() | ||
val survey2 = SurveyResponse.builder().build() | ||
|
||
assertEquals(survey1, survey2) | ||
} | ||
} |