mercredi 29 août 2018

How to write a groovy testcase for the following function?

I have written a function called "getSnomedData()" which returns 5 results (suggestions) based on particular user input for the autocomplete functionality of a textbox.The function is called every time there is a change in the input value of textbox.

How do i write a groovy test case for the "getSnomedData()" function which is seen below..

java code:

public String getSnomedData(String enteredval,String flag) throws Exception
{

    Root oRoot = null;
    JSONObject oJsonSno = null;
    JSONObject oJson = null;
    JSONArray oJsonArr = null;
    ResultSet oRsSelect = null;
    PreparedStatement oPrStmt = null;
    String strSql = null;
    String snomedcode=null;
    String snomeddesc=null;
    String str=null;
    oJson = new JSONObject(); 
    String Expression=null;
    oJson.put("status", "failure");

    try {

        oJsonSno = new JSONObject();

        oJsonArr = new JSONArray();
        oRoot = Root.createDbConnection(null);

        if(flag.equalsIgnoreCase("smdcode"))
        {       
        strSql = "SELECT  * FROM snomedinfo_data WHERE conceptid LIKE ? LIMIT 0, 5  ";
        }

        if(flag.equalsIgnoreCase("smddesc"))
        {       
        strSql = "SELECT  * FROM snomedinfo_data WHERE term LIKE ? LIMIT  0,5  ";
        }

        oPrStmt = oRoot.con.prepareStatement(strSql);
        oPrStmt.setString(1,  enteredval + "%");

        oRsSelect = oPrStmt.executeQuery();
        while (oRsSelect.next()) {
            snomedcode = Root.TrimString(oRsSelect.getString("conceptid"));
            snomeddesc = Root.TrimString(oRsSelect.getString("term"));
            if(flag.equalsIgnoreCase("smdcode"))
            {

                Expression=snomedcode+'|'+snomeddesc;
            }
            else if(flag.equalsIgnoreCase("smddesc"))
            {
                Expression=snomeddesc+'|'+snomedcode;
            }
            oJsonSno = new JSONObject();
            oJsonSno.put("value",Expression);
            oJsonArr.put(oJsonSno);
        }
        oJson.put("status", "success");
        oJson.put("snomeddata", oJsonArr);
        //str = oJson.toString(); 
    }
    catch (Exception e) {
        e.printStackTrace();

    }

    finally {

        oRsSelect = Root.EcwCloseResultSet(oRsSelect);

        oPrStmt = Root.EcwClosePreparedStatement(oPrStmt);
        oRoot = Root.closeDbConnection(null, oRoot);
    }
    str = oJson.toString(); 
    return str; 
}

Note:Here enteredval is the input(String) given by user in the textbox.

Aucun commentaire:

Enregistrer un commentaire