From 769e6a497f24c71e3d11a711d5bd12f8cef413c5 Mon Sep 17 00:00:00 2001 From: Lukasz Jarocki Date: Tue, 16 Nov 2021 15:19:28 +0100 Subject: [PATCH] SONAR-13427 implement ITs --- .../db/project/ProjectBadgeTokenDao.java | 1 + .../server/badge/ws/ProjectBadgesSupport.java | 7 +-- .../server/badge/ws/MeasureActionTest.java | 2 +- .../badge/ws/QualityGateActionTest.java | 2 +- .../client/projectbadges/MeasureRequest.java | 13 +++++ .../projectbadges/ProjectBadgesService.java | 32 +++++++++++++ .../projectbadges/QualityGateRequest.java | 13 +++++ .../projectbadges/RenewTokenRequest.java | 47 +++++++++++++++++++ .../ws/client/projectbadges/TokenRequest.java | 47 +++++++++++++++++++ 9 files changed, 159 insertions(+), 5 deletions(-) create mode 100644 sonar-ws/src/main/java/org/sonarqube/ws/client/projectbadges/RenewTokenRequest.java create mode 100644 sonar-ws/src/main/java/org/sonarqube/ws/client/projectbadges/TokenRequest.java diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/project/ProjectBadgeTokenDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/project/ProjectBadgeTokenDao.java index 9565bf0a0a7..8a6a00d1eda 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/project/ProjectBadgeTokenDao.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/project/ProjectBadgeTokenDao.java @@ -65,5 +65,6 @@ public class ProjectBadgeTokenDao implements Dao { @CheckForNull public ProjectBadgeTokenDto selectTokenByProject(DbSession session, ProjectDto projectDto) { return mapper(session).selectTokenByProjectUuid(projectDto.getUuid()); + } } diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/badge/ws/ProjectBadgesSupport.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/badge/ws/ProjectBadgesSupport.java index 981f8a7b947..405cdef4dca 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/badge/ws/ProjectBadgesSupport.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/badge/ws/ProjectBadgesSupport.java @@ -40,6 +40,7 @@ public class ProjectBadgesSupport { private static final String PARAM_PROJECT = "project"; private static final String PARAM_BRANCH = "branch"; private static final String PARAM_TOKEN = "token"; + public static final String PROJECT_HAS_NOT_BEEN_FOUND = "Project has not been found"; private final ComponentFinder componentFinder; private final DbClient dbClient; @@ -83,7 +84,7 @@ public class ProjectBadgesSupport { return branch; } catch (NotFoundException e) { - throw new NotFoundException("Project has not been found"); + throw new NotFoundException(PROJECT_HAS_NOT_BEEN_FOUND); } } @@ -98,11 +99,11 @@ public class ProjectBadgesSupport { try { projectDto = componentFinder.getProjectOrApplicationByKey(dbSession, projectKey); } catch (NotFoundException e) { - throw new NotFoundException("Project has not been found"); + throw new NotFoundException(PROJECT_HAS_NOT_BEEN_FOUND); } String token = request.param(PARAM_TOKEN); if (projectDto.isPrivate() && !isTokenValid(dbSession, projectDto, token)) { - throw generateInvalidProjectException(); + throw new NotFoundException(PROJECT_HAS_NOT_BEEN_FOUND); } } } diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/badge/ws/MeasureActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/badge/ws/MeasureActionTest.java index e4e55bf1da3..e319a912e71 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/badge/ws/MeasureActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/badge/ws/MeasureActionTest.java @@ -329,7 +329,7 @@ public class MeasureActionTest { .setParam("metric", metric.getKey()) .execute(); - checkError(response, "Project is invalid"); + checkError(response, "Project has not been found"); } @Test diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/badge/ws/QualityGateActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/badge/ws/QualityGateActionTest.java index f139f890bf0..29fc9c9d4d2 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/badge/ws/QualityGateActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/badge/ws/QualityGateActionTest.java @@ -197,7 +197,7 @@ public class QualityGateActionTest { .setParam("project", project.getKey()) .execute(); - checkError(response, "Project is invalid"); + checkError(response, "Project has not been found"); } @Test diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/projectbadges/MeasureRequest.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/projectbadges/MeasureRequest.java index 6d08e716944..ddecb76a245 100644 --- a/sonar-ws/src/main/java/org/sonarqube/ws/client/projectbadges/MeasureRequest.java +++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/projectbadges/MeasureRequest.java @@ -33,6 +33,7 @@ public class MeasureRequest { private String branch; private String metric; private String project; + private String token; /** * Example value: "feature/my_branch" @@ -84,4 +85,16 @@ public class MeasureRequest { public String getProject() { return project; } + + /** + * Tthis an optional parameter. + */ + public MeasureRequest setToken(String token) { + this.token = token; + return this; + } + + public String getToken() { + return token; + } } diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/projectbadges/ProjectBadgesService.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/projectbadges/ProjectBadgesService.java index 393a0355dcf..f06083b0d5c 100644 --- a/sonar-ws/src/main/java/org/sonarqube/ws/client/projectbadges/ProjectBadgesService.java +++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/projectbadges/ProjectBadgesService.java @@ -21,9 +21,12 @@ package org.sonarqube.ws.client.projectbadges; import javax.annotation.Generated; import org.sonarqube.ws.MediaTypes; +import org.sonarqube.ws.ProjectBadgeToken.TokenWsResponse; import org.sonarqube.ws.client.BaseService; import org.sonarqube.ws.client.GetRequest; +import org.sonarqube.ws.client.PostRequest; import org.sonarqube.ws.client.WsConnector; +import org.sonarqube.ws.client.WsResponse; /** * @see Further information about this web service online @@ -48,6 +51,7 @@ public class ProjectBadgesService extends BaseService { .setParam("branch", request.getBranch()) .setParam("metric", request.getMetric()) .setParam("project", request.getProject()) + .setParam("token", request.getToken()) .setMediaType(MediaTypes.JSON) ).content(); } @@ -64,7 +68,35 @@ public class ProjectBadgesService extends BaseService { new GetRequest(path("quality_gate")) .setParam("branch", request.getBranch()) .setParam("project", request.getProject()) + .setParam("token", request.getToken()) .setMediaType(MediaTypes.JSON) ).content(); } + + /** + * + * This is part of the internal API. + * This is a GET request. + * @see Further information about this action online (including a response example) + * @since 9.2 + * @return + */ + public TokenWsResponse token(TokenRequest request) { + return call( + new GetRequest(path("token")) + .setParam("project", request.getProject()) + .setMediaType(MediaTypes.JSON), + TokenWsResponse.parser() + ); + } + + + public WsResponse renewToken(RenewTokenRequest request) { + return call( + new PostRequest(path("renew_token")) + .setParam("project", request.getProject()) + .setMediaType(MediaTypes.JSON) + ); + + } } diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/projectbadges/QualityGateRequest.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/projectbadges/QualityGateRequest.java index dc3e47f78d9..d345552ba55 100644 --- a/sonar-ws/src/main/java/org/sonarqube/ws/client/projectbadges/QualityGateRequest.java +++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/projectbadges/QualityGateRequest.java @@ -32,6 +32,7 @@ public class QualityGateRequest { private String branch; private String project; + private String token; /** * Example value: "feature/my_branch" @@ -57,4 +58,16 @@ public class QualityGateRequest { public String getProject() { return project; } + + /** + * This is aan optional parameter. + */ + public QualityGateRequest setToken(String token) { + this.token = token; + return this; + } + + public String getToken() { + return token; + } } diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/projectbadges/RenewTokenRequest.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/projectbadges/RenewTokenRequest.java new file mode 100644 index 00000000000..8a8e86b7edb --- /dev/null +++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/projectbadges/RenewTokenRequest.java @@ -0,0 +1,47 @@ +/* + * SonarQube + * Copyright (C) 2009-2021 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.sonarqube.ws.client.projectbadges; + +import javax.annotation.Generated; + +/** + * This is part of the internal API. + * This is a POST request. + * @see Further information about this action online (including a response example) + * @since 9.2 + */ +@Generated("sonar-ws-generator") +public class RenewTokenRequest { + + private String project; + + /** + * This is a mandatory parameter. + * Example value: "my_project" + */ + public RenewTokenRequest setProject(String project) { + this.project = project; + return this; + } + + public String getProject() { + return project; + } +} diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/projectbadges/TokenRequest.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/projectbadges/TokenRequest.java new file mode 100644 index 00000000000..7054f2f6ec2 --- /dev/null +++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/projectbadges/TokenRequest.java @@ -0,0 +1,47 @@ +/* + * SonarQube + * Copyright (C) 2009-2021 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.sonarqube.ws.client.projectbadges; + +import javax.annotation.Generated; + +/** + * This is part of the internal API. + * This is a GET request. + * @see Further information about this action online (including a response example) + * @since 9.2 + */ +@Generated("sonar-ws-generator") +public class TokenRequest { + + private String project; + + /** + * This is a mandatory parameter. + * Example value: "my_project" + */ + public TokenRequest setProject(String project) { + this.project = project; + return this; + } + + public String getProject() { + return project; + } +} -- 2.39.5