package com.org

import org.junit.*
import static org.junit.Assert.*
import com.lesfurets.jenkins.unit.BasePipelineTest
import com.lesfurets.jenkins.unit.global.lib.LibraryConfiguration
import com.lesfurets.jenkins.unit.global.lib.ProjectSource
import org.yaml.snakeyaml.Yaml

class SayHelloTest extends BasePipelineTest {

    Object script

    @Override
    @Before
    @SuppressWarnings('ThrowException')
    void setUp() {
        super.setUp()
        script = loadScript("src/main/groovy/com/acquia/SayHello.groovy")
        helper.registerAllowedMethod("readYaml", [Map.class], { args ->
            return new Yaml().load((args.file as File).text)
        })
        helper.registerAllowedMethod("withCredentials", [List, Closure], { creds, closure ->
            closure.call()
        })
        helper.registerAllowedMethod("sh", [Map], { args ->
            return "mocked sh output"
        })
        helper.registerAllowedMethod("readFile", [String], { file ->
            return "mocked file content"
        })
        helper.registerAllowedMethod("httpRequest", [Map], { args ->
            return [getContent: { -> '{"items": [{"name": "groovy"}]}' }]
        })
        helper.registerAllowedMethod("readJSON", [Map], { args ->
            return new groovy.json.JsonSlurper().parseText(args.text)
        })
    }

    @Test
    void testHello() {
        def sayHello = new SayHello("World", script)
        assertEquals("Hello, World!", sayHello.hello())
    }

    @Test
    void testTryReadYaml() {
        def sayHello = new SayHello("World", script)
        def yamlMap = sayHello.tryReadYaml()
        assertNotNull(yamlMap)
    }

    @Test
    void testTryWithCredentials() {
        def sayHello = new SayHello("World", script)
        def result = sayHello.tryWithCredentials()
        assertEquals("mocked sh output", result)
    }

    @Test
    void testTryShCommands() {
        def sayHello = new SayHello("World", script)
        sayHello.tryShCommands()
    }

    @Test
    void testTryReadFile() {
        def sayHello = new SayHello("World", script)
        def content = sayHello.tryReadFile()
        assertEquals("mocked file content", content)
    }

    @Test
    void testTryReadingResponses() {
        def sayHello = new SayHello("World", script)
        def jsonResponse = sayHello.tryReadingResponses()
        assertNotNull(jsonResponse)
        assertEquals("groovy", jsonResponse.items[0].name)
    }

    @Test
    void testTryClosure() {
        def sayHello = new SayHello("World", script)
        def result = sayHello.tryClosure({ -> return "Closure executed" })
        assertEquals("Closure executed", result)
    }

    @Test
    void testTryWithEnv() {
        def sayHello = new SayHello("World", script)
        script.env.HELLO = "HelloEnv"
        sayHello.tryWithEnv()
    }

    @Test
    void testTryGettingStage() {
        def sayHello = new SayHello("World", script)
        def stageClosure = sayHello.tryGettingStage()
        stageClosure.call()
    }

    @Test
    void testTryExceptionCatch() {
        def sayHello = new SayHello("World", script)
        sayHello.tryExceptionCatch()
    }
}
