libeufin

Integration and sandbox testing for FinTech APIs and data formats
Log | Files | Refs | Submodules | README | LICENSE

OpenApiTest.kt (2128B)


      1 /*
      2  * This file is part of LibEuFin.
      3  * Copyright (C) 2026 Taler Systems S.A.
      4 
      5  * LibEuFin is free software; you can redistribute it and/or modify
      6  * it under the terms of the GNU Affero General Public License as
      7  * published by the Free Software Foundation; either version 3, or
      8  * (at your option) any later version.
      9 
     10  * LibEuFin is distributed in the hope that it will be useful, but
     11  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
     12  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General
     13  * Public License for more details.
     14 
     15  * You should have received a copy of the GNU Affero General Public
     16  * License along with LibEuFin; see the file COPYING.  If not, see
     17  * <http://www.gnu.org/licenses/>
     18  */
     19 
     20 import io.ktor.client.request.*
     21 import io.ktor.client.statement.*
     22 import io.ktor.http.*
     23 import io.ktor.server.testing.*
     24 import org.junit.Test
     25 import sun.misc.Unsafe
     26 import tech.libeufin.common.*
     27 import tech.libeufin.nexus.db.Database
     28 import tech.libeufin.nexus.nexusApi
     29 import tech.libeufin.nexus.nexusConfig
     30 import java.io.File
     31 import kotlin.io.path.Path
     32 import kotlin.test.*
     33 
     34 class OpenApiTest {
     35     private fun fakeDatabase(): Database {
     36         val field = Unsafe::class.java.getDeclaredField("theUnsafe")
     37         field.isAccessible = true
     38         val unsafe = field.get(null) as Unsafe
     39         return unsafe.allocateInstance(Database::class.java) as Database
     40     }
     41 
     42     @Test
     43     fun generateSpec() {
     44         val cfg = nexusConfig(Path("conf/test.conf"))
     45         testApplication {
     46             application {
     47                 nexusApi(fakeDatabase(), cfg, serveSpec = true)
     48             }
     49             val resp = client.get("/openapi.yaml")
     50             resp.assertOk()
     51             val spec = resp.bodyAsText()
     52             assertTrue(spec.contains("openapi: 3.1.0"), "Response should be a valid OpenAPI spec")
     53             assertTrue(spec.contains("LibEuFin Nexus API"), "Spec should contain the API title")
     54             val outFile = File("build/openapi.yaml")
     55             outFile.writeText(spec)
     56             println("OpenAPI spec written to ${outFile.absolutePath}")
     57         }
     58     }
     59 }