tests/PSTS.git.Tests.ps1

function mockGetRepository() {
        Mock -Verifiable -ModuleName PSTS Get-PSTSGitRepository  -MockWith {
    param($project)
        $obj = New-Object -TypeName "PSObject"
        $ret = [GitRepository]::new()
        $ret.id="123"
        $ret.name="fakeRepoName"
        $data=@($ret)
        $obj | Add-Member -MemberType NoteProperty "Count" -Value $data.Count
        $obj | Add-Member -MemberType NoteProperty "value" -Value $data

        return $ret
    }
}

function mockGetRepositoryRef() {
        Mock -Verifiable -ModuleName PSTS Get-PSTSGitRepositoryRef   -MockWith {
        param($project , $id )
            $obj1 = New-Object -TypeName "PSObject"
            $ret1 = [GitRepositoryRef]::new()
            $ret1.objectId="123"
            $ret1.name="refs/heads/master"
            $data1=@($ret1)
            $obj1 | Add-Member -MemberType NoteProperty "Count" -Value $data1.Count
            $obj1 | Add-Member -MemberType NoteProperty "value" -Value $data1

            return $ret1
        }

}





if ($PSScriptRoot -eq $null) {
    $PSScriptRoot = Split-Path -Parent -Path $MyInvocation.MyCommand.Definition
}

. "$PSScriptRoot\helper.ps1"


#describe 'Test-Git-cmdlets-failures-128' {

# Mock -Verifiable -ModuleName PSTS Get-PSTSGitRepository -MockWith {
# param($project)
# $obj = New-Object -TypeName "PSObject"
# $ret = [GitRepository]::new()
# $ret.name="fakeRepoName"
# $data=@($ret)
# $obj | Add-Member -MemberType NoteProperty "Count" -Value $data.Count
# $obj | Add-Member -MemberType NoteProperty "value" -Value $data

# return $ret

# }

# $result = Add-PSTSGitRepositoryRef -project "fakeProject" -repoName "fakeRepoName" -refName "fakeRefName"

# it "should return 128" {
# $result | should -Be 128
# }

# $result = Add-PSTSGitRepositoryImportRequest -project fakeProject -repoName fakeRepoName -url fakeUrl -serviceEndPointId fakeserviceEndPointId

# it "should return 128" {
# $result | should -Be 128
# }

# $result = Add-PSTSGitRepositoryInit -project fakeProject -repoName fakeRepoName

# it "should return 128" {
# $result | should -Be 128
# }

# $result = Add-PSTSGitRepositoryInit -project fakeProject -repoName fakeRepoName

# it "should return 128" {
# $result | should -Be 128
# }

# $result = Add-PSTSGitRepositoryPush -project fakeProject -repoName fakeRepoName -branch fakeBranch -comments fakeComment -fileName test `
# -localPath "./assets/Values.yaml"

# it "should return 128" {
# $result | should -Be 128
# }

# $result = Remove-PSTSGitRepositoryRef -project fakeProject -repoName fakeRepoName -branch fakeBranch

# it "should return 128" {
# $result | should -Be 128
# }

#}


describe 'Test-Git-cmdlets-failures-null' {

    Mock -Verifiable -ModuleName PSTS Get-PSTSGitRepository  -MockWith {
    param($project)
        return $null
    }



    { Get-PSTSGitRepositoryRefItemFolder -project "fakeProject" -repository "fakeRepoName" -branch "dev" -folderPath "/fakePath"  } | should Throw "repository Id for fakeRepoName could not be found"

    { Get-PSTSGitRepositoryRefItemFile -project "fakeProject" -repository "fakeRepoName" -branch "dev" -filePath "/fakePath" } | should Throw "repository Id for fakeRepoName could not be found"


    { Get-PSTSGitRepositoryRefItemBatch -project "fakeProject" -repository "fakeRepoName" -branch "dev" -folderPath "/fakePath" } | should Throw "repository Id for fakeRepoName could not be found"



}


describe 'Get-PSTSGitRepository' {


    # Arrange
    $data = @([GitRepository]::new(); [GitRepository]::new())

    $expectedResult = New-Object -TypeName "PSObject"


    $expectedResult | Add-Member -MemberType NoteProperty "Count" -Value $data.Count
    $expectedResult | Add-Member -MemberType NoteProperty "value" -Value $data

    arrange -result $expectedResult

    # Act
    $result = Get-PSTSGitRepository -project fakeProject

    #Assert
    It "Uses the mock" {
        Assert-VerifiableMock
    }

    it "should return $($expectedResult.value)" {
        $result.Count | should -Be $expectedResult.value.Count
    }
}

