-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add fields to aggregated endpoint
- Loading branch information
1 parent
8ba2f84
commit d183a0f
Showing
13 changed files
with
336 additions
and
27 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
31 changes: 31 additions & 0 deletions
31
lapis2/src/main/kotlin/org/genspectrum/lapis/request/LapisRequest.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,31 @@ | ||
package org.genspectrum.lapis.request | ||
|
||
import com.fasterxml.jackson.core.JsonParser | ||
import com.fasterxml.jackson.databind.DeserializationContext | ||
import com.fasterxml.jackson.databind.JsonDeserializer | ||
import com.fasterxml.jackson.databind.JsonNode | ||
import com.fasterxml.jackson.databind.node.ArrayNode | ||
import org.springframework.boot.jackson.JsonComponent | ||
|
||
data class AggregationRequest( | ||
val sequenceFilters: Map<String, String>, | ||
val fields: List<String>, | ||
) | ||
|
||
@JsonComponent | ||
class AggregationRequestDeserializer : JsonDeserializer<AggregationRequest>() { | ||
override fun deserialize(p: JsonParser, ctxt: DeserializationContext): AggregationRequest { | ||
val node = p.readValueAsTree<JsonNode>() | ||
|
||
val fields = when (node.get("fields")) { | ||
null -> emptyList() | ||
is ArrayNode -> node.get("fields").asSequence().map { it.asText() }.toList() | ||
else -> throw IllegalArgumentException("Fields in AggregationRequest must be an array or null") | ||
} | ||
|
||
val sequenceFilters = | ||
node.fields().asSequence().filter { it.key != "fields" }.associate { it.key to it.value.asText() } | ||
|
||
return AggregationRequest(sequenceFilters, fields) | ||
} | ||
} |
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
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
70 changes: 70 additions & 0 deletions
70
lapis2/src/test/kotlin/org/genspectrum/lapis/request/AggregationRequestDeserializerTest.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,70 @@ | ||
package org.genspectrum.lapis.request | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper | ||
import org.hamcrest.MatcherAssert | ||
import org.hamcrest.Matchers | ||
import org.junit.jupiter.api.Assertions.assertThrows | ||
import org.junit.jupiter.api.Test | ||
import org.junit.jupiter.params.ParameterizedTest | ||
import org.junit.jupiter.params.provider.Arguments | ||
import org.junit.jupiter.params.provider.MethodSource | ||
import org.springframework.beans.factory.annotation.Autowired | ||
import org.springframework.boot.test.context.SpringBootTest | ||
|
||
@SpringBootTest | ||
class AggregationRequestDeserializerTest { | ||
@Autowired | ||
private lateinit var objectMapper: ObjectMapper | ||
|
||
@ParameterizedTest(name = "Test AggregationRequestDeserializer {1}") | ||
@MethodSource("getTestAggregationRequest") | ||
fun `AggregationRequest is correctly deserialized from JSON`(underTest: String, expected: AggregationRequest) { | ||
val result = objectMapper.readValue(underTest, AggregationRequest::class.java) | ||
|
||
MatcherAssert.assertThat(result, Matchers.equalTo(expected)) | ||
} | ||
|
||
companion object { | ||
@JvmStatic | ||
fun getTestAggregationRequest() = listOf( | ||
Arguments.of( | ||
""" | ||
{ | ||
"country": "Switzerland", | ||
"fields": ["division", "country"] | ||
} | ||
""", | ||
AggregationRequest( | ||
mapOf("country" to "Switzerland"), | ||
listOf("division", "country"), | ||
), | ||
), | ||
Arguments.of( | ||
""" | ||
{ | ||
"country": "Switzerland" | ||
} | ||
""", | ||
AggregationRequest( | ||
mapOf("country" to "Switzerland"), | ||
emptyList(), | ||
), | ||
), | ||
|
||
) | ||
} | ||
|
||
@Test | ||
fun `Given an AggregationRequest with fields not null or ArrayList it should return an error`() { | ||
val underTest = """ | ||
{ | ||
"country": "Switzerland", | ||
"fields": "notAnArrayNode" | ||
} | ||
""" | ||
|
||
assertThrows(IllegalArgumentException::class.java) { | ||
objectMapper.readValue(underTest, AggregationRequest::class.java) | ||
} | ||
} | ||
} |
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
Oops, something went wrong.