|
| 1 | +## |
| 2 | +## Copyright 2023- IBM Inc. All rights reserved |
| 3 | +# SPDX-License-Identifier: MIT |
| 4 | +## |
| 5 | + |
| 6 | +####################################################################################################### |
| 7 | +# |
| 8 | +# elmclient sample for TSE |
| 9 | + |
| 10 | +#ETM scenario2: Get the test case with identifier = xxx |
| 11 | +#• Modify its title and description |
| 12 | +#• Delete an existing validates Requirement link if it exists |
| 13 | +#• Add 2 Validated By links, 1 to a DNG requirement – 1 to a DWA requirement |
| 14 | + |
| 15 | +import sys |
| 16 | +import os |
| 17 | +import csv |
| 18 | +import logging |
| 19 | +import urllib.parse |
| 20 | + |
| 21 | +import elmclient.server as elmserver |
| 22 | +import elmclient.utils as utils |
| 23 | +import elmclient.rdfxml as rdfxml |
| 24 | +import elmclient.httpops as httpops |
| 25 | +from elmclient.testcase import TestCase, TestCaseLink |
| 26 | +import lxml.etree as ET |
| 27 | + |
| 28 | +# setup logging - see levels in utils.py |
| 29 | +#loglevel = "INFO,INFO" |
| 30 | +loglevel = "TRACE,OFF" |
| 31 | +levels = [utils.loglevels.get(l,-1) for l in loglevel.split(",",1)] |
| 32 | +if len(levels)<2: |
| 33 | + # assert console logging level OFF if not provided |
| 34 | + levels.append(None) |
| 35 | +if -1 in levels: |
| 36 | + raise Exception( f'Logging level {loglevel} not valid - should be comma-separated one or two values from DEBUG, INFO, WARNING, ERROR, CRITICAL, OFF' ) |
| 37 | +utils.setup_logging( filelevel=levels[0], consolelevel=levels[1] ) |
| 38 | +logger = logging.getLogger(__name__) |
| 39 | +utils.log_commandline( os.path.basename(sys.argv[0]) ) |
| 40 | + |
| 41 | +#parameters |
| 42 | +jazzhost = 'https://jazz.ibm.com:9443' |
| 43 | + |
| 44 | +username = 'ibm' |
| 45 | +password = 'ibm' |
| 46 | + |
| 47 | +jtscontext = 'jts' |
| 48 | +qmappdomain = 'qm' |
| 49 | + |
| 50 | +# the project+component+config that will be queried |
| 51 | +proj = "SGC Quality Management" |
| 52 | +comp = "SGC MTM" |
| 53 | +conf = "SGC MTM Production stream" #conf="" if project is optout |
| 54 | + |
| 55 | + |
| 56 | +# caching control |
| 57 | +# 0=fully cached (but code below specifies queries aren't cached) - if you need to clear the cache, delet efolder .web_cache |
| 58 | +# 1=clear cache initially then continue with cache enabled |
| 59 | +# 2=clear cache and disable caching |
| 60 | +caching = 2 |
| 61 | + |
| 62 | +##################################################################################################### |
| 63 | +# create our "server" which is how we connect to ETM |
| 64 | +# first enable the proxy so if a proxy is running it can monitor the communication with server (this is ignored if proxy isn't running) |
| 65 | +elmserver.setupproxy(jazzhost,proxyport=8888) |
| 66 | +theserver = elmserver.JazzTeamServer(jazzhost, username, password, verifysslcerts=False, jtsappstring=f"jts:{jtscontext}", appstring=qmappdomain, cachingcontrol=caching) |
| 67 | + |
| 68 | +##################################################################################################### |
| 69 | +# create the ETM application interface |
| 70 | +qmapp = theserver.find_app( qmappdomain, ok_to_create=True ) |
| 71 | +if not qmapp: |
| 72 | + raise Exception( "Problem while creating the ETM application interface" ) |
| 73 | + |
| 74 | +##################################################################################################### |
| 75 | +# find the project |
| 76 | +p = qmapp.find_project( proj ) |
| 77 | +if not p: |
| 78 | + raise Exception( f"Project {proj} not found !!!" ) |
| 79 | +pa_u = p.project_uri |
| 80 | +#print( f"{pa_u=}" ) |
| 81 | +#print( f"{p.get_alias()=}" ) |
| 82 | + |
| 83 | +# find the component |
| 84 | +c = p.find_local_component( comp ) |
| 85 | +if not c: |
| 86 | + raise Exception( f"Component {comp} not found !!!" ) |
| 87 | + |
| 88 | +comp_u = c.project_uri |
| 89 | +#print( f"{comp_u=}" ) |
| 90 | + |
| 91 | + |
| 92 | +# if project is optin -> find the config |
| 93 | +if conf!="": |
| 94 | + local_config_u = c.get_local_config( conf ) |
| 95 | + if not local_config_u: |
| 96 | + raise Exception( f"Configuration {conf} not found !!!" ) |
| 97 | + |
| 98 | + # select the configuration - from now on use c for all operations in the local config |
| 99 | + c.set_local_config(local_config_u) |
| 100 | +#print(f"{local_config_u=}") |
| 101 | +##################################################################################################### |
| 102 | +#SCENARIO 2 |
| 103 | +#Get the test case with identifier = xxx |
| 104 | +#• Modify its title and description |
| 105 | +#• Delete an existing validates Requirement link if it exists |
| 106 | +#• Add 2 Validated By links, 1 to a DNG requirement – 1 to a DWA requirement |
| 107 | + |
| 108 | +tcquerybase = c.get_query_capability_uri("oslc_qm:TestCaseQuery") |
| 109 | +if not tcquerybase: |
| 110 | + raise Exception( "TestCaseQueryBase not found !!!" ) |
| 111 | +tcid = 53 |
| 112 | +print(f"Querying test case with identifier = {tcid}") |
| 113 | +tcs = c.execute_oslc_query( |
| 114 | + tcquerybase, |
| 115 | + whereterms=[['rqm_qm:shortIdentifier','=',f'"{tcid}"']], |
| 116 | + select=['*'], |
| 117 | + prefixes={rdfxml.RDF_DEFAULT_PREFIX["rqm_qm"]:'rqm_qm'} # note this is reversed - url to prefix |
| 118 | + ) |
| 119 | + |
| 120 | +if len(tcs.items())==1: |
| 121 | + |
| 122 | + tc_u = list(tcs.keys())[0] |
| 123 | + print(f"Found Test Case URL: {tc_u}") |
| 124 | + print("Doing a Get on test case url") |
| 125 | + xml_data,etag = c.execute_get_rdf_xml( tc_u, return_etag=True) |
| 126 | + #print(ET.tostring(xml_data)) |
| 127 | + |
| 128 | + print("Etag:" + etag) |
| 129 | + #put the TC data in a test case object |
| 130 | + tcObject = TestCase.from_etree(xml_data) |
| 131 | + |
| 132 | + #get the title and description |
| 133 | + print(f"Test case title: {tcObject.title}") |
| 134 | + print(f"Test case description: {tcObject.description}") |
| 135 | + print("----------------------------------------------------------") |
| 136 | + #displaying the links details |
| 137 | + print("Links details:") |
| 138 | + for link in tcObject.links: |
| 139 | + print(f" - {link.predicate} -> {link.target} (title: {link.title})") |
| 140 | + |
| 141 | + print("----------------------------------------------------------") |
| 142 | + |
| 143 | + #modifying title and description |
| 144 | + tcObject.title += " added by Python" |
| 145 | + if tcObject.description is None: |
| 146 | + tcObject.description = " added by Python" |
| 147 | + else: |
| 148 | + tcObject.description += " added by Python" |
| 149 | + |
| 150 | + |
| 151 | + #delete an existing link |
| 152 | + for link in tcObject.links: |
| 153 | + tcObject.delete_validatesRequirementLink(link.target) |
| 154 | + print(f"Deleting the link to {link.target}") |
| 155 | + break |
| 156 | + |
| 157 | + #adding a link to a DWA requirement, provide URL and link title |
| 158 | + tcObject.add_validatesRequirementLink("https://dwa9729rom1.fyre.ibm.com:8443/dwa/rm/urn:rational::1-66cdc1432a885b81-O-2-00000040","Module1 (2)") |
| 159 | + |
| 160 | + #adding a link to a DNG requirement, provide URL and link title |
| 161 | + tcObject.add_validatesRequirementLink("https://jazz.ibm.com:9443/rm/resources/BI_kC8csQ_WEfCjT5cep7iZxA","req3") |
| 162 | + |
| 163 | + print("we have updated the test case with the following:") |
| 164 | + |
| 165 | + print(f"Test case title: {tcObject.title}") |
| 166 | + print(f"Test case description: {tcObject.description}") |
| 167 | + print("----------------------------------------------------------") |
| 168 | + #displaying the links details |
| 169 | + print("Links details:") |
| 170 | + for link in tcObject.links: |
| 171 | + print(f" - {link.predicate} -> {link.target} (title: {link.title})") |
| 172 | + |
| 173 | + print("----------------------------------------------------------") |
| 174 | + |
| 175 | + #get the data from the test case object |
| 176 | + xml_data = tcObject.to_etree() |
| 177 | + #print(ET.tostring(xml_data)) |
| 178 | + print("sending the PUT request to update the test case") |
| 179 | + response = c.execute_post_rdf_xml(tc_u, data=xml_data, put=True, cacheable=False, headers={'If-Match':etag,'Content-Type':'application/rdf+xml'}, intent="Update the test case" ) |
| 180 | + if response.status_code==200: |
| 181 | + print("Update succesfull") |
| 182 | + else: |
| 183 | + print("Update failed") |
| 184 | +##################################################################################################### |
| 185 | + |
| 186 | +elif len(tcs.items())==0: |
| 187 | + print("No test case found") |
| 188 | + |
| 189 | +else: |
| 190 | + print(f"We found more than one test case with identififer {tcid} !!!???") |
| 191 | +print( "Finished" ) |
0 commit comments