diff --git a/src/main/ApiUtil.kt b/src/main/ApiUtil.kt index b8d3513..550ac76 100644 --- a/src/main/ApiUtil.kt +++ b/src/main/ApiUtil.kt @@ -1,7 +1,6 @@ package com.github.statnett.loadflowservice import com.powsybl.loadflow.LoadFlowParameters -import com.powsybl.loadflow.LoadFlowResult import com.powsybl.loadflow.json.JsonLoadFlowParameters import io.ktor.http.content.* import java.io.ByteArrayInputStream @@ -19,7 +18,7 @@ class FileContent(val name: String, val bytes: ByteArray) /** * Convenience class used to deserialize and update a load parameter instance */ -class LoadParameterContainer() { +class LoadParameterContainer { var parameters = LoadFlowParameters() private var parametersModified = false private fun update(jsonString: String) { @@ -35,13 +34,17 @@ class LoadParameterContainer() { } } -suspend fun multiPartDataHandler(multiPartData: MultiPartData, formItemHandler: (part: PartData.FormItem) -> Unit = {}): List { +suspend fun multiPartDataHandler( + multiPartData: MultiPartData, + formItemHandler: (part: PartData.FormItem) -> Unit = {} +): List { val files = mutableListOf() multiPartData.forEachPart { part -> when (part) { is PartData.FormItem -> { formItemHandler(part) } + is PartData.FileItem -> { val name = part.originalFileName as String val content = part.streamProvider().readBytes() diff --git a/src/main/Application.kt b/src/main/App.kt similarity index 97% rename from src/main/Application.kt rename to src/main/App.kt index 774d13a..d043ea4 100644 --- a/src/main/Application.kt +++ b/src/main/App.kt @@ -7,7 +7,6 @@ import io.ktor.server.plugins.contentnegotiation.* import io.ktor.server.request.* import io.ktor.server.response.* import io.ktor.server.routing.* -import java.io.ByteArrayInputStream fun main(args: Array): Unit = io.ktor.server.netty.EngineMain.main(args) diff --git a/src/main/Solver.kt b/src/main/Solver.kt index 06471d5..c42f375 100644 --- a/src/main/Solver.kt +++ b/src/main/Solver.kt @@ -3,7 +3,6 @@ package com.github.statnett.loadflowservice import com.powsybl.iidm.network.Network import com.powsybl.loadflow.LoadFlow import com.powsybl.loadflow.LoadFlowParameters -import com.powsybl.loadflow.LoadFlowResult import com.powsybl.loadflow.json.JsonLoadFlowParameters import java.io.ByteArrayInputStream import java.io.ByteArrayOutputStream diff --git a/src/main/resources/application.conf b/src/main/resources/application.conf index 5a6bfd2..534f8c7 100644 --- a/src/main/resources/application.conf +++ b/src/main/resources/application.conf @@ -4,6 +4,6 @@ ktor { port = ${?PORT} } application { - modules = [ com.github.statnett.loadflowservice.ApplicationKt.module ] + modules = [ com.github.statnett.loadflowservice.AppKt.module ] } } \ No newline at end of file diff --git a/src/test/ApplicationTest.kt b/src/test/AppTest.kt similarity index 81% rename from src/test/ApplicationTest.kt rename to src/test/AppTest.kt index f400507..c2dafb3 100644 --- a/src/test/ApplicationTest.kt +++ b/src/test/AppTest.kt @@ -1,17 +1,14 @@ import com.github.statnett.loadflowservice.busPropertiesFromNetwork import com.powsybl.ieeecdf.converter.IeeeCdfNetworkFactory import io.ktor.client.request.* -import io.ktor.client.request.forms.formData -import io.ktor.client.request.forms.submitFormWithBinaryData +import io.ktor.client.request.forms.* import io.ktor.client.statement.* -import io.ktor.http.Headers -import io.ktor.http.HttpHeaders -import io.ktor.http.HttpStatusCode +import io.ktor.http.* import io.ktor.http.content.* -import io.ktor.server.testing.testApplication +import io.ktor.server.testing.* import java.io.File import java.nio.file.Paths -import java.util.Properties +import java.util.* import kotlin.math.abs import kotlin.test.Test import kotlin.test.assertContains @@ -34,9 +31,9 @@ class ApplicationTest { client.submitFormWithBinaryData( url = "/get-buses", formData = - formData { - append("network", "not file content") - }, + formData { + append("network", "not file content") + }, ) assertEquals(HttpStatusCode.UnprocessableEntity, response.status) } @@ -57,7 +54,8 @@ class ApplicationTest { assertTrue(body.startsWith("[{")) assertTrue(body.endsWith("}]")) - val busString = "{\"id\":\"VL1_0\",\"voltage\":143.1,\"angle\":0.0,\"activePower\":0.0,\"reactivePower\":0.0}" + val busString = + "{\"id\":\"VL1_0\",\"voltage\":143.1,\"angle\":0.0,\"activePower\":0.0,\"reactivePower\":0.0}" assertContains(body, busString) } @@ -87,12 +85,12 @@ class ApplicationTest { val angles = busPropertiesFromNetwork(solvedNetwork).map { bus -> bus.angle }.toList() val regex = Regex("\"angle\":([0-9.-]+)") - val anglesFromJsonStr = regex.findAll(body).map {match -> match.groupValues[1].toDouble()}.toList() + val anglesFromJsonStr = regex.findAll(body).map { match -> match.groupValues[1].toDouble() }.toList() // It seems like the solved version from Powsybl contains rounded angles assertTrue( - angles.zip(anglesFromJsonStr).all { - pair -> abs(pair.component1() - pair.component2()) < 0.01 + angles.zip(anglesFromJsonStr).all { pair -> + abs(pair.component1() - pair.component2()) < 0.01 } ) }