Skip to content

Commit

Permalink
Get list of Sonar metrics from the karaf configuration.
Browse files Browse the repository at this point in the history
  • Loading branch information
steinarb committed Dec 9, 2017
1 parent 32b8ac4 commit 4433742
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public class SonarCollectorConfiguration {
static final String SONARCOLLECTOR_JDBC_URL = "sonar.collector.jdbc.url";
static final String SONARCOLLECTOR_JDBC_USER = "sonar.collector.jdbc.user";
static final String SONARCOLLECTOR_JDBC_PASS = "sonar.collector.jdbc.password";
static final String SONAR_MEASURES_COMPONENTS_METRIC_KEYS = "sonar.measures.components.metricKeys";
private final Properties applicationProperties = new Properties();
private Map<String, Object> injectedconfig = Collections.emptyMap();

Expand Down Expand Up @@ -75,6 +76,24 @@ public Properties getJdbcConnectionProperties() {
return properties;
}

/**
* Retrieve the list of metrics that should be retrieved from Sonar
* First the injected config is used to find the values, and if nothing
* is there, the fallback is to the system property and the final
* fallback is the built-in application.properties.
*
* @return a list of the metrics that will be retrieved from Sonar
*/
public String[] getMetricKeys() {
// Settings made in karaf configuration takes precedence
Object metricKeysFromKarafConfig = injectedconfig.get(SONAR_MEASURES_COMPONENTS_METRIC_KEYS);
if (metricKeysFromKarafConfig != null) {
return ((String) metricKeysFromKarafConfig).split(",");
}

return System.getProperty(SONAR_MEASURES_COMPONENTS_METRIC_KEYS, applicationProperties.getProperty(SONAR_MEASURES_COMPONENTS_METRIC_KEYS)).split(",");
}

public String getJdbcUrl() {
// Settings made in karaf configuration takes precedence
Object jdbcUrlFromKarafConfig = injectedconfig.get(SONARCOLLECTOR_JDBC_URL);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@
@Component(service={Servlet.class}, property={"alias=/sonar-collector", "configurationPid=no.priv.bang.sonar.sonar-collector-webhook"} )
public class SonarCollectorServlet extends HttpServlet {
private static final long serialVersionUID = -8421243385012454373L;
private static final String SONAR_MEASURES_COMPONENTS_METRIC_KEYS = "sonar.measures.components.metricKeys";
// A formatter that's able to parse ISO dates without colons in the time zone spec
static final DateTimeFormatter isoZonedDateTimeformatter = new DateTimeFormatterBuilder()
.append(DateTimeFormatter.ISO_LOCAL_DATE_TIME)
Expand Down Expand Up @@ -151,7 +150,7 @@ SonarBuild callbackToSonarServerToGetMetrics(ServletRequest request) throws IOEx
String version = componentsShowRoot.path("component").path("version").asText();
SonarBuild build = new SonarBuild(analysedAt, project, version, serverUrl);
logWarningIfVersionIsMissing(build, componentsShowUrl);
URL measurementsUrl = createSonarMeasurementsComponentUrl(build, getMetricKeys());
URL measurementsUrl = createSonarMeasurementsComponentUrl(build, configuration.getMetricKeys());
HttpURLConnection measurementsUrlConnection = openConnection(measurementsUrl);
JsonNode measurementsRoot = mapper.readTree(measurementsUrlConnection.getInputStream());
parseMeasures(build.getMeasurements(), measurementsRoot.path("component").path("measures"));
Expand Down Expand Up @@ -209,10 +208,6 @@ public Map<String, String> parseMeasures(Map<String, String> measuresResults, Js
return measuresResults;
}

public String[] getMetricKeys() {
return System.getProperty(SONAR_MEASURES_COMPONENTS_METRIC_KEYS, applicationProperties.getProperty(SONAR_MEASURES_COMPONENTS_METRIC_KEYS)).split(",");
}

public URL createSonarComponentsShowUrl(URL serverUrl, String project) throws IOException {
String localPath = String.format("/api/components/show?component=%s", URLEncoder.encode(project,"UTF-8"));
return new URL(serverUrl, localPath);
Expand All @@ -227,4 +222,8 @@ HttpURLConnection openConnection(URL url) throws IOException {
return factory.openConnection(url);
}

public SonarCollectorConfiguration getConfiguration() {
return configuration;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public void testGetJdbcConnectionProperties() {
assertEquals("jdbc:postgresql:///sonarcollector", jdbcConnectionProperties.getProperty(DataSourceFactory.JDBC_URL));
assertNull(jdbcConnectionProperties.getProperty(DataSourceFactory.JDBC_USER));
assertNull(jdbcConnectionProperties.getProperty(DataSourceFactory.JDBC_PASSWORD));
assertEquals(9, configuration.getMetricKeys().length);
}

/**
Expand All @@ -64,6 +65,7 @@ public void testGetJdbcConnectionPropertiesNullConfig() {
assertEquals("jdbc:postgresql:///sonarcollector", jdbcConnectionProperties.getProperty(DataSourceFactory.JDBC_URL));
assertNull(jdbcConnectionProperties.getProperty(DataSourceFactory.JDBC_USER));
assertNull(jdbcConnectionProperties.getProperty(DataSourceFactory.JDBC_PASSWORD));
assertEquals(9, configuration.getMetricKeys().length);
}

/**
Expand All @@ -82,6 +84,7 @@ public void testGetJdbcConnectionPropertiesInjectedConfig() {
injectedConfig.put(SonarCollectorConfiguration.SONARCOLLECTOR_JDBC_URL, "jdbc:postgresql://lorenzo.hjemme.lan/sonarcollector");
injectedConfig.put(SonarCollectorConfiguration.SONARCOLLECTOR_JDBC_USER, "karaf");
injectedConfig.put(SonarCollectorConfiguration.SONARCOLLECTOR_JDBC_PASS, "kafar");
injectedConfig.put(SonarCollectorConfiguration.SONAR_MEASURES_COMPONENTS_METRIC_KEYS, "lines,bugs,new_bugs,vulnerabilities,new_vulnerabilities,code_smells");

// Set a system property that should not be picked up, since the injected config is picked first
System.setProperty(SonarCollectorConfiguration.SONARCOLLECTOR_JDBC_USER, "notausername");
Expand All @@ -92,6 +95,7 @@ public void testGetJdbcConnectionPropertiesInjectedConfig() {
assertEquals("jdbc:postgresql://lorenzo.hjemme.lan/sonarcollector", jdbcConnectionProperties.getProperty(DataSourceFactory.JDBC_URL));
assertEquals("karaf", jdbcConnectionProperties.getProperty(DataSourceFactory.JDBC_USER));
assertEquals("kafar", jdbcConnectionProperties.getProperty(DataSourceFactory.JDBC_PASSWORD));
assertEquals(6, configuration.getMetricKeys().length);
} finally {
// Restore the original system properties
System.setProperties(originalProperties);
Expand All @@ -116,12 +120,14 @@ public void testGetJdbcConnectionPropertiesFromSystemProperties() {
System.setProperty(SonarCollectorConfiguration.SONARCOLLECTOR_JDBC_URL, "jdbc:postgresql://lorenzo.hjemme.lan/sonarcollector");
System.setProperty(SonarCollectorConfiguration.SONARCOLLECTOR_JDBC_USER, "karaf");
System.setProperty(SonarCollectorConfiguration.SONARCOLLECTOR_JDBC_PASS, "kafar");
System.setProperty(SonarCollectorConfiguration.SONAR_MEASURES_COMPONENTS_METRIC_KEYS, "lines,bugs,new_bugs,vulnerabilities,new_vulnerabilities,code_smells,new_code_smells,coverage");

Properties jdbcConnectionProperties = configuration.getJdbcConnectionProperties();

assertEquals("jdbc:postgresql://lorenzo.hjemme.lan/sonarcollector", jdbcConnectionProperties.getProperty(DataSourceFactory.JDBC_URL));
assertEquals("karaf", jdbcConnectionProperties.getProperty(DataSourceFactory.JDBC_USER));
assertEquals("kafar", jdbcConnectionProperties.getProperty(DataSourceFactory.JDBC_PASSWORD));
assertEquals(8, configuration.getMetricKeys().length);
} finally {
// Restore the original system properties
System.setProperties(originalProperties);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ public void testCallbackToSonarServerToGetMetrics() throws ServletException, IOE
public void testCreateSonarComponentsShowUrl() throws ServletException, IOException {
URLConnectionFactory factory = mock(URLConnectionFactory.class);
SonarCollectorServlet servlet = new SonarCollectorServlet(factory);
String[] metricKeys = servlet.getMetricKeys();
String[] metricKeys = servlet.getConfiguration().getMetricKeys();
assertEquals(9, metricKeys.length);
String project = "no.priv.bang.ukelonn:parent";
URL serverUrl = new URL("http://localhost:9000");
Expand All @@ -266,7 +266,7 @@ public void testCreateSonarComponentsShowUrl() throws ServletException, IOExcept
public void testCreateSonarMeasurementsComponentUrl() throws ServletException, IOException {
URLConnectionFactory factory = mock(URLConnectionFactory.class);
SonarCollectorServlet servlet = new SonarCollectorServlet(factory);
String[] metricKeys = servlet.getMetricKeys();
String[] metricKeys = servlet.getConfiguration().getMetricKeys();
assertEquals(9, metricKeys.length);
String project = "no.priv.bang.ukelonn:parent";
URL serverUrl = new URL("http://localhost:9000");
Expand All @@ -286,7 +286,7 @@ public void testParseMeasures() throws JsonProcessingException, IOException {
JsonNode measuresNode = root.path("component").path("measures");
SonarCollectorServlet servlet = new SonarCollectorServlet();

String[] metricKeys = servlet.getMetricKeys();
String[] metricKeys = servlet.getConfiguration().getMetricKeys();
HashMap<String, String> measures = new HashMap<>();
servlet.parseMeasures(measures, measuresNode);
assertEquals(metricKeys.length, measures.size());
Expand Down

0 comments on commit 4433742

Please sign in to comment.