package org.sonar.server.metric.persistence;
+import com.google.common.base.Predicate;
+import com.google.common.collect.Collections2;
import com.google.common.collect.Maps;
import org.apache.ibatis.session.RowBounds;
import org.sonar.api.server.ServerSide;
import org.sonar.server.es.SearchOptions;
import javax.annotation.CheckForNull;
+import javax.annotation.Nonnull;
import javax.annotation.Nullable;
-
import java.util.List;
import java.util.Map;
+import static com.google.common.collect.Lists.newArrayList;
+
@ServerSide
public class MetricDao implements DaoComponent {
@CheckForNull
public MetricDto selectByKey(DbSession session, String key) {
- return session.getMapper(MetricMapper.class).selectByKey(key);
+ return mapper(session).selectByKey(key);
}
public List<MetricDto> selectEnabled(DbSession session) {
- return session.getMapper(MetricMapper.class).selectAllEnabled();
+ return mapper(session).selectAllEnabled();
}
public List<MetricDto> selectEnabled(DbSession session, @Nullable Boolean isCustom, SearchOptions searchOptions) {
properties.put("isCustom", isCustom);
}
- return session.getMapper(MetricMapper.class).selectAllEnabled(properties, new RowBounds(searchOptions.getOffset(), searchOptions.getLimit()));
+ return mapper(session).selectAllEnabled(properties, new RowBounds(searchOptions.getOffset(), searchOptions.getLimit()));
}
public void insert(DbSession session, MetricDto dto) {
- session.getMapper(MetricMapper.class).insert(dto);
+ mapper(session).insert(dto);
+ }
+
+ public List<String> selectDomains(DbSession session) {
+ return newArrayList(Collections2.filter(mapper(session).selectDomains(), new Predicate<String>() {
+ @Override
+ public boolean apply(@Nonnull String input) {
+ return !input.isEmpty();
+ }
+ }));
+ }
+
+ private MetricMapper mapper(DbSession session) {
+ return session.getMapper(MetricMapper.class);
}
}
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+package org.sonar.server.metric.ws;
+
+import org.sonar.api.server.ws.Request;
+import org.sonar.api.server.ws.Response;
+import org.sonar.api.server.ws.WebService;
+import org.sonar.api.utils.text.JsonWriter;
+import org.sonar.core.persistence.DbSession;
+import org.sonar.core.persistence.MyBatis;
+import org.sonar.server.db.DbClient;
+
+import java.util.List;
+
+public class DomainsAction implements MetricsWsAction {
+
+ private final DbClient dbClient;
+
+ public DomainsAction(DbClient dbClient) {
+ this.dbClient = dbClient;
+ }
+
+ @Override
+ public void define(WebService.NewController context) {
+ context.createAction("domains")
+ .setDescription("List all custom metric domains.")
+ .setSince("5.2")
+ .setResponseExample(getClass().getResource("example-domains.json"))
+ .setHandler(this);
+ }
+
+ @Override
+ public void handle(Request request, Response response) throws Exception {
+ DbSession dbSession = dbClient.openSession(false);
+ try {
+ List<String> domains = dbClient.metricDao().selectDomains(dbSession);
+ JsonWriter json = response.newJsonWriter();
+ json.beginObject();
+ writeDomains(json, domains);
+ json.endObject();
+ json.close();
+ } finally {
+ MyBatis.closeQuietly(dbSession);
+ }
+ }
+
+ private void writeDomains(JsonWriter json, List<String> domains) {
+ json.name("domains");
+ json.beginArray();
+ json.values(domains);
+ json.endArray();
+ }
+}
MetricsWs.class,
org.sonar.server.metric.ws.ListAction.class,
org.sonar.server.metric.ws.TypesAction.class,
+ org.sonar.server.metric.ws.DomainsAction.class,
// quality gates
QualityGateDao.class,
--- /dev/null
+{
+ "domains": [
+ "API Compatibility",
+ "Issues",
+ "Rules",
+ "Tests",
+ "Documentation"
+ ]
+}
--- /dev/null
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+package org.sonar.server.metric.ws;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.ClassRule;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+import org.sonar.core.metric.db.MetricDto;
+import org.sonar.core.persistence.DbSession;
+import org.sonar.core.persistence.DbTester;
+import org.sonar.server.db.DbClient;
+import org.sonar.server.metric.persistence.MetricDao;
+import org.sonar.server.ws.WsTester;
+import org.sonar.test.DbTests;
+import org.sonar.test.JsonAssert;
+
+@Category(DbTests.class)
+public class DomainsActionTest {
+
+ @ClassRule
+ public static DbTester db = new DbTester();
+ WsTester ws;
+ DbClient dbClient;
+ DbSession dbSession;
+
+ @Before
+ public void setUp() throws Exception {
+ dbClient = new DbClient(db.database(), db.myBatis(), new MetricDao());
+ dbSession = dbClient.openSession(false);
+ ws = new WsTester(new MetricsWs(new DomainsAction(dbClient)));
+ }
+
+ @After
+ public void tearDown() throws Exception {
+ dbSession.close();
+ }
+
+ @Test
+ public void json_example_validated() throws Exception {
+ insertNewMetricDto(MetricTesting.newDto().setDomain("API Compatibility"));
+ insertNewMetricDto(MetricTesting.newDto().setDomain("Issues"));
+ insertNewMetricDto(MetricTesting.newDto().setDomain("Rules"));
+ insertNewMetricDto(MetricTesting.newDto().setDomain("Tests"));
+ insertNewMetricDto(MetricTesting.newDto().setDomain("Documentation"));
+ insertNewMetricDto(MetricTesting.newDto().setDomain(null));
+ insertNewMetricDto(MetricTesting.newDto().setDomain(""));
+
+ WsTester.Result result = ws.newGetRequest(MetricsWs.ENDPOINT, "domains").execute();
+
+ JsonAssert.assertJson(result.outputAsString()).isSimilarTo(getClass().getResource("example-domains.json"));
+ }
+
+ private void insertNewMetricDto(MetricDto metric) {
+ dbClient.metricDao().insert(dbSession, metric);
+ dbSession.commit();
+ }
+}
public interface MetricMapper {
MetricDto selectByKey(@Param("key") String key);
+
List<MetricDto> selectAllEnabled();
+
List<MetricDto> selectAllEnabled(Map<String, Object> properties, RowBounds rowBounds);
+
void insert(MetricDto dto);
+ List<String> selectDomains();
}
)
</insert>
+ <select id="selectDomains" resultType="String">
+ select distinct domain
+ from metrics m
+ where m.domain is not null
+ </select>
+
</mapper>