diff --git a/src/main/ApiUtil.kt b/src/main/ApiUtil.kt index aad9d51..6e06bc9 100644 --- a/src/main/ApiUtil.kt +++ b/src/main/ApiUtil.kt @@ -93,4 +93,16 @@ fun substationNames(network: Network): List { fun voltageLevelNames(network: Network): List { return network.voltageLevels.map { voltageLevel -> voltageLevel.nameOrId }.toList() +} + +fun generatorNames(network: Network): List { + return network.generators.map { generator -> generator.nameOrId } +} + +fun loadNames(network: Network): List { + return network.loads.map { load -> load.nameOrId } +} + +fun branchNames(network: Network): List { + return network.lines.map { line -> line.nameOrId } } \ No newline at end of file diff --git a/src/main/App.kt b/src/main/App.kt index 7696bf5..c632d98 100644 --- a/src/main/App.kt +++ b/src/main/App.kt @@ -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) } diff --git a/src/test/AppTest.kt b/src/test/AppTest.kt index 78ffc26..c2bec4a 100644 --- a/src/test/AppTest.kt +++ b/src/test/AppTest.kt @@ -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 { @@ -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 { @@ -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"]) + } + } + } + + }