<commit_before>package net.nemerosa.ontrack.graphql.support

import graphql.schema.*
import net.nemerosa.ontrack.graphql.schema.GQLTypeCache
import net.nemerosa.ontrack.graphql.schema.UpdateProjectInput
import net.nemerosa.ontrack.model.structure.NameDescriptionState
import net.nemerosa.ontrack.test.assertIs
import org.junit.Before
import org.junit.Test
import kotlin.test.assertEquals
import kotlin.test.assertNotNull
import kotlin.test.fail

class GraphQLBeanConverterTest {

    private lateinit var cache: GQLTypeCache

    @Before
    fun before() {
        cache = GQLTypeCache()
    }

    @Test
    fun `Scalar types`() {
        assertNotNull(GraphQLBeanConverter.getScalarType(String::class.java)) {
            assertEquals("String", it.name)
        }
        assertNotNull(GraphQLBeanConverter.getScalarType(Int::class.java)) {
            assertEquals("Int", it.name)
        }
        assertNotNull(GraphQLBeanConverter.getScalarType(Integer::class.java)) {
            assertEquals("Int", it.name)
        }
    }

    @Test
    fun `Simple type`() {
        val type = GraphQLBeanConverter.asObjectType(Person::class, cache)
        assertEquals("Person", type.name)
        val fields = fieldToTypeNames(type.fieldDefinitions)
        assertEquals(
            mapOf(
                "address" to "String!",
                "age" to "Int!",
                "name" to "String!",
                "developer" to "Boolean!",
                "experience" to "Int"
            ),
            fields
        )
    }

    @Test
    fun `Simple input type`() {
        val fields = GraphQLBeanConverter.asInputFields(Person::class)
        val indexedFields = fields.associate {
            it.name to typeName(it.type)
        }
        assertEquals(
            mapOf(
                "address" to "String!",
                "age" to "Int!",
                "name" to "String!",
                "developer" to "Boolean!",
                "experience" to "Int"
            ),
            indexedFields
        )
    }

    @Test
    fun `Description from annotation`() {
        val type = GraphQLBeanConverter.asObjectType(Person::class, cache)
        assertEquals("Person", type.name)
        assertNotNull(type.fieldDefinitions.find { it.name == "name" }) {
            assertEquals("Full name", it.description)
        }
        assertNotNull(type.fieldDefinitions.find { it.name == "address" }) {
            assertEquals("Full postal address", it.description)
        }
    }

    @Test
    fun `Composite type`() {
        val type = GraphQLBeanConverter.asObjectType(Account::class, cache)
        assertEquals("Account", type.name)
        val fields = fieldToTypeNames(type.fieldDefinitions)
        assertEquals(
            mapOf(
                "username" to "String!",
                "password" to "<PASSWORD>!",
                "identity" to "Person!",
                "roles" to "[String!]!",
            ),
            fields
        )
    }

    @Test
    fun `List of strings`() {
        val type = GraphQLBeanConverter.asObjectType(StringListWrapper::class, cache)
        assertNotNull(type.fieldDefinitions.find { it.name == "list" }) { listField ->
            assertIs<GraphQLNonNull>(listField.type) { nonNullList ->
                assertIs<GraphQLList>(nonNullList.wrappedType) { list ->
                    assertIs<GraphQLNonNull>(list.wrappedType) { nonNullItem ->
                        assertIs<GraphQLScalarType>(nonNullItem.wrappedType) { scalar ->
                            assertEquals("String", scalar.name)<commit_msg>#930 Fixing test<commit_after>package net.nemerosa.ontrack.graphql.support

import graphql.schema.*
import net.nemerosa.ontrack.graphql.schema.GQLTypeCache
import net.nemerosa.ontrack.graphql.schema.UpdateProjectInput
import net.nemerosa.ontrack.model.structure.NameDescriptionState
import net.nemerosa.ontrack.test.assertIs
import org.junit.Before
import org.junit.Test
import kotlin.test.assertEquals
import kotlin.test.assertNotNull
import kotlin.test.fail

class GraphQLBeanConverterTest {

    private lateinit var cache: GQLTypeCache

    @Before
    fun before() {
        cache = GQLTypeCache()
    }

    @Test
    fun `Scalar types`() {
        assertNotNull(GraphQLBeanConverter.getScalarType(String::class.java)) {
            assertEquals("String", it.name)
        }
        assertNotNull(GraphQLBeanConverter.getScalarType(Int::class.java)) {
            assertEquals("Int", it.name)
        }
        assertNotNull(GraphQLBeanConverter.getScalarType(Integer::class.java)) {
            assertEquals("Int", it.name)
        }
    }

    @Test
    fun `Simple type`() {
        val type = GraphQLBeanConverter.asObjectType(Person::class, cache)
        assertEquals("Person", type.name)
        val fields = fieldToTypeNames(type.fieldDefinitions)
        assertEquals(
            mapOf(
                "address" to "String!",
                "age" to "Int!",
                "name" to "String!",
                "developer" to "Boolean!",
                "experience" to "Int"
            ),
            fields
        )
    }

    @Test
    fun `Simple input type`() {
        val fields = GraphQLBeanConverter.asInputFields(Person::class)
        val indexedFields = fields.associate {
            it.name to typeName(it.type)
        }
        assertEquals(
            mapOf(
                "address" to "String!",
                "age" to "Int!",
                "name" to "String!",
                "developer" to "Boolean!",
                "experience" to "Int"
            ),
            indexedFields
        )
    }

    @Test
    fun `Description from annotation`() {
        val type = GraphQLBeanConverter.asObjectType(Person::class, cache)
        assertEquals("Person", type.name)
        assertNotNull(type.fieldDefinitions.find { it.name == "name" }) {
            assertEquals("Full name", it.description)
        }
        assertNotNull(type.fieldDefinitions.find { it.name == "address" }) {
            assertEquals("Full postal address", it.description)
        }
    }

    @Test
    fun `Composite type`() {
        val type = GraphQLBeanConverter.asObjectType(Account::class, cache)
        assertEquals("Account", type.name)
        val fields = fieldToTypeNames(type.fieldDefinitions)
        assertEquals(
            mapOf(
                "username" to "String!",
                "password" to "String!",
                "identity" to "Person!",
            ),
            fields
        )
    }

    @Test
    fun `List of strings`() {
        val type = GraphQLBeanConverter.asObjectType(StringListWrapper::class, cache)
        assertNotNull(type.fieldDefinitions.find { it.name == "list" }) { listField ->
            assertIs<GraphQLNonNull>(listField.type) { nonNullList ->
                assertIs<GraphQLList>(nonNullList.wrappedType) { list ->
                    assertIs<GraphQLNonNull>(list.wrappedType) { nonNullItem ->
                        assertIs<GraphQLScalarType>(nonNullItem.wrappedType) { scalar ->
                            assertEquals("String", scalar.name)
{'fraction_non_alphanumeric': 0.11001688238604389, 'fraction_numerical': 0.0004220596510973551, 'mean_word_length': 2.0855034722222223, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2149380', 'n_tokens_mistral': 1981, 'n_tokens_neox': 1910, 'n_words': 368}