add new schemas
This commit is contained in:
@@ -6,9 +6,8 @@ import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"beckn-onix/plugins/plugin_definition"
|
||||
"beckn-onix/plugins"
|
||||
|
||||
"github.com/santhosh-tekuri/jsonschema/v6"
|
||||
)
|
||||
@@ -19,157 +18,132 @@ type tekuriValidator struct {
|
||||
}
|
||||
|
||||
type tekuriValidatorProvider struct {
|
||||
//schemaCache map[string]map[string]*jsonschema.Schema
|
||||
schemaCache map[string]*jsonschema.Schema
|
||||
schemaCache map[string]map[string]*jsonschema.Schema
|
||||
//schemaCache map[string]*jsonschema.Schema
|
||||
}
|
||||
|
||||
// Validate validates the given data against the schema.
|
||||
func (v *tekuriValidator) Validate(ctx context.Context, data []byte) error {
|
||||
start := time.Now()
|
||||
// start := time.Now()
|
||||
var jsonData interface{}
|
||||
if err := json.Unmarshal(data, &jsonData); err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Println("json data : ", jsonData)
|
||||
err := v.schema.Validate(jsonData)
|
||||
if err != nil {
|
||||
fmt.Printf("Validation error: %v\n", err)
|
||||
}
|
||||
fmt.Printf("validate executed in %s\n", time.Since(start))
|
||||
|
||||
// fmt.Printf("validate executed in %s\n", time.Since(start))
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
//(Approach 2)(all json files)
|
||||
//(Approach 2)(all json files for each schema)
|
||||
|
||||
// //Initialize reads all .json files from the given schema directory, validates them using JSON Schema, and prints the result.
|
||||
// func (vp *tekuriValidatorProvider) Initialize(schemaDir string) (map[string]plugin_definition.Validator, error) {
|
||||
// start := time.Now()
|
||||
// // Initialize the cache
|
||||
// vp.schemaCache = make(map[string]map[string]*jsonschema.Schema)
|
||||
// validatorCache := make(map[string]plugin_definition.Validator)
|
||||
// Initialize reads all .json files from the given schema directory, validates them using JSON Schema, and prints the result.
|
||||
func (vp *tekuriValidatorProvider) Initialize(schemaDir string) (map[string]plugins.Validator, error) {
|
||||
//start := time.Now()
|
||||
// Initialize the cache
|
||||
vp.schemaCache = make(map[string]map[string]*jsonschema.Schema)
|
||||
validatorCache := make(map[string]plugins.Validator)
|
||||
|
||||
// Read the directory
|
||||
files, err := ioutil.ReadDir(schemaDir)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to read schema directory: %v", err)
|
||||
}
|
||||
|
||||
for _, file := range files {
|
||||
if filepath.Ext(file.Name()) == ".json" {
|
||||
// Read the JSON file
|
||||
filePath := filepath.Join(schemaDir, file.Name())
|
||||
fmt.Println("file path : ", filePath)
|
||||
compiler := jsonschema.NewCompiler()
|
||||
compiledSchema, err := compiler.Compile(filePath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to compile JSON schema from file %s: %v", file.Name(), err)
|
||||
}
|
||||
if compiledSchema == nil {
|
||||
return nil, fmt.Errorf("compiled schema is nil for file %s", file.Name())
|
||||
}
|
||||
// Extract directory and filename to use in the nested map
|
||||
dir := filepath.Base(filepath.Dir(filePath))
|
||||
if vp.schemaCache[dir] == nil {
|
||||
vp.schemaCache[dir] = make(map[string]*jsonschema.Schema)
|
||||
}
|
||||
// Store the compiled schema in the nested cache
|
||||
vp.schemaCache[dir][file.Name()] = compiledSchema
|
||||
|
||||
validatorCache[file.Name()] = &tekuriValidator{schema: compiledSchema}
|
||||
}
|
||||
}
|
||||
// fmt.Printf("initialize executed in %s\n", time.Since(start))
|
||||
|
||||
return validatorCache, nil
|
||||
}
|
||||
|
||||
// (Approach 1)
|
||||
// func (vp *tekuriValidatorProvider) Initialize(schemaDir string) (map[string]plugins.Validator, error) {
|
||||
// // start := time.Now()
|
||||
// vp.schemaCache = make(map[string]*jsonschema.Schema)
|
||||
// validatorCache := make(map[string]plugins.Validator)
|
||||
|
||||
// // Read the directory
|
||||
// files, err := ioutil.ReadDir(schemaDir)
|
||||
// if err != nil {
|
||||
// return nil, fmt.Errorf("failed to read schema directory: %v", err)
|
||||
// return nil, fmt.Errorf("failed to read schema directory: %w", err)
|
||||
// }
|
||||
|
||||
// for _, file := range files {
|
||||
// if filepath.Ext(file.Name()) == ".json" {
|
||||
// // Read the JSON file
|
||||
// filePath := filepath.Join(schemaDir, file.Name())
|
||||
// compiler := jsonschema.NewCompiler()
|
||||
// compiledSchema, err := compiler.Compile(filePath)
|
||||
// // Read the file content
|
||||
// content, err := ioutil.ReadFile(filePath)
|
||||
// if err != nil {
|
||||
// return nil, fmt.Errorf("failed to compile JSON schema from file %s: %v", file.Name(), err)
|
||||
// return nil, fmt.Errorf("failed to read file %s: %v", filePath, err)
|
||||
// }
|
||||
// if compiledSchema == nil {
|
||||
// return nil, fmt.Errorf("compiled schema is nil for file %s", file.Name())
|
||||
// }
|
||||
// // Extract directory and filename to use in the nested map
|
||||
// dir := filepath.Base(filepath.Dir(filePath))
|
||||
// if vp.schemaCache[dir] == nil {
|
||||
// vp.schemaCache[dir] = make(map[string]*jsonschema.Schema)
|
||||
// }
|
||||
// // Store the compiled schema in the nested cache
|
||||
// vp.schemaCache[dir][file.Name()] = compiledSchema
|
||||
|
||||
// validatorCache[file.Name()] = &tekuriValidator{schema: compiledSchema}
|
||||
// var schemaDoc map[string]interface{}
|
||||
// if err := json.Unmarshal(content, &schemaDoc); err != nil {
|
||||
// return nil, fmt.Errorf("failed to unmarshal JSON schema from file %s: %v", filePath, err)
|
||||
// }
|
||||
|
||||
// if defs, exists := schemaDoc["$defs"]; exists {
|
||||
// defsMap := defs.(map[string]interface{})
|
||||
|
||||
// for name, defSchema := range defsMap {
|
||||
// _, err := json.Marshal(defSchema)
|
||||
// if err != nil {
|
||||
// return nil, fmt.Errorf("failed to marshal schema definition %s: %v", name, err)
|
||||
// }
|
||||
|
||||
// compiler := jsonschema.NewCompiler()
|
||||
// if err := compiler.AddResource(name, filepath.Dir(filePath)); err != nil {
|
||||
// return nil, fmt.Errorf("failed to add resource for schema definition %s: %v", name, err)
|
||||
// }
|
||||
|
||||
// compiledSchema, err := compiler.Compile(filePath)
|
||||
// if err != nil {
|
||||
// return nil, fmt.Errorf("failed to compile schema definition: %v", err)
|
||||
// }
|
||||
|
||||
// schemaKey := fmt.Sprintf("%s.%s", file.Name(), name)
|
||||
// vp.schemaCache[schemaKey] = compiledSchema
|
||||
// validatorCache[schemaKey] = &tekuriValidator{schema: compiledSchema}
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// fmt.Printf("initialize executed in %s\n", time.Since(start))
|
||||
|
||||
// // fmt.Printf("Initialize executed in %s\n", time.Since(start))
|
||||
// return validatorCache, nil
|
||||
// }
|
||||
|
||||
// func (vp *tekuriValidatorProvider) Get(schemaKey string) (plugin_definition.Validator, error) {
|
||||
// // Extract domain, version, and defKey from the schemaKey
|
||||
// fmt.Println("before printing key :", schemaKey)
|
||||
// schemaKey = strings.Replace(schemaKey, ":", "_", -1)
|
||||
|
||||
// fmt.Println("after printing key :", schemaKey)
|
||||
|
||||
// parts := strings.Split(schemaKey, "_")
|
||||
// if len(parts) != 3 {
|
||||
// return nil, fmt.Errorf("invalid schema key format: %s", schemaKey)
|
||||
// }
|
||||
// domain := parts[0] + "_" + parts[1]
|
||||
// defKey := parts[2]
|
||||
|
||||
// // Look up the compiled schema in the nested map
|
||||
// if domainMap, ok := vp.schemaCache[domain]; ok {
|
||||
// if schema, ok := domainMap[defKey]; ok {
|
||||
// return &tekuriValidator{schema: schema}, nil
|
||||
// }
|
||||
// }
|
||||
// return nil, fmt.Errorf("schema not found: %s", schemaKey)
|
||||
// }
|
||||
|
||||
// (Approach 1)
|
||||
func (vp *tekuriValidatorProvider) Initialize(schemaDir string) (map[string]plugin_definition.Validator, error) {
|
||||
start := time.Now()
|
||||
|
||||
vp.schemaCache = make(map[string]*jsonschema.Schema)
|
||||
validatorCache := make(map[string]plugin_definition.Validator)
|
||||
|
||||
files, err := ioutil.ReadDir(schemaDir)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to read schema directory: %w", err)
|
||||
}
|
||||
for _, file := range files {
|
||||
if filepath.Ext(file.Name()) == ".json" {
|
||||
filePath := filepath.Join(schemaDir, file.Name())
|
||||
|
||||
fmt.Println("Compiling filePath:", filePath)
|
||||
|
||||
// Read the file content
|
||||
content, err := ioutil.ReadFile(filePath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to read file %s: %v", filePath, err)
|
||||
}
|
||||
|
||||
var schemaDoc map[string]interface{}
|
||||
if err := json.Unmarshal(content, &schemaDoc); err != nil {
|
||||
return nil, fmt.Errorf("failed to unmarshal JSON schema from file %s: %v", filePath, err)
|
||||
}
|
||||
|
||||
if defs, exists := schemaDoc["$defs"]; exists {
|
||||
defsMap := defs.(map[string]interface{})
|
||||
|
||||
for name, defSchema := range defsMap {
|
||||
_, err := json.Marshal(defSchema)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to marshal schema definition %s: %v", name, err)
|
||||
}
|
||||
|
||||
compiler := jsonschema.NewCompiler()
|
||||
if err := compiler.AddResource(name, filepath.Dir(filePath)); err != nil {
|
||||
return nil, fmt.Errorf("failed to add resource for schema definition %s: %v", name, err)
|
||||
}
|
||||
|
||||
compiledSchema, err := compiler.Compile(filePath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to compile schema definition: %v", err)
|
||||
}
|
||||
|
||||
schemaKey := fmt.Sprintf("%s.%s", file.Name(), name)
|
||||
vp.schemaCache[schemaKey] = compiledSchema
|
||||
validatorCache[schemaKey] = &tekuriValidator{schema: compiledSchema}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
fmt.Printf("Initialize executed in %s\n", time.Since(start))
|
||||
return validatorCache, nil
|
||||
|
||||
}
|
||||
|
||||
// Ensure tekuriValidatorProvider implements ValidatorProvider
|
||||
var _ plugin_definition.ValidatorProvider = (*tekuriValidatorProvider)(nil)
|
||||
var _ plugins.ValidatorProvider = (*tekuriValidatorProvider)(nil)
|
||||
|
||||
var providerInstance = &tekuriValidatorProvider{}
|
||||
|
||||
// Exported function to return the provider instance
|
||||
func GetProvider() plugin_definition.ValidatorProvider {
|
||||
func GetProvider() plugins.ValidatorProvider {
|
||||
return providerInstance
|
||||
}
|
||||
|
||||
179
plugins/implementations/plugin_impl_test.go
Normal file
179
plugins/implementations/plugin_impl_test.go
Normal file
@@ -0,0 +1,179 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type Payload struct {
|
||||
Context Context `json:"context"`
|
||||
Message Message `json:"message"`
|
||||
}
|
||||
|
||||
type Context struct{}
|
||||
|
||||
type Message struct{}
|
||||
|
||||
func TestInitializeValidDirectory(t *testing.T) {
|
||||
provider := &tekuriValidatorProvider{}
|
||||
schemaDir := "../schema/ondc_trv10_2.0.0/"
|
||||
_, err := provider.Initialize(schemaDir)
|
||||
if err != nil {
|
||||
t.Fatalf("expected no error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInitializeInValidDirectory(t *testing.T) {
|
||||
provider := &tekuriValidatorProvider{}
|
||||
schemaDir := "../schemas/ondc_trv10_2.0.0/"
|
||||
_, err := provider.Initialize(schemaDir)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to read schema directory: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInvalidCompileFile(t *testing.T) {
|
||||
schemaDir := "../invalid_schemas/"
|
||||
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 := "../invalid_schemas/invalid_compile_schema/"
|
||||
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) {
|
||||
schemaDir := "../schema/ondc_trv10_2.0.0/"
|
||||
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 := "../test/payload.json"
|
||||
payloadData, err := ioutil.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: %v", err)
|
||||
} else {
|
||||
fmt.Println("Validation succeeded.")
|
||||
}
|
||||
}
|
||||
|
||||
func TestInValidateData(t *testing.T) {
|
||||
schemaDir := "../schema/ondc_trv10_2.0.0/"
|
||||
|
||||
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")
|
||||
}
|
||||
invalidPayloadData := []byte(`"invalid": "data"}`)
|
||||
err = validator.Validate(context.Background(), invalidPayloadData)
|
||||
if err != nil {
|
||||
t.Errorf("Validation failed: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInValidateUnmarshalData(t *testing.T) {
|
||||
schemaDir := "../schema/ondc_trv10_2.0.0/"
|
||||
|
||||
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")
|
||||
}
|
||||
invalidPayloadData := []byte(`{"invalid": "data`)
|
||||
err = validator.Validate(context.Background(), invalidPayloadData)
|
||||
if err != nil {
|
||||
t.Errorf("Error while unmarshaling the data: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetProvider(t *testing.T) {
|
||||
expected := providerInstance
|
||||
actual := GetProvider()
|
||||
|
||||
if actual != expected {
|
||||
t.Fatalf("expected %v, got %v", expected, actual)
|
||||
} else {
|
||||
t.Logf("GetProvider returned the expected providerInstance")
|
||||
}
|
||||
}
|
||||
Binary file not shown.
Reference in New Issue
Block a user