Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
NTV-469: Migrate PushTokenBody.java to kotlin and deprecate Autoparcel (
#1602) * Migrate to kotlin, deprecate autoparcel, all parcelize * Tests * Kotlin linter Co-authored-by: Hadia <hadiamohamed.iti@gmail.com>
- Loading branch information
1 parent
ad47957
commit 150c67e
Showing
3 changed files
with
103 additions
and
28 deletions.
There are no files selected for viewing
28 changes: 0 additions & 28 deletions
28
app/src/main/java/com/kickstarter/services/apirequests/PushTokenBody.java
This file was deleted.
Oops, something went wrong.
45 changes: 45 additions & 0 deletions
45
app/src/main/java/com/kickstarter/services/apirequests/PushTokenBody.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,45 @@ | ||
package com.kickstarter.services.apirequests | ||
|
||
import android.os.Parcelable | ||
import kotlinx.parcelize.Parcelize | ||
|
||
@Parcelize | ||
class PushTokenBody private constructor( | ||
private val pushServer: String?, | ||
private val token: String? | ||
) : Parcelable { | ||
fun pushServer() = this.pushServer | ||
fun token() = this.token | ||
|
||
@Parcelize | ||
data class Builder( | ||
private var pushServer: String? = null, | ||
private var token: String? = null | ||
) : Parcelable { | ||
fun pushServer(pushServer: String?) = apply { this.pushServer = pushServer } | ||
fun token(token: String?) = apply { this.token = token } | ||
fun build() = PushTokenBody( | ||
pushServer = pushServer, | ||
token = token | ||
) | ||
} | ||
|
||
fun toBuilder() = Builder( | ||
pushServer = pushServer, | ||
token = token | ||
) | ||
|
||
companion object { | ||
@JvmStatic | ||
fun builder() = Builder() | ||
} | ||
|
||
override fun equals(other: Any?): Boolean { | ||
var equals = super.equals(other) | ||
if (other is PushTokenBody) { | ||
equals = pushServer() == other.pushServer() && | ||
token() == other.token() | ||
} | ||
return equals | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
app/src/test/java/com/kickstarter/services/apirequests/PushTokenBodyTest.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,58 @@ | ||
package com.kickstarter.services.apirequests | ||
|
||
import junit.framework.TestCase | ||
import org.junit.Test | ||
|
||
class PushTokenBodyTest : TestCase() { | ||
|
||
@Test | ||
fun testDefaultInit() { | ||
val pushTokenBody = PushTokenBody.builder() | ||
.pushServer("push_server") | ||
.token("this_is_a_token") | ||
.build() | ||
|
||
assertEquals(pushTokenBody.pushServer(), "push_server") | ||
assertEquals(pushTokenBody.token(), "this_is_a_token") | ||
} | ||
|
||
@Test | ||
fun testEquals_whenFieldsDontMatch_returnFalse() { | ||
val pushTokenBody1 = PushTokenBody.builder() | ||
.pushServer("push_server") | ||
.token("this_is_a_token") | ||
.build() | ||
|
||
val pushTokenBody2 = pushTokenBody1.toBuilder() | ||
.pushServer("push_server2") | ||
.build() | ||
|
||
val pushTokenBody3 = pushTokenBody1.toBuilder().token("this_is_a_token2").build() | ||
|
||
assertFalse(pushTokenBody1 == pushTokenBody2) | ||
assertFalse(pushTokenBody1 == pushTokenBody3) | ||
assertFalse(pushTokenBody2 == pushTokenBody3) | ||
} | ||
|
||
@Test | ||
fun testEquals_whenFieldsMatch_returnTrue() { | ||
val pushTokenBody1 = PushTokenBody.builder() | ||
.pushServer("push_server") | ||
.token("this_is_a_token") | ||
.build() | ||
|
||
val pushTokenBody2 = pushTokenBody1 | ||
|
||
assertTrue(pushTokenBody1 == pushTokenBody2) | ||
} | ||
|
||
@Test | ||
fun testToBuilder() { | ||
val pushTokenBody = PushTokenBody.builder() | ||
.token("first_token") | ||
.build().toBuilder().token("changed_token").build() | ||
|
||
assertEquals(pushTokenBody.token(), "changed_token") | ||
assertNull(pushTokenBody.pushServer()) | ||
} | ||
} |