summaryrefslogtreecommitdiffstats
path: root/server
diff options
context:
space:
mode:
authorJulien Lancelot <julien.lancelot@sonarsource.com>2014-07-22 17:04:05 +0200
committerJulien Lancelot <julien.lancelot@sonarsource.com>2014-07-22 17:04:14 +0200
commit8d31b522cd2e15d0e6069773dd1adcedec21776c (patch)
treeb2c72535aaa7b8b190e3c500754531a0c152d80e /server
parente88728fea0369029c6e197c347f3877d35a1a2ae (diff)
downloadsonarqube-8d31b522cd2e15d0e6069773dd1adcedec21776c.tar.gz
sonarqube-8d31b522cd2e15d0e6069773dd1adcedec21776c.zip
Return project key, sub project key and sub project name in /api/duplications/show WS
Diffstat (limited to 'server')
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/duplication/ws/DuplicationsJsonWriter.java (renamed from server/sonar-server/src/main/java/org/sonar/server/duplication/ws/DuplicationsWriter.java)42
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/duplication/ws/ShowAction.java8
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/platform/ServerComponents.java4
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/duplication/ws/DuplicationsJsonWriterTest.java (renamed from server/sonar-server/src/test/java/org/sonar/server/duplication/ws/DuplicationsWriterTest.java)79
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/duplication/ws/DuplicationsWsTest.java2
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/duplication/ws/ShowActionTest.java8
6 files changed, 112 insertions, 31 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/duplication/ws/DuplicationsWriter.java b/server/sonar-server/src/main/java/org/sonar/server/duplication/ws/DuplicationsJsonWriter.java
index 1f081011343..0e9f286f265 100644
--- a/server/sonar-server/src/main/java/org/sonar/server/duplication/ws/DuplicationsWriter.java
+++ b/server/sonar-server/src/main/java/org/sonar/server/duplication/ws/DuplicationsJsonWriter.java
@@ -32,11 +32,11 @@ import java.util.Map;
import static com.google.common.collect.Maps.newHashMap;
-public class DuplicationsWriter implements ServerComponent {
+public class DuplicationsJsonWriter implements ServerComponent {
private final ComponentDao componentDao;
- public DuplicationsWriter(ComponentDao componentDao) {
+ public DuplicationsJsonWriter(ComponentDao componentDao) {
this.componentDao = componentDao;
}
@@ -82,7 +82,8 @@ public class DuplicationsWriter implements ServerComponent {
}
private void writeFiles(Map<String, String> refByComponentKey, JsonWriter json, DbSession session) {
- Map<Long, ComponentDto> projectById = newHashMap();
+ Map<Long, ComponentDto> projectsById = newHashMap();
+ Map<Long, ComponentDto> subProjectsById = newHashMap();
for (Map.Entry<String, String> entry : refByComponentKey.entrySet()) {
String componentKey = entry.getKey();
String ref = entry.getValue();
@@ -91,12 +92,37 @@ public class DuplicationsWriter implements ServerComponent {
json.name(ref).beginObject();
json.prop("key", file.key());
json.prop("name", file.longName());
- ComponentDto project = projectById.get(file.projectId());
- if (project == null) {
- project = componentDao.getById(file.projectId(), session);
- projectById.put(file.projectId(), project);
+
+ Long projectId = file.projectId();
+ ComponentDto project = projectsById.get(file.projectId());
+ if (project == null && projectId != null) {
+ project = componentDao.getById(projectId, session);
+ if (project != null) {
+ projectsById.put(projectId, project);
+ }
+ }
+
+ Long subProjectId = file.subProjectId();
+ ComponentDto subProject = subProjectsById.get(subProjectId);
+ if (subProject == null && subProjectId != null) {
+ subProject = componentDao.getById(subProjectId, session);
+ if (subProject != null) {
+ subProjectsById.put(subProject.getId(), subProject);
+ }
}
- json.prop("projectName", project != null ? project.longName() : null);
+
+ if (project != null) {
+ json.prop("project", project.key());
+ json.prop("projectName", project.longName());
+
+ // Do not return sub project if sub project and project are the same
+ boolean displaySubProject = subProject != null && !subProject.getId().equals(project.getId());
+ if (displaySubProject) {
+ json.prop("subProject", subProject.key());
+ json.prop("subProjectName", subProject.longName());
+ }
+ }
+
json.endObject();
}
}
diff --git a/server/sonar-server/src/main/java/org/sonar/server/duplication/ws/ShowAction.java b/server/sonar-server/src/main/java/org/sonar/server/duplication/ws/ShowAction.java
index 3246bd19303..f6c228ab57f 100644
--- a/server/sonar-server/src/main/java/org/sonar/server/duplication/ws/ShowAction.java
+++ b/server/sonar-server/src/main/java/org/sonar/server/duplication/ws/ShowAction.java
@@ -49,14 +49,14 @@ public class ShowAction implements RequestHandler {
private final ComponentDao componentDao;
private final MeasureDao measureDao;
private final DuplicationsParser parser;
- private final DuplicationsWriter duplicationsWriter;
+ private final DuplicationsJsonWriter duplicationsJsonWriter;
- public ShowAction(DbClient dbClient, ComponentDao componentDao, MeasureDao measureDao, DuplicationsParser parser, DuplicationsWriter duplicationsWriter) {
+ public ShowAction(DbClient dbClient, ComponentDao componentDao, MeasureDao measureDao, DuplicationsParser parser, DuplicationsJsonWriter duplicationsJsonWriter) {
this.dbClient = dbClient;
this.componentDao = componentDao;
this.measureDao = measureDao;
this.parser = parser;
- this.duplicationsWriter = duplicationsWriter;
+ this.duplicationsJsonWriter = duplicationsJsonWriter;
}
void define(WebService.NewController controller) {
@@ -84,7 +84,7 @@ public class ShowAction implements RequestHandler {
JsonWriter json = response.newJsonWriter().beginObject();
String duplications = findDataFromComponent(fileKey, CoreMetrics.DUPLICATIONS_DATA_KEY, session);
List<DuplicationsParser.Block> blocks = parser.parse(component, duplications, session);
- duplicationsWriter.write(blocks, json, session);
+ duplicationsJsonWriter.write(blocks, json, session);
json.endObject().close();
} finally {
MyBatis.closeQuietly(session);
diff --git a/server/sonar-server/src/main/java/org/sonar/server/platform/ServerComponents.java b/server/sonar-server/src/main/java/org/sonar/server/platform/ServerComponents.java
index 3b30ea64a68..db2109db7f9 100644
--- a/server/sonar-server/src/main/java/org/sonar/server/platform/ServerComponents.java
+++ b/server/sonar-server/src/main/java/org/sonar/server/platform/ServerComponents.java
@@ -94,8 +94,8 @@ import org.sonar.server.db.EmbeddedDatabaseFactory;
import org.sonar.server.db.migrations.DatabaseMigrations;
import org.sonar.server.db.migrations.DatabaseMigrator;
import org.sonar.server.debt.*;
+import org.sonar.server.duplication.ws.DuplicationsJsonWriter;
import org.sonar.server.duplication.ws.DuplicationsParser;
-import org.sonar.server.duplication.ws.DuplicationsWriter;
import org.sonar.server.duplication.ws.DuplicationsWs;
import org.sonar.server.issue.*;
import org.sonar.server.issue.actionplan.ActionPlanService;
@@ -493,7 +493,7 @@ class ServerComponents {
// Duplications
pico.addSingleton(DuplicationsParser.class);
pico.addSingleton(DuplicationsWs.class);
- pico.addSingleton(DuplicationsWriter.class);
+ pico.addSingleton(DuplicationsJsonWriter.class);
pico.addSingleton(org.sonar.server.duplication.ws.ShowAction.class);
// text
diff --git a/server/sonar-server/src/test/java/org/sonar/server/duplication/ws/DuplicationsWriterTest.java b/server/sonar-server/src/test/java/org/sonar/server/duplication/ws/DuplicationsJsonWriterTest.java
index 83dcbde34ec..6a26a071871 100644
--- a/server/sonar-server/src/test/java/org/sonar/server/duplication/ws/DuplicationsWriterTest.java
+++ b/server/sonar-server/src/test/java/org/sonar/server/duplication/ws/DuplicationsJsonWriterTest.java
@@ -37,13 +37,12 @@ import java.util.Collections;
import java.util.List;
import static com.google.common.collect.Lists.newArrayList;
-import static org.mockito.Matchers.anyLong;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.*;
@RunWith(MockitoJUnitRunner.class)
-public class DuplicationsWriterTest {
+public class DuplicationsJsonWriterTest {
@Mock
ComponentDao componentDao;
@@ -51,23 +50,82 @@ public class DuplicationsWriterTest {
@Mock
DbSession session;
- DuplicationsWriter writer;
+ DuplicationsJsonWriter writer;
@Before
public void setUp() throws Exception {
- writer = new DuplicationsWriter(componentDao);
+ writer = new DuplicationsJsonWriter(componentDao);
}
@Test
public void write_duplications() throws Exception {
String key1 = "org.codehaus.sonar:sonar-ws-client:src/main/java/org/sonar/wsclient/services/PropertyDeleteQuery.java";
+ ComponentDto file1 = new ComponentDto().setId(10L).setQualifier("FIL").setKey(key1).setLongName("PropertyDeleteQuery").setProjectId(1L).setSubProjectId(5L);
+ String key2 = "org.codehaus.sonar:sonar-ws-client:src/main/java/org/sonar/wsclient/services/PropertyUpdateQuery.java";
+ ComponentDto file2 = new ComponentDto().setId(11L).setQualifier("FIL").setKey(key2).setLongName("PropertyUpdateQuery").setProjectId(1L).setSubProjectId(5L);
+
+ when(componentDao.getNullableByKey(session, key1)).thenReturn(file1);
+ when(componentDao.getNullableByKey(session, key2)).thenReturn(file2);
+ when(componentDao.getById(1L, session)).thenReturn(new ComponentDto().setId(1L).setKey("org.codehaus.sonar:sonar").setLongName("SonarQube"));
+ when(componentDao.getById(5L, session)).thenReturn(new ComponentDto().setId(5L).setKey("org.codehaus.sonar:sonar-ws-client").setLongName("SonarQube :: Web Service Client"));
+
+ List<DuplicationsParser.Block> blocks = newArrayList();
+ blocks.add(new DuplicationsParser.Block(newArrayList(
+ new DuplicationsParser.Duplication(file1, 57, 12),
+ new DuplicationsParser.Duplication(file2, 73, 12)
+ )));
+
+ test(blocks,
+ "{\n" +
+ " \"duplications\": [\n" +
+ " {\n" +
+ " \"blocks\": [\n" +
+ " {\n" +
+ " \"from\": 57, \"size\": 12, \"_ref\": \"1\"\n" +
+ " },\n" +
+ " {\n" +
+ " \"from\": 73, \"size\": 12, \"_ref\": \"2\"\n" +
+ " }\n" +
+ " ]\n" +
+ " }," +
+ " ],\n" +
+ " \"files\": {\n" +
+ " \"1\": {\n" +
+ " \"key\": \"org.codehaus.sonar:sonar-ws-client:src/main/java/org/sonar/wsclient/services/PropertyDeleteQuery.java\",\n" +
+ " \"name\": \"PropertyDeleteQuery\",\n" +
+ " \"project\": \"org.codehaus.sonar:sonar\",\n" +
+ " \"projectName\": \"SonarQube\",\n" +
+ " \"subProject\": \"org.codehaus.sonar:sonar-ws-client\",\n" +
+ " \"subProjectName\": \"SonarQube :: Web Service Client\"\n" +
+ " },\n" +
+ " \"2\": {\n" +
+ " \"key\": \"org.codehaus.sonar:sonar-ws-client:src/main/java/org/sonar/wsclient/services/PropertyUpdateQuery.java\",\n" +
+ " \"name\": \"PropertyUpdateQuery\",\n" +
+ " \"project\": \"org.codehaus.sonar:sonar\",\n" +
+ " \"projectName\": \"SonarQube\",\n" +
+ " \"subProject\": \"org.codehaus.sonar:sonar-ws-client\",\n" +
+ " \"subProjectName\": \"SonarQube :: Web Service Client\"\n" +
+ " }\n" +
+ " }" +
+ "}"
+ );
+
+ verify(componentDao, times(2)).getNullableByKey(eq(session), anyString());
+ // Verify call to dao is cached when searching for project / sub project
+ verify(componentDao, times(1)).getById(eq(1L), eq(session));
+ verify(componentDao, times(1)).getById(eq(5L), eq(session));
+ }
+
+ @Test
+ public void write_duplications_without_sub_project() throws Exception {
+ String key1 = "org.codehaus.sonar:sonar-ws-client:src/main/java/org/sonar/wsclient/services/PropertyDeleteQuery.java";
ComponentDto file1 = new ComponentDto().setId(10L).setQualifier("FIL").setKey(key1).setLongName("PropertyDeleteQuery").setProjectId(1L);
String key2 = "org.codehaus.sonar:sonar-ws-client:src/main/java/org/sonar/wsclient/services/PropertyUpdateQuery.java";
ComponentDto file2 = new ComponentDto().setId(11L).setQualifier("FIL").setKey(key2).setLongName("PropertyUpdateQuery").setProjectId(1L);
when(componentDao.getNullableByKey(session, key1)).thenReturn(file1);
when(componentDao.getNullableByKey(session, key2)).thenReturn(file2);
- when(componentDao.getById(1L, session)).thenReturn(new ComponentDto().setId(1L).setLongName("SonarQube"));
+ when(componentDao.getById(1L, session)).thenReturn(new ComponentDto().setId(1L).setKey("org.codehaus.sonar:sonar").setLongName("SonarQube"));
List<DuplicationsParser.Block> blocks = newArrayList();
blocks.add(new DuplicationsParser.Block(newArrayList(
@@ -93,19 +151,18 @@ public class DuplicationsWriterTest {
" \"1\": {\n" +
" \"key\": \"org.codehaus.sonar:sonar-ws-client:src/main/java/org/sonar/wsclient/services/PropertyDeleteQuery.java\",\n" +
" \"name\": \"PropertyDeleteQuery\",\n" +
+ " \"project\": \"org.codehaus.sonar:sonar\",\n" +
" \"projectName\": \"SonarQube\"\n" +
" },\n" +
" \"2\": {\n" +
" \"key\": \"org.codehaus.sonar:sonar-ws-client:src/main/java/org/sonar/wsclient/services/PropertyUpdateQuery.java\",\n" +
" \"name\": \"PropertyUpdateQuery\",\n" +
+ " \"project\": \"org.codehaus.sonar:sonar\",\n" +
" \"projectName\": \"SonarQube\"\n" +
" }\n" +
" }" +
"}"
);
-
- verify(componentDao, times(2)).getNullableByKey(eq(session), anyString());
- verify(componentDao, times(1)).getById(anyLong(), eq(session));
}
@Test
@@ -114,7 +171,7 @@ public class DuplicationsWriterTest {
ComponentDto file1 = new ComponentDto().setId(10L).setQualifier("FIL").setKey(key1).setLongName("PropertyDeleteQuery").setProjectId(1L);
when(componentDao.getNullableByKey(session, key1)).thenReturn(file1);
- when(componentDao.getById(1L, session)).thenReturn(new ComponentDto().setId(1L).setLongName("SonarQube"));
+ when(componentDao.getById(1L, session)).thenReturn(new ComponentDto().setId(1L).setKey("org.codehaus.sonar:sonar").setLongName("SonarQube"));
List<DuplicationsParser.Block> blocks = newArrayList();
@@ -142,14 +199,12 @@ public class DuplicationsWriterTest {
" \"1\": {\n" +
" \"key\": \"org.codehaus.sonar:sonar-ws-client:src/main/java/org/sonar/wsclient/services/PropertyDeleteQuery.java\",\n" +
" \"name\": \"PropertyDeleteQuery\",\n" +
+ " \"project\": \"org.codehaus.sonar:sonar\",\n" +
" \"projectName\": \"SonarQube\"\n" +
" }\n" +
" }" +
"}"
);
-
- verify(componentDao, times(1)).getNullableByKey(eq(session), anyString());
- verify(componentDao, times(1)).getById(anyLong(), eq(session));
}
@Test
diff --git a/server/sonar-server/src/test/java/org/sonar/server/duplication/ws/DuplicationsWsTest.java b/server/sonar-server/src/test/java/org/sonar/server/duplication/ws/DuplicationsWsTest.java
index 0a753f11ee5..8f392dbedaf 100644
--- a/server/sonar-server/src/test/java/org/sonar/server/duplication/ws/DuplicationsWsTest.java
+++ b/server/sonar-server/src/test/java/org/sonar/server/duplication/ws/DuplicationsWsTest.java
@@ -33,7 +33,7 @@ import static org.mockito.Mockito.mock;
public class DuplicationsWsTest {
WsTester tester = new WsTester(new DuplicationsWs(
- new ShowAction(mock(DbClient.class), mock(ComponentDao.class), mock(MeasureDao.class), mock(DuplicationsParser.class), mock(DuplicationsWriter.class))));
+ new ShowAction(mock(DbClient.class), mock(ComponentDao.class), mock(MeasureDao.class), mock(DuplicationsParser.class), mock(DuplicationsJsonWriter.class))));
@Test
public void define_ws() throws Exception {
diff --git a/server/sonar-server/src/test/java/org/sonar/server/duplication/ws/ShowActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/duplication/ws/ShowActionTest.java
index 361d4d1f2ec..987a387b69d 100644
--- a/server/sonar-server/src/test/java/org/sonar/server/duplication/ws/ShowActionTest.java
+++ b/server/sonar-server/src/test/java/org/sonar/server/duplication/ws/ShowActionTest.java
@@ -67,14 +67,14 @@ public class ShowActionTest {
DuplicationsParser parser;
@Mock
- DuplicationsWriter duplicationsWriter;
+ DuplicationsJsonWriter duplicationsJsonWriter;
WsTester tester;
@Before
public void setUp() throws Exception {
when(dbClient.openSession(false)).thenReturn(session);
- tester = new WsTester(new DuplicationsWs(new ShowAction(dbClient, componentDao, measureDao, parser, duplicationsWriter)));
+ tester = new WsTester(new DuplicationsWs(new ShowAction(dbClient, componentDao, measureDao, parser, duplicationsJsonWriter)));
}
@Test
@@ -97,7 +97,7 @@ public class ShowActionTest {
WsTester.TestRequest request = tester.newGetRequest("api/duplications", "show").setParam("key", componentKey);
request.execute();
- verify(duplicationsWriter).write(eq(blocks), any(JsonWriter.class), eq(session));
+ verify(duplicationsJsonWriter).write(eq(blocks), any(JsonWriter.class), eq(session));
}
@Test
@@ -114,7 +114,7 @@ public class ShowActionTest {
WsTester.TestRequest request = tester.newGetRequest("api/duplications", "show").setParam("key", componentKey);
request.execute();
- verify(duplicationsWriter).write(eq(Lists.<DuplicationsParser.Block>newArrayList()), any(JsonWriter.class), eq(session));
+ verify(duplicationsJsonWriter).write(eq(Lists.<DuplicationsParser.Block>newArrayList()), any(JsonWriter.class), eq(session));
}
@Test(expected = NotFoundException.class)