1

I am trying to create a new defect. I have to do it using REST API in my C# ASP.Net project. Below is the used URL:

qcbin/rest/domains/TESTDOMAIN/projects/TESTPROJECT/defects

Below is my request body :

<?xml version="1.0" encoding="UTF-8"?>
<Entity Type="defect">
  <Fields>  
    <Field Name="priority">
      <Value>3-High</Value>
    </Field>
    <Field Name="description">
      <Value>test description</Value>
    </Field>
    <Field Name="name">
      <Value>test with rest API</Value>
    </Field>    
    <Field Name="creation-time">
      <Value>2011-08-16 11:34:08</Value>
    </Field>
  </Fields>
</Entity>

I can successfully create the defect only if I do not use creation-time in XML. May I know how to use columns that contain hyphen?

I have written the code on C#. Below is my C# code

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(HPALMUrl + "/rest/domains/" + HPALMDomain + "/projects/" + HPALMProject + "/defects/");
request.Method = "POST";
request.Accept = "application/xml";
request.ContentType = "application/xml";
authRequest.KeepAlive = true;
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36";
request.CookieContainer = createSessionRequest.CookieContainer; //Authenticated cookie
using (var requestStream = request.GetRequestStream())
{
    requestStream.Write(data, 0, data.Length);
}
var response = (HttpWebResponse)request.GetResponse();

string responseText = string.Empty;
if (response.StatusCode == HttpStatusCode.OK)
    responseText = "Update completed";
else
    responseText = "Error in update";

I am facing this hyphen column problem when updating Test table column also. So, please let me know how to use hyphen contain column

Thanks in advance!

Karthick Raju
  • 757
  • 8
  • 29
  • It should not be a problem, the issue you are facing may be, you are trying to set some value for field that would auto-generate by ALM when you insert a new record. Be specific about what fields in test plan causing the problem. – Barney Nov 11 '17 at 03:55
  • @Barney, I tried the same with To-Mail, which is not auto generate. – Karthick Raju Nov 11 '17 at 04:34
  • @Barney, It's Defect not test plan. – Karthick Raju Nov 11 '17 at 04:34
  • `I am facing this hyphen column problem when updating Test table column ` Alright post the error message too – Barney Nov 11 '17 at 04:35
  • Sorry, it happened for Test table too. But at the moment, Defect creation is high priority. Thanks! – Karthick Raju Nov 11 '17 at 04:39
  • Getting 404 bad request. I could create defect (without using column with hyphen). – Karthick Raju Nov 11 '17 at 04:40
  • `https://github.com/macroking/ALM-Integration/blob/master/ALM_Integration_Util.py` I have used some column names with "-". If you have access to Management tab in HP ALM, cross-check the columns names you use in your program actually exists in respective alm table. – Barney Nov 11 '17 at 14:38
  • @Barney, Thanks for the URL. URL is not working. Since hyphen column works for you, could you please share me your code? I cross checked the column. It's same as Column field – Karthick Raju Nov 13 '17 at 06:05
  • https://github.com/macroking/ALM-Integration – Barney Nov 13 '17 at 06:19
  • @Barney, I have looked at the URL. They remove special character (hyphen) in the code. Do I have to pass column without hyphen? I am not much familiar with Python. Could you please help me out? – Karthick Raju Nov 13 '17 at 07:05

1 Answers1

1

How to approach,

  1. Make a get request to extract all the field names and data format it accepts.

    Eg: https://almserver/qcbin/rest/domains/WWT/projects/content/defects/1

  2. Identify the required fields.

  3. Set the request header to XML/JSON according to your payload

Defect Creation in Python

ALM_USER_NAME = "username"
ALM_PASSWORD = "password"
ALM_DOMAIN = "domain"

ALM_URL = "https://almserver/qcbin/"
AUTH_END_POINT = ALM_URL + "authentication-point/authenticate"
QC_SESSION_END_POINT = ALM_URL + "rest/site-session"
QC_LOGOUT_END_POINT = ALM_URL + "authentication-point/logout"
ALM_MIDPOINT = ALM_URL + "rest/domains/" + ALM_DOMAIN + "/projects/"

def generate_xml_data(inputdata):
    '''
        Function    :   generateXMLData
        Description :   Generate an xml string
        Parameters  :   Dictionary of variable
    '''
    root = Element('Entity')
    root.set('Type', inputdata['Type'])
    inputdata.pop('Type')
    childs = SubElement(root, 'Fields')

    for key, value in inputdata.iteritems():
        child1 = SubElement(childs, 'Field')
        child1.set('Name', key)
        child2 = SubElement(child1, 'Value')
        child2.text = value
    return tostring(root)

def createdefect():
    '''
        alm defect
    '''
    alm_session = requests.Session()
    # Login
    try:
        res = alm_session.post(AUTH_END_POINT, auth=HTTPBasicAuth(
            ALM_USER_NAME, ALM_PASSWORD))
        if res.status_code == 200:
            print "ALM: Logged in"
        alm_session.post(QC_SESSION_END_POINT)
        alm_session.headers.update({'Accept': 'application/json',
                                    'Content-Type': 'application/xml'})
        # Get all the projects
        res = alm_session.get(ALM_MIDPOINT)

    # Post a New Defect
    defect = dict()
    defect['Type'] = 'defect'
    defect['name'] = 'StackOverflow'
    defect['user-10'] = 'Content'  # User defined field
    defect['severity'] = '1-Low'
    defect['priority'] = '1-Low'
    defect['detected-by'] = 'userid'
    defect['creation-time'] = '2017-11-13'

    # Call the generic method to build the xml data
    defect_payload = generate_xml_data(defect)

    response = alm_session.post(ALM_MIDPOINT + "content/defects", data=defect_payload)
    print response.url

except (KeyboardInterrupt, SystemExit, Exception) as err:
    print "keyboard interrupt"
    print err.message
finally:
    if alm_session:
        res = alm_session.post(QC_LOGOUT_END_POINT)
        if res.status_code == 200:
            print "ALM: Logged out"

if __name__ == "__main__":
    try:
        createdefect()
    except (KeyboardInterrupt, SystemExit):
        print "done with errors"
    
Community
  • 1
  • 1
Barney
  • 1,851
  • 2
  • 19
  • 36