diff options
author | Julien Lancelot <julien.lancelot@sonarsource.com> | 2014-10-03 16:13:36 +0200 |
---|---|---|
committer | Julien Lancelot <julien.lancelot@sonarsource.com> | 2014-10-03 16:13:47 +0200 |
commit | 1f1399a327dd79619ec950ebdd8474a35fde0866 (patch) | |
tree | e0a5894c03e785c25952cf454d7e4a7bd24ee8c9 /server/sonar-server | |
parent | 16d1264cf1bc64c8eb1459d8a5fd5ee9be2e9cd7 (diff) | |
download | sonarqube-1f1399a327dd79619ec950ebdd8474a35fde0866.tar.gz sonarqube-1f1399a327dd79619ec950ebdd8474a35fde0866.zip |
Fix quality flaws
Diffstat (limited to 'server/sonar-server')
7 files changed, 75 insertions, 77 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/component/ComponentService.java b/server/sonar-server/src/main/java/org/sonar/server/component/ComponentService.java index c70e96f8541..0f02f07b5c8 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/component/ComponentService.java +++ b/server/sonar-server/src/main/java/org/sonar/server/component/ComponentService.java @@ -30,6 +30,7 @@ import org.sonar.core.preview.PreviewCache; import org.sonar.core.resource.ResourceDto; import org.sonar.core.resource.ResourceKeyUpdaterDao; import org.sonar.server.db.DbClient; +import org.sonar.server.exceptions.NotFoundException; import org.sonar.server.permission.InternalPermissionService; import org.sonar.server.user.UserSession; @@ -87,10 +88,10 @@ public class ComponentService implements ServerComponent { resourceKeyUpdaterDao.updateKey(projectOrModule.getId(), newKey); session.commit(); - ResourceDto newRootProject = dbClient.resourceDao().getRootProjectByComponentKey(session, newKey); - updateIssuesIndex(session, oldRootProject.getKey(), newRootProject.getKey()); + String newRootProjectKey = getRootProjectKeyByComponentKey(session, newKey); + updateIssuesIndex(session, oldRootProject.getKey(), newRootProjectKey); - previewCache.reportResourceModification(newRootProject.getKey()); + previewCache.reportResourceModification(newRootProjectKey); session.commit(); } finally { @@ -119,7 +120,7 @@ public class ComponentService implements ServerComponent { resourceKeyUpdaterDao.bulkUpdateKey(project.getId(), stringToReplace, replacementString); session.commit(); - AuthorizedComponentDto newProject = dbClient.componentDao().getNullableAuthorizedComponentById(project.getId(), session); + AuthorizedComponentDto newProject = dbClient.componentDao().getAuthorizedComponentById(project.getId(), session); updateIssuesIndex(session, projectKey, newProject.key()); previewCache.reportResourceModification(newProject.key()); @@ -142,4 +143,12 @@ public class ComponentService implements ServerComponent { ImmutableMap.of("project", newKey)); } + private String getRootProjectKeyByComponentKey(DbSession session, String key){ + ResourceDto root = dbClient.resourceDao().getRootProjectByComponentKey(session, key); + if (root != null) { + return root.getKey(); + } + throw new NotFoundException(String.format("Root project of '%s' has not been found", key)); + } + } diff --git a/server/sonar-server/src/main/java/org/sonar/server/issue/IssueQuery.java b/server/sonar-server/src/main/java/org/sonar/server/issue/IssueQuery.java index e8935b5e87a..41f66724da2 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/issue/IssueQuery.java +++ b/server/sonar-server/src/main/java/org/sonar/server/issue/IssueQuery.java @@ -340,7 +340,8 @@ public class IssueQuery { public IssueQuery build() { if (issueKeys != null) { - Preconditions.checkArgument(issueKeys.size() <= QueryContext.MAX_LIMIT, "Number of issue keys must be less than " + QueryContext.MAX_LIMIT + " (got " + issueKeys.size() + ")"); + Preconditions.checkArgument(issueKeys.size() <= QueryContext.MAX_LIMIT, "Number of issue keys must be less than " + QueryContext.MAX_LIMIT + " (got " + issueKeys.size() + + ")"); } return new IssueQuery(this); } diff --git a/server/sonar-server/src/main/java/org/sonar/server/issue/IssueService.java b/server/sonar-server/src/main/java/org/sonar/server/issue/IssueService.java index a281c14f5fa..e75111e83c7 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/issue/IssueService.java +++ b/server/sonar-server/src/main/java/org/sonar/server/issue/IssueService.java @@ -293,7 +293,7 @@ public class IssueService implements ServerComponent { Map<String, Integer> result = newLinkedHashMap(); List<FacetValue> facetValues = indexClient.get(IssueIndex.class).listAssignees(query); for (FacetValue facetValue : facetValues) { - if (facetValue.getKey().equals("_notAssigned_")) { + if ("_notAssigned_".equals(facetValue.getKey())) { result.put(null, facetValue.getValue()); } else { result.put(facetValue.getKey(), facetValue.getValue()); diff --git a/server/sonar-server/src/main/java/org/sonar/server/issue/ServerIssueStorage.java b/server/sonar-server/src/main/java/org/sonar/server/issue/ServerIssueStorage.java index bdc93fbc925..5bb41739273 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/issue/ServerIssueStorage.java +++ b/server/sonar-server/src/main/java/org/sonar/server/issue/ServerIssueStorage.java @@ -26,8 +26,6 @@ import org.sonar.core.issue.db.IssueDto; import org.sonar.core.issue.db.IssueStorage; import org.sonar.core.persistence.DbSession; import org.sonar.core.persistence.MyBatis; -import org.sonar.core.resource.ResourceDto; -import org.sonar.core.resource.ResourceQuery; import org.sonar.server.db.DbClient; import java.util.Date; @@ -45,8 +43,8 @@ public class ServerIssueStorage extends IssueStorage implements ServerComponent } protected void doInsert(DbSession session, Date now, DefaultIssue issue) { - long componentId = componentId(issue); - long projectId = projectId(issue); + long componentId = componentId(session, issue); + long projectId = projectId(session, issue); int ruleId = ruleId(issue); IssueDto dto = IssueDto.toDtoForInsert(issue, componentId, projectId, ruleId, now); @@ -54,26 +52,16 @@ public class ServerIssueStorage extends IssueStorage implements ServerComponent } protected void doUpdate(DbSession session, Date now, DefaultIssue issue) { - IssueDto dto = IssueDto.toDtoForUpdate(issue, projectId(issue), now); + IssueDto dto = IssueDto.toDtoForUpdate(issue, projectId(session, issue), now); dbClient.issueDao().update(session, dto); } - protected long componentId(DefaultIssue issue) { - // TODO should be using ComponentDao - ResourceDto resourceDto = dbClient.resourceDao().getResource(ResourceQuery.create().setKey(issue.componentKey())); - if (resourceDto == null) { - throw new IllegalStateException("Unknown component: " + issue.componentKey()); - } - return resourceDto.getId(); + protected long componentId(DbSession session, DefaultIssue issue) { + return dbClient.componentDao().getAuthorizedComponentByKey(issue.componentKey(), session).getId(); } - protected long projectId(DefaultIssue issue) { - // TODO should be using ComponentDao - ResourceDto resourceDto = dbClient.resourceDao().getResource(ResourceQuery.create().setKey(issue.projectKey())); - if (resourceDto == null) { - throw new IllegalStateException("Unknown project: " + issue.projectKey()); - } - return resourceDto.getId(); + protected long projectId(DbSession session, DefaultIssue issue) { + return dbClient.componentDao().getAuthorizedComponentByKey(issue.projectKey(), session).getId(); } } diff --git a/server/sonar-server/src/main/java/org/sonar/server/platform/BackendCleanup.java b/server/sonar-server/src/main/java/org/sonar/server/platform/BackendCleanup.java index dcc0185feb3..0bd07c3e198 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/platform/BackendCleanup.java +++ b/server/sonar-server/src/main/java/org/sonar/server/platform/BackendCleanup.java @@ -146,10 +146,23 @@ public class BackendCleanup implements ServerComponent { } } + /** + * Completely remove a index with all types + */ public void clearIndex(IndexDefinition indexDefinition){ searchClient.prepareDeleteByQuery(searchClient.admin().cluster().prepareState().get() .getState().getMetaData().concreteIndices(new String[]{indexDefinition.getIndexName()})) .setQuery(QueryBuilders.matchAllQuery()) .get(); } + + /** + * Remove only the type of an index + */ + public void clearIndexType(IndexDefinition indexDefinition){ + searchClient.prepareDeleteByQuery(searchClient.admin().cluster().prepareState().get() + .getState().getMetaData().concreteIndices(new String[]{indexDefinition.getIndexName()})).setTypes(indexDefinition.getIndexType()) + .setQuery(QueryBuilders.matchAllQuery()) + .get(); + } } diff --git a/server/sonar-server/src/test/java/org/sonar/server/batch/UploadReportActionMediumTest.java b/server/sonar-server/src/test/java/org/sonar/server/batch/UploadReportActionMediumTest.java index f0cc57a8ab9..af99070d482 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/batch/UploadReportActionMediumTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/batch/UploadReportActionMediumTest.java @@ -96,28 +96,6 @@ public class UploadReportActionMediumTest { } @Test - public void add_project_issue_permission_index() throws Exception { - ComponentDto project = new ComponentDto() - .setKey("MyProject"); - db.componentDao().insert(session, project); - - // project can be seen by anyone - tester.get(PermissionFacade.class).insertGroupPermission(project.getId(), DefaultGroups.ANYONE, UserRole.USER, session); - - session.commit(); - - assertThat(tester.get(IssueAuthorizationIndex.class).getNullableByKey(project.getKey())).isNull(); - - MockUserSession.set().setLogin("john").setGlobalPermissions(GlobalPermissions.SCAN_EXECUTION); - WsTester.TestRequest request = wsTester.newGetRequest(BatchWs.API_ENDPOINT, UploadReportAction.UPLOAD_REPORT_ACTION); - request.setParam(UploadReportAction.PARAM_PROJECT, project.key()); - request.execute(); - - // Check that issue authorization index has been created - assertThat(tester.get(IssueAuthorizationIndex.class).getNullableByKey(project.getKey())).isNotNull(); - } - - @Test public void add_analysis_report_in_database() throws Exception { final String projectKey = "123456789-987654321"; ComponentDto project = new ComponentDto() @@ -157,7 +135,29 @@ public class UploadReportActionMediumTest { } @Test - public void index_project_issues() throws Exception { + public void add_project_issue_permission_in_index() throws Exception { + ComponentDto project = new ComponentDto() + .setKey("MyProject"); + db.componentDao().insert(session, project); + + // project can be seen by anyone + tester.get(PermissionFacade.class).insertGroupPermission(project.getId(), DefaultGroups.ANYONE, UserRole.USER, session); + + session.commit(); + + assertThat(tester.get(IssueAuthorizationIndex.class).getNullableByKey(project.getKey())).isNull(); + + MockUserSession.set().setLogin("john").setGlobalPermissions(GlobalPermissions.SCAN_EXECUTION); + WsTester.TestRequest request = wsTester.newGetRequest(BatchWs.API_ENDPOINT, UploadReportAction.UPLOAD_REPORT_ACTION); + request.setParam(UploadReportAction.PARAM_PROJECT, project.key()); + request.execute(); + + // Check that issue authorization index has been created + assertThat(tester.get(IssueAuthorizationIndex.class).getNullableByKey(project.getKey())).isNotNull(); + } + + @Test + public void add_issues_in_index() throws Exception { ComponentDto project = new ComponentDto() .setKey("MyProject"); db.componentDao().insert(session, project); @@ -189,8 +189,8 @@ public class UploadReportActionMediumTest { session.commit(); - // Clear issue index to simulate that the issue has been inserted by the batch, so that it's not yet index in E/S - tester.get(BackendCleanup.class).clearIndex(IndexDefinition.ISSUES); + // Clear issue index (and not issue authorization index) to simulate that the issue has been inserted by the batch, so that it's not yet index in E/S + tester.get(BackendCleanup.class).clearIndexType(IndexDefinition.ISSUES); assertThat(db.issueDao().getByKey(session, issue.getKey())).isNotNull(); assertThat(tester.get(IssueIndex.class).getNullableByKey(issue.getKey())).isNull(); diff --git a/server/sonar-server/src/test/java/org/sonar/server/issue/ServerIssueStorageTest.java b/server/sonar-server/src/test/java/org/sonar/server/issue/ServerIssueStorageTest.java index 1ce4db1a1b7..5372854fd95 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/issue/ServerIssueStorageTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/issue/ServerIssueStorageTest.java @@ -20,6 +20,7 @@ package org.sonar.server.issue; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.sonar.api.issue.internal.DefaultIssue; @@ -33,6 +34,7 @@ import org.sonar.api.utils.DateUtils; import org.sonar.api.utils.Duration; import org.sonar.api.utils.System2; import org.sonar.core.persistence.AbstractDaoTestCase; +import org.sonar.core.persistence.DbSession; import org.sonar.core.resource.ResourceDao; import org.sonar.server.component.db.ComponentDao; import org.sonar.server.db.DbClient; @@ -42,11 +44,11 @@ import java.util.Collection; import java.util.Date; import static org.fest.assertions.Assertions.assertThat; -import static org.fest.assertions.Fail.fail; public class ServerIssueStorageTest extends AbstractDaoTestCase { DbClient dbClient; + DbSession session; ServerIssueStorage storage; @@ -56,53 +58,37 @@ public class ServerIssueStorageTest extends AbstractDaoTestCase { new ComponentDao(System2.INSTANCE), new IssueDao(System2.INSTANCE), new ResourceDao(getMyBatis(), System2.INSTANCE)); + session = dbClient.openSession(false); storage = new ServerIssueStorage(getMyBatis(), new FakeRuleFinder(), dbClient); } + @After + public void tearDown() throws Exception { + session.close(); + } + @Test public void load_component_id_from_db() throws Exception { setupData("load_component_id_from_db"); + session.commit(); - long componentId = storage.componentId(new DefaultIssue().setComponentKey("struts:Action.java")); + long componentId = storage.componentId(session, new DefaultIssue().setComponentKey("struts:Action.java")); assertThat(componentId).isEqualTo(123); } @Test - public void fail_to_load_component_id_if_unknown_component() throws Exception { - setupData("empty"); - - try { - storage.componentId(new DefaultIssue().setComponentKey("struts:Action.java")); - fail(); - } catch (Exception e) { - assertThat(e).hasMessage("Unknown component: struts:Action.java"); - } - } - - @Test public void load_project_id_from_db() throws Exception { setupData("load_project_id_from_db"); + session.commit(); - long projectId = storage.projectId(new DefaultIssue().setProjectKey("struts")); + long projectId = storage.projectId(session, new DefaultIssue().setProjectKey("struts")); assertThat(projectId).isEqualTo(1); } @Test - public void fail_to_load_project_id_if_unknown_component() throws Exception { - setupData("empty"); - - try { - storage.projectId(new DefaultIssue().setProjectKey("struts")); - fail(); - } catch (Exception e) { - assertThat(e).hasMessage("Unknown project: struts"); - } - } - - @Test public void should_insert_new_issues() throws Exception { setupData("should_insert_new_issues"); @@ -116,6 +102,7 @@ public class ServerIssueStorageTest extends AbstractDaoTestCase { .setNew(true) .setRuleKey(RuleKey.of("squid", "AvoidCycle")) + .setProjectKey("struts") .setLine(5000) .setDebt(Duration.create(10L)) .setReporter("emmerik") |