---
policy: /ce/unit-test/is-equal/json
logic: /ce/unit-test/is-equal/json/unit-test.logic.yaml
executionTime: 2025-04-24T23:48:01.124493056Z
rows:
  - id: Test-1
    match: true
    status:
      expected: COMPLIANT
      actual: COMPLIANT
    conditionIndex:
      expected: 199
      actual: 199
    conditionText:
      expected: "unitTest(Test-1): {\"name\":\"John\",\"age\":30} == {\"age\":30,\"\
        name\":\"John\"}"
      actual: "unitTest(Test-1): {\"name\":\"John\",\"age\":30} == {\"age\":30,\"\
        name\":\"John\"}"
    runtimeError: {}
  - id: Test-2
    match: true
    status:
      expected: COMPLIANT
      actual: COMPLIANT
    conditionIndex:
      expected: 299
      actual: 299
    conditionText:
      expected: "unitTest(Test-2): {\"names\":[\"John\",\"Jane\"]} != {\"names\":[\"\
        Jane\",\"John\"]}"
      actual: "unitTest(Test-2): {\"names\":[\"John\",\"Jane\"]} != {\"names\":[\"\
        Jane\",\"John\"]}"
    runtimeError: {}
  - id: Test-3
    match: true
    status:
      expected: COMPLIANT
      actual: COMPLIANT
    conditionIndex:
      expected: 399
      actual: 399
    conditionText:
      expected: "unitTest(Test-3): {\"age\":30} != {\"age\":\"30\"}"
      actual: "unitTest(Test-3): {\"age\":30} != {\"age\":\"30\"}"
    runtimeError: {}
  - id: Test-4
    match: true
    status:
      expected: COMPLIANT
      actual: COMPLIANT
    conditionIndex:
      expected: 499
      actual: 499
    conditionText:
      expected: "unitTest(Test-4): {\"name\":\"John\",\"age\":null} == {\"name\":\"\
        John\"}"
      actual: "unitTest(Test-4): {\"name\":\"John\",\"age\":null} == {\"name\":\"\
        John\"}"
    runtimeError: {}
  - id: Test-5
    match: true
    status:
      expected: COMPLIANT
      actual: COMPLIANT
    conditionIndex:
      expected: 599
      actual: 599
    conditionText:
      expected: "unitTest(Test-5): {\"name\":null,\"age\":null} == {}"
      actual: "unitTest(Test-5): {\"name\":null,\"age\":null} == {}"
    runtimeError: {}
  - id: Test-6
    match: true
    status:
      expected: COMPLIANT
      actual: COMPLIANT
    conditionIndex:
      expected: 699
      actual: 699
    conditionText:
      expected: "unitTest(Test-6): {} == {}"
      actual: "unitTest(Test-6): {} == {}"
    runtimeError: {}
  - id: Test-7
    match: true
    status:
      expected: COMPLIANT
      actual: COMPLIANT
    conditionIndex:
      expected: 799
      actual: 799
    conditionText:
      expected: "unitTest(Test-7): {} == null"
      actual: "unitTest(Test-7): {} == null"
    runtimeError: {}
  - id: Test-8
    match: true
    status:
      expected: COMPLIANT
      actual: COMPLIANT
    conditionIndex:
      expected: 899
      actual: 899
    conditionText:
      expected: "unitTest(Test-8): null == null"
      actual: "unitTest(Test-8): null == null"
    runtimeError: {}
