Skip to content

Commit

Permalink
added integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Dominik Przybyl committed Oct 3, 2024
1 parent 5abd048 commit 0b0a36d
Show file tree
Hide file tree
Showing 6 changed files with 333 additions and 0 deletions.
213 changes: 213 additions & 0 deletions pkg/content_manager_int_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
//go:build int_test

package pkg_test

import (
"crypto/sha256"
"embed"
"fmt"
"github.com/wttech/aemc/pkg"
"github.com/wttech/aemc/pkg/common/pathx"
"github.com/wttech/aemc/pkg/common/timex"
"github.com/wttech/aemc/pkg/content"
"io/fs"
"os"
"path/filepath"
"strings"
"testing"
)

//go:embed test_cases
var VaultFS embed.FS

func TestPullSimpleDir(t *testing.T) {
testPullContent(t, "test_cases/pull_tests/simple_dir", "/content/mysite", "", nil, "")
}

func TestPullOnePage(t *testing.T) {
testPullContent(t, "test_cases/pull_tests/one_page", "", "/content/mysite/us/.content.xml", nil, "")
}

func testPullContent(t *testing.T, expectedDir string, relDir string, relFile string, filterRoots []string, filterFile string) {
aem := pkg.DefaultAEM()
contentManager := pkg.NewContentManager(aem)
instance := aem.InstanceManager().NewLocalAuthor()
packageManager := instance.PackageManager()

remotePath, err := installTestContent(packageManager)
defer func() {
_ = packageManager.Uninstall(remotePath)
_ = packageManager.Delete(remotePath)
}()
if err != nil {
t.Fatal(err)
}

resultDir, err := pullContent(contentManager, instance, relDir, relFile, filterRoots, filterFile)
defer func() { _ = pathx.DeleteIfExists(resultDir) }()
if err != nil {
t.Fatal(err)
}

if err = compareDirectories(resultDir, expectedDir); err != nil {
t.Errorf("testPullContent(%s, %s, %v, %s) -> %v", relDir, relFile, filterRoots, filterFile, err)
}
}

func pullContent(contentManager *pkg.ContentManager, instance pkg.Instance, relDir string, relFile string, filterRoots []string, filterFile string) (string, error) {
resultDir := pathx.RandomDir(os.TempDir(), "content_pull")
var dir string
if relDir != "" {
dir = filepath.Join(resultDir, content.JCRRoot, relDir)
}
var file string
if relFile != "" {
file = filepath.Join(resultDir, content.JCRRoot, relFile)
}
if dir != "" {
if err := contentManager.PullDir(&instance, dir, true, false, pkg.PackageCreateOpts{
PID: fmt.Sprintf("aemc:content-pull:%s-SNAPSHOT", timex.FileTimestampForNow()),
FilterRoots: determineFilterRoots(dir, file, filterRoots, filterFile),
FilterFile: filterFile,
}); err != nil {
return resultDir, err
}
} else if file != "" {
if err := contentManager.PullFile(&instance, file, true, false, pkg.PackageCreateOpts{
PID: fmt.Sprintf("aemc:content-pull:%s-SNAPSHOT", timex.FileTimestampForNow()),
FilterRoots: determineFilterRoots(dir, file, filterRoots, filterFile),
FilterFile: filterFile,
}); err != nil {
return resultDir, err
}
}
return resultDir, nil
}

func installTestContent(packageManager *pkg.PackageManager) (string, error) {
data, err := VaultFS.ReadFile("test_cases/test-content.zip")
if err != nil {
return "", err
}
pkgFile := pathx.RandomFileName(os.TempDir(), "content_pull", ".zip")
defer func() { _ = pathx.DeleteIfExists(pkgFile) }()
if err = os.WriteFile(pkgFile, data, 0755); err != nil {
return "", err
}
remotePath, err := packageManager.Upload(pkgFile)
if err != nil {
return "", err
}
if err = packageManager.Install(remotePath); err != nil {
return remotePath, err
}
return remotePath, nil
}

