From: Teryk Bellahsene Date: Thu, 14 Jan 2016 10:49:13 +0000 (+0100) Subject: Fix quality flaws X-Git-Tag: 5.4-M6~8 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=2fdae0c4a7b913f9476502998b8645ab07c730bf;p=sonarqube.git Fix quality flaws --- diff --git a/server/sonar-server/src/main/java/org/sonar/server/component/ws/LanguageParamUtils.java b/server/sonar-server/src/main/java/org/sonar/server/component/ws/LanguageParamUtils.java index c746743768b..e6e967dee41 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/component/ws/LanguageParamUtils.java +++ b/server/sonar-server/src/main/java/org/sonar/server/component/ws/LanguageParamUtils.java @@ -19,13 +19,13 @@ */ package org.sonar.server.component.ws; +import com.google.common.base.Function; import com.google.common.collect.Collections2; -import org.sonar.api.resources.Language; -import org.sonar.api.resources.Languages; -import org.sonar.core.util.NonNullInputFunction; - import java.util.Arrays; import java.util.Collection; +import javax.annotation.Nonnull; +import org.sonar.api.resources.Language; +import org.sonar.api.resources.Languages; public class LanguageParamUtils { @@ -43,11 +43,15 @@ public class LanguageParamUtils { } public static Collection getLanguageKeys(Languages languages) { - return Collections2.transform(Arrays.asList(languages.all()), new NonNullInputFunction() { - @Override - public String doApply(Language input) { - return input.getKey(); - } - }); + return Collections2.transform(Arrays.asList(languages.all()), LanguageToKeyFunction.INSTANCE); + } + + private enum LanguageToKeyFunction implements Function { + INSTANCE; + + @Override + public String apply(@Nonnull Language input) { + return input.getKey(); + } } } diff --git a/server/sonar-server/src/main/java/org/sonar/server/measure/custom/ws/DeleteAction.java b/server/sonar-server/src/main/java/org/sonar/server/measure/custom/ws/DeleteAction.java index 8ce4308fc69..980d9552cb8 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/measure/custom/ws/DeleteAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/measure/custom/ws/DeleteAction.java @@ -24,11 +24,11 @@ import org.sonar.api.server.ws.Response; import org.sonar.api.server.ws.WebService; import org.sonar.api.web.UserRole; import org.sonar.core.permission.GlobalPermissions; +import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.MyBatis; import org.sonar.db.component.ComponentDto; import org.sonar.db.measure.custom.CustomMeasureDto; -import org.sonar.server.db.DbClient; import org.sonar.server.user.UserSession; public class DeleteAction implements CustomMeasuresWsAction { diff --git a/server/sonar-server/src/main/java/org/sonar/server/measure/custom/ws/MetricsAction.java b/server/sonar-server/src/main/java/org/sonar/server/measure/custom/ws/MetricsAction.java index a6218866fba..37af94449ab 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/measure/custom/ws/MetricsAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/measure/custom/ws/MetricsAction.java @@ -25,12 +25,12 @@ 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.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.MyBatis; import org.sonar.db.component.ComponentDto; import org.sonar.db.metric.MetricDto; import org.sonar.server.component.ComponentFinder; -import org.sonar.server.db.DbClient; import org.sonar.server.metric.ws.MetricJsonWriter; import org.sonar.server.user.UserSession; @@ -77,7 +77,8 @@ public class MetricsAction implements CustomMeasuresWsAction { DbSession dbSession = dbClient.openSession(false); try { - ComponentDto project = componentFinder.getByUuidOrKey(dbSession, request.param(CreateAction.PARAM_PROJECT_ID), request.param(CreateAction.PARAM_PROJECT_KEY), PROJECT_ID_AND_KEY); + ComponentDto project = componentFinder.getByUuidOrKey(dbSession, request.param(CreateAction.PARAM_PROJECT_ID), request.param(CreateAction.PARAM_PROJECT_KEY), + PROJECT_ID_AND_KEY); checkPermissions(userSession, project); List metrics = searchMetrics(dbSession, project); diff --git a/server/sonar-server/src/main/java/org/sonar/server/measure/custom/ws/UpdateAction.java b/server/sonar-server/src/main/java/org/sonar/server/measure/custom/ws/UpdateAction.java index b2ccbf997b6..33490bb4845 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/measure/custom/ws/UpdateAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/measure/custom/ws/UpdateAction.java @@ -26,12 +26,12 @@ import org.sonar.api.server.ws.WebService; import org.sonar.api.user.User; import org.sonar.api.utils.System2; import org.sonar.api.utils.text.JsonWriter; +import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.MyBatis; import org.sonar.db.component.ComponentDto; import org.sonar.db.measure.custom.CustomMeasureDto; import org.sonar.db.metric.MetricDto; -import org.sonar.server.db.DbClient; import org.sonar.server.user.UserSession; import org.sonar.server.user.index.UserIndex; diff --git a/server/sonar-server/src/main/java/org/sonar/server/measure/ws/ComponentTreeDataLoader.java b/server/sonar-server/src/main/java/org/sonar/server/measure/ws/ComponentTreeDataLoader.java index 051f84a1df1..176db2a75b9 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/measure/ws/ComponentTreeDataLoader.java +++ b/server/sonar-server/src/main/java/org/sonar/server/measure/ws/ComponentTreeDataLoader.java @@ -88,7 +88,6 @@ public class ComponentTreeDataLoader { this.resourceTypes = resourceTypes; } - @CheckForNull ComponentTreeData load(ComponentTreeWsRequest wsRequest) { DbSession dbSession = dbClient.openSession(false); try { @@ -164,12 +163,7 @@ public class ComponentTreeDataLoader { private List searchMetrics(DbSession dbSession, ComponentTreeWsRequest request) { List metrics = dbClient.metricDao().selectByKeys(dbSession, request.getMetricKeys()); if (metrics.size() < request.getMetricKeys().size()) { - List foundMetricKeys = Lists.transform(metrics, new Function() { - @Override - public String apply(@Nonnull MetricDto input) { - return input.getKey(); - } - }); + List foundMetricKeys = Lists.transform(metrics, MetricDtoFunctions.toKey()); Set missingMetricKeys = Sets.difference( new LinkedHashSet<>(request.getMetricKeys()), new LinkedHashSet<>(foundMetricKeys)); diff --git a/server/sonar-server/src/main/java/org/sonar/server/measure/ws/ComponentTreeSort.java b/server/sonar-server/src/main/java/org/sonar/server/measure/ws/ComponentTreeSort.java index c5ee3d246a9..2cd5f81b1f9 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/measure/ws/ComponentTreeSort.java +++ b/server/sonar-server/src/main/java/org/sonar/server/measure/ws/ComponentTreeSort.java @@ -47,6 +47,10 @@ class ComponentTreeSort { static List sortComponents(List components, ComponentTreeWsRequest wsRequest, List metrics, Table measuresByComponentUuidAndMetric) { + List sortParameters = wsRequest.getSort(); + if (sortParameters == null || sortParameters.isEmpty()) { + return components; + } boolean isAscending = wsRequest.getAsc(); Map> orderingsBySortField = ImmutableMap.>builder() .put(NAME_SORT, componentNameOrdering(isAscending)) @@ -55,7 +59,6 @@ class ComponentTreeSort { .put(METRIC_SORT, metricOrdering(wsRequest, metrics, measuresByComponentUuidAndMetric)) .build(); - List sortParameters = wsRequest.getSort(); String firstSortParameter = sortParameters.get(0); Ordering primaryOrdering = orderingsBySortField.get(firstSortParameter); if (sortParameters.size() > 1) { diff --git a/server/sonar-server/src/main/java/org/sonar/server/measure/ws/MeasureDtoToWsMeasure.java b/server/sonar-server/src/main/java/org/sonar/server/measure/ws/MeasureDtoToWsMeasure.java index 37c85a57297..bd405520624 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/measure/ws/MeasureDtoToWsMeasure.java +++ b/server/sonar-server/src/main/java/org/sonar/server/measure/ws/MeasureDtoToWsMeasure.java @@ -23,7 +23,7 @@ import org.sonar.db.measure.MeasureDto; import org.sonar.db.metric.MetricDto; import org.sonarqube.ws.WsMeasures; -import static org.sonar.server.measure.ws.MeasureValueFormatter.formatDoubleValue; +import static org.sonar.server.measure.ws.MeasureValueFormatter.formatNumericalValue; import static org.sonar.server.measure.ws.MeasureValueFormatter.formatMeasureValue; class MeasureDtoToWsMeasure { @@ -41,19 +41,19 @@ class MeasureDtoToWsMeasure { measure.setValue(formatMeasureValue(measureDto, metricDto)); } if (measureDto.getVariation(1) != null) { - measure.setVariationValueP1(formatDoubleValue(measureDto.getVariation(1), metricDto)); + measure.setVariationValueP1(formatNumericalValue(measureDto.getVariation(1), metricDto)); } if (measureDto.getVariation(2) != null) { - measure.setVariationValueP2(formatDoubleValue(measureDto.getVariation(2), metricDto)); + measure.setVariationValueP2(formatNumericalValue(measureDto.getVariation(2), metricDto)); } if (measureDto.getVariation(3) != null) { - measure.setVariationValueP3(formatDoubleValue(measureDto.getVariation(3), metricDto)); + measure.setVariationValueP3(formatNumericalValue(measureDto.getVariation(3), metricDto)); } if (measureDto.getVariation(4) != null) { - measure.setVariationValueP4(formatDoubleValue(measureDto.getVariation(4), metricDto)); + measure.setVariationValueP4(formatNumericalValue(measureDto.getVariation(4), metricDto)); } if (measureDto.getVariation(5) != null) { - measure.setVariationValueP5(formatDoubleValue(measureDto.getVariation(5), metricDto)); + measure.setVariationValueP5(formatNumericalValue(measureDto.getVariation(5), metricDto)); } return measure.build(); diff --git a/server/sonar-server/src/main/java/org/sonar/server/measure/ws/MeasureValueFormatter.java b/server/sonar-server/src/main/java/org/sonar/server/measure/ws/MeasureValueFormatter.java index c3a8881b46b..fcf5e28bd22 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/measure/ws/MeasureValueFormatter.java +++ b/server/sonar-server/src/main/java/org/sonar/server/measure/ws/MeasureValueFormatter.java @@ -57,7 +57,7 @@ class MeasureValueFormatter { } } - static String formatDoubleValue(Double value, MetricDto metric) { + static String formatNumericalValue(Double value, MetricDto metric) { Metric.ValueType metricType = Metric.ValueType.valueOf(metric.getValueType()); switch (metricType) { diff --git a/server/sonar-server/src/main/java/org/sonar/server/measure/ws/MetricDtoToWsMetric.java b/server/sonar-server/src/main/java/org/sonar/server/measure/ws/MetricDtoToWsMetric.java index c4bc881c8a4..c8b9c0a5253 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/measure/ws/MetricDtoToWsMetric.java +++ b/server/sonar-server/src/main/java/org/sonar/server/measure/ws/MetricDtoToWsMetric.java @@ -22,7 +22,7 @@ package org.sonar.server.measure.ws; import org.sonar.db.metric.MetricDto; import org.sonarqube.ws.Common.Metric; -import static org.sonar.server.measure.ws.MeasureValueFormatter.formatDoubleValue; +import static org.sonar.server.measure.ws.MeasureValueFormatter.formatNumericalValue; class MetricDtoToWsMetric { private MetricDtoToWsMetric() { @@ -48,10 +48,10 @@ class MetricDtoToWsMetric { metric.setDecimalScale(metricDto.getDecimalScale()); } if (metricDto.getBestValue() != null) { - metric.setBestValue(formatDoubleValue(metricDto.getBestValue(), metricDto)); + metric.setBestValue(formatNumericalValue(metricDto.getBestValue(), metricDto)); } if (metricDto.getWorstValue() != null) { - metric.setWorstValue(formatDoubleValue(metricDto.getWorstValue(), metricDto)); + metric.setWorstValue(formatNumericalValue(metricDto.getWorstValue(), metricDto)); } return metric.build(); diff --git a/server/sonar-server/src/main/java/org/sonar/server/metric/ws/CreateAction.java b/server/sonar-server/src/main/java/org/sonar/server/metric/ws/CreateAction.java index bc8627321ae..023ed3b06b0 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/metric/ws/CreateAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/metric/ws/CreateAction.java @@ -27,11 +27,11 @@ 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.permission.GlobalPermissions; +import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.MyBatis; import org.sonar.db.measure.custom.CustomMeasureDto; import org.sonar.db.metric.MetricDto; -import org.sonar.server.db.DbClient; import org.sonar.server.exceptions.BadRequestException; import org.sonar.server.ruby.RubyBridge; import org.sonar.server.user.UserSession; diff --git a/server/sonar-server/src/main/java/org/sonar/server/metric/ws/DeleteAction.java b/server/sonar-server/src/main/java/org/sonar/server/metric/ws/DeleteAction.java index 91457bdf42e..d4447f6563c 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/metric/ws/DeleteAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/metric/ws/DeleteAction.java @@ -27,10 +27,10 @@ import org.sonar.api.server.ws.Request; import org.sonar.api.server.ws.Response; import org.sonar.api.server.ws.WebService; import org.sonar.core.permission.GlobalPermissions; +import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.MyBatis; import org.sonar.db.metric.MetricDto; -import org.sonar.server.db.DbClient; import org.sonar.server.ruby.RubyBridge; import org.sonar.server.user.UserSession; diff --git a/server/sonar-server/src/main/java/org/sonar/server/metric/ws/SearchAction.java b/server/sonar-server/src/main/java/org/sonar/server/metric/ws/SearchAction.java index 914e57823f5..281b12ec80c 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/metric/ws/SearchAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/metric/ws/SearchAction.java @@ -27,10 +27,10 @@ import org.sonar.api.server.ws.Response; import org.sonar.api.server.ws.WebService; import org.sonar.api.server.ws.WebService.Param; import org.sonar.api.utils.text.JsonWriter; +import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.MyBatis; import org.sonar.db.metric.MetricDto; -import org.sonar.server.db.DbClient; import org.sonar.server.es.SearchOptions; import static com.google.common.collect.Sets.newHashSet; diff --git a/server/sonar-server/src/main/java/org/sonar/server/metric/ws/UpdateAction.java b/server/sonar-server/src/main/java/org/sonar/server/metric/ws/UpdateAction.java index 8eb3e8ce7ad..d9b5f493833 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/metric/ws/UpdateAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/metric/ws/UpdateAction.java @@ -27,11 +27,11 @@ 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.permission.GlobalPermissions; +import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.MyBatis; import org.sonar.db.measure.custom.CustomMeasureDto; import org.sonar.db.metric.MetricDto; -import org.sonar.server.db.DbClient; import org.sonar.server.exceptions.BadRequestException; import org.sonar.server.ruby.RubyBridge; import org.sonar.server.user.UserSession; diff --git a/server/sonar-server/src/test/java/org/sonar/server/measure/custom/ws/DeleteActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/measure/custom/ws/DeleteActionTest.java index b22388b696e..bf1cf7b0486 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/measure/custom/ws/DeleteActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/measure/custom/ws/DeleteActionTest.java @@ -19,7 +19,6 @@ */ package org.sonar.server.measure.custom.ws; -import org.junit.After; import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -28,15 +27,13 @@ import org.junit.rules.ExpectedException; import org.sonar.api.utils.System2; import org.sonar.api.web.UserRole; import org.sonar.core.permission.GlobalPermissions; +import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.DbTester; import org.sonar.db.RowNotFoundException; -import org.sonar.db.component.ComponentDao; import org.sonar.db.component.ComponentDto; -import org.sonar.db.measure.custom.CustomMeasureDao; -import org.sonar.db.measure.custom.CustomMeasureDto; import org.sonar.db.component.ComponentTesting; -import org.sonar.server.db.DbClient; +import org.sonar.db.measure.custom.CustomMeasureDto; import org.sonar.server.exceptions.ForbiddenException; import org.sonar.server.tester.UserSessionRule; import org.sonar.server.ws.WsTester; @@ -51,28 +48,21 @@ public class DeleteActionTest { public static final String ACTION = "delete"; - @Rule - public DbTester db = DbTester.create(System2.INSTANCE); @Rule public UserSessionRule userSessionRule = UserSessionRule.standalone(); @Rule public ExpectedException expectedException = ExpectedException.none(); + @Rule + public DbTester db = DbTester.create(System2.INSTANCE); + DbClient dbClient = db.getDbClient(); + DbSession dbSession = db.getSession(); + WsTester ws; - DbClient dbClient; - DbSession dbSession; @Before public void setUp() { - dbClient = new DbClient(db.database(), db.myBatis(), new CustomMeasureDao(), new ComponentDao()); - dbSession = dbClient.openSession(false); ws = new WsTester(new CustomMeasuresWs(new DeleteAction(dbClient, userSessionRule))); userSessionRule.setGlobalPermissions(GlobalPermissions.SYSTEM_ADMIN); - db.truncateTables(); - } - - @After - public void tearDown() { - dbSession.close(); } @Test diff --git a/server/sonar-server/src/test/java/org/sonar/server/measure/custom/ws/MetricsActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/measure/custom/ws/MetricsActionTest.java index 76ccfaafc3f..bd4ff5cb2ef 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/measure/custom/ws/MetricsActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/measure/custom/ws/MetricsActionTest.java @@ -19,7 +19,6 @@ */ package org.sonar.server.measure.custom.ws; -import org.junit.After; import org.junit.Before; import org.junit.BeforeClass; import org.junit.ClassRule; @@ -32,19 +31,16 @@ import org.sonar.api.measures.Metric; import org.sonar.api.utils.System2; import org.sonar.api.web.UserRole; import org.sonar.core.permission.GlobalPermissions; +import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.DbTester; -import org.sonar.db.component.ComponentDao; import org.sonar.db.component.ComponentDto; +import org.sonar.db.component.ComponentTesting; import org.sonar.db.measure.custom.CustomMeasureDto; import org.sonar.db.metric.MetricDto; import org.sonar.server.component.ComponentFinder; -import org.sonar.db.component.ComponentTesting; -import org.sonar.server.db.DbClient; import org.sonar.server.es.EsTester; import org.sonar.server.exceptions.ForbiddenException; -import org.sonar.db.measure.custom.CustomMeasureDao; -import org.sonar.db.metric.MetricDao; import org.sonar.server.tester.UserSessionRule; import org.sonar.server.user.index.UserDoc; import org.sonar.server.user.index.UserIndexDefinition; @@ -53,29 +49,26 @@ import org.sonar.test.DbTests; import static org.assertj.core.api.Assertions.assertThat; import static org.sonar.db.measure.custom.CustomMeasureTesting.newCustomMeasureDto; +import static org.sonar.db.metric.MetricTesting.newMetricDto; import static org.sonar.server.measure.custom.ws.CustomMeasuresWs.ENDPOINT; import static org.sonar.server.measure.custom.ws.MetricsAction.ACTION; -import static org.sonar.db.metric.MetricTesting.newMetricDto; @Category(DbTests.class) public class MetricsActionTest { private static final String DEFAULT_PROJECT_UUID = "project-uuid"; private static final String DEFAULT_PROJECT_KEY = "project-key"; + @ClassRule + public static EsTester es = new EsTester().addDefinitions(new UserIndexDefinition(new Settings())); @Rule public ExpectedException expectedException = ExpectedException.none(); - @Rule public UserSessionRule userSession = UserSessionRule.standalone(); - - @ClassRule - public static EsTester es = new EsTester().addDefinitions(new UserIndexDefinition(new Settings())); - @Rule public DbTester db = DbTester.create(System2.INSTANCE); + DbClient dbClient = db.getDbClient(); + DbSession dbSession = db.getSession(); - DbClient dbClient; - DbSession dbSession; WsTester ws; ComponentDto defaultProject; @@ -90,19 +83,11 @@ public class MetricsActionTest { @Before public void setUp() { - dbClient = new DbClient(db.database(), db.myBatis(), new MetricDao(), new ComponentDao(), new CustomMeasureDao()); - dbSession = dbClient.openSession(false); - db.truncateTables(); ws = new WsTester(new CustomMeasuresWs(new MetricsAction(dbClient, userSession, new ComponentFinder(dbClient)))); userSession.login("login").setGlobalPermissions(GlobalPermissions.SYSTEM_ADMIN); defaultProject = insertDefaultProject(); } - @After - public void tearDown() { - dbSession.close(); - } - @Test public void list_metrics() throws Exception { insertCustomMetric("metric-key-1"); diff --git a/server/sonar-server/src/test/java/org/sonar/server/measure/custom/ws/UpdateActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/measure/custom/ws/UpdateActionTest.java index ccc917edb78..79cf52fbf44 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/measure/custom/ws/UpdateActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/measure/custom/ws/UpdateActionTest.java @@ -19,7 +19,6 @@ */ package org.sonar.server.measure.custom.ws; -import org.junit.After; import org.junit.Before; import org.junit.BeforeClass; import org.junit.ClassRule; @@ -31,18 +30,15 @@ import org.sonar.api.config.Settings; import org.sonar.api.measures.Metric.ValueType; import org.sonar.api.utils.System2; import org.sonar.core.permission.GlobalPermissions; +import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.DbTester; import org.sonar.db.RowNotFoundException; -import org.sonar.db.component.ComponentDao; import org.sonar.db.component.ComponentDto; -import org.sonar.db.measure.custom.CustomMeasureDao; +import org.sonar.db.component.ComponentTesting; import org.sonar.db.measure.custom.CustomMeasureDto; -import org.sonar.db.metric.MetricDao; import org.sonar.db.metric.MetricDto; import org.sonar.db.metric.MetricTesting; -import org.sonar.db.component.ComponentTesting; -import org.sonar.server.db.DbClient; import org.sonar.server.es.EsTester; import org.sonar.server.exceptions.ForbiddenException; import org.sonar.server.exceptions.ServerException; @@ -75,9 +71,9 @@ public class UpdateActionTest { public DbTester db = DbTester.create(System2.INSTANCE); @ClassRule public static EsTester es = new EsTester().addDefinitions(new UserIndexDefinition(new Settings())); - DbClient dbClient; - DbSession dbSession; - System2 system; + DbClient dbClient = db.getDbClient(); + DbSession dbSession = db.getSession(); + System2 system = mock(System2.class); WsTester ws; @BeforeClass @@ -91,21 +87,13 @@ public class UpdateActionTest { @Before public void setUp() { - dbClient = new DbClient(db.database(), db.myBatis(), new CustomMeasureDao(), new ComponentDao(), new MetricDao()); - dbSession = dbClient.openSession(false); - db.truncateTables(); - system = mock(System2.class); CustomMeasureValidator validator = new CustomMeasureValidator(newFullTypeValidations()); + ws = new WsTester(new CustomMeasuresWs(new UpdateAction(dbClient, userSessionRule, system, validator, new CustomMeasureJsonWriter(new UserJsonWriter(userSessionRule)), new UserIndex(es.client())))); userSessionRule.login("login").setGlobalPermissions(GlobalPermissions.SYSTEM_ADMIN); } - @After - public void tearDown() { - dbSession.close(); - } - @Test public void update_text_value_and_description_in_db() throws Exception { MetricDto metric = insertNewMetric(ValueType.STRING); diff --git a/server/sonar-server/src/test/java/org/sonar/server/measure/ws/MeasureValueFormatterTest.java b/server/sonar-server/src/test/java/org/sonar/server/measure/ws/MeasureValueFormatterTest.java new file mode 100644 index 00000000000..9ad6ea678e5 --- /dev/null +++ b/server/sonar-server/src/test/java/org/sonar/server/measure/ws/MeasureValueFormatterTest.java @@ -0,0 +1,84 @@ +/* + * SonarQube + * Copyright (C) 2009-2016 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * This program 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. + * + * This program 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.measure.ws; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.sonar.api.measures.Metric; +import org.sonar.db.measure.MeasureDto; +import org.sonar.db.metric.MetricDto; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.sonar.api.measures.Metric.ValueType.BOOL; +import static org.sonar.api.measures.Metric.ValueType.DATA; +import static org.sonar.api.measures.Metric.ValueType.FLOAT; +import static org.sonar.api.measures.Metric.ValueType.INT; +import static org.sonar.api.measures.Metric.ValueType.MILLISEC; +import static org.sonar.api.measures.Metric.ValueType.PERCENT; +import static org.sonar.api.measures.Metric.ValueType.WORK_DUR; +import static org.sonar.db.metric.MetricTesting.newMetricDto; +import static org.sonar.server.measure.ws.MeasureValueFormatter.formatMeasureValue; +import static org.sonar.server.measure.ws.MeasureValueFormatter.formatNumericalValue; + +public class MeasureValueFormatterTest { + + @Rule + public ExpectedException expectedException = ExpectedException.none(); + + @Test + public void test_formatNumericalValue() { + assertThat(formatNumericalValue(-1.0d, newMetric(BOOL))).isEqualTo("false"); + assertThat(formatNumericalValue(1.0d, newMetric(BOOL))).isEqualTo("true"); + assertThat(formatNumericalValue(1_000.123d, newMetric(FLOAT))).isEqualTo("1000.123"); + assertThat(formatNumericalValue(1_000.0d, newMetric(INT))).isEqualTo("1000"); + assertThat(formatNumericalValue(1_000.0d, newMetric(WORK_DUR))).isEqualTo("1000"); + } + + @Test + public void test_formatMeasureValue() { + assertThat(formatMeasureValue(newNumericMeasure(-1.0d), newMetric(BOOL))).isEqualTo("false"); + assertThat(formatMeasureValue(newNumericMeasure(1.0d), newMetric(BOOL))).isEqualTo("true"); + assertThat(formatMeasureValue(newNumericMeasure(1000.123d), newMetric(PERCENT))).isEqualTo("1000.123"); + assertThat(formatMeasureValue(newNumericMeasure(1000d), newMetric(MILLISEC))).isEqualTo("1000"); + assertThat(formatMeasureValue(newNumericMeasure(1000.0d), newMetric(WORK_DUR))).isEqualTo("1000"); + assertThat(formatMeasureValue(newTextMeasure("text-value"), newMetric(DATA))).isEqualTo("text-value"); + } + + @Test + public void fail_if_text_value_type_for_numeric_formatter() { + expectedException.expect(IllegalArgumentException.class); + expectedException.expectMessage("Unsupported metric type 'DATA' for numerical value"); + + formatNumericalValue(42.0d, newMetric(DATA)); + } + + private static MetricDto newMetric(Metric.ValueType valueType) { + return newMetricDto().setValueType(valueType.name()); + } + + private static MeasureDto newNumericMeasure(Double value) { + return new MeasureDto().setValue(value); + } + + private static MeasureDto newTextMeasure(String data) { + return new MeasureDto().setData(data); + } +} diff --git a/server/sonar-server/src/test/java/org/sonar/server/metric/ws/CreateActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/metric/ws/CreateActionTest.java index e9ed5c014cf..98af5a29009 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/metric/ws/CreateActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/metric/ws/CreateActionTest.java @@ -19,7 +19,6 @@ */ package org.sonar.server.metric.ws; -import org.junit.After; import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -28,16 +27,14 @@ import org.junit.rules.ExpectedException; import org.sonar.api.measures.Metric.ValueType; import org.sonar.api.utils.System2; import org.sonar.core.permission.GlobalPermissions; +import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.DbTester; +import org.sonar.db.measure.custom.CustomMeasureTesting; import org.sonar.db.metric.MetricDto; import org.sonar.db.metric.MetricTesting; -import org.sonar.server.db.DbClient; import org.sonar.server.exceptions.ForbiddenException; import org.sonar.server.exceptions.ServerException; -import org.sonar.db.measure.custom.CustomMeasureDao; -import org.sonar.db.measure.custom.CustomMeasureTesting; -import org.sonar.db.metric.MetricDao; import org.sonar.server.ruby.RubyBridge; import org.sonar.server.tester.UserSessionRule; import org.sonar.server.ws.WsTester; @@ -61,31 +58,23 @@ public class CreateActionTest { private static final String DEFAULT_DESCRIPTION = "custom-metric-description"; private static final String DEFAULT_TYPE = ValueType.INT.name(); - @Rule - public DbTester db = DbTester.create(System2.INSTANCE); @Rule public ExpectedException expectedException = ExpectedException.none(); @Rule public UserSessionRule userSessionRule = UserSessionRule.standalone(); + @Rule + public DbTester db = DbTester.create(System2.INSTANCE); + DbClient dbClient = db.getDbClient(); + DbSession dbSession = db.getSession(); + WsTester ws; - DbClient dbClient; - DbSession dbSession; @Before public void setUp() { - dbClient = new DbClient(db.database(), db.myBatis(), new MetricDao(), new CustomMeasureDao()); - dbSession = dbClient.openSession(false); - db.truncateTables(); - ws = new WsTester(new MetricsWs(new CreateAction(dbClient, userSessionRule, mock(RubyBridge.class, RETURNS_DEEP_STUBS)))); userSessionRule.login("login").setGlobalPermissions(GlobalPermissions.SYSTEM_ADMIN); } - @After - public void tearDown() { - dbSession.close(); - } - @Test public void insert_new_minimalist_metric() throws Exception { newRequest() diff --git a/server/sonar-server/src/test/java/org/sonar/server/metric/ws/DeleteActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/metric/ws/DeleteActionTest.java index cffbcd71b9b..7fe950ebc36 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/metric/ws/DeleteActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/metric/ws/DeleteActionTest.java @@ -21,7 +21,6 @@ package org.sonar.server.metric.ws; import java.util.Arrays; import java.util.List; -import org.junit.After; import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -29,15 +28,14 @@ import org.junit.experimental.categories.Category; import org.junit.rules.ExpectedException; import org.sonar.api.utils.System2; import org.sonar.core.permission.GlobalPermissions; +import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.DbTester; import org.sonar.db.measure.custom.CustomMeasureDto; -import org.sonar.db.metric.MetricDto; -import org.sonar.server.db.DbClient; -import org.sonar.server.exceptions.ForbiddenException; -import org.sonar.db.measure.custom.CustomMeasureDao; import org.sonar.db.measure.custom.CustomMeasureTesting; import org.sonar.db.metric.MetricDao; +import org.sonar.db.metric.MetricDto; +import org.sonar.server.exceptions.ForbiddenException; import org.sonar.server.ruby.RubyBridge; import org.sonar.server.tester.UserSessionRule; import org.sonar.server.ws.WsTester; @@ -51,32 +49,25 @@ import static org.sonar.db.metric.MetricTesting.newMetricDto; @Category(DbTests.class) public class DeleteActionTest { - @Rule - public DbTester db = DbTester.create(System2.INSTANCE); @Rule public UserSessionRule userSessionRule = UserSessionRule.standalone(); @Rule public ExpectedException expectedException = ExpectedException.none(); - DbClient dbClient; - DbSession dbSession; - WsTester ws; + @Rule + public DbTester db = DbTester.create(System2.INSTANCE); + DbClient dbClient = db.getDbClient(); + DbSession dbSession = db.getSession(); MetricDao metricDao; + WsTester ws; + @Before public void setUp() { - dbClient = new DbClient(db.database(), db.myBatis(), new MetricDao(), new CustomMeasureDao()); - dbSession = dbClient.openSession(false); - db.truncateTables(); userSessionRule.login("login").setGlobalPermissions(GlobalPermissions.SYSTEM_ADMIN); ws = new WsTester(new MetricsWs(new DeleteAction(dbClient, userSessionRule, mock(RubyBridge.class, RETURNS_DEEP_STUBS)))); metricDao = dbClient.metricDao(); } - @After - public void tearDown() { - dbSession.close(); - } - @Test public void delete_by_keys() throws Exception { insertCustomEnabledMetrics(1, 2, 3); diff --git a/server/sonar-server/src/test/java/org/sonar/server/metric/ws/SearchActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/metric/ws/SearchActionTest.java index 993d722d405..22dcb3e62d1 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/metric/ws/SearchActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/metric/ws/SearchActionTest.java @@ -20,18 +20,16 @@ package org.sonar.server.metric.ws; import org.apache.commons.lang.StringUtils; -import org.junit.After; import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.experimental.categories.Category; import org.sonar.api.server.ws.WebService.Param; import org.sonar.api.utils.System2; +import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.DbTester; import org.sonar.db.metric.MetricDto; -import org.sonar.server.db.DbClient; -import org.sonar.db.metric.MetricDao; import org.sonar.server.ws.WsTester; import org.sonar.test.DbTests; @@ -44,21 +42,13 @@ public class SearchActionTest { @Rule public DbTester db = DbTester.create(System2.INSTANCE); - DbClient dbClient; - DbSession dbSession; + DbClient dbClient = db.getDbClient(); + DbSession dbSession = db.getSession(); WsTester ws; @Before public void setUp() { - dbClient = new DbClient(db.database(), db.myBatis(), new MetricDao()); - dbSession = dbClient.openSession(false); ws = new WsTester(new MetricsWs(new SearchAction(dbClient))); - db.truncateTables(); - } - - @After - public void tearDown() { - dbSession.close(); } @Test diff --git a/server/sonar-server/src/test/java/org/sonar/server/metric/ws/UpdateActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/metric/ws/UpdateActionTest.java index 8d078663f25..f278e8d5876 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/metric/ws/UpdateActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/metric/ws/UpdateActionTest.java @@ -19,7 +19,6 @@ */ package org.sonar.server.metric.ws; -import org.junit.After; import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -28,14 +27,12 @@ import org.junit.rules.ExpectedException; import org.sonar.api.measures.Metric.ValueType; import org.sonar.api.utils.System2; import org.sonar.core.permission.GlobalPermissions; +import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.DbTester; import org.sonar.db.metric.MetricDto; -import org.sonar.server.db.DbClient; import org.sonar.server.exceptions.ForbiddenException; import org.sonar.server.exceptions.ServerException; -import org.sonar.db.measure.custom.CustomMeasureDao; -import org.sonar.db.metric.MetricDao; import org.sonar.server.ruby.RubyBridge; import org.sonar.server.tester.UserSessionRule; import org.sonar.server.ws.WsTester; @@ -61,31 +58,23 @@ public class UpdateActionTest { private static final String DEFAULT_DESCRIPTION = "custom-metric-description"; private static final String DEFAULT_TYPE = ValueType.INT.name(); - @Rule - public DbTester db = DbTester.create(System2.INSTANCE); @Rule public ExpectedException expectedException = ExpectedException.none(); @Rule public UserSessionRule userSessionRule = UserSessionRule.standalone(); + @Rule + public DbTester db = DbTester.create(System2.INSTANCE); + DbClient dbClient = db.getDbClient(); + DbSession dbSession = db.getSession(); + WsTester ws; - DbClient dbClient; - DbSession dbSession; @Before public void setUp() { - dbClient = new DbClient(db.database(), db.myBatis(), new MetricDao(), new CustomMeasureDao()); - dbSession = dbClient.openSession(false); - db.truncateTables(); - ws = new WsTester(new MetricsWs(new UpdateAction(dbClient, userSessionRule, mock(RubyBridge.class, RETURNS_DEEP_STUBS)))); userSessionRule.login("login").setGlobalPermissions(GlobalPermissions.SYSTEM_ADMIN); } - @After - public void tearDown() { - dbSession.close(); - } - @Test public void update_all_fields() throws Exception { int id = insertMetric(newDefaultMetric()); diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/issue/IssuesService.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/issue/IssuesService.java index 1ff7010885a..e8c99d569e9 100644 --- a/sonar-ws/src/main/java/org/sonarqube/ws/client/issue/IssuesService.java +++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/issue/IssuesService.java @@ -19,7 +19,6 @@ */ package org.sonarqube.ws.client.issue; -import com.google.common.base.Joiner; import org.sonarqube.ws.Issues.SearchWsResponse; import org.sonarqube.ws.client.BaseService; import org.sonarqube.ws.client.GetRequest; @@ -60,7 +59,6 @@ import static org.sonarqube.ws.client.issue.IssueFilterParameters.STATUSES; import static org.sonarqube.ws.client.issue.IssueFilterParameters.TAGS; public class IssuesService extends BaseService { - private static final Joiner LIST_TO_PARAMS_STRING = Joiner.on(",").skipNulls(); public IssuesService(WsConnector wsConnector) { super(wsConnector, "api/issues");