1919 */
2020package com .sonar .python .it .plugin ;
2121
22- import com .google .common .collect .ImmutableMap ;
2322import com .sonar .orchestrator .Orchestrator ;
2423import com .sonar .orchestrator .build .SonarScanner ;
2524import java .io .File ;
2625import java .util .List ;
27- import java .util .regex .Matcher ;
28- import java .util .regex .Pattern ;
2926import org .junit .Before ;
3027import org .junit .ClassRule ;
3128import org .junit .Test ;
32- import org .sonar .wsclient .SonarClient ;
3329import org .sonar .wsclient .issue .Issue ;
3430import org .sonar .wsclient .issue .IssueQuery ;
31+ import org .sonarqube .ws .QualityProfiles ;
32+ import org .sonarqube .ws .client .PostRequest ;
33+ import org .sonarqube .ws .client .qualityprofile .SearchWsRequest ;
3534
35+ import static com .sonar .python .it .plugin .Tests .newAdminWsClient ;
36+ import static com .sonar .python .it .plugin .Tests .newWsClient ;
37+ import static java .lang .String .format ;
3638import static org .assertj .core .api .Assertions .assertThat ;
3739
3840public class XPathRuleTest {
@@ -55,27 +57,7 @@ public void resetData() throws Exception {
5557
5658 @ Test
5759 public void testXPathRule () {
58- SonarClient sonarClient = orchestrator .getServer ().adminWsClient ();
59- sonarClient .post ("/api/rules/create" , ImmutableMap .<String , Object >builder ()
60- .put ("name" , "XPathTestRule" )
61- .put ("markdown_description" , "XPath test rule" )
62- .put ("severity" , "INFO" )
63- .put ("status" , "READY" )
64- .put ("template_key" , "python:XPath" )
65- .put ("custom_key" , RULE_KEY )
66- .put ("prevent_reactivation" , "true" )
67- .put ("params" , "message=\" Do something fantastic!\" ;xpathQuery=\" //FILE_INPUT\" " )
68- .build ());
69- String profiles = sonarClient .get ("api/rules/app" );
70- Pattern pattern = Pattern .compile ("py-" + PROFILE_NAME + "-\\ d+" );
71- Matcher matcher = pattern .matcher (profiles );
72- assertThat (matcher .find ()).isTrue ();
73- String profilekey = matcher .group ();
74- sonarClient .post ("api/qualityprofiles/activate_rule" , ImmutableMap .<String , Object >of (
75- "profile_key" , profilekey ,
76- "rule_key" , RULE_KEY_WITH_PREFIX ,
77- "severity" , "INFO" ,
78- "params" , "" ));
60+ createAndActivateRuleFromTemplate ();
7961
8062 orchestrator .getServer ().provisionProject (PROJECT , PROJECT );
8163 orchestrator .getServer ().associateProjectToQualityProfile (PROJECT , "py" , PROFILE_NAME );
@@ -100,4 +82,28 @@ private List<Issue> getIssues(String ruleKey) {
10082 return orchestrator .getServer ().wsClient ().issueClient ().find (query ).list ();
10183 }
10284
85+ private void createAndActivateRuleFromTemplate () {
86+ String language = "py" ;
87+ newAdminWsClient ().wsConnector ().call (new PostRequest ("api/rules/create" )
88+ .setParam ("name" , "XPathTestRule" )
89+ .setParam ("markdown_description" , "XPath test rule" )
90+ .setParam ("severity" , "INFO" )
91+ .setParam ("status" , "READY" )
92+ .setParam ("template_key" , "python:XPath" )
93+ .setParam ("custom_key" , RULE_KEY )
94+ .setParam ("prevent_reactivation" , "true" )
95+ .setParam ("params" , "message=\" Do something fantastic!\" ;xpathQuery=\" //FILE_INPUT\" " )).failIfNotSuccessful ();
96+
97+ QualityProfiles .SearchWsResponse .QualityProfile qualityProfile = newWsClient ().qualityProfiles ().search (new SearchWsRequest ()).getProfilesList ().stream ()
98+ .filter (qp -> qp .getLanguage ().equals (language ))
99+ .filter (qp -> qp .getName ().equals (PROFILE_NAME ))
100+ .findFirst ().orElseThrow (() -> new IllegalStateException (format ("Could not find quality profile '%s' for language '%s' " , PROFILE_NAME , language )));
101+ String profileKey = qualityProfile .getKey ();
102+
103+ newAdminWsClient ().wsConnector ().call (new PostRequest ("api/qualityprofiles/activate_rule" )
104+ .setParam ("profile_key" , profileKey )
105+ .setParam ("rule_key" , RULE_KEY_WITH_PREFIX )
106+ .setParam ("severity" , "INFO" )).failIfNotSuccessful ();
107+ }
108+
103109}
0 commit comments