Skip to content

Commit

Permalink
NTV-480: Migrate InternalBuildEnvelope.java to kotlin and deprecate A…
Browse files Browse the repository at this point in the history
…utoparcel (#1612)

* Mograte to kotlin, deprecate autoparcel, tests

* Linter

Co-authored-by: Isabel Martin <[email protected]>
Co-authored-by: Hadia <[email protected]>
  • Loading branch information
3 people authored May 5, 2022
1 parent 4b8ce8c commit cd5d8a8
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 52 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.kickstarter.mock.factories

import com.kickstarter.services.apiresponses.InternalBuildEnvelope

object InternalBuildEnvelopeFactory {
fun internalBuildEnvelope(): InternalBuildEnvelope {
return InternalBuildEnvelope.builder()
.build(123456)
.changelog("Bug fixes")
.newerBuildAvailable(false)
.build()
}

@JvmStatic
fun newerBuildAvailable(): InternalBuildEnvelope {
return internalBuildEnvelope().toBuilder()
.newerBuildAvailable(true)
.build()
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.kickstarter.services.apiresponses

import android.os.Parcelable
import kotlinx.parcelize.Parcelize

@Parcelize
class InternalBuildEnvelope private constructor(
private val build: Int?,
private val changelog: String?,
private val newerBuildAvailable: Boolean
) : Parcelable {
fun build() = this.build
fun changelog() = this.changelog
fun newerBuildAvailable() = this.newerBuildAvailable

@Parcelize
data class Builder(
private var build: Int? = null,
private var changelog: String? = null,
private var newerBuildAvailable: Boolean = false
) : Parcelable {
fun build(build: Int?) = apply { this.build = build ?: 0 }
fun changelog(changelog: String?) = apply { this.changelog = changelog ?: "" }
fun newerBuildAvailable(newerBuildAvailable: Boolean?) = apply { this.newerBuildAvailable = newerBuildAvailable ?: false }
fun build() = InternalBuildEnvelope(
build = build,
changelog = changelog,
newerBuildAvailable = newerBuildAvailable
)
}

fun toBuilder() = Builder(
build = build,
changelog = changelog,
newerBuildAvailable = newerBuildAvailable
)

override fun equals(other: Any?): Boolean {
var equals = super.equals(other)
if (other is InternalBuildEnvelope) {
equals = build() == other.build() &&
changelog() == other.changelog() &&
newerBuildAvailable() == other.newerBuildAvailable()
}
return equals
}

companion object {
fun builder(): Builder {
return Builder()
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package com.kickstarter.services.apiresponses

import junit.framework.TestCase
import org.junit.Test

class InternalBuildEnvelopeTest : TestCase() {

@Test
fun testInternalBuildEnvelopeDefaultInit() {
val internalBuildEnvelope = InternalBuildEnvelope.builder().build(343).changelog("changelog").newerBuildAvailable(true).build()

assertEquals(internalBuildEnvelope.build(), 343)
assertEquals(internalBuildEnvelope.changelog(), "changelog")
assertEquals(internalBuildEnvelope.newerBuildAvailable(), true)
}

@Test
fun testInternalBuildEnvelopeEquals_whenFieldsDontMatch_returnsFalse() {
val internalBuildEnvelope1 = InternalBuildEnvelope.builder()
.build(343)
.changelog("changelog1")
.newerBuildAvailable(true)
.build()

val internalBuildEnvelope2 = internalBuildEnvelope1.toBuilder()
.build(876)
.build()

val internalBuildEnvelope3 = internalBuildEnvelope1.toBuilder()
.changelog("changelog3")
.build()

val internalBuildEnvelope4 = internalBuildEnvelope1.toBuilder()
.newerBuildAvailable(false)
.build()

assertFalse(internalBuildEnvelope1 == internalBuildEnvelope2)
assertFalse(internalBuildEnvelope1 == internalBuildEnvelope3)
assertFalse(internalBuildEnvelope1 == internalBuildEnvelope4)
assertFalse(internalBuildEnvelope2 == internalBuildEnvelope3)
assertFalse(internalBuildEnvelope2 == internalBuildEnvelope4)
assertFalse(internalBuildEnvelope3 == internalBuildEnvelope4)
}

@Test
fun testInternalBuildEnvelopeEquals_whenFieldsMatch_returnsTrue() {
val internalBuildEnvelope1 = InternalBuildEnvelope.builder()
.build(343)
.changelog("changelog1")
.newerBuildAvailable(true)
.build()

val internalBuildEnvelope2 = internalBuildEnvelope1
assertTrue(internalBuildEnvelope1 == internalBuildEnvelope2)
}

@Test
fun testProjectsEnvelopToBuilder() {
val internalBuildEnvelope = InternalBuildEnvelope.builder()
.build(343)
.changelog("changelog1")
.newerBuildAvailable(true)
.build()
.toBuilder()
.changelog("another_changelog")
.build()

assertEquals(internalBuildEnvelope.changelog(), "another_changelog")
}
}

0 comments on commit cd5d8a8

Please sign in to comment.