Skip to content

Commit

Permalink
WIP: Refactor test suite + add support for CIM/XML import from zipped…
Browse files Browse the repository at this point in the history
… file
  • Loading branch information
davidkleiven committed Oct 3, 2023
1 parent 6e4fa62 commit 19b66cc
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 54 deletions.
63 changes: 10 additions & 53 deletions src/test/AppTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,16 @@ class ApplicationTest {
assertContains(body, busString)
}

@Test
fun `test load 14 bus network cgmes`() =
testApplication {
val response = client.submitFormWithBinaryData(
url = "/buses",
formData = formDataFromFile(ieeeCdfNetwork14CgmesFile())
)
assertEquals(HttpStatusCode.OK, response.status)
}

@Test
fun `test default load parameters`() =
testApplication {
Expand Down Expand Up @@ -264,67 +274,14 @@ class ApplicationTest {
}
}

fun formDataFromFile(file: File): List<PartData> {
return formData {
append(
"network",
file.readBytes(),
Headers.build {
append(HttpHeaders.ContentDisposition, "filename=${file.name}")
},
)
}
}

fun formDataWithEmptyNetwork(): List<PartData> {
return formData {
append(
"network",
byteArrayOf(),
Headers.build {
append(HttpHeaders.ContentDisposition, "filename=emptyFile.xiidm")
}
)
}
}

fun ieeeCdfNetwork14File(): File {
// Initialize temporary file
val file = File.createTempFile("network", ".xiidm")
file.deleteOnExit()

IeeeCdfNetworkFactory.create14().write("XIIDM", Properties(), Paths.get(file.path))
return file
}

// Function for checking some properties of a body to verify that the returned body
// is a valid svg image
fun isPlausibleSvg(body: String): Boolean {
return body.contains("<svg") && body.contains("<?xml version")
}

fun formDataMinimalNetworkRawx(): List<PartData> {
return formData {
append(
"network",
minimalRawx(),
Headers.build {
append(HttpHeaders.ContentDisposition, "filename=network.rawx")
}
)
}
}

fun minimalRawx(): ByteArray {
return ("{\"network\":{\"caseid\":{" +
"\"fields\":[\"ic\",\"sbase\",\"rev\",\"xfrrat\",\"nxfrat\",\"basfrq\",\"title1\"]," +
"\"data\":[0,100.00,35,0,0,60.00,\"PSS(R)EMinimumRAWXCase\"]}," +
"\"bus\":{\"fields\":[\"ibus\",\"name\",\"baskv\",\"ide\"]," +
"\"data\":[[1,\"Slack-Bus\",138.0,3],[2,\"Load-Bus\",138.01]]}," +
"\"load\":{\"fields\":[\"ibus\",\"loadid\",\"stat\",\"pl\",\"ql\"]," +
"\"data\":[[2,\"1\",1,40.0,15.0]]}," +
"\"generator\":{\"fields\":[\"ibus\",\"machid\",\"pg\",\"qg\"]," +
"\"data\":[[1,\"1\",\"40.35\",\"10.87\"]]}," +
"\"acline\":{\"fields\":[\"ibus\",\"jbus\",\"ckt\",\"rpu\",\"xpu\",\"bpu\"]," +
"\"data\":[[1,2,\"1\",0.01938,0.05917,0.05280]]}}}").toByteArray()
}
94 changes: 94 additions & 0 deletions src/test/DataFactory.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import com.powsybl.ieeecdf.converter.IeeeCdfNetworkFactory
import io.ktor.client.request.forms.*
import io.ktor.http.*
import io.ktor.http.content.*
import java.io.File
import java.io.FileOutputStream
import java.nio.file.Paths
import java.util.*
import java.util.zip.ZipEntry
import java.util.zip.ZipOutputStream

