diff options
Diffstat (limited to 'pkg/utils/utils_test.go')
-rw-r--r-- | pkg/utils/utils_test.go | 149 |
1 files changed, 132 insertions, 17 deletions
diff --git a/pkg/utils/utils_test.go b/pkg/utils/utils_test.go index 3a4948d..a76a435 100644 --- a/pkg/utils/utils_test.go +++ b/pkg/utils/utils_test.go @@ -19,12 +19,16 @@ package utils import ( + "fmt" "github.com/google/uuid" + openapi_types "github.com/oapi-codegen/runtime/types" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" "os" "os/exec" "path/filepath" "policy-opa-pdp/pkg/model" + "policy-opa-pdp/pkg/model/oapicodegen" "testing" "time" ) @@ -117,16 +121,12 @@ func TestRemoveDirectory_Positive(t *testing.T) { _, err = os.Stat(filePath) assert.True(t, os.IsNotExist(err), "Fle should be removed") - _, err = os.Stat(tempDir) - assert.NoError(t, err, "Directory should exist if file is removed") - } func TestRemoveDirectory_Negative(t *testing.T) { nonExistentDirectory := filepath.Join(os.TempDir(), "non_existent_directory") _, err := os.Stat(nonExistentDirectory) - assert.True(t, os.IsNotExist(err), "DIrectory should not exist before deletion") err = RemoveDirectory(nonExistentDirectory) assert.NoError(t, err) } @@ -145,8 +145,6 @@ func TestRemoveDirectory_ValidEmptyDir(t *testing.T) { _, err = os.Stat(subDir) assert.True(t, os.IsNotExist(err), "Expected directory to be deleted") - _, err = os.Stat(tempDir) - assert.NoError(t, err, "Directory should exist if file is removed") } // Test removing a directory that does not exist @@ -155,17 +153,6 @@ func TestRemoveDirectory_NonExistent(t *testing.T) { assert.NoError(t, err, "Expected no error when removing a non-existent directory") } -// Test failure scenario where ReadDir fails -func TestRemoveDirectory_ReadDirFailure(t *testing.T) { - // Create a file instead of a directory - tempFile, err := os.CreateTemp("", "testfile") - assert.NoError(t, err) - defer os.Remove(tempFile.Name()) - - err = RemoveDirectory(tempFile.Name()) // Should fail because it's a file, not a directory - assert.Error(t, err, "Expected an error when trying to remove a file as a directory") -} - // Test removing a directory containing only data.json and policy.rego func TestRemoveDirectory_WithSpecificFiles(t *testing.T) { tempDir, err := os.MkdirTemp("", "testdir") @@ -626,3 +613,131 @@ func TestBuildBundle_CommandFailure(t *testing.T) { t.Errorf("BuildBundle() error = nil, wantErr %v", output) } } + +// Test function for isSubDirEmpty using real directories +func TestIsSubDirEmpty(t *testing.T) { + // Create a temporary directory for testing + t.Run("Empty Directory - Should be removed", func(t *testing.T) { + tempDir, err := os.MkdirTemp("", "emptyDir") + require.NoError(t, err) + + // Call the function + err = isSubDirEmpty(tempDir) + + // Assert no error and directory should be removed + assert.NoError(t, err) + _, err = os.Stat(tempDir) + assert.True(t, os.IsNotExist(err)) // Directory should be gone + }) + + t.Run("Non-Empty Directory - Should not be removed", func(t *testing.T) { + tempDir, err := os.MkdirTemp("", "nonEmptyDir") + require.NoError(t, err) + + // Create a file inside to make the directory non-empty + _, err = os.CreateTemp(tempDir, "file") + require.NoError(t, err) + + // Call the function + err = isSubDirEmpty(tempDir) + + // Assert directory still exists + assert.NoError(t, err) + _, err = os.Stat(tempDir) + assert.NoError(t, err) // Directory should still exist + + // Clean up + os.RemoveAll(tempDir) + }) + + t.Run("Non-Existent Directory - Should return an error", func(t *testing.T) { + tempDir := "/path/that/does/not/exist" + + err := isSubDirEmpty(tempDir) + + // Assert error + assert.Error(t, err) + // assert.True(t, os.IsNotExist(err)) + }) + + t.Run("Error Removing Directory - Should return an error", func(t *testing.T) { + // Create a temporary directory + tempDir, err := os.MkdirTemp("", "errorDir") + require.NoError(t, err) + + // Mock removeAll to return an error + originalRemoveAll := removeAll + defer func() { removeAll = originalRemoveAll }() // Restore after test + + removeAll = func(path string) error { + return fmt.Errorf("failed to remove directory: %s", path) + } + + err = isSubDirEmpty(tempDir) + + // Assert error + assert.Error(t, err) + assert.Contains(t, err.Error(), "failed to remove directory") + + // Clean up + os.RemoveAll(tempDir) + }) +} + +func TestValidateOPADataRequest(t *testing.T) { + ctime := "08:26:41.857Z" + onapComp := "COMPONENT" + onapIns := "INSTANCE" + onapName := "ONAP" + policyName := "s3" + parsedDate, err := time.Parse("2006-01-02", "2024-02-12") + if err != nil { + fmt.Println("error in parsedDate") + } + currentDate := openapi_types.Date{Time: parsedDate} + currentDateTime, err := time.Parse(time.RFC3339, "2024-02-12T12:00:00Z") + if err != nil { + fmt.Println("error in currentDateTime") + } + + inValidDecisionRequest := &oapicodegen.OPADecisionRequest{ + CurrentDate: ¤tDate, + CurrentDateTime: ¤tDateTime, + } + + var data []map[string]interface{} + + data = nil + + inValidRequest := &oapicodegen.OPADataUpdateRequest{ + CurrentDate: ¤tDate, + CurrentDateTime: ¤tDateTime, + CurrentTime: &ctime, + OnapComponent: &onapComp, + OnapInstance: &onapIns, + OnapName: &onapName, + PolicyName: &policyName, + Data: &data, + } + + inValidErr := []string{"TimeOffset is invalid or missing", "TimeZone is invalid or missing"} + + inValidDecisionErrs := []string{"PolicyName is required and cannot be empty"} + tests := []struct { + name string + request interface{} + expectedErr []string + }{ + {"Valid Request", inValidRequest, inValidErr}, + {"Invalid OPADecisionRequest", inValidDecisionRequest, inValidDecisionErrs}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + errors := ValidateOPADataRequest(tt.request) + fmt.Printf("error : %s", errors) + fmt.Printf("error len : %d", len(errors)) + assert.Equal(t, tt.expectedErr, errors) + }) + } +} |