From 0940aa7ec60e0499fa0b3c279fb640585035f339 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Lievremont Date: Mon, 2 Mar 2015 15:01:32 +0100 Subject: [PATCH] SONAR-5884 Apply feedback on unit test and field names --- .../sonar/server/source/ws/LinesAction.java | 8 +- .../server/source/ws/LinesActionTest.java | 169 +++++++++--------- .../ws/LinesActionTest/show_source.json | 12 +- 3 files changed, 94 insertions(+), 95 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/source/ws/LinesAction.java b/server/sonar-server/src/main/java/org/sonar/server/source/ws/LinesAction.java index 5f3c0a64328..4ad075bdb77 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/source/ws/LinesAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/source/ws/LinesAction.java @@ -95,7 +95,7 @@ public class LinesAction implements RequestHandler { public void handle(Request request, Response response) { String fileUuid = request.mandatoryParam("uuid"); ComponentDto component = componentService.getByUuid(fileUuid); - UserSession.get().checkComponentPermission(UserRole.CODEVIEWER, component.key()); + UserSession.get().checkProjectUuidPermission(UserRole.CODEVIEWER, component.projectUuid()); int from = Math.max(request.mandatoryParamAsInt("from"), 1); int to = (Integer) ObjectUtils.defaultIfNull(request.paramAsInt("to"), Integer.MAX_VALUE); @@ -121,9 +121,9 @@ public class LinesAction implements RequestHandler { .prop("scmRevision", line.scmRevision()); Date scmDate = line.scmDate(); json.prop("scmDate", scmDate == null ? null : DateUtils.formatDateTime(scmDate)); - json.prop("lineHits", line.utLineHits()) - .prop("conditions", line.utConditions()) - .prop("coveredConditions", line.utCoveredConditions()) + json.prop("utLineHits", line.utLineHits()) + .prop("utConditions", line.utConditions()) + .prop("utCoveredConditions", line.utCoveredConditions()) .prop("itLineHits", line.itLineHits()) .prop("itConditions", line.itConditions()) .prop("itCoveredConditions", line.itCoveredConditions()); diff --git a/server/sonar-server/src/test/java/org/sonar/server/source/ws/LinesActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/source/ws/LinesActionTest.java index cd0d23f83f1..acfc1527b63 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/source/ws/LinesActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/source/ws/LinesActionTest.java @@ -21,7 +21,6 @@ package org.sonar.server.source.ws; import com.google.common.collect.ImmutableList; import com.google.common.collect.Lists; -import com.google.common.collect.Maps; import org.apache.commons.lang.StringEscapeUtils; import org.junit.Before; import org.junit.Test; @@ -35,7 +34,6 @@ import org.sonar.core.component.ComponentDto; import org.sonar.server.component.ComponentService; import org.sonar.server.exceptions.ForbiddenException; import org.sonar.server.exceptions.NotFoundException; -import org.sonar.server.search.BaseNormalizer; import org.sonar.server.source.HtmlSourceDecorator; import org.sonar.server.source.index.SourceLineDoc; import org.sonar.server.source.index.SourceLineIndex; @@ -44,7 +42,6 @@ import org.sonar.server.user.MockUserSession; import org.sonar.server.ws.WsTester; import java.util.Date; -import java.util.Map; import static com.google.common.collect.Lists.newArrayList; import static org.assertj.core.api.Assertions.assertThat; @@ -93,66 +90,67 @@ public class LinesActionTest { @Test public void show_source() throws Exception { + String projectUuid = "abcd"; String componentUuid = "efgh"; Date updatedAt = new Date(); String scmDate = "2014-01-01T12:34:56.789Z"; - Map map1 = Maps.newHashMap(); - map1.put(SourceLineIndexDefinition.FIELD_PROJECT_UUID, "abcd"); - map1.put(SourceLineIndexDefinition.FIELD_FILE_UUID, "efgh"); - map1.put(SourceLineIndexDefinition.FIELD_LINE, 1); - map1.put(SourceLineIndexDefinition.FIELD_SCM_REVISION, "cafebabe"); - map1.put(SourceLineIndexDefinition.FIELD_SCM_DATE, scmDate); - map1.put(SourceLineIndexDefinition.FIELD_SCM_AUTHOR, "polop"); - map1.put(SourceLineIndexDefinition.FIELD_SOURCE, "class Polop {"); - map1.put(SourceLineIndexDefinition.FIELD_HIGHLIGHTING, "h1"); - map1.put(SourceLineIndexDefinition.FIELD_SYMBOLS, "palap"); - map1.put(SourceLineIndexDefinition.FIELD_UT_LINE_HITS, 3); - map1.put(SourceLineIndexDefinition.FIELD_UT_CONDITIONS, 2); - map1.put(SourceLineIndexDefinition.FIELD_UT_COVERED_CONDITIONS, 1); - map1.put(SourceLineIndexDefinition.FIELD_IT_LINE_HITS, 3); - map1.put(SourceLineIndexDefinition.FIELD_IT_CONDITIONS, 2); - map1.put(SourceLineIndexDefinition.FIELD_IT_COVERED_CONDITIONS, 1); - map1.put(SourceLineIndexDefinition.FIELD_DUPLICATIONS, ImmutableList.of()); - map1.put(BaseNormalizer.UPDATED_AT_FIELD, updatedAt); - SourceLineDoc line1 = new SourceLineDoc(map1); - Map map2 = Maps.newHashMap(); - map2.put(SourceLineIndexDefinition.FIELD_PROJECT_UUID, "abcd"); - map2.put(SourceLineIndexDefinition.FIELD_FILE_UUID, "efgh"); - map2.put(SourceLineIndexDefinition.FIELD_LINE, 2); - map2.put(SourceLineIndexDefinition.FIELD_SCM_REVISION, "cafebabe"); - map2.put(SourceLineIndexDefinition.FIELD_SCM_DATE, scmDate); - map2.put(SourceLineIndexDefinition.FIELD_SCM_AUTHOR, "polop"); - map2.put(SourceLineIndexDefinition.FIELD_SOURCE, " // Empty"); - map2.put(SourceLineIndexDefinition.FIELD_HIGHLIGHTING, "h2"); - map2.put(SourceLineIndexDefinition.FIELD_SYMBOLS, "pulup"); - map2.put(SourceLineIndexDefinition.FIELD_UT_LINE_HITS, 3); - map2.put(SourceLineIndexDefinition.FIELD_UT_CONDITIONS, 2); - map2.put(SourceLineIndexDefinition.FIELD_UT_COVERED_CONDITIONS, 1); - map2.put(SourceLineIndexDefinition.FIELD_IT_LINE_HITS, null); - map2.put(SourceLineIndexDefinition.FIELD_IT_CONDITIONS, null); - map2.put(SourceLineIndexDefinition.FIELD_IT_COVERED_CONDITIONS, null); - map2.put(SourceLineIndexDefinition.FIELD_DUPLICATIONS, ImmutableList.of(1)); - map2.put(BaseNormalizer.UPDATED_AT_FIELD, updatedAt); - SourceLineDoc line2 = new SourceLineDoc(map2); - Map map3 = Maps.newHashMap(); - map3.put(SourceLineIndexDefinition.FIELD_PROJECT_UUID, "abcd"); - map3.put(SourceLineIndexDefinition.FIELD_FILE_UUID, "efgh"); - map3.put(SourceLineIndexDefinition.FIELD_LINE, 3); - map3.put(SourceLineIndexDefinition.FIELD_SCM_REVISION, "cafebabe"); - map3.put(SourceLineIndexDefinition.FIELD_SCM_DATE, scmDate); - map3.put(SourceLineIndexDefinition.FIELD_SCM_AUTHOR, "polop"); - map3.put(SourceLineIndexDefinition.FIELD_SOURCE, "}"); - map3.put(SourceLineIndexDefinition.FIELD_HIGHLIGHTING, "h3"); - map3.put(SourceLineIndexDefinition.FIELD_SYMBOLS, "pylyp"); - map3.put(SourceLineIndexDefinition.FIELD_UT_LINE_HITS, null); - map3.put(SourceLineIndexDefinition.FIELD_UT_CONDITIONS, null); - map3.put(SourceLineIndexDefinition.FIELD_UT_COVERED_CONDITIONS, null); - map3.put(SourceLineIndexDefinition.FIELD_IT_LINE_HITS, 3); - map3.put(SourceLineIndexDefinition.FIELD_IT_CONDITIONS, 2); - map3.put(SourceLineIndexDefinition.FIELD_IT_COVERED_CONDITIONS, 1); - map3.put(SourceLineIndexDefinition.FIELD_DUPLICATIONS, ImmutableList.of()); - map3.put(BaseNormalizer.UPDATED_AT_FIELD, updatedAt); - SourceLineDoc line3 = new SourceLineDoc(map3); + SourceLineDoc line1 = new SourceLineDoc() + .setProjectUuid(projectUuid) + .setFileUuid(componentUuid) + .setLine(1) + .setScmRevision("cafebabe") + .setScmAuthor("polop") + .setSource("class Polop {") + .setHighlighting("h1") + .setSymbols("palap") + .setUtLineHits(3) + .setUtConditions(2) + .setUtCoveredConditions(1) + .setItLineHits(3) + .setItConditions(2) + .setItCoveredConditions(1) + .setDuplications(ImmutableList.of()) + .setUpdateDate(updatedAt); + line1.setField(SourceLineIndexDefinition.FIELD_SCM_DATE, scmDate); + + SourceLineDoc line2 = new SourceLineDoc() + .setProjectUuid(projectUuid) + .setFileUuid(componentUuid) + .setLine(2) + .setScmRevision("cafebabe") + .setScmAuthor("polop") + .setSource(" // Empty") + .setHighlighting("h2") + .setSymbols("pulup") + .setUtLineHits(3) + .setUtConditions(2) + .setUtCoveredConditions(1) + .setItLineHits(null) + .setItConditions(null) + .setItCoveredConditions(null) + .setDuplications(ImmutableList.of(1)) + .setUpdateDate(updatedAt); + line2.setField(SourceLineIndexDefinition.FIELD_SCM_DATE, scmDate); + + SourceLineDoc line3 = new SourceLineDoc() + .setProjectUuid(projectUuid) + .setFileUuid(componentUuid) + .setLine(3) + .setScmRevision("cafebabe") + .setScmAuthor("polop") + .setSource("}") + .setHighlighting("h3") + .setSymbols("pylyp") + .setUtLineHits(null) + .setUtConditions(null) + .setUtCoveredConditions(null) + .setItLineHits(3) + .setItConditions(2) + .setItCoveredConditions(1) + .setDuplications(ImmutableList.of()) + .setUpdateDate(updatedAt); + line3.setField(SourceLineIndexDefinition.FIELD_SCM_DATE, scmDate); + when(sourceLineIndex.getLines(eq(componentUuid), anyInt(), anyInt())).thenReturn(newArrayList( line1, line2, @@ -160,8 +158,8 @@ public class LinesActionTest { )); String componentKey = "componentKey"; - when(componentService.getByUuid(componentUuid)).thenReturn(new ComponentDto().setKey(componentKey)); - MockUserSession.set().setLogin("login").addComponentPermission(UserRole.CODEVIEWER, "polop", componentKey); + when(componentService.getByUuid(componentUuid)).thenReturn(new ComponentDto().setKey(componentKey).setProjectUuid(projectUuid)); + MockUserSession.set().setLogin("login").addProjectUuidPermissions(UserRole.CODEVIEWER, projectUuid); WsTester.TestRequest request = tester.newGetRequest("api/sources", "lines").setParam("uuid", componentUuid); // Using non-strict match b/c of dates @@ -171,11 +169,12 @@ public class LinesActionTest { @Test public void fail_to_show_source_if_no_source_found() throws Exception { String componentUuid = "abcd"; + String projectUuid = "efgh"; when(sourceLineIndex.getLines(anyString(), anyInt(), anyInt())).thenReturn(Lists.newArrayList()); String componentKey = "componentKey"; - when(componentService.getByUuid(componentUuid)).thenReturn(new ComponentDto().setKey(componentKey)); - MockUserSession.set().setLogin("login").addComponentPermission(UserRole.CODEVIEWER, "polop", componentKey); + when(componentService.getByUuid(componentUuid)).thenReturn(new ComponentDto().setKey(componentKey).setProjectUuid(projectUuid)); + MockUserSession.set().setLogin("login").addProjectUuidPermissions(UserRole.CODEVIEWER, projectUuid); try { WsTester.TestRequest request = tester.newGetRequest("api/sources", "lines").setParam("uuid", componentUuid); @@ -188,32 +187,32 @@ public class LinesActionTest { @Test public void show_source_with_from_and_to_params() throws Exception { + String projectUuid = "abcd"; String fileUuid = "efgh"; - Map fieldMap = Maps.newHashMap(); - fieldMap.put(SourceLineIndexDefinition.FIELD_PROJECT_UUID, "abcd"); - fieldMap.put(SourceLineIndexDefinition.FIELD_FILE_UUID, "efgh"); - fieldMap.put(SourceLineIndexDefinition.FIELD_LINE, 3); - fieldMap.put(SourceLineIndexDefinition.FIELD_SCM_REVISION, "cafebabe"); - fieldMap.put(SourceLineIndexDefinition.FIELD_SCM_DATE, null); - fieldMap.put(SourceLineIndexDefinition.FIELD_SCM_AUTHOR, "polop"); - fieldMap.put(SourceLineIndexDefinition.FIELD_SOURCE, "}"); - fieldMap.put(SourceLineIndexDefinition.FIELD_HIGHLIGHTING, ""); - fieldMap.put(SourceLineIndexDefinition.FIELD_SYMBOLS, ""); - fieldMap.put(SourceLineIndexDefinition.FIELD_UT_LINE_HITS, null); - fieldMap.put(SourceLineIndexDefinition.FIELD_UT_CONDITIONS, null); - fieldMap.put(SourceLineIndexDefinition.FIELD_UT_COVERED_CONDITIONS, null); - fieldMap.put(SourceLineIndexDefinition.FIELD_IT_LINE_HITS, null); - fieldMap.put(SourceLineIndexDefinition.FIELD_IT_CONDITIONS, null); - fieldMap.put(SourceLineIndexDefinition.FIELD_IT_COVERED_CONDITIONS, null); - fieldMap.put(SourceLineIndexDefinition.FIELD_DUPLICATIONS, null); - fieldMap.put(BaseNormalizer.UPDATED_AT_FIELD, new Date()); String componentKey = "componentKey"; - when(componentService.getByUuid(fileUuid)).thenReturn(new ComponentDto().setKey(componentKey)); - MockUserSession.set().setLogin("login").addComponentPermission(UserRole.CODEVIEWER, "polop", componentKey); + when(componentService.getByUuid(fileUuid)).thenReturn(new ComponentDto().setKey(componentKey).setProjectUuid(projectUuid)); + MockUserSession.set().setLogin("login").addProjectUuidPermissions(UserRole.CODEVIEWER, projectUuid); when(sourceLineIndex.getLines(fileUuid, 3, 3)).thenReturn(newArrayList( - new SourceLineDoc(fieldMap) + new SourceLineDoc() + .setProjectUuid(projectUuid) + .setFileUuid(fileUuid) + .setLine(3) + .setScmRevision("cafebabe") + .setScmDate(null) + .setScmAuthor("polop") + .setSource("}") + .setHighlighting("") + .setSymbols("") + .setUtLineHits(null) + .setUtConditions(null) + .setUtCoveredConditions(null) + .setItLineHits(null) + .setItConditions(null) + .setItCoveredConditions(null) + .setDuplications(null) + .setUpdateDate(new Date()) )); WsTester.TestRequest request = tester .newGetRequest("api/sources", "lines") diff --git a/server/sonar-server/src/test/resources/org/sonar/server/source/ws/LinesActionTest/show_source.json b/server/sonar-server/src/test/resources/org/sonar/server/source/ws/LinesActionTest/show_source.json index 318538ea81f..7655e20c817 100644 --- a/server/sonar-server/src/test/resources/org/sonar/server/source/ws/LinesActionTest/show_source.json +++ b/server/sonar-server/src/test/resources/org/sonar/server/source/ws/LinesActionTest/show_source.json @@ -5,9 +5,9 @@ "line": 1, "scmAuthor": "polop", "scmRevision": "cafebabe", - "lineHits": 3, - "conditions": 2, - "coveredConditions": 1, + "utLineHits": 3, + "utConditions": 2, + "utCoveredConditions": 1, "itLineHits": 3, "itConditions": 2, "itCoveredConditions": 1 @@ -17,9 +17,9 @@ "line": 2, "scmAuthor": "polop", "scmRevision": "cafebabe", - "lineHits": 3, - "conditions": 2, - "coveredConditions": 1, + "utLineHits": 3, + "utConditions": 2, + "utCoveredConditions": 1, "duplicated": true }, { -- 2.39.5