Skip to content

Commit 766ed3f

Browse files
ROMAIN BARTHROMAIN BARTH
authored andcommitted
testcase class and sample
1 parent abc3bf5 commit 766ed3f

File tree

7 files changed

+819
-3
lines changed

7 files changed

+819
-3
lines changed
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
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 scenario1: Run a query for all Test Case modified since 01/01/2025 -> Display their URLs, identifier and title
11+
#
12+
13+
import sys
14+
import os
15+
import csv
16+
import logging
17+
import urllib.parse
18+
19+
import elmclient.server as elmserver
20+
import elmclient.utils as utils
21+
import elmclient.rdfxml as rdfxml
22+
import elmclient.httpops as httpops
23+
24+
# setup logging - see levels in utils.py
25+
#loglevel = "INFO,INFO"
26+
loglevel = "TRACE,OFF"
27+
levels = [utils.loglevels.get(l,-1) for l in loglevel.split(",",1)]
28+
if len(levels)<2:
29+
# assert console logging level OFF if not provided
30+
levels.append(None)
31+
if -1 in levels:
32+
raise Exception( f'Logging level {loglevel} not valid - should be comma-separated one or two values from DEBUG, INFO, WARNING, ERROR, CRITICAL, OFF' )
33+
utils.setup_logging( filelevel=levels[0], consolelevel=levels[1] )
34+
logger = logging.getLogger(__name__)
35+
utils.log_commandline( os.path.basename(sys.argv[0]) )
36+
37+
#parameters
38+
jazzhost = 'https://jazz.ibm.com:9443'
39+
40+
username = 'ibm'
41+
password = 'ibm'
42+
43+
jtscontext = 'jts'
44+
qmappdomain = 'qm'
45+
46+
# the project+component+config that will be queried
47+
proj = "SGC Quality Management"
48+
comp = "SGC MTM"
49+
conf = "SGC MTM Production stream"
50+
51+
52+
# caching control
53+
# 0=fully cached (but code below specifies queries aren't cached) - if you need to clear the cache, delet efolder .web_cache
54+
# 1=clear cache initially then continue with cache enabled
55+
# 2=clear cache and disable caching
56+
caching = 2
57+
58+
#####################################################################################################
59+
# create our "server" which is how we connect to ETM
60+
# 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)
61+
elmserver.setupproxy(jazzhost,proxyport=8888)
62+
theserver = elmserver.JazzTeamServer(jazzhost, username, password, verifysslcerts=False, jtsappstring=f"jts:{jtscontext}", appstring=qmappdomain, cachingcontrol=caching)
63+
64+
#####################################################################################################
65+
# create the ETM application interface
66+
qmapp = theserver.find_app( qmappdomain, ok_to_create=True )
67+
if not qmapp:
68+
raise Exception( "Problem while creating the ETM application interface" )
69+
70+
#####################################################################################################
71+
# find the project
72+
p = qmapp.find_project( proj )
73+
if not p:
74+
raise Exception( f"Project {proj} not found !!!" )
75+
pa_u = p.project_uri
76+
#print( f"{pa_u=}" )
77+
#print( f"{p.get_alias()=}" )
78+
79+
# find the component
80+
c = p.find_local_component( comp )
81+
if not c:
82+
raise Exception( f"Component {comp} not found !!!" )
83+
84+
comp_u = c.project_uri
85+
#print( f"{comp_u=}" )
86+
87+
# find the config
88+
local_config_u = c.get_local_config( conf )
89+
if not local_config_u:
90+
raise Exception( f"Configuration {conf} not found !!!" )
91+
92+
# select the configuration - from now on use c for all operations in the local config
93+
c.set_local_config(local_config_u)
94+
95+
#####################################################################################################
96+
#SCENARIO 1
97+
# find the test cases with dcterms modified > 2025-01-01
98+
tcquerybase = c.get_query_capability_uri("oslc_qm:TestCaseQuery")
99+
if not tcquerybase:
100+
raise Exception( "TestCaseQueryBase not found !!!" )
101+
102+
tcs = c.execute_oslc_query(
103+
tcquerybase,
104+
whereterms=[['dcterms:modified','>','"2025-01-01T00:00:00.000Z"^^xsd:dateTime']],
105+
select=['dcterms:identifier,dcterms:title,rqm_qm:shortIdentifier'],
106+
prefixes={rdfxml.RDF_DEFAULT_PREFIX["dcterms"]:'dcterms',rdfxml.RDF_DEFAULT_PREFIX["rqm_qm"]:'rqm_qm'} # note this is reversed - url to prefix
107+
)
108+
109+
nbTC = len(tcs) #count the number of Test case returned by the query
110+
print(f"The query returned {nbTC} Test Cases")
111+
print("----------------------------------------------------------")
112+
count = 0
113+
for TCurl in tcs:
114+
count+=1
115+
print(f"Test case #{count}")
116+
print(TCurl)
117+
print("Title: " + tcs[TCurl]['dcterms:title'])
118+
print("Identifier: " + tcs[TCurl]['rqm_qm:shortIdentifier'])
119+
print("----------------------------------------------------------")
120+
121+
#####################################################################################################
122+
123+
124+
print( "Finished" )
Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
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

Comments
 (0)