0

I am writing code in Apache nifi's execute script processor in javascript.

NOTE: NO in-built function or method of javascript like isEmpty(), Object.Entries, or etc doesn't works in nifi's processor. So I have to write very basic code

My code is giving Failure even for success cases.

Requirement is Variable temp has value which needs to be validate with API's response, Now there can be these cases.

  1. temp's value will exist in api response and will be matched so this will be a pass.
  2. temp has data but API response doesnt have any such value so it will be a fail.
  3. temp has data but API response is empty, fail.
  4. temp is empty but API response is not null, pass because temp is empty .we need to validate temp only
  5. Neither temp nor API response has data, pass

Data stored in Attributes

ListData=
{
  "Emp_Id" : 121314,
  "Emp_Loc_Id" : 126,
  "Dept_Name" : "Flowers",
  "Sub_Debt_Name1" : "Lily"
}


apiData = 
{
  "status" : "success",
  "data" : {
    "noMoreWorkFlow" : 0,
    "reason" : {
      "labels" : "",
      "options" : {
        "17" : [ "Rose", "" ] ],
        "12" : [ "Tulips", "", ] ],
        "23" : [ "MaryGold", "" ] ]
      }
    },
    "workflow" : [ ],
    "orgContext" : [ ]
  }
}

Sub_Debt_Name1= Lily

Here, result should be 0 and temp_Status = fail as no matched data is present in apiData

This is what I write

    var InputStreamCallback = Java.type("org.apache.nifi.processor.io.InputStreamCallback")
    var IOUtils = Java.type("org.apache.commons.io.IOUtils");
    var OutputStreamCallback =  Java.type("org.apache.nifi.processor.io.OutputStreamCallback");
    var StandardCharsets = Java.type("java.nio.charset.StandardCharsets");
    var Set = Java.type("java.util.HashSet");
    var Map = Java.type("java.util.HashMap");
    var flowFile = session.get();
    
   if (flowFile != null) {
        var text = ''
        session.read(flowFile,
            new InputStreamCallback(function (inputStream) {
                text = IOUtils.toString(inputStream, StandardCharsets.UTF_8);             
            }));
    var Data ={}
    var api={}
    var temp_Status = ""
    var res=0
    var hasApiData=false;
   
    var Data = JSON.parse(flowFile.getAttribute('ListData'))
    var api= JSON.parse(flowFile.getAttribute('apiData'))
    var x= JSON.stringify(flowFile.getAttribute('Sub_Debt_Name1')) 
    var temp= JSON.parse(x)
     
       for(var key in api.data.reason.options){
         hasApiData= true;
       }
      var dataName = temp;
       if((!(hasApiData)) &&(!(dataName))){
        temp_Status = "fail";   
        } 
       if(hasApiData===true){
       for (var key in api.data.reason.options) {
       if (api.data.reason.options[key][0] === dataName) {
        res = JSON.parse(key);
        temp_Status = "pass";       
        }else{
          temp_Status = "fail"; 
        }
      }
       flowFile = session.putAttribute(flowFile, 'temp_Status ', temp_Status );
            
    }
 }     
     flowFile = session.write(flowFile,
                           new OutputStreamCallback(function(outputStream) {
                                  outputStream.write(JSON.stringify(res).getBytes(StandardCharsets.UTF_8))
                           })
                  );
 
       session.transfer(flowFile, REL_SUCCESS);

My code is giving Fail in temp_Status for success data also.

Any other logic writing this in very basic javascript? without any in-built functions

dracile
  • 59
  • 6

1 Answers1

0

EDIT: Replaced answer based on edits to the question (see the comments for more details)

I believe this is the problematic part of the code:

for (var key in api.data.reason.options) {
   if (api.data.reason.options[key][0] === dataName) {
     res = JSON.parse(key);
     temp_Status = "pass";       
   }else{
     temp_Status = "fail"; 
   }
}

You're iterating over all the keys and setting it to "fail" if it doesn't match. But only one at most will match, the others will set it to "fail". I recommend setting temp_Status to "fail" before the for loop and only setting it to "pass" if the keys match.

mattyb
  • 11,693
  • 15
  • 20
  • I had all that already in my code, My logic is failing that's why I only gave logic part earlier, Now I have updated Can you please chcek my Logic why is it giving Fail in success casses also – dracile Jun 09 '23 at 06:32