github.com/cobalt77/jfrog-client-go@v0.14.5/artifactory/usage/reportusage_test.go (about)

     1  package usage
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/cobalt77/jfrog-client-go/utils"
     8  )
     9  
    10  func TestIsVersionCompatible(t *testing.T) {
    11  	tests := []struct {
    12  		artifactoryVersion string
    13  		expectedResult     bool
    14  	}{
    15  		{"6.5.0", false},
    16  		{"6.2.0", false},
    17  		{"5.9.0", false},
    18  		{"6.0.0", false},
    19  		{"6.6.0", false},
    20  		{"6.9.0", true},
    21  		{utils.Development, true},
    22  		{"6.10.2", true},
    23  		{"6.15.2", true},
    24  	}
    25  	for _, test := range tests {
    26  		t.Run(test.artifactoryVersion, func(t *testing.T) {
    27  			result := isVersionCompatible(test.artifactoryVersion)
    28  			if test.expectedResult != result {
    29  				t.Error(fmt.Errorf("Expected %t, got %t", test.expectedResult, result))
    30  			}
    31  		})
    32  	}
    33  }
    34  
    35  func TestReportUsageJson(t *testing.T) {
    36  	type test struct {
    37  		productId      string
    38  		commandName    string
    39  		expectedResult string
    40  	}
    41  
    42  	json := `{"productId":"%s","features":[{"featureId":"%s"}]}`
    43  	preTests := []test{
    44  		{"jfrog-cli-go/1.26.0", "rt_copy", ""},
    45  		{"jfrog-client-go", "rt_download", ""},
    46  		{"test", "rt_build", ""},
    47  		{"agent/1.25.0", "rt_go", ""},
    48  	}
    49  
    50  	var tests []test
    51  	// Create the expected json
    52  	for _, test := range preTests {
    53  		test.expectedResult = fmt.Sprintf(json, test.productId, test.commandName)
    54  		tests = append(tests, test)
    55  	}
    56  
    57  	for _, test := range tests {
    58  		t.Run(test.commandName, func(t *testing.T) {
    59  			body, err := reportUsageToJson(test.productId, test.commandName)
    60  			if err != nil {
    61  				t.Error(err)
    62  			}
    63  
    64  			if string(body) != test.expectedResult {
    65  				t.Error(fmt.Errorf("Expected %s, got %s", test.expectedResult, string(body)))
    66  			}
    67  		})
    68  	}
    69  }