Skip to content

Instantly share code, notes, and snippets.

@FrancisBaileyH
Created January 26, 2024 05:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FrancisBaileyH/216f03256a6f65a8ea95f45c5c69dd58 to your computer and use it in GitHub Desktop.
Save FrancisBaileyH/216f03256a6f65a8ea95f45c5c69dd58 to your computer and use it in GitHub Desktop.
package com.francisbailey.pathbulletin.service.intake
import com.francisbailey.pathbulletin.persistence.model.RestrictedArea
import com.francisbailey.pathbulletin.persistence.model.RestrictionPeriod
import com.francisbailey.pathbulletin.persistence.model.RestrictionStatus
import com.francisbailey.pathbulletin.persistence.repository.RestrictedAreaRepository
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
import org.mockito.kotlin.mock
import org.mockito.kotlin.verify
import org.springframework.data.geo.Point
import org.springframework.data.mongodb.core.geo.GeoJsonPolygon
import java.time.MonthDay
class MVAPolygonImporterTest {
private val restrictedAreaRepository = mock<RestrictedAreaRepository>()
private val importer = MVAPolygonImporter(restrictedAreaRepository)
@Test
fun `converts geojson into expected restricted area and saves it`() {
val geoJson = """
{
"type": "FeatureCollection",
"name": "WHSE_ADMIN_BOUNDARIES.ADM_NR_REGIONS_SP",
"features": [{
"type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": [
[
[-123.08158436280799, 49.71244989462963],
[-123.08138112619768, 49.71277384363288],
[-123.08158436280799, 49.71244989462963]
]
]
},
"properties": {
"OBJECTID": 1,
"Closure_Date": "April 1 to November 30",
"Closure_ID": "South Coast Region",
"SHAPE_Length": 0.000000,
"SHAPE_Area": 0.000000
}
}]
}
""".trimIndent()
importer.import(geoJson)
verify(restrictedAreaRepository).saveAll(org.mockito.kotlin.check<List<RestrictedArea>> {
assertEquals(1, it.size)
assertEquals("mva-1", it.first().id)
assertEquals("South Coast Region", it.first().name)
assertEquals("Motor Vehicle Act", it.first().reason)
assertEquals(RestrictionPeriod(MonthDay.of(4, 1), MonthDay.of(11, 30)), it.first().restrictionPeriods.first())
assertEquals("Area is temporarily restricted https://www.bclaws.gov.bc.ca/civix/document/id/complete/statreg/196_99#section76", it.first().message)
assertEquals(RestrictionStatus.INACTIVE, it.first().status)
assertEquals(GeoJsonPolygon(listOf(
Point(-123.081584, 49.712450),
Point(-123.081381, 49.712774),
Point(-123.081584, 49.712450)
)), it.first().area)
})
}
@Test
fun `handles multiple dates correctly`() {
val geoJson = """
{
"type": "FeatureCollection",
"name": "WHSE_ADMIN_BOUNDARIES.ADM_NR_REGIONS_SP",
"features": [{
"type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": [
[
[-123.08158436280799, 49.71244989462963],
[-123.08138112619768, 49.71277384363288],
[-123.08158436280799, 49.71244989462963]
]
]
},
"properties": {
"OBJECTID": 1,
"Closure_Date": "April 1 to June 15 and September 16 to November 30",
"Closure_ID": "South Coast Region",
"SHAPE_Length": 0.000000,
"SHAPE_Area": 0.000000
}
}]
}
""".trimIndent()
importer.import(geoJson)
verify(restrictedAreaRepository).saveAll(org.mockito.kotlin.check<List<RestrictedArea>> {
assertEquals(listOf(
RestrictionPeriod(MonthDay.of(4, 1), MonthDay.of(6, 15)),
RestrictionPeriod(MonthDay.of(9, 16), MonthDay.of(11, 30))
), it.first().restrictionPeriods)
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment