aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-webserver-api
diff options
context:
space:
mode:
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>2019-10-08 11:12:12 +0200
committerSonarTech <sonartech@sonarsource.com>2019-10-09 20:21:06 +0200
commit140e84fbdfd4ec9ddc15a7574d984ff1c059f6a0 (patch)
treef90f9b95ee59ece7a2347a12316cb5a1c2d5ad11 /server/sonar-webserver-api
parent82c9246f858728ef01603989d129342fb9830785 (diff)
downloadsonarqube-140e84fbdfd4ec9ddc15a7574d984ff1c059f6a0.tar.gz
sonarqube-140e84fbdfd4ec9ddc15a7574d984ff1c059f6a0.zip
fix some quality flaws on the new code period
Diffstat (limited to 'server/sonar-webserver-api')
-rw-r--r--server/sonar-webserver-api/src/main/java/org/sonar/server/exceptions/NotFoundException.java9
-rw-r--r--server/sonar-webserver-api/src/main/java/org/sonar/server/util/GlobalLockManagerImpl.java1
-rw-r--r--server/sonar-webserver-api/src/test/java/org/sonar/server/exceptions/NotFoundExceptionTest.java94
3 files changed, 94 insertions, 10 deletions
diff --git a/server/sonar-webserver-api/src/main/java/org/sonar/server/exceptions/NotFoundException.java b/server/sonar-webserver-api/src/main/java/org/sonar/server/exceptions/NotFoundException.java
index f21a98b5157..44b64953585 100644
--- a/server/sonar-webserver-api/src/main/java/org/sonar/server/exceptions/NotFoundException.java
+++ b/server/sonar-webserver-api/src/main/java/org/sonar/server/exceptions/NotFoundException.java
@@ -19,7 +19,6 @@
*/
package org.sonar.server.exceptions;
-import com.google.common.base.Optional;
import javax.annotation.Nullable;
import static java.lang.String.format;
@@ -47,14 +46,6 @@ public class NotFoundException extends ServerException {
* @throws NotFoundException if the value is not present
* @return the value
*/
- public static <T> T checkFoundWithOptional(Optional<T> value, String message, Object... messageArguments) {
- if (!value.isPresent()) {
- throw new NotFoundException(format(message, messageArguments));
- }
-
- return value.get();
- }
-
public static <T> T checkFoundWithOptional(java.util.Optional<T> value, String message, Object... messageArguments) {
if (!value.isPresent()) {
throw new NotFoundException(format(message, messageArguments));
diff --git a/server/sonar-webserver-api/src/main/java/org/sonar/server/util/GlobalLockManagerImpl.java b/server/sonar-webserver-api/src/main/java/org/sonar/server/util/GlobalLockManagerImpl.java
index 7611475cd2d..d072808d252 100644
--- a/server/sonar-webserver-api/src/main/java/org/sonar/server/util/GlobalLockManagerImpl.java
+++ b/server/sonar-webserver-api/src/main/java/org/sonar/server/util/GlobalLockManagerImpl.java
@@ -23,7 +23,6 @@ import org.sonar.api.ce.ComputeEngineSide;
import org.sonar.api.server.ServerSide;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
-import org.sonar.db.property.InternalPropertiesDao;
import static org.sonar.api.utils.Preconditions.checkArgument;
diff --git a/server/sonar-webserver-api/src/test/java/org/sonar/server/exceptions/NotFoundExceptionTest.java b/server/sonar-webserver-api/src/test/java/org/sonar/server/exceptions/NotFoundExceptionTest.java
new file mode 100644
index 00000000000..d2e9304b9e2
--- /dev/null
+++ b/server/sonar-webserver-api/src/test/java/org/sonar/server/exceptions/NotFoundExceptionTest.java
@@ -0,0 +1,94 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2019 SonarSource SA
+ * mailto:info 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.exceptions;
+
+import java.util.Optional;
+import org.junit.Test;
+
+import static org.apache.commons.lang.RandomStringUtils.randomAlphabetic;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
+import static org.sonar.server.exceptions.NotFoundException.checkFound;
+import static org.sonar.server.exceptions.NotFoundException.checkFoundWithOptional;
+
+public class NotFoundExceptionTest {
+ @Test
+ public void http_code_is_404() {
+ NotFoundException underTest = new NotFoundException(randomAlphabetic(12));
+
+ assertThat(underTest.httpCode()).isEqualTo(404);
+ }
+ @Test
+ public void message_is_constructor_argument() {
+ String message = randomAlphabetic(12);
+ NotFoundException underTest = new NotFoundException(message);
+
+ assertThat(underTest.getMessage()).isEqualTo(message);
+ }
+
+ @Test
+ public void checkFound_type_throws_NotFoundException_if_parameter_is_null() {
+ String message = randomAlphabetic(12);
+ assertThatExceptionOfType(NotFoundException.class)
+ .isThrownBy(() -> checkFound(null, message))
+ .withMessage(message);
+ }
+
+ @Test
+ public void checkFound_type_throws_NotFoundException_if_parameter_is_null_and_formats_message() {
+ String message = "foo %s";
+ assertThatExceptionOfType(NotFoundException.class)
+ .isThrownBy(() -> checkFound(null, message, "bar"))
+ .withMessage("foo bar");
+ }
+
+ @Test
+ public void checkFound_return_parameter_if_parameter_is_not_null() {
+ String message = randomAlphabetic(12);
+ Object o = new Object();
+
+ assertThat(checkFound(o, message)).isSameAs(o);
+ }
+
+ @Test
+ public void checkFoundWithOptional_throws_NotFoundException_if_empty() {
+ String message = randomAlphabetic(12);
+ assertThatExceptionOfType(NotFoundException.class)
+ .isThrownBy(() -> checkFoundWithOptional(Optional.empty(), message))
+ .withMessage(message);
+ }
+
+ @Test
+ public void checkFoundWithOptional_throws_NotFoundException_if_empty_and_formats_message() {
+ String message = "foo %s";
+ assertThatExceptionOfType(NotFoundException.class)
+ .isThrownBy(() -> checkFoundWithOptional(Optional.empty(), message, "bar"))
+ .withMessage("foo bar");
+ }
+
+ @Test
+ public void checkFoundWithOptional_return_content_of_if_not_empty() {
+ String message = randomAlphabetic(12);
+ Object o = new Object();
+
+ assertThat(checkFoundWithOptional(Optional.of(o), message)).isSameAs(o);
+ }
+
+}