-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #61 from ReneeVandervelde/master
Fix null crash for updatestate lastInstall field
- Loading branch information
Showing
6 changed files
with
120 additions
and
3 deletions.
There are no files selected for viewing
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
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
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
20 changes: 20 additions & 0 deletions
20
lights/src/main/kotlin/inkapplications/shade/lights/HueUpdateStateTransformer.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,20 @@ | ||
package inkapplications.shade.lights | ||
|
||
import com.squareup.moshi.FromJson | ||
import com.squareup.moshi.ToJson | ||
import org.threeten.bp.Instant | ||
|
||
internal object HueUpdateStateTransformer { | ||
@ToJson | ||
fun toHueFormat(state: UpdateState): HueUpdateState = HueUpdateState( | ||
state = state.state, | ||
lastInstall = state.lastKnownInstall.takeIf { it != Instant.MIN } | ||
) | ||
|
||
@FromJson | ||
fun fromHueFormat(state: HueUpdateState): UpdateState = UpdateState( | ||
state = state.state, | ||
lastInstall = state.lastInstall ?: Instant.MIN, | ||
lastKnownInstall = state.lastInstall | ||
) | ||
} |
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
74 changes: 74 additions & 0 deletions
74
lights/src/test/kotlin/inkapplications/shade/lights/HueUpdateStateTransformerTest.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,74 @@ | ||
package inkapplications.shade.lights | ||
|
||
import junit.framework.TestCase.assertEquals | ||
import junit.framework.TestCase.assertNull | ||
import org.junit.Test | ||
import org.threeten.bp.Instant | ||
|
||
class HueUpdateStateTransformerTest { | ||
@Test | ||
fun fromHueFormat() { | ||
val now = Instant.now() | ||
val given = HueUpdateState("fake", now) | ||
|
||
val result = HueUpdateStateTransformer.fromHueFormat(given) | ||
|
||
assertEquals("State is not modified", "fake", result.state) | ||
assertEquals("Last install is used in nullable", now, result.lastKnownInstall) | ||
assertEquals("Last install is used in non-nullable field", now, result.lastInstall) | ||
} | ||
|
||
@Test | ||
fun fromNullHueFormat() { | ||
val given = HueUpdateState("fake", null) | ||
|
||
val result = HueUpdateStateTransformer.fromHueFormat(given) | ||
|
||
assertEquals("State is not modified", "fake", result.state) | ||
assertNull("Nullable field should reflect null last install", result.lastKnownInstall) | ||
assertEquals("Non-nullable field should use MIN as timestamp", Instant.MIN, result.lastInstall) | ||
} | ||
|
||
@Test | ||
fun toHueFormatCompat() { | ||
val now = Instant.now() | ||
val given = UpdateState("fake", now) | ||
|
||
val result = HueUpdateStateTransformer.toHueFormat(given) | ||
|
||
assertEquals("State is not modified", "fake", result.state) | ||
assertEquals("Nullable last install should be passed along", now, result.lastInstall) | ||
} | ||
|
||
@Test | ||
fun toHueFormatCompatSpecified() { | ||
val now = Instant.now() | ||
val given = UpdateState("fake", Instant.MIN, now) | ||
|
||
val result = HueUpdateStateTransformer.toHueFormat(given) | ||
|
||
assertEquals("State is not modified", "fake", result.state) | ||
assertEquals("Nullable field should take precedent if specified", now, result.lastInstall) | ||
} | ||
|
||
@Test | ||
fun toHueFormatCompatNull() { | ||
val now = Instant.now() | ||
val given = UpdateState("fake", now, null) | ||
|
||
val result = HueUpdateStateTransformer.toHueFormat(given) | ||
|
||
assertEquals("State is not modified", "fake", result.state) | ||
assertNull("Nullable field should take precedent if specified, even if null", result.lastInstall) | ||
} | ||
|
||
@Test | ||
fun toHueFormatCompatNullFromMin() { | ||
val given = UpdateState("fake", Instant.MIN) | ||
|
||
val result = HueUpdateStateTransformer.toHueFormat(given) | ||
|
||
assertEquals("State is not modified", "fake", result.state) | ||
assertNull("Min instants should be interpreted as null for compatibility", result.lastInstall) | ||
} | ||
} |