github.com/jfrog/jfrog-cli-core/v2@v2.51.0/artifactory/commands/replication/create_test.go (about) 1 package replication 2 3 import ( 4 "encoding/json" 5 "io" 6 "net/http" 7 "net/http/httptest" 8 "path/filepath" 9 "testing" 10 11 "github.com/jfrog/jfrog-cli-core/v2/utils/config" 12 "github.com/jfrog/jfrog-client-go/artifactory/services/utils" 13 "github.com/stretchr/testify/assert" 14 ) 15 16 var ( 17 templatesPath = filepath.Join("..", "testdata", "replication") 18 expected = utils.CreateUpdateReplicationBody( 19 utils.ReplicationParams{ 20 CronExp: "repl-cronExp", 21 RepoKey: "repl-RepoKey", 22 EnableEventReplication: true, 23 SocketTimeoutMillis: 123, 24 Enabled: true, 25 SyncDeletes: true, 26 SyncProperties: true, 27 SyncStatistics: true, 28 PathPrefix: "repl-pathprefix", 29 IncludePathPrefixPattern: "repl-pathprefix", 30 }, 31 ) 32 ) 33 34 func TestCreateReplicationPathPrefix(t *testing.T) { 35 // Create replication command 36 replicationCmd := NewReplicationCreateCommand() 37 testServer := createMockServer(t, replicationCmd) 38 defer testServer.Close() 39 40 // Test create replication with template containing "pathPrefix" 41 replicationCmd.SetTemplatePath(filepath.Join(templatesPath, "template-pathPrefix.json")) 42 assert.NoError(t, replicationCmd.Run()) 43 } 44 45 func TestReplicationIncludePathPrefix(t *testing.T) { 46 // Create replication command 47 replicationCmd := NewReplicationCreateCommand() 48 testServer := createMockServer(t, replicationCmd) 49 defer testServer.Close() 50 51 // Test create replication with template containing "includePathPrefixPattern" 52 replicationCmd.SetTemplatePath(filepath.Join(templatesPath, "template-includePathPrefixPattern.json")) 53 assert.NoError(t, replicationCmd.Run()) 54 } 55 56 // Create mock server to test replication body 57 // t - The testing object 58 // replicationCmd - The replication-create command to populate with the server URL 59 func createMockServer(t *testing.T, replicationCmd *ReplicationCreateCommand) *httptest.Server { 60 testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 61 w.WriteHeader(http.StatusOK) 62 63 // Read body 64 content, err := io.ReadAll(r.Body) 65 assert.NoError(t, err) 66 67 // Unmarshal body 68 var actual utils.UpdateReplicationBody 69 assert.NoError(t, json.Unmarshal(content, &actual)) 70 71 // Make sure the sent replication body equals to the expected 72 assert.Equal(t, *expected, actual) 73 })) 74 serverDetails := &config.ServerDetails{ArtifactoryUrl: testServer.URL + "/"} 75 replicationCmd.SetServerDetails(serverDetails) 76 return testServer 77 }