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

com.deviniti.testflo.jira.JiraRestClient.kt Maven / Gradle / Ivy

There is a newer version: 1.0.11
Show newest version
package com.deviniti.testflo.jira

import com.deviniti.testflo.testsender.Configuration
import com.deviniti.testflo.testsender.ConfigurationField.*
import com.github.kittinunf.fuel.Fuel
import com.github.kittinunf.fuel.core.FileDataPart
import com.github.kittinunf.fuel.core.FuelError
import com.github.kittinunf.fuel.core.extensions.authentication
import com.github.kittinunf.fuel.gson.responseObject
import com.google.gson.JsonObject
import org.apache.log4j.Logger
import java.io.File
import java.net.MalformedURLException
import java.util.concurrent.TimeUnit

interface JiraRestClient {
    fun validateJiraCredentials(jiraUrl: String, username: String, password: String): JiraCredentialsValidationResult

    fun sendTestResultFileAsync(configuration: Configuration, testResultZipFile: File): FuelError?

    fun isImportTestResultsActive(configuration: Configuration): ImportTestResultsActive
}

class JiraRestClientImpl : JiraRestClient {

    private val log: Logger = Logger.getLogger(JiraRestClientImpl::class.java)

    private val uploadTimout = TimeUnit.HOURS.toMillis(1)

    override fun validateJiraCredentials(jiraUrl: String, username: String, password: String): JiraCredentialsValidationResult {
        try {
            return Fuel.get("$jiraUrl/rest/api/2/myself")
                    .authentication().basic(username, password)
                    .responseObject()
                    .third.fold(
                            { jsonObject ->
                                return if (jsonObject.getAsJsonPrimitive("name").asString != username) {
                                    InvalidJiraCredentials
                                } else {
                                    ValidJiraCredentials
                                }
                            },
                            { fuelError ->
                                return if (fuelError.response.statusCode == 401) {
                                    InvalidJiraCredentials
                                } else {
                                    InvalidJiraUrl
                                }
                            }
                    )
        } catch (e: MalformedURLException) {
            return InvalidJiraUrl
        } catch (e: Exception) {
            log.error(e.message, e)
            return UnexpectedError(e.message)
        }
    }

    override fun sendTestResultFileAsync(configuration: Configuration, testResultZipFile: File): FuelError? {
        val parameters = listOf(
                TEST_PLAN_KEY.fieldName to configuration.testPlanKey,
                TEST_CASE_CREATION_STRATEGY.fieldName to configuration.testCaseCreationStrategy.toString(),
                TARGET_ITERATION.fieldName to configuration.targetIteration.toString(),
                BUILD_URL.fieldName to configuration.buildUrl,
                TEST_RESULTS_TYPE.fieldName to configuration.testResultsType,
                TEST_FLO_IMPORT_RESULTS_PARAMETERS.fieldName to configuration.importResultsParameters
        )
        val url = "${configuration.jiraUrl}/rest/tms/1.0/automation/import-test-results?"
        return Fuel.upload(path = url, parameters = parameters)
                .add(FileDataPart(file = testResultZipFile, name = "testResults", filename = "test_results.zip"))
                .timeoutRead(uploadTimout.toInt())
                .authentication().basic(configuration.jiraUsername, configuration.jiraPassword)
                .response()
                .third
                .fold({ null }, { it })
    }

    override fun isImportTestResultsActive(configuration: Configuration): ImportTestResultsActive {
        return Fuel.get("${configuration.jiraUrl}/rest/tms/1.0/testplan/is-import-test-results-active/${configuration.testPlanKey}")
                .authentication().basic(configuration.jiraUsername, configuration.jiraPassword)
                .responseObject()
                .third
                .fold(
                        {
                            ImportTestResultsActive(it.getAsJsonPrimitive("isActive").asBoolean, null)
                        },
                        {
                            ImportTestResultsActive(null, it)
                        }
                )
    }
}

sealed class JiraCredentialsValidationResult
class UnexpectedError(val message: String?) : JiraCredentialsValidationResult()
object InvalidJiraUrl : JiraCredentialsValidationResult()
object InvalidJiraCredentials : JiraCredentialsValidationResult()
object ValidJiraCredentials : JiraCredentialsValidationResult()

data class ImportTestResultsActive(val isActive: Boolean?, val error: FuelError?)




© 2015 - 2024 Weber Informatics LLC | Privacy Policy