fun ieeeCdfNetwork14CgmesFile(): File {
val file = File.createTempFile("network_cgmes", ".zip")
file.deleteOnExit()

IeeeCdfNetworkFactory.create14().write("CGMES", null, Paths.get(file.path.replace(".zip", "")))

// Read the produced files in EQ/SSH/SV/TP and create one zip archive
val withOutExtension = file.path.toString().replace(".zip", "")
val cimXmlFiles = listOf("EQ", "TP", "SV", "SSH").map { profile -> "${withOutExtension}_${profile}.xml"}

val fileOutputStream = FileOutputStream(file)
val zipOutputStream = ZipOutputStream(fileOutputStream)
for (cimXmlFile in cimXmlFiles) {
val zipEntry = ZipEntry(cimXmlFile) // TODO: Fix zip entry should not contain full path
zipOutputStream.putNextEntry(zipEntry)
val cimFile = File(cimXmlFile)
val bytes = cimFile.readBytes()
zipOutputStream.write(bytes, 0, bytes.size)
zipOutputStream.closeEntry()
cimFile.delete()
}
zipOutputStream.close()
return file
}

fun formDataMinimalNetworkRawx(): List<PartData> {
return formData {
append(
"network",
minimalRawx(),
Headers.build {
append(HttpHeaders.ContentDisposition, "filename=network.rawx")
}
)
}
}

fun ieeeCdfNetwork14File(): File {
// Initialize temporary file
val file = File.createTempFile("network", ".xiidm")
file.deleteOnExit()

IeeeCdfNetworkFactory.create14().write("XIIDM", Properties(), Paths.get(file.path))
return file
}

fun minimalRawx(): ByteArray {
return ("{\"network\":{\"caseid\":{" +
"\"fields\":[\"ic\",\"sbase\",\"rev\",\"xfrrat\",\"nxfrat\",\"basfrq\",\"title1\"]," +
"\"data\":[0,100.00,35,0,0,60.00,\"PSS(R)EMinimumRAWXCase\"]}," +
"\"bus\":{\"fields\":[\"ibus\",\"name\",\"baskv\",\"ide\"]," +
"\"data\":[[1,\"Slack-Bus\",138.0,3],[2,\"Load-Bus\",138.01]]}," +
"\"load\":{\"fields\":[\"ibus\",\"loadid\",\"stat\",\"pl\",\"ql\"]," +
"\"data\":[[2,\"1\",1,40.0,15.0]]}," +
"\"generator\":{\"fields\":[\"ibus\",\"machid\",\"pg\",\"qg\"]," +
"\"data\":[[1,\"1\",\"40.35\",\"10.87\"]]}," +
"\"acline\":{\"fields\":[\"ibus\",\"jbus\",\"ckt\",\"rpu\",\"xpu\",\"bpu\"]," +
"\"data\":[[1,2,\"1\",0.01938,0.05917,0.05280]]}}}").toByteArray()
}

fun formDataWithEmptyNetwork(): List<PartData> {
return formData {
append(
"network",
byteArrayOf(),
Headers.build {
append(HttpHeaders.ContentDisposition, "filename=emptyFile.xiidm")
}
)
}
}

fun formDataFromFile(file: File): List<PartData> {
return formData {
append(
"network",
file.readBytes(),
Headers.build {
append(HttpHeaders.ContentDisposition, "filename=${file.name}")
},
)
}
}
5 changes: 4 additions & 1 deletion src/test/SolverTest.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import com.github.statnett.loadflowservice.defaultLoadFlowParameters
import com.powsybl.ieeecdf.converter.IeeeCdfNetworkFactory
import com.powsybl.loadflow.LoadFlowParameters
import com.powsybl.loadflow.json.JsonLoadFlowParameters
import org.junit.Test
import java.io.ByteArrayInputStream
import java.io.File
import java.nio.file.Paths
import java.util.*
import kotlin.test.assertEquals

class SolverTest {
Expand All @@ -13,5 +17,4 @@ class SolverTest {
val fromJson = JsonLoadFlowParameters.read(ByteArrayInputStream(defaultJson.toByteArray()))
assertEquals(parameters.toString(), fromJson.toString())
}

}

0 comments on commit 19b66cc

Please sign in to comment.