Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert GeoIPCookie and its test case to Kotlin #4659

Merged
merged 4 commits into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.wikipedia.feed.announcement

import android.location.Location

class GeoIPCookie(
private val country: String,
private val region: String,
private val city: String,
private val location: Location?
) {
fun country(): String {
cooltey marked this conversation as resolved.
Show resolved Hide resolved
return country
}

fun region(): String {
return region
}

fun city(): String {
return city
}

fun location(): Location? {
return location
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package org.wikipedia.feed.announcement

import org.hamcrest.MatcherAssert
import org.hamcrest.Matchers
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.wikipedia.feed.announcement.GeoIPCookieUnmarshaller.unmarshal

@RunWith(RobolectricTestRunner::class)
class GeoIPCookieUnmarshallerTest {
@Test
fun testGeoIPWithLocation() {
val cookie = unmarshal("US:California:San Francisco:$LATITUDE:$LONGITUDE:v4")
MatcherAssert.assertThat(cookie.country(), Matchers.`is`("US"))
MatcherAssert.assertThat(cookie.region(), Matchers.`is`("California"))
MatcherAssert.assertThat(cookie.city(), Matchers.`is`("San Francisco"))
MatcherAssert.assertThat(cookie.location(), Matchers.`is`(Matchers.notNullValue()))
MatcherAssert.assertThat(cookie.location()?.latitude, Matchers.`is`(LATITUDE))
MatcherAssert.assertThat(cookie.location()?.longitude, Matchers.`is`(LONGITUDE))
}

@Test
fun testGeoIPWithoutLocation() {
val cookie = unmarshal("FR::Paris:::v4")
MatcherAssert.assertThat(cookie.country(), Matchers.`is`("FR"))
MatcherAssert.assertThat(cookie.region(), Matchers.`is`(""))
MatcherAssert.assertThat(cookie.city(), Matchers.`is`("Paris"))
MatcherAssert.assertThat(cookie.location(), Matchers.`is`(Matchers.nullValue()))
}

@Test
fun testGeoIPEmpty() {
val cookie = unmarshal(":::::v4")
MatcherAssert.assertThat(cookie.country(), Matchers.`is`(""))
MatcherAssert.assertThat(cookie.region(), Matchers.`is`(""))
MatcherAssert.assertThat(cookie.city(), Matchers.`is`(""))
MatcherAssert.assertThat(cookie.location(), Matchers.`is`(Matchers.nullValue()))
}

@Test(expected = IllegalArgumentException::class)
fun testGeoIPWrongVersion() {
unmarshal("RU::Moscow:1:2:v5")
}

@Test(expected = IllegalArgumentException::class)
fun testGeoIPWrongParamCount() {
unmarshal("CA:Toronto:v4")
}

@Test(expected = IllegalArgumentException::class)
fun testGeoIPMalformed() {
unmarshal("foo")
}

@Test(expected = IllegalArgumentException::class)
fun testGeoIPWithBadLocation() {
unmarshal("US:California:San Francisco:foo:bar:v4")
}

companion object {
private const val LATITUDE = 37.33
private const val LONGITUDE = -121.89
}
}
Loading