Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix main class #6

Merged
merged 2 commits into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/main/ApiUtil.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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) {
Expand All @@ -35,13 +34,17 @@ class LoadParameterContainer() {
}
}

suspend fun multiPartDataHandler(multiPartData: MultiPartData, formItemHandler: (part: PartData.FormItem) -> Unit = {}): List<FileContent> {
suspend fun multiPartDataHandler(
multiPartData: MultiPartData,
formItemHandler: (part: PartData.FormItem) -> Unit = {}
): List<FileContent> {
val files = mutableListOf<FileContent>()
multiPartData.forEachPart { part ->
when (part) {
is PartData.FormItem -> {
formItemHandler(part)
}

is PartData.FileItem -> {
val name = part.originalFileName as String
val content = part.streamProvider().readBytes()
Expand Down
1 change: 0 additions & 1 deletion src/main/Application.kt → src/main/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>): Unit = io.ktor.server.netty.EngineMain.main(args)

Expand Down
1 change: 0 additions & 1 deletion src/main/Solver.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/application.conf
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ ktor {
port = ${?PORT}
}
application {
modules = [ com.github.statnett.loadflowservice.ApplicationKt.module ]
modules = [ com.github.statnett.loadflowservice.AppKt.module ]
}
}
26 changes: 12 additions & 14 deletions src/test/ApplicationTest.kt → src/test/AppTest.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
}
Expand All @@ -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)
}

Expand Down Expand Up @@ -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
}
)
}
Expand Down
Loading