fun testSecuritySchemeSerialization()

in a2a/a2a-core/src/commonTest/kotlin/ai/koog/a2a/model/AgentCardSerializationTest.kt [300:399]


    fun testSecuritySchemeSerialization() {
        // API Key Security Scheme
        val apiKeyScheme = APIKeySecurityScheme(
            `in` = In.Header,
            name = "Authorization",
            description = "Bearer token"
        )
        //language=JSON
        val apiKeyJson = """
            {
                "in": "header",
                "name": "Authorization",
                "description": "Bearer token",
                "type": "apiKey"
            }
        """.trimIndent()
        assertEquals(apiKeyJson, TestJson.encodeToString<SecurityScheme>(apiKeyScheme))

        // HTTP Auth Security Scheme
        val httpScheme = HTTPAuthSecurityScheme(
            scheme = "Bearer",
            bearerFormat = "JWT",
            description = "JWT Bearer token"
        )
        //language=JSON
        val httpJson = """
            {
                "scheme": "Bearer",
                "bearerFormat": "JWT",
                "description": "JWT Bearer token",
                "type": "http"
            }
        """.trimIndent()
        assertEquals(httpJson, TestJson.encodeToString<SecurityScheme>(httpScheme))

        // OAuth2 Security Scheme
        val oauth2Scheme = OAuth2SecurityScheme(
            flows = OAuthFlows(
                authorizationCode = AuthorizationCodeOAuthFlow(
                    authorizationUrl = "https://auth.example.com/oauth/authorize",
                    tokenUrl = "https://auth.example.com/oauth/token",
                    scopes = mapOf("read" to "Read access", "write" to "Write access")
                )
            ),
            description = "OAuth2 with authorization code flow"
        )
        //language=JSON
        val expectedOAuth2Json = """
            {
                "flows": {
                    "authorizationCode": {
                        "authorizationUrl": "https://auth.example.com/oauth/authorize",
                        "tokenUrl": "https://auth.example.com/oauth/token",
                        "scopes": {
                            "read": "Read access",
                            "write": "Write access"
                        }
                    }
                },
                "description": "OAuth2 with authorization code flow",
                "type": "oauth2"
            }
        """.trimIndent()

        val oauth2Json = TestJson.encodeToString<SecurityScheme>(oauth2Scheme)
        assertEquals(expectedOAuth2Json, oauth2Json)

        // OpenID Connect Security Scheme
        val oidcScheme = OpenIdConnectSecurityScheme(
            openIdConnectUrl = "https://auth.example.com/.well-known/openid_configuration"
        )
        //language=JSON
        val oidcJson = """
            {
                "openIdConnectUrl": "https://auth.example.com/.well-known/openid_configuration",
                "type": "openIdConnect"
            }
        """.trimIndent()
        assertEquals(oidcJson, TestJson.encodeToString<SecurityScheme>(oidcScheme))

        // Mutual TLS Security Scheme
        val mtlsScheme = MutualTLSSecurityScheme(
            description = "Client certificate authentication"
        )
        //language=JSON
        val mtlsJson = """
            {
                "description": "Client certificate authentication",
                "type": "mutualTLS"
            }
        """.trimIndent()
        assertEquals(mtlsJson, TestJson.encodeToString<SecurityScheme>(mtlsScheme))

        // Test deserialization
        assertEquals(apiKeyScheme, TestJson.decodeFromString<SecurityScheme>(apiKeyJson))
        assertEquals(httpScheme, TestJson.decodeFromString<SecurityScheme>(httpJson))
        assertEquals(oauth2Scheme, TestJson.decodeFromString<SecurityScheme>(expectedOAuth2Json))
        assertEquals(oidcScheme, TestJson.decodeFromString<SecurityScheme>(oidcJson))
        assertEquals(mtlsScheme, TestJson.decodeFromString<SecurityScheme>(mtlsJson))
    }