github.com/jfrog/jfrog-client-go@v1.40.2/tests/xraysummary_test.go (about)

     1  package tests
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  	"github.com/stretchr/testify/assert"
     7  	"strconv"
     8  	"strings"
     9  	"testing"
    10  
    11  	"github.com/jfrog/jfrog-client-go/artifactory/services/utils/tests/xray"
    12  	"github.com/jfrog/jfrog-client-go/http/jfroghttpclient"
    13  	"github.com/jfrog/jfrog-client-go/xray/services"
    14  )
    15  
    16  var testsXraySummaryService *services.SummaryService
    17  
    18  func TestNewXraySummaryService(t *testing.T) {
    19  	initXrayTest(t)
    20  	xrayServerPort := xray.StartXrayMockServer(t)
    21  	xrayDetails := GetXrayDetails()
    22  	client, err := jfroghttpclient.JfrogClientBuilder().
    23  		SetClientCertPath(xrayDetails.GetClientCertPath()).
    24  		SetClientCertKeyPath(xrayDetails.GetClientCertKeyPath()).
    25  		AppendPreRequestInterceptor(xrayDetails.RunPreRequestFunctions).
    26  		Build()
    27  	if err != nil {
    28  		t.Error(err)
    29  	}
    30  
    31  	testsXraySummaryService = services.NewSummaryService(client)
    32  	testsXraySummaryService.XrayDetails = xrayDetails
    33  	testsXraySummaryService.XrayDetails.SetUrl("http://localhost:" + strconv.Itoa(xrayServerPort) + "/")
    34  
    35  	// Run tests
    36  	tests := []struct {
    37  		name      string
    38  		checksums []string
    39  		paths     []string
    40  		expected  string
    41  	}{
    42  		{name: "getVulnerableArtifactSummary", checksums: []string{"0000"}, paths: nil, expected: xray.VulnerableXraySummaryArtifactResponse},
    43  	}
    44  	for _, test := range tests {
    45  		t.Run(test.name, func(t *testing.T) {
    46  			artifactSummary(t, test.checksums, test.paths, test.expected)
    47  		})
    48  	}
    49  }
    50  
    51  func artifactSummary(t *testing.T, checksums []string, paths []string, expected string) {
    52  	params := services.ArtifactSummaryParams{
    53  		Checksums: checksums,
    54  		Paths:     paths,
    55  	}
    56  	result, err := testsXraySummaryService.GetArtifactSummary(params)
    57  	if err != nil {
    58  		t.Error(err)
    59  	}
    60  
    61  	resultString, err := json.Marshal(result)
    62  	if err != nil {
    63  		t.Error(err)
    64  	}
    65  
    66  	buf := bytes.NewBuffer([]byte{})
    67  	err = json.Compact(buf, []byte(expected))
    68  	assert.NoError(t, err)
    69  	expected = buf.String()
    70  
    71  	expected = strings.ReplaceAll(expected, "\n", "")
    72  	actual := strings.ReplaceAll(string(resultString), "\n", "")
    73  	if actual != expected {
    74  		t.Error("\nExpected:\n", expected, "\n\nGot:\n", actual)
    75  	}
    76  }