describe 'Get-PSTSGitRepositoryRef' {


    # Arrange
    $data = @([GitRepositoryRef]::new(); [GitRepositoryRef]::new())

    $expectedResult = New-Object -TypeName "PSObject"


    $expectedResult | Add-Member -MemberType NoteProperty "Count" -Value $data.Count
    $expectedResult | Add-Member -MemberType NoteProperty "value" -Value $data

    arrange -result $expectedResult

    # Act
    $result = Get-PSTSGitRepositoryRef -project fakeProject -id fakeGitRepositoryId

    #Assert
    It "Uses the mock" {
        Assert-VerifiableMock
    }

    it "should return $($expectedResult.value)" {
        $result.Count | should -Be $expectedResult.value.Count
    }
}

describe 'Add-PSTSGitRepository' {


    # Arrange
    $expectedResult = [GitRepository]::new()
    $expectedResult.name = "fakeRepoName"

    arrange -result $expectedResult

    # Act
    $result = Add-PSTSGitRepository -project fakeProject -repoName fakeRepoName

    #Assert
    It "Uses the mock" {
        Assert-VerifiableMock
    }

    it "should return $([GitRepository]::new())" {
        $result.name | should -Be $expectedResult.name
    }
}

describe 'Add-PSTSGitRepositoryRef' {
    Mock -Verifiable -ModuleName PSTS Get-PSTSGitRepository  -MockWith {
    param($project)
        $obj = New-Object -TypeName "PSObject"
        $ret = [GitRepository]::new()
        $ret.id="123"
        $ret.name="fakeRepoName"
        $data=@($ret)
        $obj | Add-Member -MemberType NoteProperty "Count" -Value $data.Count
        $obj | Add-Member -MemberType NoteProperty "value" -Value $data

        return $ret
    }
    Mock -Verifiable -ModuleName PSTS Get-PSTSGitRepositoryRef   -MockWith {
        param($project , $id )
            $obj1 = New-Object -TypeName "PSObject"
            $ret1 = [GitRepositoryRef]::new()
            $ret1.objectId="123"
            $ret1.name="refs/heads/master"
            $data1=@($ret1)
            $obj1 | Add-Member -MemberType NoteProperty "Count" -Value $data1.Count
            $obj1 | Add-Member -MemberType NoteProperty "value" -Value $data1

            return $ret1
        }
    # Arrange


    $gitRefResult = [GitRefUpdateResult]::new()
    $gitRefResult.name = "fakeRefName"
    $data=@($gitRefResult)

    $expectedResult = New-Object -TypeName "PSObject"


    $expectedResult | Add-Member -MemberType NoteProperty "Count" -Value $data.Count
    $expectedResult | Add-Member -MemberType NoteProperty "value" -Value $data
    arrange -result $expectedResult

    # Act
    $result = Add-PSTSGitRepositoryRef -project fakeProject -repoName fakeRepoName -refName fakeRefName

    #Assert
    It "Uses the mock" {
        Assert-VerifiableMock
    }

    it "should return $($expectedResult.value)" {
        $result.name | should -Be $expectedResult.value.name
    }
}

describe 'Add-PSTSGitRepositoryImportRequest' {
    Mock -Verifiable -ModuleName PSTS Get-PSTSGitRepository  -MockWith {
    param($project)
        $obj = New-Object -TypeName "PSObject"
        $ret = [GitRepository]::new()
        $ret.id="123"
        $ret.name="fakeRepoName"
        $data=@($ret)
        $obj | Add-Member -MemberType NoteProperty "Count" -Value $data.Count
        $obj | Add-Member -MemberType NoteProperty "value" -Value $data

        return $ret
    }

    # Arrange


    $GitImportRequest  = [GitImportRequest]::new()


    $expectedResult = New-Object -TypeName "PSObject"


    $expectedResult | Add-Member -MemberType NoteProperty "Count" -Value $data.Count
    $expectedResult | Add-Member -MemberType NoteProperty "value" -Value $data
    arrange -result $expectedResult

    # Act
    $result = Add-PSTSGitRepositoryImportRequest -project fakeProject -repoName fakeRepoName -url fakeUrl -serviceEndPointId fakeserviceEndPointId

    #Assert
    It "Uses the mock" {
        Assert-VerifiableMock
    }

    it "should return $([GitImportRequest]::new())" {
        $result.name | should -Be $expectedResult.value.name
    }
}

