|
| 1 | +// mpr_test.go |
| 2 | +package mpr |
| 3 | + |
| 4 | +import ( |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + "strings" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "gopkg.in/yaml.v3" |
| 12 | +) |
| 13 | + |
| 14 | +// TestAdd tests the Add function to ensure it returns correct results. |
| 15 | +func TestMPRMetadata(t *testing.T) { |
| 16 | + t.Run("single-mpr", func(t *testing.T) { |
| 17 | + if err := exportMetadata("./../resources/app-mpr-v1", "./../tmp", nil); err != nil { |
| 18 | + t.Errorf("Failed to export metadata from MPR file") |
| 19 | + } |
| 20 | + |
| 21 | + // open metadata file |
| 22 | + metadataFile, err := os.ReadFile("./../tmp/Metadata.yaml") |
| 23 | + if err != nil { |
| 24 | + t.Errorf("Failed to read metadata file") |
| 25 | + } |
| 26 | + // read metadata file |
| 27 | + var metadataObj MxMetadata |
| 28 | + var node yaml.Node |
| 29 | + if err := yaml.Unmarshal(metadataFile, &node); err != nil { |
| 30 | + t.Errorf("Failed to unmarshal metadata file: %v", err) |
| 31 | + } |
| 32 | + if err := node.Decode(&metadataObj); err != nil { |
| 33 | + t.Errorf("Failed to decode metadata file: %v", err) |
| 34 | + } |
| 35 | + // check metadata |
| 36 | + expectedProductVersion := "10.18.3.58900" |
| 37 | + if metadataObj.ProductVersion != expectedProductVersion { |
| 38 | + t.Errorf("ProductVersion is incorrect. Expected: %s, Got: %s", expectedProductVersion, metadataObj.ProductVersion) |
| 39 | + } |
| 40 | + }) |
| 41 | +} |
| 42 | + |
| 43 | +func TestMPRUnits(t *testing.T) { |
| 44 | + t.Run("single-mpr", func(t *testing.T) { |
| 45 | + if err := exportUnits("./../resources/app-mpr-v1", "./../tmp", false, "basic"); err != nil { |
| 46 | + t.Errorf("Failed to export units from MPR file") |
| 47 | + } |
| 48 | + }) |
| 49 | +} |
| 50 | + |
| 51 | +func TestIDAttributesExclusion(t *testing.T) { |
| 52 | + t.Run("verify-id-attributes-excluded", func(t *testing.T) { |
| 53 | + // Export units with ID attributes excluded |
| 54 | + if err := exportUnits("./../resources/app-mpr-v1", "./../tmp", false, "basic"); err != nil { |
| 55 | + t.Errorf("Failed to export units from MPR file: %v", err) |
| 56 | + return |
| 57 | + } |
| 58 | + |
| 59 | + // Read a sample unit file |
| 60 | + files, err := os.ReadDir("./../tmp/MyFirstModule") |
| 61 | + if err != nil { |
| 62 | + t.Errorf("Failed to read directory: %v", err) |
| 63 | + return |
| 64 | + } |
| 65 | + |
| 66 | + // Find a file to test |
| 67 | + var filePath string |
| 68 | + for _, file := range files { |
| 69 | + if !file.IsDir() && strings.HasSuffix(file.Name(), ".yaml") { |
| 70 | + filePath = filepath.Join("./../tmp/MyFirstModule", file.Name()) |
| 71 | + break |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + if filePath == "" { |
| 76 | + t.Errorf("No unit files found to test") |
| 77 | + return |
| 78 | + } |
| 79 | + |
| 80 | + // Read the file content |
| 81 | + content, err := os.ReadFile(filePath) |
| 82 | + if err != nil { |
| 83 | + t.Errorf("Failed to read file %s: %v", filePath, err) |
| 84 | + return |
| 85 | + } |
| 86 | + |
| 87 | + // Check that ID attributes are excluded |
| 88 | + contentStr := string(content) |
| 89 | + if strings.Contains(contentStr, "\"ID\":") || strings.Contains(contentStr, "\"$ID\":") { |
| 90 | + t.Errorf("ID attributes were not excluded from unit document: %s", filePath) |
| 91 | + } |
| 92 | + |
| 93 | + // Also check for other ignored attributes |
| 94 | + for _, attr := range ignoredAttributes { |
| 95 | + if strings.Contains(contentStr, fmt.Sprintf("\"%s\":", attr)) { |
| 96 | + t.Errorf("Ignored attribute '%s' was not excluded from unit document: %s", attr, filePath) |
| 97 | + } |
| 98 | + } |
| 99 | + }) |
| 100 | +} |
0 commit comments