func determineFilterRoots(dir string, file string, filterRoots []string, filterFile string) []string {
if len(filterRoots) > 0 {
return filterRoots
}
if filterFile != "" {
return nil
}
if dir != "" {
return []string{pkg.DetermineFilterRoot(dir)}
}
if file != "" {
return []string{pkg.DetermineFilterRoot(file)}
}
return nil
}

func determineExcludePatterns(file string) []string {
if file == "" || !strings.HasSuffix(file, content.JCRContentFile) || content.IsPageContentFile(file) {
return nil
}

dir := filepath.Dir(file)
entries, err := os.ReadDir(dir)
if err != nil {
return nil
}

var excludePatterns []string
for _, entry := range entries {
if entry.Name() != content.JCRContentFile {
jcrPath := pkg.DetermineFilterRoot(filepath.Join(dir, entry.Name()))
excludePattern := fmt.Sprintf("%s(/.*)?", jcrPath)
excludePatterns = append(excludePatterns, excludePattern)
}
}
return excludePatterns
}

func compareDirectories(dir1 string, dir2 string) error {
files1 := map[string]string{}
if err := filepath.Walk(dir1, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
relPath, err := filepath.Rel(dir1, path)
if err != nil {
return err
}
data, err := os.ReadFile(path)
if err != nil {
return err
}
files1[relPath] = calculateHashCode(data)
return nil
}); err != nil {
return err
}

files2 := map[string]string{}
if err := fs.WalkDir(VaultFS, dir2, func(path string, entry fs.DirEntry, err error) error {
if err != nil {
return err
}
if entry.IsDir() {
return nil
}
relPath, err := filepath.Rel(dir2, path)
if err != nil {
return err
}
data, err := VaultFS.ReadFile(path)
if err != nil {
return err
}
relPath = strings.ReplaceAll(relPath, "$", "")
files2[relPath] = calculateHashCode(data)
return nil
}); err != nil {
return err
}

for relPath, hashCode := range files1 {
hashCode2, exists := files2[relPath]
if !exists {
return fmt.Errorf("file %s exists in %s but not in %s", relPath, dir1, dir2)
} else if hashCode != hashCode2 {
return fmt.Errorf("file %s has different contents in %s and %s", relPath, dir1, dir2)
}
}

for relPath := range files2 {
if _, exists := files1[relPath]; !exists {
return fmt.Errorf("File %s exists in %s but not in %s\n", relPath, dir2, dir1)
}
}

return nil
}