usedFiles:
  - path: /ce/unit-test/is-equal/json/policy.yaml
    md5Hash: 60A29140BE1D8E4FAE271DF4582FCE33
    content: |
      ---
      names:
        full: "IS_EQUAL / NOT_EQUAL Unit Tests: JsonType"
        contextual: "JsonType"
      description: ""
  - path: /ce/unit-test/is-equal/json/unit-test.logic.yaml
    md5Hash: 2E9C491CB2E08924D5F97A1DF89DABBA
    content: |-
      ---
      inputType: CA10__CaAwsInstance__c
      testData:
        - file: test-data.json
      conditions:
        - status: COMPLIANT
          currentStateMessage: Test-1
          check:
            UNIT_TEST:
              id: Test-1
              arg:
                IS_EQUAL:
                  left:
                    JSON: { name: "John", age: 30 }
                  right:
                    JSON: { age: 30, name: "John" }
        - status: COMPLIANT
          currentStateMessage: Test-2
          check:
            UNIT_TEST:
              id: Test-2
              arg:
                NOT_EQUAL:
                  left:
                    JSON: { names: ["John", "Jane"] }
                  right:
                    JSON: { names: ["Jane", "John"] }
        - status: COMPLIANT
          currentStateMessage: Test-3
          check:
            UNIT_TEST:
              id: Test-3
              arg:
                NOT_EQUAL:
                  left:
                    JSON: { age: 30 }
                  right:
                    JSON: { age: "30" }
        - status: COMPLIANT
          currentStateMessage: Test-4
          check:
            UNIT_TEST:
              id: Test-4
              arg:
                IS_EQUAL:
                  left:
                    JSON: { name: "John", age: null }
                  right:
                    JSON: { name: "John" }
        - status: COMPLIANT
          currentStateMessage: Test-5
          check:
            UNIT_TEST:
              id: Test-5
              arg:
                IS_EQUAL:
                  left:
                    JSON: { name: null, age: null}
                  right:
                    JSON: {}
        - status: COMPLIANT
          currentStateMessage: Test-6
          check:
            UNIT_TEST:
              id: Test-6
              arg:
                IS_EQUAL:
                  left:
                    JSON: {}
                  right:
                    JSON: {}
        - status: COMPLIANT
          currentStateMessage: Test-7
          check:
            UNIT_TEST:
              id: Test-7
              arg:
                IS_EQUAL:
                  left:
                    JSON: {}
                  right:
                    UNIT_TEST_NULL:
                      returnType: JSON
        - status: COMPLIANT
          currentStateMessage: Test-8
          check:
            UNIT_TEST:
              id: Test-8
              arg:
                IS_EQUAL:
                  left:
                    UNIT_TEST_NULL:
                      returnType: JSON
                  right:
                    UNIT_TEST_NULL:
                      returnType: JSON
      otherwise:
        status: INCOMPLIANT
        currentStateMessage: Test Failed
        remediationMessage: Developer Fix Required
  - path: /ce/unit-test/is-equal/json/test-data.json
    md5Hash: 809E246F998423827ACDBD554B49CF4E
    content: |-
      [
        {
          "expectedResult": {
            "status": "COMPLIANT",
            "conditionIndex": "199",
            "conditionText": "unitTest(Test-1): {\"name\":\"John\",\"age\":30} == {\"age\":30,\"name\":\"John\"}",
            "runtimeError": null
          },
          "context": {
            "snapshotTime": "2024-01-01T00:00:00Z"
          },
          "Id": "Test-1",
          "Name": "Test-1",
          "CA10__disappearanceTime__c": null
        },
        {
          "expectedResult": {
            "status": "COMPLIANT",
            "conditionIndex": "299",
            "conditionText": "unitTest(Test-2): {\"names\":[\"John\",\"Jane\"]} != {\"names\":[\"Jane\",\"John\"]}",
            "runtimeError": null
          },
          "context": {
            "snapshotTime": "2024-01-01T00:00:00Z"
          },
          "Id": "Test-2",
          "Name": "Test-2",
          "CA10__disappearanceTime__c": null
        },
        {
          "expectedResult": {
            "status": "COMPLIANT",
            "conditionIndex": "399",
            "conditionText": "unitTest(Test-3): {\"age\":30} != {\"age\":\"30\"}",
            "runtimeError": null
          },
          "context": {
            "snapshotTime": "2024-01-01T00:00:00Z"
          },
          "Id": "Test-3",
          "Name": "Test-3",
          "CA10__disappearanceTime__c": null
        },
        {
          "expectedResult": {
            "status": "COMPLIANT",
            "conditionIndex": "499",
            "conditionText": "unitTest(Test-4): {\"name\":\"John\",\"age\":null} == {\"name\":\"John\"}",
            "runtimeError": null
          },
          "context": {
            "snapshotTime": "2024-01-01T00:00:00Z"
          },
          "Id": "Test-4",
          "Name": "Test-4",
          "CA10__disappearanceTime__c": null
        },
        {
          "expectedResult": {
            "status": "COMPLIANT",
            "conditionIndex": "599",
            "conditionText": "unitTest(Test-5): {\"name\":null,\"age\":null} == {}",
            "runtimeError": null
          },
          "context": {
            "snapshotTime": "2024-01-01T00:00:00Z"
          },
          "Id": "Test-5",
          "Name": "Test-5",
          "CA10__disappearanceTime__c": null
        },
        {
          "expectedResult": {
            "status": "COMPLIANT",
            "conditionIndex": "699",
            "conditionText": "unitTest(Test-6): {} == {}",
            "runtimeError": null
          },
          "context": {
            "snapshotTime": "2024-01-01T00:00:00Z"
          },
          "Id": "Test-6",
          "Name": "Test-6",
          "CA10__disappearanceTime__c": null
        },
        {
          "expectedResult": {
            "status": "COMPLIANT",
            "conditionIndex": "799",
            "conditionText": "unitTest(Test-7): {} == null",
            "runtimeError": null
          },
          "context": {
            "snapshotTime": "2024-01-01T00:00:00Z"
          },
          "Id": "Test-7",
          "Name": "Test-7",
          "CA10__disappearanceTime__c": null
        },
        {
          "expectedResult": {
            "status": "COMPLIANT",
            "conditionIndex": "899",
            "conditionText": "unitTest(Test-8): null == null",
            "runtimeError": null
          },
          "context": {
            "snapshotTime": "2024-01-01T00:00:00Z"
          },
          "Id": "Test-8",
          "Name": "Test-8",
          "CA10__disappearanceTime__c": null
        }
      ]
