---
policy: /ce/unit-test/tag-value
logic: /ce/unit-test/tag-value/unit-test.logic.yaml
executionTime: 2025-04-24T23:48:08.49868926Z
rows:
  - id: Test-1
    match: true
    status:
      expected: COMPLIANT
      actual: COMPLIANT
    conditionIndex:
      expected: 199
      actual: 199
    conditionText:
      expected: "unitTest(Test-1): CA10__tagsJson__c.getExact(bytes('test-tag1'))\
        \ == bytes('testv')"
      actual: "unitTest(Test-1): CA10__tagsJson__c.getExact(bytes('test-tag1')) ==\
        \ bytes('testv')"
    runtimeError: {}
  - id: Test-2
    match: true
    status:
      expected: COMPLIANT
      actual: COMPLIANT
    conditionIndex:
      expected: 299
      actual: 299
    conditionText:
      expected: "unitTest(Test-2): CA10__tagsJson__c.getInexact('testtag1') == 'testv'"
      actual: "unitTest(Test-2): CA10__tagsJson__c.getInexact('testtag1') == 'testv'"
    runtimeError: {}
  - id: Test-3
    match: true
    status:
      expected: COMPLIANT
      actual: COMPLIANT
    conditionIndex:
      expected: 399
      actual: 399
    conditionText:
      expected: "unitTest(Test-3): CA10__tagsJson__c.getInexact('testtag1') == 'testv\
        \ '"
      actual: "unitTest(Test-3): CA10__tagsJson__c.getInexact('testtag1') == 'testv\
        \ '"
    runtimeError: {}
