github.com/0chain/gosdk@v1.17.11/zboxcore/sdk/allocation_file_delete_test.go (about)

     1  package sdk
     2  
     3  import (
     4  	"encoding/json"
     5  	"net/http"
     6  	"strconv"
     7  	"testing"
     8  	"time"
     9  
    10  	"github.com/0chain/errors"
    11  	"github.com/0chain/gosdk/core/resty"
    12  	"github.com/0chain/gosdk/core/zcncrypto"
    13  	"github.com/0chain/gosdk/zboxcore/blockchain"
    14  	zclient "github.com/0chain/gosdk/zboxcore/client"
    15  	"github.com/0chain/gosdk/zboxcore/fileref"
    16  	"github.com/0chain/gosdk/zboxcore/mocks"
    17  	"github.com/0chain/gosdk/zboxcore/zboxutil"
    18  	"github.com/stretchr/testify/require"
    19  )
    20  
    21  func TestAllocation_DeleteFile(t *testing.T) {
    22  	const (
    23  		mockType = "f"
    24  	)
    25  
    26  	rawClient := zboxutil.Client
    27  	createClient := resty.CreateClient
    28  
    29  	var mockClient = mocks.HttpClient{}
    30  	zboxutil.Client = &mockClient
    31  
    32  	client := zclient.GetClient()
    33  	client.Wallet = &zcncrypto.Wallet{
    34  		ClientID:  mockClientId,
    35  		ClientKey: mockClientKey,
    36  	}
    37  
    38  	zboxutil.Client = &mockClient
    39  	resty.CreateClient = func(t *http.Transport, timeout time.Duration) resty.Client {
    40  		return &mockClient
    41  	}
    42  
    43  	defer func() {
    44  		zboxutil.Client = rawClient
    45  		resty.CreateClient = createClient
    46  	}()
    47  
    48  	require := require.New(t)
    49  
    50  	a := &Allocation{
    51  		DataShards:   2,
    52  		ParityShards: 2,
    53  		FileOptions:  63,
    54  	}
    55  	a.InitAllocation()
    56  	sdkInitialized = true
    57  
    58  	for i := 0; i < numBlobbers; i++ {
    59  		a.Blobbers = append(a.Blobbers, &blockchain.StorageNode{
    60  			ID:      mockBlobberId + strconv.Itoa(i),
    61  			Baseurl: "http://TestAllocation_DeleteFile" + mockBlobberUrl + strconv.Itoa(i),
    62  		})
    63  	}
    64  
    65  	body, err := json.Marshal(&fileref.ReferencePath{
    66  		Meta: map[string]interface{}{
    67  			"type": mockType,
    68  		},
    69  	})
    70  	require.NoError(err)
    71  	setupMockHttpResponse(t, &mockClient, "TestAllocation_DeleteFile", "", a, http.MethodPost, http.StatusOK, body)
    72  	setupMockHttpResponse(t, &mockClient, "TestAllocation_DeleteFile", "", a, http.MethodDelete, http.StatusOK, []byte(""))
    73  	setupMockRollback(a, &mockClient)
    74  	setupMockCommitRequest(a)
    75  	setupMockWriteLockRequest(a, &mockClient)
    76  
    77  	err = a.DeleteFile("/1.txt")
    78  	require.NoErrorf(err, "unexpected error: %v", err)
    79  }
    80  
    81  func TestAllocation_deleteFile(t *testing.T) {
    82  	const (
    83  		mockType = "f"
    84  	)
    85  
    86  	rawClient := zboxutil.Client
    87  	createClient := resty.CreateClient
    88  
    89  	var mockClient = mocks.HttpClient{}
    90  	zboxutil.Client = &mockClient
    91  
    92  	client := zclient.GetClient()
    93  	client.Wallet = &zcncrypto.Wallet{
    94  		ClientID:  mockClientId,
    95  		ClientKey: mockClientKey,
    96  	}
    97  
    98  	zboxutil.Client = &mockClient
    99  	resty.CreateClient = func(t *http.Transport, timeout time.Duration) resty.Client {
   100  		return &mockClient
   101  	}
   102  
   103  	defer func() {
   104  		zboxutil.Client = rawClient
   105  		resty.CreateClient = createClient
   106  	}()
   107  
   108  	type parameters struct {
   109  		path string
   110  	}
   111  	tests := []struct {
   112  		name       string
   113  		parameters parameters
   114  		setup      func(*testing.T, string, *Allocation) (teardown func(*testing.T))
   115  		wantErr    bool
   116  		errMsg     string
   117  	}{
   118  		{
   119  			name: "Test_Uninitialized_Failed",
   120  			setup: func(t *testing.T, testCaseName string, a *Allocation) (teardown func(t *testing.T)) {
   121  				a.initialized = false
   122  				return func(t *testing.T) {
   123  					a.initialized = true
   124  				}
   125  			},
   126  			wantErr: true,
   127  			errMsg:  "sdk_not_initialized: Please call InitStorageSDK Init and use GetAllocation to get the allocation object",
   128  		},
   129  		{
   130  			name:    "Test_Invalid_Path_Failed",
   131  			wantErr: true,
   132  			errMsg:  "invalid_path: Invalid path for the list",
   133  		},
   134  		{
   135  			name: "Test_Not_Abs_Path_Failed",
   136  			parameters: parameters{
   137  				path: "x.txt",
   138  			},
   139  			wantErr: true,
   140  			errMsg:  "invalid_path: Path should be valid and absolute",
   141  		},
   142  		{
   143  			name: "Test_Success",
   144  			parameters: parameters{
   145  				path: "/1.txt",
   146  			},
   147  			setup: func(t *testing.T, testCaseName string, a *Allocation) (teardown func(t *testing.T)) {
   148  				body, err := json.Marshal(&fileref.ReferencePath{
   149  					Meta: map[string]interface{}{
   150  						"type": mockType,
   151  					},
   152  				})
   153  				require.NoError(t, err)
   154  				setupMockHttpResponse(t, &mockClient, "TestAllocation_deleteFile", testCaseName, a, http.MethodPost, http.StatusOK, body)
   155  				setupMockHttpResponse(t, &mockClient, "TestAllocation_deleteFile", testCaseName, a, http.MethodDelete, http.StatusOK, []byte(""))
   156  				setupMockCommitRequest(a)
   157  				setupMockRollback(a, &mockClient)
   158  				setupMockWriteLockRequest(a, &mockClient)
   159  
   160  				return nil
   161  			},
   162  		},
   163  	}
   164  	for _, tt := range tests {
   165  		t.Run(tt.name, func(t *testing.T) {
   166  			require := require.New(t)
   167  			a := &Allocation{
   168  				DataShards:   2,
   169  				ParityShards: 2,
   170  				FileOptions:  63,
   171  			}
   172  			a.InitAllocation()
   173  			sdkInitialized = true
   174  			for i := 0; i < numBlobbers; i++ {
   175  				a.Blobbers = append(a.Blobbers, &blockchain.StorageNode{
   176  					ID:      tt.name + mockBlobberId + strconv.Itoa(i),
   177  					Baseurl: "http://TestAllocation_deleteFile" + tt.name + mockBlobberUrl + strconv.Itoa(i),
   178  				})
   179  			}
   180  			if tt.setup != nil {
   181  				if teardown := tt.setup(t, tt.name, a); teardown != nil {
   182  					defer teardown(t)
   183  				}
   184  			}
   185  			err := a.DeleteFile(tt.parameters.path)
   186  			require.EqualValues(tt.wantErr, err != nil, "Message: ", err)
   187  			if err != nil {
   188  				require.EqualValues(tt.errMsg, errors.Top(err))
   189  				return
   190  			}
   191  			require.NoErrorf(err, "unexpected error: %v", err)
   192  		})
   193  	}
   194  }