describe 'Remove-PSTSGitRepositoryRef' {
    Mock -Verifiable -ModuleName PSTS Get-PSTSGitRepository  -MockWith {
    param($project)
        $obj = New-Object -TypeName "PSObject"
        $ret = [GitRepository]::new()
        $ret.id="123"
        $ret.name="fakeRepoName"
        $data=@($ret)
        $obj | Add-Member -MemberType NoteProperty "Count" -Value $data.Count
        $obj | Add-Member -MemberType NoteProperty "value" -Value $data

        return $ret
    }
    Mock -Verifiable -ModuleName PSTS Get-PSTSGitRepositoryRef   -MockWith {
        param($project , $id )
            $obj1 = New-Object -TypeName "PSObject"
            $ret1 = [GitRepositoryRef]::new()
            $ret1.objectId="123"
            $ret1.name="refs/heads/master"
            $data1=@($ret1)
            $obj1 | Add-Member -MemberType NoteProperty "Count" -Value $data1.Count
            $obj1 | Add-Member -MemberType NoteProperty "value" -Value $data1

            return $ret1
        }
    # Arrange


    $gitRefResult = [GitRefUpdateResult]::new()
    $gitRefResult.name = "fakeRefName"
    $data=@($gitRefResult)

    $expectedResult = New-Object -TypeName "PSObject"


    $expectedResult | Add-Member -MemberType NoteProperty "Count" -Value $data.Count
    $expectedResult | Add-Member -MemberType NoteProperty "value" -Value $data
    arrange -result $expectedResult

    # Act
    $result = Remove-PSTSGitRepositoryRef -project fakeProject -repoName fakeRepoName -branch fakeBranch

    #Assert
    It "Uses the mock" {
        Assert-VerifiableMock
    }

    it "should return $($expectedResult.value)" {
        $result.name | should -Be $expectedResult.value.name
    }
}


describe 'Get-PSTSGitRepositoryRefItemFolder' {


    # Arrange

  Mock -Verifiable -ModuleName PSTS Get-PSTSGitRepository  -MockWith {
    param($project)
        $obj = New-Object -TypeName "PSObject"
        $ret = [GitRepository]::new()
        $ret.id="123"
        $ret.name="fakeRepoName"
        $data=@($ret)
        $obj | Add-Member -MemberType NoteProperty "Count" -Value $data.Count
        $obj | Add-Member -MemberType NoteProperty "value" -Value $data

        return $ret
    }



    $data = [GitItem]::new()
    $data.path = "/fakePath"
    $expectedResult = New-Object -TypeName "PSObject"


    $expectedResult | Add-Member -MemberType NoteProperty "Count" -Value $data.Count
    $expectedResult | Add-Member -MemberType NoteProperty "value" -Value $data

    arrange -result $expectedResult

    # Act
    $result = Get-PSTSGitRepositoryRefItemFolder -project "fakeProject" -repository "fakeRepoName" -branch "dev" -folderPath "/fakePath"

    #Assert
    It "Uses the mock" {
        Assert-VerifiableMock
    }

    it "should return $($expectedResult.value)" {
        $result.Count | should -Be $expectedResult.value.Count
    }
}

describe 'Get-PSTSGitRepositoryRefItemFile' {



  Mock -Verifiable -ModuleName PSTS Get-PSTSGitRepository  -MockWith {
    param($project)
        $obj = New-Object -TypeName "PSObject"
        $ret = [GitRepository]::new()
        $ret.id="123"
        $ret.name="fakeRepoName"
        $data=@($ret)
        $obj | Add-Member -MemberType NoteProperty "Count" -Value $data.Count
        $obj | Add-Member -MemberType NoteProperty "value" -Value $data

        return $ret
    }



    $data = [GitItem]::new()
    $data.path = "/fakePath"

    arrange -result $data

    # Act
    $result = Get-PSTSGitRepositoryRefItemFile -project "fakeProject" -repository "fakeRepoName" -branch "dev" -filePath "/fakePath"

    #Assert
    It "Uses the mock" {
        Assert-VerifiableMock
    }

    it "should return /fakePath" {
        $result.path -eq $data.path
    }
}

describe 'Get-PSTSGitRepositoryRefItemBatch' {



    # Arrange

  Mock -Verifiable -ModuleName PSTS Get-PSTSGitRepository  -MockWith {
    param($project)
        $obj = New-Object -TypeName "PSObject"
        $ret = [GitRepository]::new()
        $ret.id="123"
        $ret.name="fakeRepoName"
        $data=@($ret)
        $obj | Add-Member -MemberType NoteProperty "Count" -Value $data.Count
        $obj | Add-Member -MemberType NoteProperty "value" -Value $data

        return $ret
    }



    $data = [GitItem]::new()
    $data.path = "/fakePath"
    $expectedResult = New-Object -TypeName "PSObject"


    $expectedResult | Add-Member -MemberType NoteProperty "Count" -Value $data.Count
    $expectedResult | Add-Member -MemberType NoteProperty "value" -Value $data

    arrange -result $expectedResult

    # Act
    $result = Get-PSTSGitRepositoryRefItemBatch -project "fakeProject" -repository "fakeRepoName" -branch "dev" -folderPath "/fakePath"

    #Assert
    It "Uses the mock" {
        Assert-VerifiableMock
    }

    it "should return $($expectedResult.value)" {
        $result.Count | should -Be $expectedResult.value.Count
    }
}