usedFiles:
  - path: /ce/unit-test/tag-value/policy.yaml
    md5Hash: F8CFEFE6CE0A5E420C8BC21EE61C1BF5
    content: |-
      ---
      names:
        full: "TAG_VALUE Unit Tests"
        contextual: "TAG_VALUE"
      description: ""
  - path: /ce/unit-test/tag-value/unit-test.logic.yaml
    md5Hash: E5FC2D4D93BE3DF9D3AEB6BF4EE0B567
    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:
                    TAG_VALUE_BYTES:
                      name:
                        BYTES: test-tag1
                  right:
                    BYTES: testv
        - status: COMPLIANT
          currentStateMessage: Test-2
          check:
            UNIT_TEST:
              id: Test-2
              arg:
                IS_EQUAL:
                  left:
                    TAG_VALUE_TEXT:
                      name:
                        TEXT: testtag1
                  right:
                    TEXT: testv
        - status: COMPLIANT
          currentStateMessage: Test-3
          check:
            UNIT_TEST:
              id: Test-3
              arg:
                IS_EQUAL:
                  left:
                    TAG_VALUE_TEXT:
                      name:
                        TEXT: testtag1
                  right:
                    TEXT: 'testv '
      otherwise:
        status: INCOMPLIANT
        currentStateMessage: Test Failed
        remediationMessage: Developer Fix Required
  - path: /ce/unit-test/tag-value/test-data.json
    md5Hash: 3DCAC5A025B9187C8543D246F8E0B08B
    content: |-
      [
        {
          "expectedResult": {
            "status": "COMPLIANT",
            "conditionIndex": "199",
            "conditionText": "unitTest(Test-1): CA10__tagsJson__c.getExact(bytes('test-tag1')) == bytes('testv')",
            "runtimeError": null
          },
          "context": {
            "snapshotTime": "2025-03-26T11:34:49Z"
          },
          "Id": "Test-1",
          "Name": "Test-1",
          "CA10__tagsJson__c": "{\"test-tag1\":\"testv\"}",
          "CA10__disappearanceTime__c": null
        },
        {
          "expectedResult": {
            "status": "COMPLIANT",
            "conditionIndex": "299",
            "conditionText": "unitTest(Test-2): CA10__tagsJson__c.getInexact('testtag1') == 'testv'",
            "runtimeError": null
          },
          "context": {
            "snapshotTime": "2025-03-26T11:34:49Z"
          },
          "Id": "Test-2",
          "Name": "Test-2",
          "CA10__tagsJson__c": "{\"test-tag1\":\"testv\"}",
          "CA10__disappearanceTime__c": null
        },
        {
          "expectedResult": {
            "status": "COMPLIANT",
            "conditionIndex": "399",
            "conditionText": "unitTest(Test-3): CA10__tagsJson__c.getInexact('testtag1') == 'testv '",
            "runtimeError": null
          },
          "context": {
            "snapshotTime": "2025-03-26T11:34:49Z"
          },
          "Id": "Test-3",
          "Name": "Test-3",
          "CA10__tagsJson__c": "{\"test-tag1\":\"testv\"}",
          "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): CA10__tagsJson__c.getExact(bytes('test-tag1')) == bytes('testv')",
      "runtimeError" : null
    }
  }, {
    "Id" : "Test-2",
    "expectedResult" : {
      "status" : "COMPLIANT",
      "conditionIndex" : "299",
      "conditionText" : "unitTest(Test-2): CA10__tagsJson__c.getInexact('testtag1') == 'testv'",
      "runtimeError" : null
    }
  }, {
    "Id" : "Test-3",
    "expectedResult" : {
      "status" : "COMPLIANT",
      "conditionIndex" : "399",
      "conditionText" : "unitTest(Test-3): CA10__tagsJson__c.getInexact('testtag1') == 'testv '",
      "runtimeError" : null
    }
  } ];
  """;

  CREATE TEMP FUNCTION mock_CA10__CaAwsInstance__c()
  RETURNS ARRAY<STRUCT<
      Id STRING,
      CA10__disappearanceTime__c TIMESTAMP,
      CA10__tagsJson__c STRING,
      context STRUCT<
          snapshotTime TIMESTAMP
      >
  >>
  DETERMINISTIC
  LANGUAGE js
  AS r"""
    return [ {
    "context" : {
      "snapshotTime" : new Date("2025-03-26T11:34:49Z")
    },
    "Id" : "Test-1",
    "CA10__tagsJson__c" : "{\"test-tag1\":\"testv\"}"
  }, {
    "context" : {
      "snapshotTime" : new Date("2025-03-26T11:34:49Z")
    },
    "Id" : "Test-2",
    "CA10__tagsJson__c" : "{\"test-tag1\":\"testv\"}"
  }, {
    "context" : {
      "snapshotTime" : new Date("2025-03-26T11:34:49Z")
    },
    "Id" : "Test-3",
    "CA10__tagsJson__c" : "{\"test-tag1\":\"testv\"}"
  } ];
  """;

  CREATE TEMP FUNCTION process_CA10__CaAwsInstance__c(
      obj STRUCT<
          Id STRING,
          CA10__disappearanceTime__c TIMESTAMP,
          CA10__tagsJson__c STRING
      >,
      snapshotTime TIMESTAMP
  )
  RETURNS STRUCT<status STRING, conditionIndex DECIMAL, conditionText STRING, currentStateMessage STRING, currentStateReferences STRING, remediation STRING, runtimeError STRING>
  DETERMINISTIC
  LANGUAGE js
  AS r"""
      var TagsLib = new function () {
          this.inexact = function(tag) {
              return tag == null ? null : tag.replace(/[^a-zA-Z0-9]/g, '').toLowerCase();
          }
          this.tagExists = function(tagName, exact, tagsJson) {
              var checkTag = exact ? tagName : this.inexact(tagName);
              var parsed = JSON.parse(tagsJson);
              var exists = false;
              for (var parsedTagName in parsed) {
                  var currentTagName = exact ? parsedTagName : this.inexact(parsedTagName);
                  if (currentTagName === checkTag) {
                      exists = true;
                      break;
                  }
              }
              return exists;
          }
          this.tagValue = function(tagName, exact, tagsJson) {
              var checkTag = exact ? tagName : this.inexact(tagName);
              var parsed = JSON.parse(tagsJson);
              var value = '';
              for (var parsedTagName in parsed) {
                  var currentTagName = exact ? parsedTagName : this.inexact(parsedTagName);
                  if (currentTagName === checkTag) {
                      value = parsed[parsedTagName];
                      break;
                  }
              }
              return value;
          }
      }();
      var TextLib = new function () {
          this.normalize = function(arg) {
              return arg == null ? '' : arg.replace(/\s+/g, ' ').trim().toLowerCase();
          };
          this.isEmpty = function(arg) {
              return this.normalize(arg) == '';
          };
          this.isNotEmpty = function(arg) {
              return this.normalize(arg) != '';
          };
          this.equal = function(left, right) {
              return this.normalize(left) == this.normalize(right);
          };
          this.notEqual = function(left, right) {
              return this.normalize(left) != this.normalize(right);
          };
          this.startsWith = function(arg, substring) {
              return this.normalize(arg).startsWith(this.normalize(substring));
          };
          this.endsWith = function(arg, substring) {
              return this.normalize(arg).endsWith(this.normalize(substring));
          };
          this.contains = function(arg, substring) {
              return this.normalize(arg).includes(this.normalize(substring));
          };
      }();
      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]
      function bytesEqual2() {
          var equalLeft3 = TagsLib.tagValue('test-tag1', true, obj.CA10__tagsJson__c);
          return (equalLeft3 == null ? '' : equalLeft3) == ('testv' == null ? '' : 'testv');
      }
      try {
          if (obj.Id == 'Test-1' && bytesEqual2()) {
              return {status: 'COMPLIANT', conditionIndex: 199, conditionText: "unitTest(Test-1): CA10__tagsJson__c.getExact(bytes('test-tag1')) == bytes('testv')", currentStateMessage: "Test-1", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null};
          }
      } catch (err) {
          if (err.cause && err.cause.status) { return err.cause; } else { throw err; }
      }
      // condition[2], conditionIndex:[200..299]
      if (obj.Id == 'Test-2' && TextLib.equal(TagsLib.tagValue('testtag1', false, obj.CA10__tagsJson__c), 'testv')) {
          return {status: 'COMPLIANT', conditionIndex: 299, conditionText: "unitTest(Test-2): CA10__tagsJson__c.getInexact('testtag1') == 'testv'", currentStateMessage: "Test-2", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null};
      }
      // condition[3], conditionIndex:[300..399]
      if (obj.Id == 'Test-3' && TextLib.equal(TagsLib.tagValue('testtag1', false, obj.CA10__tagsJson__c), 'testv ')) {
          return {status: 'COMPLIANT', conditionIndex: 399, conditionText: "unitTest(Test-3): CA10__tagsJson__c.getInexact('testtag1') == 'testv '", currentStateMessage: "Test-3", currentStateReferences: references1.join('\n'), remediation: null, runtimeError: null};
      }
      return {status: 'INCOMPLIANT', conditionIndex: 400, 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,
          sObject.CA10__tagsJson__c AS CA10__tagsJson__c,
          process_CA10__CaAwsInstance__c(
              STRUCT(
                  sObject.Id AS Id,
                  sObject.CA10__disappearanceTime__c AS CA10__disappearanceTime__c,
                  sObject.CA10__tagsJson__c AS CA10__tagsJson__c
              ),
              sObject.context.snapshotTime
          ) as result
      FROM UNNEST(mock_CA10__CaAwsInstance__c()) AS sObject
  ) sObject ON sObject.Id = expectedResult.Id;
