Core/Helpers/CommonHelper.ps1

Set-StrictMode -Version Latest 
class CommonHelper {
    static [bool] CompareCSV([string] $sourceFilePath,[string] $destinationFilePath) {
        $result = $false
        try {
        $sourceFile = import-csv -Path $sourceFilePath
        $destinationFile = import-csv -Path $destinationFilePath
        [PSObject[]] $difference = $null
        $difference = Compare-Object $sourceFile $destinationFile -property ControlID,Status 
        $difference = $difference | Sort-Object -Property "ControlID" -Unique
        if($null -ne $difference){
            if($difference.Count -gt 0){
            [CommonHelper]::Log("Output CSV does not match with the baseline!", [MessageType]::Error)
            [CommonHelper]::Log("Controls that differ are:", [MessageType]::Information)
            foreach($diff in $difference){
                [CommonHelper]::Log($diff.ControlID, [MessageType]::Information)
            }
                $result = $false
            }
            else{
                $result = $true 
            }
        }
        else {
                $result = $true     
            }
        }
        catch {
             [CommonHelper]::Log("Error comparing csv files.", [MessageType]::Error)
             [CommonHelper]::Log($_, [MessageType]::Error)
        }
        return $result
    }

    static [string] GetRootPath() {
        return (get-item $PSScriptRoot).Parent.Parent.FullName
    }

    static [string] GetPath([PathList] $pathList,[TestCase]$testcase ) {
        $Path = [CommonHelper]::GetRootPath()
        try {
        switch ($pathList.ToString()) {
            ([PathList]::TestCases){
                $Path+="\TestCasesMaster.json"
            }
            ([PathList]::TestData) {
                $Path +="\TestCases\"+$testcase.Feature +"\"+$testcase.ModuleName+"\TestData\"
            }
            Default {
            }
        }
                }
        catch {
            [CommonHelper]::Log("Error fetching path for: $pathList", [MessageType]::Error)
        }
                return $Path
    }

    static [string] GetPath([PathList] $pathList) {
        $Path = [CommonHelper]::GetRootPath()
        switch ($pathList.ToString()) {
            ([PathList]::Constants){
                $Path+="\Config\Constants.json"
            }
            ([PathList]::TestCases){
                $Path+="\TestCasesMaster.json"
            }
            Default {
            }
        }
        return $Path
    }

    static [void] Log([string] $Message,[MessageType] $MessageType){
    switch ($MessageType.ToString()) {
            ([MessageType]::Information){
        Write-Host $Message -foregroundcolor "White"
            }
            ([MessageType]::Warning) {
        Write-Host "################################" $Message "################################" -foregroundcolor "Magenta"
            }
            ([MessageType]::Error){
        Write-Host "================================" $Message "================================" -foregroundcolor "Red"
            }
            ([MessageType]::Header){
        Write-Host "********************************" $Message "********************************" -foregroundcolor "Green"
            }
        Default {
        Write-Host $Message -foregroundcolor "White"
            }
        }
    }

    static [string] GetValueFromJson([string]$path, [string]$keyNode, [string]$valueNode){
        $val = [string]::Empty
        $jsonFile =  Get-Content -Path $path | ConvertFrom-Json
        $val = $jsonFile.parameters.$keyNode.$valueNode
        return $val
    }

    static [void] SetValueIntoJson([string]$path, [string]$keyNode, [string]$valueNode, [string]$value){
        $jsonFile =  Get-Content -Path $path | ConvertFrom-Json
        $jsonFile.parameters.$keyNode.$valueNode = $value
        $jsonFile |ConvertTo-Json | Set-Content $path
    }

    static [string] GetOutputPath(){
        [string] $AzSdkTestLogFolderPath = $Env:LOCALAPPDATA + "\Microsoft\AzSDKTestLogs" + "\" + $(get-date -f MMddyyHHmmss)
        
        if(-not (Test-Path $AzSdkTestLogFolderPath))
        {
            try
            {
                mkdir -Path $AzSdkTestLogFolderPath -ErrorAction Stop | Out-Null
            }
            catch
            {
             [CommonHelper]::Log("Error creating output directory!", [MessageType]::Error)
             [CommonHelper]::Log($_, [MessageType]::Error)
            }
        }
        [string] $AzSDKTestLogFile = $AzSdkTestLogFolderPath
        return $AzSDKTestLogFile
    }

    static [void] SetOSSURL()
    {
        $OSSPolicyURL = "https://azsdkossep.azureedge.net/`$Version/`$FileName"
        #Set OSS settings
        Set-AzSDKPolicySettings -OnlinePolicyStoreUrl $OSSPolicyURL
    }
    static [void] SetMSITURL()
    {
        $MSITPolicyURL = "https://getazsdkcontrolsms.azurewebsites.net/api/files?version=`$Version&fileName=`$FileName"
        #Set MSIT settings
        Set-AzSDKPolicySettings -OnlinePolicyStoreUrl $MSITPolicyURL -EnableAADAuthForOnlinePolicyStore
    }

    static[bool]IsSecurityReportGenerated([string]$OutputPath)
    {
        [bool]$result = $false
        if(![string]::IsNullOrEmpty($OutputPath)){
            try{
                $OverallControlStatuscsv = Get-ChildItem -Path $outputpath -Include "SecurityReport-*.csv" -Recurse   
                $result = $true
            }
            catch{
            
            }    
        }
        return $result
    }

    static[bool]VerifyCSVForError([string] $sourceFilePath, [string] $columnName)
    {
        $result = $true
        try{
            $sourceFileName=  Get-ChildItem -Path $sourceFilePath -Include "SecurityReport-*.csv" -Recurse
            $sourceFile = import-csv -Path $sourceFileName
            
            $sourceFile | ForEach-Object {
                 foreach ($property in $_.PSObject.Properties ) 
                {
                    if($property.Name -eq $columnName){
                        if($property.Value -eq "Error"){
                            $result = $false
                            return $result
                        }
                    }                    
                }
            }
        }
        catch{
             [CommonHelper]::Log("Error verifying CSV for error.", [MessageType]::Error)
             [CommonHelper]::Log($_, [MessageType]::Error)
        }
        return $result
    }
}