change in Initialse function to read each folder and add test cases
This commit is contained in:
@@ -6,6 +6,8 @@ import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
@@ -15,10 +17,9 @@ type Payload struct {
|
||||
}
|
||||
|
||||
type Context struct{}
|
||||
|
||||
type Message struct{}
|
||||
|
||||
func TestInitializeValidDirectory(t *testing.T) {
|
||||
func TestValidDirectory(t *testing.T) {
|
||||
provider := &TekuriValidatorProvider{}
|
||||
schemaDir := "../testData/schema_valid/"
|
||||
_, err := provider.Initialize(schemaDir)
|
||||
@@ -27,46 +28,7 @@ func TestInitializeValidDirectory(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestInitializeInValidDirectory(t *testing.T) {
|
||||
provider := &TekuriValidatorProvider{}
|
||||
schemaDir := "../testData/schema/ondc_trv10/"
|
||||
_, err := provider.Initialize(schemaDir)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to read schema directory: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInvalidCompileFile(t *testing.T) {
|
||||
schemaDir := "../testData/invalid_compile_schema/"
|
||||
if _, err := os.Stat(schemaDir); os.IsNotExist(err) {
|
||||
t.Fatalf("Schema directory does not exist: %v", schemaDir)
|
||||
}
|
||||
provider := &TekuriValidatorProvider{}
|
||||
compiledSchema, err := provider.Initialize(schemaDir)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to compile JSON schema : %v", err)
|
||||
}
|
||||
if compiledSchema == nil {
|
||||
t.Fatalf("compiled schema is nil : ")
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
func TestInvalidCompileSchema(t *testing.T) {
|
||||
schemaDir := "../testData/invalid_schemas/"
|
||||
if _, err := os.Stat(schemaDir); os.IsNotExist(err) {
|
||||
t.Fatalf("Schema directory does not exist: %v", schemaDir)
|
||||
}
|
||||
provider := &TekuriValidatorProvider{}
|
||||
compiledSchema, _ := provider.Initialize(schemaDir)
|
||||
fmt.Println(compiledSchema)
|
||||
if compiledSchema == nil {
|
||||
t.Fatalf("compiled schema is nil : ")
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateData(t *testing.T) {
|
||||
func TestValidPayload(t *testing.T) {
|
||||
schemaDir := "../testData/schema_valid/"
|
||||
if _, err := os.Stat(schemaDir); os.IsNotExist(err) {
|
||||
t.Fatalf("Schema directory does not exist: %v", schemaDir)
|
||||
@@ -88,7 +50,7 @@ func TestValidateData(t *testing.T) {
|
||||
t.Fatalf("No validators found in the map")
|
||||
}
|
||||
|
||||
payloadFilePath := "../testData/cancel.json"
|
||||
payloadFilePath := "../testData/payloads/search.json"
|
||||
payloadData, err := os.ReadFile(payloadFilePath)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to read payload data: %v", err)
|
||||
@@ -106,13 +68,127 @@ func TestValidateData(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestInValidateData(t *testing.T) {
|
||||
func TestInValidDirectory(t *testing.T) {
|
||||
provider := &TekuriValidatorProvider{}
|
||||
schemaDir := "../testData/schema/ondc_trv10/"
|
||||
_, err := provider.Initialize(schemaDir)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to read schema directory: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInvalidCompileFile(t *testing.T) {
|
||||
schemaDir := "../testData/invalid_compile_schema/"
|
||||
// if _, err := os.Stat(schemaDir); os.IsNotExist(err) {
|
||||
// t.Fatalf("Schema directory does not exist: %v", schemaDir)
|
||||
// }
|
||||
provider := &TekuriValidatorProvider{}
|
||||
_, err := provider.Initialize(schemaDir)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to compile JSON schema : %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInvalidCompileSchema(t *testing.T) {
|
||||
schemaDir := "../testData/invalid_schemas/"
|
||||
if _, err := os.Stat(schemaDir); os.IsNotExist(err) {
|
||||
t.Fatalf("Schema directory does not exist: %v", schemaDir)
|
||||
}
|
||||
provider := &TekuriValidatorProvider{}
|
||||
compiledSchema, _ := provider.Initialize(schemaDir)
|
||||
fmt.Println(compiledSchema)
|
||||
if compiledSchema == nil {
|
||||
t.Fatalf("compiled schema is nil : ")
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
func TestPayloadWithExtraFields(t *testing.T) {
|
||||
schemaDir := "../testData/schema_valid/"
|
||||
if _, err := os.Stat(schemaDir); os.IsNotExist(err) {
|
||||
t.Fatalf("Schema directory does not exist: %v", schemaDir)
|
||||
}
|
||||
provider := &TekuriValidatorProvider{}
|
||||
validators, err := provider.Initialize(schemaDir)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to initialize schema provider: %v", err)
|
||||
}
|
||||
var validator *TekuriValidator
|
||||
for _, v := range validators {
|
||||
var ok bool
|
||||
validator, ok = v.(*TekuriValidator)
|
||||
if ok {
|
||||
break
|
||||
}
|
||||
}
|
||||
if validator == nil {
|
||||
t.Fatalf("No validators found in the map")
|
||||
}
|
||||
payloadFilePath := "../testData/payloads/search_extraField.json"
|
||||
payloadData, err := os.ReadFile(payloadFilePath)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to read payload data: %v", err)
|
||||
}
|
||||
var payload Payload
|
||||
err = json.Unmarshal(payloadData, &payload)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to unmarshal payload data: %v", err)
|
||||
}
|
||||
err = validator.Validate(context.Background(), payloadData)
|
||||
if err != nil {
|
||||
t.Errorf("Validation failed due to extra fields: %v", err)
|
||||
} else {
|
||||
fmt.Println("Validation succeeded.")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestPayloadWithMissingFields(t *testing.T) {
|
||||
schemaDir := "../testData/schema_valid/"
|
||||
if _, err := os.Stat(schemaDir); os.IsNotExist(err) {
|
||||
t.Fatalf("Schema directory does not exist: %v", schemaDir)
|
||||
}
|
||||
provider := &TekuriValidatorProvider{}
|
||||
validators, err := provider.Initialize(schemaDir)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to initialize schema provider: %v", err)
|
||||
}
|
||||
var validator *TekuriValidator
|
||||
for _, v := range validators {
|
||||
var ok bool
|
||||
validator, ok = v.(*TekuriValidator)
|
||||
if ok {
|
||||
break
|
||||
}
|
||||
}
|
||||
if validator == nil {
|
||||
t.Fatalf("No validators found in the map")
|
||||
}
|
||||
payloadFilePath := "../testData/payloads/search_missingField.json"
|
||||
payloadData, err := os.ReadFile(payloadFilePath)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to read payload data: %v", err)
|
||||
}
|
||||
var payload Payload
|
||||
err = json.Unmarshal(payloadData, &payload)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to unmarshal payload data: %v", err)
|
||||
}
|
||||
err = validator.Validate(context.Background(), payloadData)
|
||||
if err != nil {
|
||||
t.Errorf("Validation failed with missing fields: %v", err)
|
||||
} else {
|
||||
fmt.Println("Validation succeeded.")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestInValidPayload(t *testing.T) {
|
||||
schemaDir := "../testData/schema_valid/"
|
||||
|
||||
if _, err := os.Stat(schemaDir); os.IsNotExist(err) {
|
||||
t.Fatalf("Schema directory does not exist: %v", schemaDir)
|
||||
}
|
||||
|
||||
provider := &TekuriValidatorProvider{}
|
||||
validators, err := provider.Initialize(schemaDir)
|
||||
if err != nil {
|
||||
@@ -132,7 +208,7 @@ func TestInValidateData(t *testing.T) {
|
||||
invalidPayloadData := []byte(`"invalid": "data"}`)
|
||||
err = validator.Validate(context.Background(), invalidPayloadData)
|
||||
if err != nil {
|
||||
t.Errorf("Validation failed: %v", err)
|
||||
t.Fatalf("Validation failed: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -176,3 +252,52 @@ func TestGetProvider(t *testing.T) {
|
||||
t.Logf("GetProvider returned the expected providerInstance")
|
||||
}
|
||||
}
|
||||
|
||||
func TestInitialize_SchemaPathNotDirectory(t *testing.T) {
|
||||
vp := &TekuriValidatorProvider{}
|
||||
filePath := "../testdata/directory.json"
|
||||
|
||||
_, err := vp.Initialize(filePath)
|
||||
|
||||
if err == nil || !strings.Contains(err.Error(), "provided schema path is not a directory") {
|
||||
t.Errorf("Expected error about non-directory schema path, got: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInitialize_InvalidSchemaFileStructure(t *testing.T) {
|
||||
schemaDir := "../testData/invalid_structure"
|
||||
provider := &TekuriValidatorProvider{}
|
||||
|
||||
_, err := provider.Initialize(schemaDir)
|
||||
if err == nil || !strings.Contains(err.Error(), "invalid schema file structure") {
|
||||
t.Fatalf("Expected error for invalid schema file structure, got: %v", err)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestInitialize_FailedToGetRelativePath(t *testing.T) {
|
||||
schemaDir := "../testData/valid_schemas"
|
||||
provider := &TekuriValidatorProvider{}
|
||||
|
||||
// Use an absolute path for schemaDir and a relative path for the file to simulate the error
|
||||
absSchemaDir, err := filepath.Abs(schemaDir)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to get absolute path for schema directory: %v", err)
|
||||
}
|
||||
|
||||
// Temporarily change the working directory to simulate different volumes
|
||||
originalWd, err := os.Getwd()
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to get current working directory: %v", err)
|
||||
}
|
||||
defer os.Chdir(originalWd) // Restore the original working directory after the test
|
||||
|
||||
// Change to a different directory
|
||||
os.Chdir("/tmp")
|
||||
|
||||
_, err = provider.Initialize(absSchemaDir)
|
||||
if err == nil || !strings.Contains(err.Error(), "failed to get relative path for file") {
|
||||
t.Fatalf("Expected error for failing to get relative path, got: %v", err)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user