]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-17352 Fix quality issues
authorLéo Geoffroy <99647462+leo-geoffroy-sonarsource@users.noreply.github.com>
Tue, 11 Oct 2022 09:00:11 +0000 (11:00 +0200)
committersonartech <sonartech@sonarsource.com>
Wed, 12 Oct 2022 20:03:44 +0000 (20:03 +0000)
Co-authored-by: Duarte Meneses <duarte.meneses@sonarsource.com>
server/sonar-db-dao/src/main/java/org/sonar/db/component/ComponentDao.java
server/sonar-db-dao/src/test/java/org/sonar/db/component/ComponentDaoTest.java
server/sonar-server-common/src/test/java/org/sonar/server/issue/index/IssueIteratorFactoryTest.java
server/sonar-webserver-webapi/src/main/java/org/sonar/server/hotspot/ws/HotspotWsResponseFormatter.java
server/sonar-webserver-webapi/src/main/java/org/sonar/server/measure/ws/ComponentTreeData.java
server/sonar-webserver-webapi/src/main/java/org/sonar/server/setting/ws/ValuesAction.java
server/sonar-webserver-webapi/src/test/java/org/sonar/server/branch/ws/ListActionTest.java
server/sonar-webserver-webapi/src/test/java/org/sonar/server/notification/ws/AddActionTest.java
server/sonar-webserver-webapi/src/test/java/org/sonar/server/projectanalysis/ws/UnsetBaselineActionTest.java
server/sonar-webserver-webapi/src/test/java/org/sonar/server/projectdump/ws/ExportActionTest.java
server/sonar-webserver-webapi/src/test/java/org/sonar/server/ui/ws/ComponentActionTest.java

index c561e71775ead49059b007517838de48bcb2cbd6..4eb3b7e177d9c40e1fd0c1a95398f6644845d772 100644 (file)
@@ -392,7 +392,7 @@ public class ComponentDao implements Dao {
     mapper(session).setPrivateForRootComponentUuid(branchUuid, isPrivate);
   }
 