describe 'Add-PSTSGitRepositoryInit' {
    Mock -Verifiable -ModuleName PSTS Get-PSTSGitRepository  -MockWith {
    param($project)
        $obj = New-Object -TypeName "PSObject"
        $ret = [GitRepository]::new()
        $ret.id="123"
        $ret.name="fakeRepoName"
        $data=@($ret)
        $obj | Add-Member -MemberType NoteProperty "Count" -Value $data.Count
        $obj | Add-Member -MemberType NoteProperty "value" -Value $data

        return $ret
    }
    Mock -Verifiable -ModuleName PSTS Get-PSTSGitRepositoryRef   -MockWith {
        param($project , $id )
            $obj1 = New-Object -TypeName "PSObject"
            $ret1 = [GitRepositoryRef]::new()
            $ret1.objectId="123"
            $ret1.name="refs/heads/master"
            $data1=@($ret1)
            $obj1 | Add-Member -MemberType NoteProperty "Count" -Value $data1.Count
            $obj1 | Add-Member -MemberType NoteProperty "value" -Value $data1

            return $ret1
        }
    # Arrange


    $gitPushItem = [GitPushItem]::new()
    $gitCommit = [GitCommitRef]::new()

    $gitCommit.comment = "my git commit"
    $gitPushItem.commits+= $gitCommit


    arrange -result $gitPushItem

    # Act
    $result = Add-PSTSGitRepositoryInit -project fakeProject -repoName fakeRepoName

    #Assert
    It "Uses the mock" {
        Assert-VerifiableMock
    }

    it "should return $($gitCommit.comment)" {
        $result.commits.Item(0).comment | should -Be $gitCommit.comment
    }
}


describe 'Add-PSTSGitRepositoryPush-LocalPath' {

    mockGetRepository
    mockGetRepositoryRef



    # Arrange
    $expectedResult = [GitRepository]::new()
    $expectedResult.name = "fakeRepoName"

    arrange -result $expectedResult

    # Act
    $result = Add-PSTSGitRepositoryPush -project fakeProject -repoName fakeRepoName -branch fakeBranch -comments fakeComment -fileName test `
    -localPath "./assets/Values.yaml"

    #Assert
    It "Uses the mock" {
        Assert-VerifiableMock
    }

}


describe 'Add-PSTSGitRepositoryPush-content' {

    mockGetRepository
    mockGetRepositoryRef



    # Arrange
    $expectedResult = [GitRepository]::new()
    $expectedResult.name = "fakeRepoName"

    arrange -result $expectedResult

    # Act
    $result = Add-PSTSGitRepositoryPush -project fakeProject -repoName fakeRepoName -branch fakeBranch -comments fakeComment -fileName test `
    -content "fake content"

    #Assert
    It "Uses the mock" {
        Assert-VerifiableMock
    }

}


describe 'Add-PSTSGitRepositoryPush-git2git' {

    mockGetRepository
    mockGetRepositoryRef

    Mock -Verifiable -ModuleName PSTS Get-PSTSGitRepositoryRefItemBatch  -MockWith {
    param($project)
        $data = @()
        $obj = New-Object -TypeName "PSObject"
        $ret = [GitItem]::new()
        $ret.isFolder = $true
        $ret.path = "/fakePath"
        $data+=$ret

        $ret = [GitItem]::new()
        $ret.isFolder = $false
        $ret.path = "/fakePath"
        $data+=$ret


        $obj | Add-Member -MemberType NoteProperty "Count" -Value $data.Count
        $obj | Add-Member -MemberType NoteProperty "value" -Value $data

        return $ret
    }

    Mock -Verifiable -ModuleName PSTS Get-PSTSGitRepositoryRefItemFile  -MockWith {
    param($project)
        $ret = [GitItem]::new()
        $ret.path = "/fakePath"
        $ret.content = "fake content youhou"
        return $ret
    }


    # Arrange
    $expectedResult = [GitRepository]::new()
    $expectedResult.name = "fakeRepoName"

    arrange -result $expectedResult

    # Act
    $result = Add-PSTSGitRepositoryPush -project fakeProject -repoName fakeRepoName -branch fakeBranch -comments fakeComment -fileName test `
    -sourceRepository fakeSourceRepo -sourceProject fakeSourceProject -sourceFolder FakeSourceFolder -sourceBranch FakeSourceBranch

    #Assert
    It "Uses the mock" {
        Assert-VerifiableMock
    }

}