From d038d4de7019a2efa309ff9bb8fab2dd384d07d7 Mon Sep 17 00:00:00 2001 From: =?utf8?q?S=C3=A9bastien=20Lesaint?= Date: Mon, 14 Jan 2019 18:20:29 +0100 Subject: [PATCH] SONAR-11631 move version event data validation to UI code --- .../java/org/sonar/db/component/SnapshotDto.java | 4 ++-- .../org/sonar/db/component/SnapshotDtoTest.java | 3 ++- .../projectanalysis/ws/CreateEventAction.java | 2 ++ .../server/projectanalysis/ws/EventValidator.java | 14 ++++++++++++++ .../projectanalysis/ws/UpdateEventAction.java | 10 ++-------- .../projectanalysis/ws/UpdateEventActionTest.java | 2 +- 6 files changed, 23 insertions(+), 12 deletions(-) diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/component/SnapshotDto.java b/server/sonar-db-dao/src/main/java/org/sonar/db/component/SnapshotDto.java index f30fb1a5ff5..9f4291d9e67 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/component/SnapshotDto.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/component/SnapshotDto.java @@ -32,7 +32,7 @@ public final class SnapshotDto { */ public static final String STATUS_UNPROCESSED = "U"; public static final String STATUS_PROCESSED = "P"; - private static final int MAX_VERSION_LENGTH = 100; + public static final int MAX_VERSION_LENGTH = 100; private Long id; private String uuid; @@ -122,7 +122,7 @@ public final class SnapshotDto { public SnapshotDto setCodePeriodVersion(@Nullable String codePeriodVersion) { if (codePeriodVersion != null) { checkArgument(codePeriodVersion.length() <= MAX_VERSION_LENGTH, - "Event name length (%s) is longer than the maximum authorized (%s). '%s' was provided.", codePeriodVersion.length(), MAX_VERSION_LENGTH, codePeriodVersion); + "codePeriodVersion length (%s) is longer than the maximum authorized (%s). '%s' was provided.", codePeriodVersion.length(), MAX_VERSION_LENGTH, codePeriodVersion); } this.codePeriodVersion = codePeriodVersion; return this; diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/component/SnapshotDtoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/component/SnapshotDtoTest.java index b57b9dd068c..bc5eaa6a8a8 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/component/SnapshotDtoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/component/SnapshotDtoTest.java @@ -62,7 +62,8 @@ public class SnapshotDtoTest { snapshotDto.setCodePeriodVersion(repeat("a", 100)); expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Event name length (101) is longer than the maximum authorized (100). " + + expectedException.expectMessage("codePeriodVersion" + + " length (101) is longer than the maximum authorized (100). " + "'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' was provided."); snapshotDto.setCodePeriodVersion(repeat("a", 101)); diff --git a/server/sonar-server/src/main/java/org/sonar/server/projectanalysis/ws/CreateEventAction.java b/server/sonar-server/src/main/java/org/sonar/server/projectanalysis/ws/CreateEventAction.java index 29a9c35102b..989df2eb478 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/projectanalysis/ws/CreateEventAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/projectanalysis/ws/CreateEventAction.java @@ -52,6 +52,7 @@ import static org.sonar.db.event.EventValidator.MAX_NAME_LENGTH; import static org.sonar.server.projectanalysis.ws.EventCategory.OTHER; import static org.sonar.server.projectanalysis.ws.EventCategory.VERSION; import static org.sonar.server.projectanalysis.ws.EventCategory.fromLabel; +import static org.sonar.server.projectanalysis.ws.EventValidator.checkVersionName; import static org.sonar.server.projectanalysis.ws.ProjectAnalysesWsParameters.PARAM_ANALYSIS; import static org.sonar.server.projectanalysis.ws.ProjectAnalysesWsParameters.PARAM_CATEGORY; import static org.sonar.server.projectanalysis.ws.ProjectAnalysesWsParameters.PARAM_NAME; @@ -150,6 +151,7 @@ public class CreateEventAction implements ProjectAnalysesWsAction { private void checkRequest(CreateEventRequest request, ComponentDto component) { userSession.checkComponentPermission(UserRole.ADMIN, component); checkArgument(EventCategory.VERSION != request.getCategory() || Qualifiers.PROJECT.equals(component.qualifier()), "A version event must be created on a project"); + checkVersionName(request.getCategory(), request.getName()); } private static CreateEventRequest toAddEventRequest(Request request) { diff --git a/server/sonar-server/src/main/java/org/sonar/server/projectanalysis/ws/EventValidator.java b/server/sonar-server/src/main/java/org/sonar/server/projectanalysis/ws/EventValidator.java index 336ced6a698..c6f293eb819 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/projectanalysis/ws/EventValidator.java +++ b/server/sonar-server/src/main/java/org/sonar/server/projectanalysis/ws/EventValidator.java @@ -23,6 +23,8 @@ import com.google.common.base.Joiner; import com.google.common.collect.ImmutableSet; import java.util.Set; import java.util.function.Consumer; +import javax.annotation.Nullable; +import org.sonar.db.component.SnapshotDto; import org.sonar.db.event.EventDto; import static com.google.common.base.Preconditions.checkArgument; @@ -43,4 +45,16 @@ class EventValidator { "Event of category '%s' cannot be modified. Authorized categories: %s", EventCategory.fromLabel(event.getCategory()), AUTHORIZED_CATEGORIES_INLINED); } + + static void checkVersionName(EventCategory category, @Nullable String name) { + checkVersionName(category.getLabel(), name); + } + + static void checkVersionName(@Nullable String category, @Nullable String name) { + if (VERSION.getLabel().equals(category) && name != null) { + // check against max version length defined on SnapshotDto to enforce consistency between version events and snapshot versions + checkArgument(name.length() <= SnapshotDto.MAX_VERSION_LENGTH, + "Event name length (%s) is longer than the maximum authorized (%s). '%s' was provided.", name.length(), SnapshotDto.MAX_VERSION_LENGTH, name); + } + } } diff --git a/server/sonar-server/src/main/java/org/sonar/server/projectanalysis/ws/UpdateEventAction.java b/server/sonar-server/src/main/java/org/sonar/server/projectanalysis/ws/UpdateEventAction.java index e0309b7c5ec..c63827aea36 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/projectanalysis/ws/UpdateEventAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/projectanalysis/ws/UpdateEventAction.java @@ -46,6 +46,7 @@ import static java.util.Objects.requireNonNull; import static java.util.Optional.ofNullable; import static org.apache.commons.lang.StringUtils.isNotBlank; import static org.sonar.server.projectanalysis.ws.EventValidator.checkModifiable; +import static org.sonar.server.projectanalysis.ws.EventValidator.checkVersionName; import static org.sonar.server.ws.WsUtils.writeProtobuf; import static org.sonar.server.projectanalysis.ws.EventCategory.VERSION; import static org.sonar.server.projectanalysis.ws.EventCategory.fromLabel; @@ -53,7 +54,6 @@ import static org.sonar.server.projectanalysis.ws.ProjectAnalysesWsParameters.PA import static org.sonar.server.projectanalysis.ws.ProjectAnalysesWsParameters.PARAM_NAME; public class UpdateEventAction implements ProjectAnalysesWsAction { - private static final int MAX_NAME_LENGTH = 100; private final DbClient dbClient; private final UserSession userSession; @@ -152,13 +152,7 @@ public class UpdateEventAction implements ProjectAnalysesWsAction { } private static Consumer checkVersionNameLength(UpdateEventRequest request) { - String name = request.getName(); - return candidateEvent -> { - if (name != null && VERSION.getLabel().equals(candidateEvent.getCategory())) { - checkArgument(name.length() <= MAX_NAME_LENGTH, - "Version length (%s) is longer than the maximum authorized (%s). '%s' was provided.", name.length(), MAX_NAME_LENGTH, name); - } - }; + return candidateEvent -> checkVersionName(candidateEvent.getCategory(), request.getName()); } private SnapshotDto getAnalysis(DbSession dbSession, EventDto event) { diff --git a/server/sonar-server/src/test/java/org/sonar/server/projectanalysis/ws/UpdateEventActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/projectanalysis/ws/UpdateEventActionTest.java index 3c1b4d10097..ce8f057c75b 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/projectanalysis/ws/UpdateEventActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/projectanalysis/ws/UpdateEventActionTest.java @@ -236,7 +236,7 @@ public class UpdateEventActionTest { call("E2", repeat("a", 100)); expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Version length (101) is longer than the maximum authorized (100). 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' was provided"); + expectedException.expectMessage("Event name length (101) is longer than the maximum authorized (100). 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' was provided"); call("E2", repeat("a", 101)); } -- 2.39.5