-  public void setPrivateForRootComponentUuid(DbSession session, String branchUuid, boolean isPrivate, @Nullable String qualifier, String componentKey, String componentName) {
+  public void setPrivateForRootComponentUuid(DbSession session, String branchUuid, boolean isPrivate, String qualifier, String componentKey, String componentName) {
     ComponentNewValue componentNewValue = new ComponentNewValue(branchUuid, componentName, componentKey, isPrivate, qualifier);
     auditPersister.updateComponentVisibility(session, componentNewValue);
     mapper(session).setPrivateForRootComponentUuid(branchUuid, isPrivate);
index 3533a00faea2f1d1c79469f75d9b3c8f32c44003..706b10129175d88ebde6f902aab7bf0faaa9dd72 100644 (file)
@@ -299,7 +299,9 @@ public class ComponentDaoTest {
 
   @Test
   public void select_by_keys_throws_ISE_if_both_branch_and_pr_are_passed() {
-    assertThatThrownBy(() -> underTest.selectByKeys(db.getSession(), List.of("key"), "branch", "pr"))
+    DbSession session = db.getSession();
+    List<String> keys = List.of("key");
+    assertThatThrownBy(() -> underTest.selectByKeys(session, keys, "branch", "pr"))
       .isInstanceOf(IllegalStateException.class);
   }
 
index f77fa99816dca55847177aa850b1c3cf845e325b..98e203b53414112d34fa9ac537de134bed98bd0f 100644 (file)
@@ -142,10 +142,10 @@ public class IssueIteratorFactoryTest {
       .map(project2Component -> dbTester.issues().insert(rule, project2, project2Component).getKey())
       .toArray(String[]::new);
 
-    assertThat(issuesByKey(factory -> factory.createForBranch(project1.uuid())).keySet())
-      .containsOnly(project1IssueKeys);
-    assertThat(issuesByKey(factory -> factory.createForBranch(project2.uuid())).keySet())
-      .containsOnly(project2IssueKeys);
+    assertThat(issuesByKey(factory -> factory.createForBranch(project1.uuid())))
+      .containsOnlyKeys(project1IssueKeys);
+    assertThat(issuesByKey(factory -> factory.createForBranch(project2.uuid())))
+      .containsOnlyKeys(project2IssueKeys);
     assertThat(issuesByKey(factory -> factory.createForBranch("does not exist")))
       .isEmpty();
   }
index 2171dfeabb02a914f6e2461228fa7d1d68b49192..7a2a6eab358f8c8608e6913e9f7563891482331c 100644 (file)
@@ -45,8 +45,8 @@ public class HotspotWsResponseFormatter {
     return builder.build();
   }
 
-  Hotspots.Component formatComponent(Hotspots.Component.Builder builder, ComponentDto component, BranchDto branchDto) {
-    if (branchDto.isMain()) {
+  Hotspots.Component formatComponent(Hotspots.Component.Builder builder, ComponentDto component, @Nullable BranchDto branchDto) {
+    if (branchDto == null || branchDto.isMain()) {
       return formatComponent(builder, component, null, null);
     }
     return formatComponent(builder, component, branchDto.getBranchKey(), branchDto.getPullRequestKey());
index 151c322bddfb9530bbe6af2d84d2f620ae50219e..83141c2f6b7d70ab8792ad2d907386f58ea53c6e 100644 (file)
@@ -72,7 +72,6 @@ class ComponentTreeData {
     return componentCount;
   }
 
-  @CheckForNull
   public Map<String, ComponentDto> getReferenceComponentsByUuid() {
     return referenceComponentsByUuid;
   }
index 6048974fec220f8cf71436c9928acf234abb66b6..038bbe20bacfc3c2ef8aa75e0d19fa4062aced3d 100644 (file)
@@ -123,7 +123,6 @@ public class ValuesAction implements SettingsWsAction {
     try (DbSession dbSession = dbClient.openSession(true)) {
       ValuesRequest valuesRequest = ValuesRequest.from(request);
       Optional<ComponentDto> component = loadComponent(dbSession, valuesRequest);
-      // TODO can portfolios fail here?
       BranchDto branchDto = component.map(c -> componentFinder.getBranchByUuid(dbSession, c.branchUuid())).orElse(null);
 
       Set<String> keys = loadKeys(valuesRequest);
@@ -177,7 +176,7 @@ public class ValuesAction implements SettingsWsAction {
   }
 
   @CheckForNull
-  private String getBranchKeySafely(@Nullable BranchDto branchDto) {
+  private static String getBranchKeySafely(@Nullable BranchDto branchDto) {
     if (branchDto != null) {
       return branchDto.isMain() ? null : branchDto.getBranchKey();
     }
index dd17b32b9425d43dcb6f9c8ea6f1e55c152caee6..1ef0af77e4d7c3868433cfbac842ae4a1f74b335 100644 (file)
@@ -39,6 +39,7 @@ import org.sonar.server.issue.index.IssueIndexer;
 import org.sonar.server.issue.index.IssueIteratorFactory;
 import org.sonar.server.permission.index.PermissionIndexerTester;
 import org.sonar.server.tester.UserSessionRule;
+import org.sonar.server.ws.TestRequest;
 import org.sonar.server.ws.WsActionTester;
 import org.sonarqube.ws.Common.BranchType;
 import org.sonarqube.ws.ProjectBranches;
@@ -274,8 +275,8 @@ public class ListActionTest {
     ComponentDto project = db.components().insertPrivateProject();
     ComponentDto file = db.components().insertComponent(ComponentTesting.newFileDto(project));
     userSession.logIn().addProjectPermission(USER, project);
-
-    assertThatThrownBy(() -> ws.newRequest().setParam("project", file.getKey()).execute())
+    TestRequest request = ws.newRequest().setParam("project", file.getKey());
+    assertThatThrownBy(request::execute)
       .isInstanceOf(NotFoundException.class)
       .hasMessageContaining("Project '" + file.getKey() + "' not found");
   }
index 05cc511c98d6b97db017cf22845df17bf2ade665..8f5d78402f5b6f1fe4cee189a9b9201abfd1258b 100644 (file)
@@ -318,8 +318,9 @@ public class AddActionTest {
     userSession.logIn().setNonSystemAdministrator();
     when(dispatchers.getGlobalDispatchers()).thenReturn(singletonList(NOTIF_MY_NEW_ISSUES));
     when(dispatchers.getProjectDispatchers()).thenReturn(singletonList(NOTIF_MY_NEW_ISSUES));
-
-    assertThatThrownBy(() -> call(NOTIF_MY_NEW_ISSUES, null, project.getKey(), userSession.getLogin()))
+    String key = project.getKey();
+    String login = userSession.getLogin();
+    assertThatThrownBy(() -> call(NOTIF_MY_NEW_ISSUES, null, key, login))
       .isInstanceOf(ForbiddenException.class);
   }
 
index 25d6eb6c67bac8055f1dcd28043a5dcd6f489e98..335363f7e5eb30340cbf2b1f7a78ae3adedabeed 100644 (file)
@@ -143,7 +143,8 @@ public class UnsetBaselineActionTest {
     String branchName = randomAlphanumeric(248);
     db.components().insertProjectBranch(project, b -> b.setKey(branchName));
 
-    assertThatThrownBy(() ->  call(project.getKey(), branchName))
+    String key = project.getKey();
+    assertThatThrownBy(() ->  call(key, branchName))
       .isInstanceOf(ForbiddenException.class)
       .hasMessage("Insufficient privileges");
   }
index c70f5026e176e89ab15c5b571cc0b26db78c36b9..f70f3ff900e27e947bf9d7f154ce39b2c571c487 100644 (file)
@@ -34,6 +34,7 @@ import org.sonar.server.ce.projectdump.ExportSubmitter;
 import org.sonar.server.component.ComponentFinder;
 import org.sonar.server.exceptions.ForbiddenException;
 import org.sonar.server.tester.UserSessionRule;
+import org.sonar.server.ws.TestRequest;
 import org.sonar.server.ws.TestResponse;
 import org.sonar.server.ws.WsActionTester;
 
@@ -86,7 +87,8 @@ public class ExportActionTest {
   public void fails_if_not_project_administrator() {
     userSession.logIn();
 
-    assertThatThrownBy(() -> actionTester.newRequest().setMethod("POST").setParam("key", project.getKey()).execute())
+    TestRequest request = actionTester.newRequest().setMethod("POST").setParam("key", project.getKey());
+    assertThatThrownBy(request::execute)
       .isInstanceOf(ForbiddenException.class);
   }
 
@@ -105,7 +107,8 @@ public class ExportActionTest {
   public void fails_to_trigger_task_if_anonymous() {
     userSession.anonymous();
 
-    assertThatThrownBy(() -> actionTester.newRequest().setMethod("POST").setParam("key", project.getKey()).execute())
+    TestRequest request = actionTester.newRequest().setMethod("POST").setParam("key", project.getKey());
+    assertThatThrownBy(request::execute)
       .isInstanceOf(ForbiddenException.class)
       .hasMessage("Insufficient privileges");
   }
index f817b25019c782fa635c410197857b1664ea6c2e..851f98214bc680ee188880d116bfa5d8d01e422a 100644 (file)
@@ -772,8 +772,8 @@ public class ComponentActionTest {
     ComponentDto directory = componentDbTester.insertComponent(newDirectory(module, "src/main/xoo"));
     userSession.addProjectPermission(UserRole.USER, project);
     init();
-
-    assertThatThrownBy(() -> execute(directory.getKey()))
+    String dirKey = directory.getKey();
+    assertThatThrownBy(() -> execute(dirKey))
       .isInstanceOf(BadRequestException.class);
   }