All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.serenitybdd.reports.model.CustomReportFields.kt Maven / Gradle / Ivy

package net.serenitybdd.reports.model

import net.serenitybdd.model.environment.EnvironmentSpecificConfiguration
import net.thucydides.model.util.NameConverter.humanize
import net.thucydides.model.util.EnvironmentVariables

/**
 * Custom report fields are stored in the serenity.properties file as report.customfields.* properties, or passed in on the command line.
 * They can be ordered by providing the list of fields in the report.customfields.order field.
 *
 *
 */
class CustomReportFields(val environmentVariables: EnvironmentVariables) {

    companion object {
        const val CUSTOM_FIELDS_PREFIX = "report.customfields"
        const val CUSTOM_FIELD_ORDER = "report.customfields.order"
    }

    private val orderedFields: List =
        orderedFields(environmentVariables).map { field -> "report.customfields." + field }
    private val fields: List = customFieldsDefinedIn(environmentVariables)
    val fieldNames: List = customFieldOrderDefinedIn(fields, environmentVariables)
    val values: List = customFieldValueDefinedIn(fields, environmentVariables)
    private val env
        get() = environmentVariables.getProperty("environment", "default")
    private val fieldsRegex
        get() = "($CUSTOM_FIELDS_PREFIX|environments\\.(all|$env)\\.$CUSTOM_FIELDS_PREFIX).*".toRegex()
    private val orderedFieldsRegex
        get() = "($CUSTOM_FIELD_ORDER|environments\\.(all|$env)\\.$CUSTOM_FIELD_ORDER).*".toRegex()

    private fun customFieldsDefinedIn(environmentVariables: EnvironmentVariables): List {

        return if (orderedFields.isNotEmpty()) {
            orderedFields
        } else {
            environmentVariables
                .keys
                .filter { key -> key.matches(fieldsRegex) }
        }
    }

    private fun customFieldValueDefinedIn(
        fields: List,
        environmentVariables: EnvironmentVariables
    ): List =
        fields.map { key ->
            environmentVariables.injectSystemPropertiesInto(
                EnvironmentSpecificConfiguration.from(environmentVariables).getOptionalProperty(key).orElse("")
            ) ?: ""
        }


    private fun customFieldOrderDefinedIn(
        fields: List,
        environmentVariables: EnvironmentVariables
    ): List {
        val predefinedFields = orderedFields(environmentVariables)
        return if (predefinedFields.isNotEmpty()) {
            predefinedFields.map { field -> humanize(field) }
        } else {
            fields.map { fieldName -> humanReadableLabelOf(fieldName) }
        }
    }

    private fun humanReadableLabelOf(field: String): String = humanize(field.substringAfter("$CUSTOM_FIELDS_PREFIX."))

    private fun orderedFields(environmentVariables: EnvironmentVariables): List = environmentVariables
        .keys
        .firstOrNull { key ->
            key.matches(orderedFieldsRegex)
        }?.let { key ->
            EnvironmentSpecificConfiguration.from(environmentVariables).getOptionalProperty(key).orElse("")
                .split(",").map { field -> field.trim() }
        } ?: emptyList()

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy