adobe / aem-core-wcm-components

Standardized components to build websites with AEM.
https://docs.adobe.com/content/help/en/experience-manager-core-components/using/introduction.html
Apache License 2.0
737 stars 745 forks source link

[PageImpl] Non-deterministic behavior or `HashSet` might fail tests in `PageImplTest` #2612

Closed SaaiVenkat closed 4 days ago

SaaiVenkat commented 11 months ago

Bug Report

Current Behavior

Compile the module byte-buddy-dep

mvn clean install -pl bundles/core -am -DskipTests

(Optional) Run the unit test

mvn -pl bundles/core test -Dtest=

Run the unit test using NonDex

mvn -pl bundles/core edu.illinois:nondex-maven-plugin:2.1.1:nondex -Dtest=

- Following the above steps for the mentioned tests will produce the following result:

[INFO] Results: [INFO] [ERROR] Failures: [ERROR] PageImplTest.testPage:109->testPage:142 \ expected: <{"id":"page-bb590de134","designPath":"/etc/designs/mysite","title":"Templated Page","description":"Description","brandSlug":"Brand Slug","lastModifiedDate":1453282416000,"templateName":"product-page","appResourcesPath":"/core/etc.clientlibs/wcm/core/page/clientlibs/favicon/resources","htmlPageItems":[{"element":"link","location":"header","attributes":{"href":"/_theme/theme.css","rel":"preload","as":"style"}},{"element":"script","location":"footer","attributes":{"async":true,"defer":false,"src":"/_theme/theme.js"}},{"element":"meta","location":"header","attributes":{"charset":"UTF-8"}}],"cssClassNames":"class1 class2","language":"en-GB",":itemsOrder":["root"],":items":{"root":{":itemsOrder":["text"],":items":{"text":{"id":"text-41bcd02e64","text":"

This is a test/p>","richText":true,":type":"core/wcm/components/text/v2/text","dataLayer":{"text-41bcd02e64":{"@type":"core/wcm/components/text/v2/text","repo:modifyDate":"2016-01-18T15:59:43Z","xdm:text":"<pThis is a test

"}}}},":type":"wcm/foundation/components/responsivegrid"}},":type":"core/wcm/components/page/v2/page","dataLayer":{"page-bb590de134":{"@type":"core/wcm/components/page/v2/page","repo:modifyDate":"2016-01-20T09:33:36Z","dc:title":"Templated Page","dc:description":"Description","xdm:template":"/conf/coretest/settings/wcm/templates/product-page","xdm:language":"en-GB","xdm:tags":["three","one","two"],"repo:path":"/core/content/page/templated-page.html"}}}> \ but was: <{"id":"page-bb590de134","lastModifiedDate":1453282416000,"brandSlug":"Brand Slug","description":"Description","title":"Templated Page","designPath":"/etc/designs/mysite","templateName":"product-page","htmlPageItems":[{"attributes":{"href":"/_theme/theme.css","rel":"preload","as":"style"},"element":"link","location":"header"},{"attributes":{"async":true,"defer":false,"src":"/_theme/theme.js"},"element":"script","location":"footer"},{"attributes":{"charset":"UTF-8"},"element":"meta","location":"header"}],"appResourcesPath":"/core/etc.clientlibs/wcm/core/page/clientlibs/favicon/resources","cssClassNames":"class1 class2","language":"en-GB",":items":{"root":{":itemsOrder":["text"],":items":{"text":{"id":"text-41bcd02e64","text":"

This is a test/p>","richText":true,":type":"core/wcm/components/text/v2/text","dataLayer":{"text-41bcd02e64":{"repo:modifyDate":"2016-01-18T15:59:43Z","@type":"core/wcm/components/text/v2/text","xdm:text":"<pThis is a test

"}}}},":type":"wcm/foundation/components/responsivegrid"}},":itemsOrder":["root"],":type":"core/wcm/components/page/v2/page","dataLayer":{"page-bb590de134":{"repo:modifyDate":"2016-01-20T09:33:36Z","dc:title":"Templated Page","@type":"core/wcm/components/page/v2/page","dc:description":"Description","xdm:language":"en-GB","xdm:template":"/conf/coretest/settings/wcm/templates/product-page","xdm:tags":["one","three","two"],"repo:path":"/core/content/page/templated-page.html"}}}> [INFO] [ERROR] Tests run: 1, Failures: 1, Errors: 0, Skipped: 0 [INFO]

- Filtering out the tags from the above log

[INFO] Results: [INFO] [ERROR] Failures: [ERROR] PageImplTest.testPage:109->testPage:142 expected: ..."xdm:tags":["three","one","two"]... but was: ..."xdm:tags":["one","three","two"]... [INFO] [ERROR] Tests run: 1, Failures: 1, Errors: 0, Skipped: 0 [INFO]


**Expected behavior/code**
- The ordering of the tags should have been maintain in a specific order for all runs 
- The tests should have passed successfully when run through `NonDex`

**Environment**
- Core Components version: `2.23.5-SNAPSHOT`
- Java version: `Java(TM) SE Runtime Environment (build 1.8.0_381-b09)`

**Possible Solution**
<!--- If you have suggestions for the bug fix. Please also consider creating a Pull Request 🤘. -->
- Even though the order of key value pair could change in the exported JSON, the order of an array needs to be maintained. So, changing the test function to compare based on the presence of elements in an array (like a Set) is incorrect.
- 1st Possible Solution
  - Change the implementation of the dependency `MockTagManager` to use `LinkedHashSet` instead of `HashSet`
  - This ensures that the order of tags are maintained for all the runs.
  - However, this solution requires changing code out of the project scope.

+/- io.wcm.testing.mock.aem.MockTagManager.java:380

      "xdm:tags": [
-       "three",
-       "one",
        "two"
      ]

      "xdm:tags": [
+       "one",
+       "three",
        "two"
      ]

Additional context / Screenshots