package com.sonymusic

import com.lesfurets.jenkins.unit.BasePipelineTest
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test

import static org.junit.jupiter.api.Assertions.assertEquals
import static org.junit.jupiter.api.Assertions.assertThrows

class LoadTestsTest extends BasePipelineTest {
    def loadTests

    @BeforeEach
    void setUp() {
        super.setUp()
        loadTests = loadScript("vars/loadTests.groovy")

        helper.registerAllowedMethod("build", [Map])
    }

    @Test
    void testCallNoArgs() {
        // Assert error is thrown since required args are not supplied
        assertThrows(AssertionError.class, {
            loadTests.call(dummy: 'dummy')
        })
    }


    @Test
    void testCallWithHost() {
        loadTests(host: 'example.com')

        // Verify that the 'build' step was called
        assertEquals(1, helper.callStack.findAll { it.methodName == 'build' }.size())
    }

    @Test
    void testCallWithWaitCompletionFalse() {
        loadTests(host: 'example.com',waitCompletion: false, propagateFailure: false)

        // Verify that the 'build' step was called
        def buildCalls = helper.callStack.findAll { it.methodName == 'build' }
        assertEquals(1, buildCalls.size())
        assertEquals(false, buildCalls[0].args[0].wait)
        assertEquals(false, buildCalls[0].args[0].propagate)
    }

    @Test
    void testCallWithWaitCompletionTrue() {
        loadTests(host: 'example.com',waitCompletion: true, propagateFailure: true)

        // Verify that the 'build' step was called
        def buildCalls = helper.callStack.findAll { it.methodName == 'build' }
        assertEquals(1, buildCalls.size())
        assertEquals(true, buildCalls[0].args[0].wait)
        assertEquals(true, buildCalls[0].args[0].propagate)
    }

    @Test
    void testCallWithWaitCompletionUnset() {
        loadTests(host: 'example.com')

        // Verify that the 'build' step was called
        def buildCalls = helper.callStack.findAll { it.methodName == 'build' }
        assertEquals(1, buildCalls.size())
        assertEquals(false, buildCalls[0].args[0].wait)
        assertEquals(false, buildCalls[0].args[0].propagate)
    }
}
