Skip to content

Commit

Permalink
Add utility end-point for extracting generator names, load names and …
Browse files Browse the repository at this point in the history
…branch names
  • Loading branch information
davidkleiven committed Oct 12, 2023
1 parent d1b0173 commit 28ec806
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 2 deletions.
12 changes: 12 additions & 0 deletions src/main/ApiUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,16 @@ fun substationNames(network: Network): List<String> {

fun voltageLevelNames(network: Network): List<String> {
return network.voltageLevels.map { voltageLevel -> voltageLevel.nameOrId }.toList()
}

fun generatorNames(network: Network): List<String> {
return network.generators.map { generator -> generator.nameOrId }
}

fun loadNames(network: Network): List<String> {
return network.loads.map { load -> load.nameOrId }
}

fun branchNames(network: Network): List<String> {
return network.lines.map { line -> line.nameOrId }
}
30 changes: 30 additions & 0 deletions src/main/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,36 @@ fun Application.module() {
}
}

post("/generator-names") {
val files = multiPartDataHandler(call.receiveMultipart())
if (files.isEmpty()) {
call.response.status(HttpStatusCode.UnprocessableEntity)
} else {
val network = networkFromFileContent(files[0])
call.respond(generatorNames(network))
}
}

post("/load-names") {
val files = multiPartDataHandler(call.receiveMultipart())
if (files.isEmpty()) {
call.response.status(HttpStatusCode.UnprocessableEntity)
} else {
val network = networkFromFileContent(files[0])
call.respond(loadNames(network))
}
}

post("/branch-names") {
val files = multiPartDataHandler(call.receiveMultipart())
if (files.isEmpty()) {
call.response.status(HttpStatusCode.UnprocessableEntity)
} else {
val network = networkFromFileContent(files[0])
call.respond(branchNames(network))
}
}

get("/default-load-parameters") {
call.respondText(defaultLoadFlowParameters(), ContentType.Application.Json, HttpStatusCode.OK)
}
Expand Down
33 changes: 31 additions & 2 deletions src/test/AppTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ class ApplicationTest {
"/voltage-level-names",
"/diagram",
"/diagram/substation/S1",
"/diagram/voltage-level/VL1"
"/diagram/voltage-level/VL1",
"/generator-names",
"/branch-names",
"/load-names"
).map { url ->
DynamicTest.dynamicTest("422 when no network is passed to $url") {
testApplication {
Expand All @@ -66,7 +69,10 @@ class ApplicationTest {
"/voltage-level-names",
"/diagram",
"/diagram/substation/S1",
"/diagram/voltage-level/VL1"
"/diagram/voltage-level/VL1",
"/generator-names",
"/branch-names",
"/load-names"
).map { url ->
DynamicTest.dynamicTest("500 when file content can not be parsed $url") {
testApplication {
Expand Down Expand Up @@ -295,6 +301,29 @@ class ApplicationTest {
}
}
}

@TestFactory
fun `test response 200 and that known substring is part of the body`() = listOf(
mapOf("url" to "/generator-names", "content-substring" to "B1-G"),
mapOf("url" to "/load-names", "content-substring" to "B2-L"),
mapOf("url" to "/branch-names", "content-substring" to "L7-8-1")
).map { args ->
DynamicTest.dynamicTest("Test ${args["url"]}") {
testApplication {
val response = client.submitFormWithBinaryData(
url = args["url"]!!,
formData = sensitivityFormData.network
)

assertEquals(HttpStatusCode.OK, response.status)
val body = response.bodyAsText()
assertTrue(body.contains(args["content-substring"]!!))
assertEquals("application/json; charset=UTF-8", response.headers["content-type"])
}
}
}


}


Expand Down

0 comments on commit 28ec806

Please sign in to comment.