func calculateHashCode(data []byte) string {
hash := sha256.New()
hash.Write(data)
return fmt.Sprintf("%x", hash.Sum(nil))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<jcr:root xmlns:sling="http://sling.apache.org/jcr/sling/1.0" xmlns:cq="http://www.day.com/jcr/cq/1.0" xmlns:jcr="http://www.jcp.org/jcr/1.0"
jcr:primaryType="cq:Page">
<jcr:content
cq:redirectTarget="/content/mysite/us/en"
cq:template="/conf/mysite/settings/wcm/templates/page-content"
jcr:primaryType="cq:PageContent"
jcr:title="us"
sling:resourceType="mysite/components/page"/>
<en/>
</jcr:root>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<jcr:root xmlns:sling="http://sling.apache.org/jcr/sling/1.0" xmlns:cq="http://www.day.com/jcr/cq/1.0" xmlns:jcr="http://www.jcp.org/jcr/1.0"
jcr:primaryType="cq:Page">
<jcr:content
cq:allowedTemplates="[/conf/mysite/settings/wcm/templates/(?!xf-).*]"
cq:conf="/conf/mysite"
cq:redirectTarget="/content/mysite/us/en"
cq:template="/conf/mysite/settings/wcm/templates/page-content"
jcr:primaryType="cq:PageContent"
jcr:title="My Site"
sling:configRef="/conf/mysite"
sling:redirect="{Boolean}true"
sling:redirectStatus="{Long}302"
sling:resourceType="mysite/components/page"/>
<us/>
</jcr:root>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<jcr:root xmlns:sling="http://sling.apache.org/jcr/sling/1.0" xmlns:cq="http://www.day.com/jcr/cq/1.0" xmlns:jcr="http://www.jcp.org/jcr/1.0"
jcr:primaryType="cq:Page">
<jcr:content
cq:redirectTarget="/content/mysite/us/en"
cq:template="/conf/mysite/settings/wcm/templates/page-content"
jcr:primaryType="cq:PageContent"
jcr:title="us"
sling:resourceType="mysite/components/page"/>
<en/>
</jcr:root>
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?xml version="1.0" encoding="UTF-8"?>
<jcr:root xmlns:sling="http://sling.apache.org/jcr/sling/1.0" xmlns:cq="http://www.day.com/jcr/cq/1.0" xmlns:jcr="http://www.jcp.org/jcr/1.0" xmlns:nt="http://www.jcp.org/jcr/nt/1.0"
jcr:primaryType="cq:Page">
<jcr:content
cq:contextHubPath="/etc/cloudsettings/default/contexthub"
cq:contextHubSegmentsPath="/etc/segmentation/contexthub"
cq:template="/conf/mysite/settings/wcm/templates/page-content"
jcr:primaryType="cq:PageContent"
jcr:title="en"
sling:resourceType="mysite/components/page"
pageTitle="My Site">
<root
jcr:primaryType="nt:unstructured"
sling:resourceType="mysite/components/container"
layout="responsiveGrid">
<container
jcr:primaryType="nt:unstructured"
sling:resourceType="mysite/components/container">
<title
jcr:primaryType="nt:unstructured"
sling:resourceType="mysite/components/title"/>
<container
jcr:primaryType="nt:unstructured"
sling:resourceType="mysite/components/container"
layout="responsiveGrid">
<teaser
jcr:description="&lt;p>Don't stop half way, go for the top!&lt;/p>&#xd;&#xa;"
jcr:primaryType="nt:unstructured"
jcr:title="Epic Journey"
sling:resourceType="mysite/components/teaser"
actionsEnabled="false"
descriptionFromPage="false"
textIsRich="true"
titleFromPage="false">
<cq:responsive jcr:primaryType="nt:unstructured">
<default
jcr:primaryType="nt:unstructured"
offset="0"
width="4"/>
</cq:responsive>
</teaser>
<teaser_copy
jcr:description="&lt;p>Don't stop half way, go for the top!&lt;/p>&#xd;&#xa;"
jcr:primaryType="nt:unstructured"
jcr:title="Epic Journey"
sling:resourceType="mysite/components/teaser"
actionsEnabled="false"
descriptionFromPage="false"
textIsRich="true"
titleFromPage="false">
<cq:responsive jcr:primaryType="nt:unstructured">
<default
jcr:primaryType="nt:unstructured"
offset="0"
width="4"/>
</cq:responsive>
</teaser_copy>
<teaser_copy_1579324932
jcr:description="&lt;p>Don't stop half way, go for the top!&lt;/p>&#xd;&#xa;"
jcr:primaryType="nt:unstructured"
jcr:title="Epic Journey"
sling:resourceType="mysite/components/teaser"
actionsEnabled="false"
descriptionFromPage="false"
textIsRich="true"
titleFromPage="false">
<cq:responsive jcr:primaryType="nt:unstructured">
<default
jcr:primaryType="nt:unstructured"
offset="0"
width="4"/>
</cq:responsive>
</teaser_copy_1579324932>
<helloworld
jcr:primaryType="nt:unstructured"
sling:resourceType="mysite/components/helloworld"
text="lalala :)"/>
</container>
</container>
</root>
</jcr:content>
</jcr:root>
Binary file added pkg/test_cases/test-content.zip
Binary file not shown.

0 comments on commit 0b0a36d

Please sign in to comment.