Skip to content
This repository was archived by the owner on Mar 30, 2025. It is now read-only.

Commit d17da6d

Browse files
committed
fix sort order of highscore
1 parent a428963 commit d17da6d

File tree

4 files changed

+25
-21
lines changed

4 files changed

+25
-21
lines changed

adapter/src/main/kotlin/org/railwaystations/rsapi/adapter/db/StationAdapter.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,6 @@ class StationAdapter(private val dsl: DSLContext) : StationPort {
186186
.join(UserTable).on(UserTable.id.eq(PhotoTable.photographerid))
187187
.where(value(countryCode).isNull.or(StationTable.countrycode.eq(countryCode)))
188188
.groupBy(UserTable.name)
189-
.orderBy(count())
190189
.fetch { it.value1()!! to it.value2() }.toMap()
191190

192191
@Transactional

adapter/src/main/kotlin/org/railwaystations/rsapi/adapter/web/controller/PhotographersController.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ import org.springframework.web.bind.annotation.RestController
88
@RestController
99
class PhotographersController(private val loadPhotographersUseCase: LoadPhotographersUseCase) : PhotographersApi {
1010
override fun getPhotographers(country: String?): ResponseEntity<Any> {
11-
return ResponseEntity.ok(loadPhotographersUseCase.getPhotographersPhotocountMap(country))
11+
return ResponseEntity.ok(
12+
loadPhotographersUseCase.getPhotographersPhotocountMap(country)
13+
.entries.sortedByDescending { it.value }.associate { it.toPair() }
14+
)
1215
}
1316

1417
}

adapter/src/test/kotlin/org/railwaystations/rsapi/adapter/db/StationAdapterTest.kt

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,18 +110,26 @@ class StationAdapterTest : AbstractPostgreSqlTest() {
110110

111111
assertThat(photographers).isEqualTo(
112112
mapOf<String, Int>(
113-
"@user0" to 10, "@user10" to 17, "@user2" to 6, "@user27" to 31, "@user4" to 1, "@user8" to 29
113+
"@user27" to 31,
114+
"@user8" to 29,
115+
"@user10" to 17,
116+
"@user0" to 10,
117+
"@user2" to 6,
118+
"@user4" to 1,
114119
)
115120
)
116121
}
117122

118123
@Test
119-
fun getPhotographerMapAllDe() {
124+
fun getPhotographerMapDe() {
120125
val photographers = sut.getPhotographerMap("de")
121126

122127
assertThat(photographers).isEqualTo(
123128
mapOf<String, Int>(
124-
"@user0" to 9, "@user10" to 16, "@user27" to 31, "@user8" to 29
129+
"@user27" to 31,
130+
"@user8" to 29,
131+
"@user10" to 16,
132+
"@user0" to 9,
125133
)
126134
)
127135
}

adapter/src/test/kotlin/org/railwaystations/rsapi/adapter/web/controller/PhotographersControllerTest.kt

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
1818
import org.springframework.test.context.ContextConfiguration
1919
import org.springframework.test.web.servlet.MockMvc
2020
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
21-
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath
21+
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.content
2222
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status
2323

2424
@WebMvcTest(controllers = [PhotographersController::class])
@@ -50,32 +50,26 @@ internal class PhotographersControllerTest {
5050

5151
@Test
5252
fun getPhotographersOfCountryDe() {
53-
every { stationAdapter.getPhotographerMap("de") } returns createPhotographersResponse()
53+
every { stationAdapter.getPhotographerMap("de") } returns photographersMap
5454

5555
mvc.perform(get("/photographers?country=de"))
5656
.andExpect(status().isOk())
57-
.andExpect(jsonPath("$.@user27").value(31))
58-
.andExpect(jsonPath("$.@user8").value(29))
59-
.andExpect(jsonPath("$.@user10").value(15))
60-
.andExpect(jsonPath("$.@user0").value(9))
57+
.andExpect(content().string(photographersResponse)) // use string comparison because of ordering
6158
.andExpect(validOpenApiResponse())
6259
}
6360

6461
@Test
6562
fun getPhotographersOfAllCountries() {
66-
every { stationAdapter.getPhotographerMap(null) } returns createPhotographersResponse()
63+
every { stationAdapter.getPhotographerMap(null) } returns photographersMap
6764

6865
mvc.perform(get("/photographers"))
6966
.andExpect(status().isOk())
70-
.andExpect(jsonPath("$.@user27").value(31))
71-
.andExpect(jsonPath("$.@user8").value(29))
72-
.andExpect(jsonPath("$.@user10").value(15))
73-
.andExpect(jsonPath("$.@user0").value(9))
67+
.andExpect(content().string(photographersResponse)) // use string comparison because of ordering
7468
.andExpect(validOpenApiResponse())
7569
}
7670

77-
private fun createPhotographersResponse(): Map<String, Int> {
78-
return mapOf("@user27" to 31, "@user8" to 29, "@user10" to 15, "@user0" to 9)
79-
}
80-
81-
}
71+
}
72+
73+
private val photographersMap = mapOf("@user8" to 29, "@user0" to 9, "@user10" to 15, "@user27" to 31)
74+
75+
private const val photographersResponse = """{"@user27":31,"@user8":29,"@user10":15,"@user0":9}"""

0 commit comments

Comments
 (0)