script: |-
  CREATE TEMP FUNCTION mock_ExpectedResult()
  RETURNS ARRAY<STRUCT<
          Id STRING,
          expectedResult STRUCT<status STRING, conditionIndex DECIMAL, conditionText STRING, runtimeError STRING>
      >>
  DETERMINISTIC
  LANGUAGE js
  AS r"""
    return [ {
    "Id" : "Test-1",
    "expectedResult" : {
      "status" : "COMPLIANT",
      "conditionIndex" : "199",
      "conditionText" : "unitTest(Test-1): {\"name\":\"John\",\"age\":30} == {\"age\":30,\"name\":\"John\"}",
      "runtimeError" : null
    }
  }, {
    "Id" : "Test-2",
    "expectedResult" : {
      "status" : "COMPLIANT",
      "conditionIndex" : "299",
      "conditionText" : "unitTest(Test-2): {\"names\":[\"John\",\"Jane\"]} != {\"names\":[\"Jane\",\"John\"]}",
      "runtimeError" : null
    }
  }, {
    "Id" : "Test-3",
    "expectedResult" : {
      "status" : "COMPLIANT",
      "conditionIndex" : "399",
      "conditionText" : "unitTest(Test-3): {\"age\":30} != {\"age\":\"30\"}",
      "runtimeError" : null
    }
  }, {
    "Id" : "Test-4",
    "expectedResult" : {
      "status" : "COMPLIANT",
      "conditionIndex" : "499",
      "conditionText" : "unitTest(Test-4): {\"name\":\"John\",\"age\":null} == {\"name\":\"John\"}",
      "runtimeError" : null
    }
  }, {
    "Id" : "Test-5",
    "expectedResult" : {
      "status" : "COMPLIANT",
      "conditionIndex" : "599",
      "conditionText" : "unitTest(Test-5): {\"name\":null,\"age\":null} == {}",
      "runtimeError" : null
    }
  }, {
    "Id" : "Test-6",
    "expectedResult" : {
      "status" : "COMPLIANT",
      "conditionIndex" : "699",
      "conditionText" : "unitTest(Test-6): {} == {}",
      "runtimeError" : null
    }
  }, {
    "Id" : "Test-7",
    "expectedResult" : {
      "status" : "COMPLIANT",
      "conditionIndex" : "799",
      "conditionText" : "unitTest(Test-7): {} == null",
      "runtimeError" : null
    }
  }, {
    "Id" : "Test-8",
    "expectedResult" : {
      "status" : "COMPLIANT",
      "conditionIndex" : "899",
      "conditionText" : "unitTest(Test-8): null == null",
      "runtimeError" : null
    }
  } ];
  """;

  CREATE TEMP FUNCTION mock_CA10__CaAwsInstance__c()
  RETURNS ARRAY<STRUCT<
      Id STRING,
      CA10__disappearanceTime__c TIMESTAMP,
      context STRUCT<
          snapshotTime TIMESTAMP
      >
  >>
  DETERMINISTIC
  LANGUAGE js
  AS r"""
    return [ {
    "context" : {
      "snapshotTime" : new Date("2024-01-01T00:00:00Z")
    },
    "Id" : "Test-1"
  }, {
    "context" : {
      "snapshotTime" : new Date("2024-01-01T00:00:00Z")
    },
    "Id" : "Test-2"
  }, {
    "context" : {
      "snapshotTime" : new Date("2024-01-01T00:00:00Z")
    },
    "Id" : "Test-3"
  }, {
    "context" : {
      "snapshotTime" : new Date("2024-01-01T00:00:00Z")
    },
    "Id" : "Test-4"
  }, {
    "context" : {
      "snapshotTime" : new Date("2024-01-01T00:00:00Z")
    },
    "Id" : "Test-5"
  }, {
    "context" : {
      "snapshotTime" : new Date("2024-01-01T00:00:00Z")
    },
    "Id" : "Test-6"
  }, {
    "context" : {
      "snapshotTime" : new Date("2024-01-01T00:00:00Z")
    },
    "Id" : "Test-7"
  }, {
    "context" : {
      "snapshotTime" : new Date("2024-01-01T00:00:00Z")
    },
    "Id" : "Test-8"
  } ];
  """;

  CREATE TEMP FUNCTION process_CA10__CaAwsInstance__c(
      obj STRUCT<
          Id STRING,
          CA10__disappearanceTime__c TIMESTAMP
      >,
      snapshotTime TIMESTAMP
  )
  RETURNS STRUCT<status STRING, conditionIndex DECIMAL, conditionText STRING, currentStateMessage STRING, currentStateReferences STRING, remediation STRING, runtimeError STRING>
  DETERMINISTIC
  LANGUAGE js
  AS r"""
      var JsonLib = new function () {
          this.isEmpty = function(arg) {
              return this.equal(arg, {});
          };
          this.isNotEmpty = function(arg) {
              return this.notEqual(arg, {});
          };
          this.equal = function jsonEqual(left, right) {
              function removeNullProperties(obj) {
                if (obj === null || typeof obj !== 'object') {
                  return obj;
                }
                if (Array.isArray(obj)) {
                  return obj.map(removeNullProperties);
                }
                const newObj = {};
                for (const key in obj) {
                  if (obj[key] !== null) {
                    newObj[key] = removeNullProperties(obj[key]);
                  }
                }
                return newObj;
              }
              const replacer = (key, value) => {
                  if (value && typeof value === 'object' && !Array.isArray(value)) {
                      return Object.keys(value).sort().reduce((sorted, k) => {
                          sorted[k] = value[k];
                          return sorted;
                      }, {});
                  }
                  return value;
              };
              return JSON.stringify(removeNullProperties(left == null ? {} : left), replacer) === JSON.stringify(removeNullProperties(right == null ? {} : right), replacer);
          }
          this.notEqual = function(left, right) {
              return !this.equal(left, right);
          };
      }();
      var references1 = [];
      // condition[0], conditionIndex:[0..99]
      references1.push('Deleted From AWS [CA10__disappearanceTime__c]: ' + obj.CA10__disappearanceTime__c);
      if (obj.CA10__disappearanceTime__c != null) {
          return {status: 'DISAPPEARED', conditionIndex: 99, conditionText: "isDisappeared(CA10__disappearanceTime__c)", currentStateMessage: "Object is deleted in the source", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null};
      }
      // condition[1], conditionIndex:[100..199]
      if (obj.Id == 'Test-1' && JsonLib.equal({"name":"John","age":30}, {"age":30,"name":"John"})) {
          return {status: 'COMPLIANT', conditionIndex: 199, conditionText: "unitTest(Test-1): {\"name\":\"John\",\"age\":30} == {\"age\":30,\"name\":\"John\"}", currentStateMessage: "Test-1", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null};
      }
      // condition[2], conditionIndex:[200..299]
      if (obj.Id == 'Test-2' && JsonLib.notEqual({"names":["John","Jane"]}, {"names":["Jane","John"]})) {
          return {status: 'COMPLIANT', conditionIndex: 299, conditionText: "unitTest(Test-2): {\"names\":[\"John\",\"Jane\"]} != {\"names\":[\"Jane\",\"John\"]}", currentStateMessage: "Test-2", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null};
      }
      // condition[3], conditionIndex:[300..399]
      if (obj.Id == 'Test-3' && JsonLib.notEqual({"age":30}, {"age":"30"})) {
          return {status: 'COMPLIANT', conditionIndex: 399, conditionText: "unitTest(Test-3): {\"age\":30} != {\"age\":\"30\"}", currentStateMessage: "Test-3", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null};
      }
      // condition[4], conditionIndex:[400..499]
      if (obj.Id == 'Test-4' && JsonLib.equal({"name":"John","age":null}, {"name":"John"})) {
          return {status: 'COMPLIANT', conditionIndex: 499, conditionText: "unitTest(Test-4): {\"name\":\"John\",\"age\":null} == {\"name\":\"John\"}", currentStateMessage: "Test-4", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null};
      }
      // condition[5], conditionIndex:[500..599]
      if (obj.Id == 'Test-5' && JsonLib.equal({"name":null,"age":null}, {})) {
          return {status: 'COMPLIANT', conditionIndex: 599, conditionText: "unitTest(Test-5): {\"name\":null,\"age\":null} == {}", currentStateMessage: "Test-5", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null};
      }
      // condition[6], conditionIndex:[600..699]
      if (obj.Id == 'Test-6' && JsonLib.equal({}, {})) {
          return {status: 'COMPLIANT', conditionIndex: 699, conditionText: "unitTest(Test-6): {} == {}", currentStateMessage: "Test-6", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null};
      }
      // condition[7], conditionIndex:[700..799]
      if (obj.Id == 'Test-7' && JsonLib.equal({}, null)) {
          return {status: 'COMPLIANT', conditionIndex: 799, conditionText: "unitTest(Test-7): {} == null", currentStateMessage: "Test-7", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null};
      }
      // condition[8], conditionIndex:[800..899]
      if (obj.Id == 'Test-8' && JsonLib.equal(null, null)) {
          return {status: 'COMPLIANT', conditionIndex: 899, conditionText: "unitTest(Test-8): null == null", currentStateMessage: "Test-8", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null};
      }
      return {status: 'INCOMPLIANT', conditionIndex: 900, conditionText: "otherwise", currentStateMessage: "Test Failed", currentStateReferences: references1.join('\n'), remediation: "Developer Fix Required", runtimeError: null};
  """;


  SELECT
    expectedResult.Id as Id,
    IF (
      IFNULL(expectedResult.expectedResult.status, '') = IFNULL(sObject.result.status, '')
      AND IFNULL(expectedResult.expectedResult.conditionIndex, -1) = IFNULL(sObject.result.conditionIndex, -1)
      AND IFNULL(expectedResult.expectedResult.conditionText, '') = IFNULL(sObject.result.conditionText, '')
      AND IFNULL(expectedResult.expectedResult.runtimeError, '') = IFNULL(sObject.result.runtimeError, ''),
      "MATCH",
      "FAIL"
    ) as match,
    expectedResult.expectedResult.status as expectedStatus, sObject.result.status as actualStatus,
    expectedResult.expectedResult.conditionIndex as expectedConditionIndex, sObject.result.conditionIndex as actualConditionIndex,
    expectedResult.expectedResult.conditionText as expectedConditionText, sObject.result.conditionText as actualConditionText,
    expectedResult.expectedResult.runtimeError as expectedRuntimeError, sObject.result.runtimeError as actualRuntimeError
  FROM UNNEST(mock_ExpectedResult()) expectedResult
  LEFT JOIN (
      SELECT
          sObject.Id AS Id,
          sObject.CA10__disappearanceTime__c AS CA10__disappearanceTime__c,
          process_CA10__CaAwsInstance__c(
              STRUCT(
                  sObject.Id AS Id,
                  sObject.CA10__disappearanceTime__c AS CA10__disappearanceTime__c
              ),
              sObject.context.snapshotTime
          ) as result
      FROM UNNEST(mock_CA10__CaAwsInstance__c()) AS sObject
  ) sObject ON sObject.Id = expectedResult.Id;
