aboutsummaryrefslogtreecommitdiffstats
path: root/server
diff options
context:
space:
mode:
authorDuarte Meneses <duarte.meneses@sonarsource.com>2018-11-01 10:38:43 -0500
committerSonarTech <sonartech@sonarsource.com>2018-11-07 20:21:04 +0100
commit5cade4c9e1eea27703e182ae71f00918a8b530a2 (patch)
treecfc54e6db53c2d8cab1000a13404c39a08f27476 /server
parent7f59b2e51acb56b9d280fbeea4564194f3dd07cd (diff)
downloadsonarqube-5cade4c9e1eea27703e182ae71f00918a8b530a2.tar.gz
sonarqube-5cade4c9e1eea27703e182ae71f00918a8b530a2.zip
SONAR-11394 Apply feedback and fix IT
Diffstat (limited to 'server')
-rw-r--r--server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/source/linereader/DuplicationLineReader.java12
-rw-r--r--server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/DuplicationDataMeasuresStep.java4
-rw-r--r--server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/LoadDuplicationsFromReportStep.java8
-rw-r--r--server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/duplication/DuplicationMeasuresTest.java4
-rw-r--r--server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/duplication/DuplicationRepositoryRule.java21
-rw-r--r--server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/step/DuplicationDataMeasuresStepTest.java15
-rw-r--r--server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/step/LoadDuplicationsFromReportStepTest.java25
-rw-r--r--server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/step/NewSizeMeasuresStepTest.java4
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/duplication/ws/Duplication.java16
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/duplication/ws/DuplicationsParser.java8
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/duplication/ws/ShowResponseBuilder.java29
11 files changed, 94 insertions, 52 deletions
diff --git a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/source/linereader/DuplicationLineReader.java b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/source/linereader/DuplicationLineReader.java
index b3c05f3660a..8c7b07af79e 100644
--- a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/source/linereader/DuplicationLineReader.java
+++ b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/source/linereader/DuplicationLineReader.java
@@ -55,7 +55,7 @@ public class DuplicationLineReader implements LineReader {
// in {@link DbFileSources.Line#getDuplicationList()}
duplicatedTextBlockIndexByTextBlock.entrySet().stream()
.filter(containsLine)
- .map(MapEntryToBlockId.INSTANCE)
+ .map(Map.Entry::getValue)
.sorted(Comparator.naturalOrder())
.forEach(lineBuilder::addDuplication);
@@ -111,16 +111,6 @@ public class DuplicationLineReader implements LineReader {
}
}
- private enum MapEntryToBlockId implements Function<Map.Entry<TextBlock, Integer>, Integer> {
- INSTANCE;
-
- @Override
- @Nonnull
- public Integer apply(@Nonnull Map.Entry<TextBlock, Integer> input) {
- return input.getValue();
- }
- }
-
private static class TextBlockIndexGenerator implements Function<TextBlock, Integer> {
int i = 1;
diff --git a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/DuplicationDataMeasuresStep.java b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/DuplicationDataMeasuresStep.java
index 1fc73c04092..6135d5b5630 100644
--- a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/DuplicationDataMeasuresStep.java
+++ b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/DuplicationDataMeasuresStep.java
@@ -128,11 +128,11 @@ public class DuplicationDataMeasuresStep implements ComputationStep {
appendDuplication(xml, componentDbKey, duplicate.getTextBlock(), false);
}
- private void appendDuplication(StringBuilder xml, String componentDbKey, TextBlock textBlock, boolean onlyText) {
+ private void appendDuplication(StringBuilder xml, String componentDbKey, TextBlock textBlock, boolean disableLink) {
int length = textBlock.getEnd() - textBlock.getStart() + 1;
xml.append("<b s=\"").append(textBlock.getStart())
.append("\" l=\"").append(length)
- .append("\" t=\"").append(onlyText)
+ .append("\" t=\"").append(disableLink)
.append("\" r=\"").append(StringEscapeUtils.escapeXml(componentDbKey))
.append("\"/>");
}
diff --git a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/LoadDuplicationsFromReportStep.java b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/LoadDuplicationsFromReportStep.java
index f8a7fefffa5..af7d3f37728 100644
--- a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/LoadDuplicationsFromReportStep.java
+++ b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/step/LoadDuplicationsFromReportStep.java
@@ -19,7 +19,8 @@
*/
package org.sonar.ce.task.projectanalysis.step;
-import com.google.common.base.Function;
+import java.util.function.Function;
+import java.util.stream.Collectors;
import javax.annotation.Nonnull;
import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolder;
import org.sonar.ce.task.projectanalysis.batch.BatchReportReader;
@@ -41,7 +42,6 @@ import org.sonar.core.util.CloseableIterator;
import org.sonar.scanner.protocol.output.ScannerReport;
import static com.google.common.base.Preconditions.checkArgument;
-import static com.google.common.collect.FluentIterable.from;
/**
* Loads duplication information from the report and loads them into the {@link DuplicationRepository}.
@@ -122,8 +122,8 @@ public class LoadDuplicationsFromReportStep implements ComputationStep {
duplicationRepository.add(file,
new Duplication(
convert(duplication.getOriginPosition(), id),
- from(duplication.getDuplicateList())
- .transform(new BatchDuplicateToCeDuplicate(file))));
+ duplication.getDuplicateList().stream()
+ .map(new BatchDuplicateToCeDuplicate(file)).collect(Collectors.toList())));
}
private DetailedTextBlock convert(ScannerReport.TextRange textRange, int id) {
diff --git a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/duplication/DuplicationMeasuresTest.java b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/duplication/DuplicationMeasuresTest.java
index c413ed4d4ef..504937a1683 100644
--- a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/duplication/DuplicationMeasuresTest.java
+++ b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/duplication/DuplicationMeasuresTest.java
@@ -129,7 +129,7 @@ public class DuplicationMeasuresTest {
@Test
public void compute_duplicated_blocks_one_for_original_and_ignores_CrossProjectDuplicate() {
- duplicationRepository.addDuplication(FILE_1_REF, new TextBlock(1, 1), SOME_FILE_KEY, new TextBlock(2, 2));
+ duplicationRepository.addCrossProjectDuplication(FILE_1_REF, new TextBlock(1, 1), SOME_FILE_KEY, new TextBlock(2, 2));
underTest.execute();
@@ -185,7 +185,7 @@ public class DuplicationMeasuresTest {
@Test
public void compute_duplicated_lines_counts_lines_from_original_and_ignores_CrossProjectDuplicate() {
TextBlock original = new TextBlock(1, 1);
- duplicationRepository.addDuplication(FILE_1_REF, original, SOME_FILE_KEY, new TextBlock(2, 2));
+ duplicationRepository.addCrossProjectDuplication(FILE_1_REF, original, SOME_FILE_KEY, new TextBlock(2, 2));
underTest.execute();
diff --git a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/duplication/DuplicationRepositoryRule.java b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/duplication/DuplicationRepositoryRule.java
index 31ba9be8c2a..72a7cb24477 100644
--- a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/duplication/DuplicationRepositoryRule.java
+++ b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/duplication/DuplicationRepositoryRule.java
@@ -23,6 +23,7 @@ import com.google.common.base.Function;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
import java.util.Arrays;
+import java.util.Collections;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import org.junit.rules.ExternalResource;
@@ -116,12 +117,12 @@ public class DuplicationRepositoryRule extends ExternalResource implements Dupli
delegate.add(component,
new Duplication(
original,
- Arrays.asList(new InProjectDuplicate(componentProvider.getByRef(otherFileRef), duplicate))));
+ Collections.singletonList(new InProjectDuplicate(componentProvider.getByRef(otherFileRef), duplicate))));
return this;
}
- public DuplicationRepositoryRule addDuplication(int fileRef, TextBlock original, String otherFileKey, TextBlock duplicate) {
+ public DuplicationRepositoryRule addExtendedProjectDuplication(int fileRef, TextBlock original, int otherFileRef, TextBlock duplicate) {
ensureComponentProviderInitialized();
Component component = componentProvider.getByRef(fileRef);
checkArgument(!componentRefsWithCrossProjectDuplications.containsEntry(component, original), "CrossProject duplications for file %s and original %s already set", fileRef);
@@ -130,7 +131,21 @@ public class DuplicationRepositoryRule extends ExternalResource implements Dupli
delegate.add(componentProvider.getByRef(fileRef),
new Duplication(
original,
- Arrays.asList(new CrossProjectDuplicate(otherFileKey, duplicate))));
+ Collections.singletonList(new InExtendedProjectDuplicate(componentProvider.getByRef(otherFileRef), duplicate))));
+
+ return this;
+ }
+
+ public DuplicationRepositoryRule addCrossProjectDuplication(int fileRef, TextBlock original, String otherFileKey, TextBlock duplicate) {
+ ensureComponentProviderInitialized();
+ Component component = componentProvider.getByRef(fileRef);
+ checkArgument(!componentRefsWithCrossProjectDuplications.containsEntry(component, original), "CrossProject duplications for file %s and original %s already set", fileRef);
+
+ componentRefsWithCrossProjectDuplications.put(component, original);
+ delegate.add(componentProvider.getByRef(fileRef),
+ new Duplication(
+ original,
+ Collections.singletonList(new CrossProjectDuplicate(otherFileKey, duplicate))));
return this;
}
diff --git a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/step/DuplicationDataMeasuresStepTest.java b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/step/DuplicationDataMeasuresStepTest.java
index cee90705919..687cd705196 100644
--- a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/step/DuplicationDataMeasuresStepTest.java
+++ b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/step/DuplicationDataMeasuresStepTest.java
@@ -111,9 +111,22 @@ public class DuplicationDataMeasuresStepTest extends BaseStepTest {
}
@Test
+ public void compute_duplications_on_unchanged_file() {
+ duplicationRepository.addExtendedProjectDuplication(FILE_1_REF, new TextBlock(1, 5), FILE_2_REF, new TextBlock(6, 10));
+
+ underTest.execute(new TestComputationStepContext());
+
+ assertThat(measureRepository.getAddedRawMeasure(FILE_1_REF, DUPLICATIONS_DATA_KEY)).isPresent();
+ assertThat(measureRepository.getAddedRawMeasure(FILE_1_REF, DUPLICATIONS_DATA_KEY).get().getData()).isEqualTo(
+ "<duplications><g><b s=\"1\" l=\"5\" t=\"false\" r=\"" + FILE_1_KEY + "\"/><b s=\"6\" l=\"5\" t=\"true\" r=\""
+ + FILE_2_KEY + "\"/></g></duplications>");
+ assertThat(measureRepository.getAddedRawMeasure(FILE_2_REF, DUPLICATIONS_DATA_KEY)).isAbsent();
+ }
+
+ @Test
public void compute_duplications_on_different_projects() {
String fileKeyFromOtherProject = "PROJECT2_KEY:file2";
- duplicationRepository.addDuplication(FILE_1_REF, new TextBlock(1, 5), fileKeyFromOtherProject, new TextBlock(6, 10));
+ duplicationRepository.addCrossProjectDuplication(FILE_1_REF, new TextBlock(1, 5), fileKeyFromOtherProject, new TextBlock(6, 10));
underTest.execute(new TestComputationStepContext());
diff --git a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/step/LoadDuplicationsFromReportStepTest.java b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/step/LoadDuplicationsFromReportStepTest.java
index f9c63bbb619..1c679e8ca11 100644
--- a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/step/LoadDuplicationsFromReportStepTest.java
+++ b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/step/LoadDuplicationsFromReportStepTest.java
@@ -24,6 +24,7 @@ import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolderRule;
+import org.sonar.ce.task.projectanalysis.analysis.Branch;
import org.sonar.ce.task.projectanalysis.batch.BatchReportReaderRule;
import org.sonar.ce.task.projectanalysis.component.Component;
import org.sonar.ce.task.projectanalysis.component.TreeRootHolderRule;
@@ -32,13 +33,17 @@ import org.sonar.ce.task.projectanalysis.duplication.DetailedTextBlock;
import org.sonar.ce.task.projectanalysis.duplication.Duplicate;
import org.sonar.ce.task.projectanalysis.duplication.Duplication;
import org.sonar.ce.task.projectanalysis.duplication.DuplicationRepositoryRule;
+import org.sonar.ce.task.projectanalysis.duplication.InExtendedProjectDuplicate;
import org.sonar.ce.task.projectanalysis.duplication.InProjectDuplicate;
import org.sonar.ce.task.projectanalysis.duplication.InnerDuplicate;
import org.sonar.ce.task.projectanalysis.duplication.TextBlock;
import org.sonar.ce.task.step.TestComputationStepContext;
+import org.sonar.db.component.BranchType;
import org.sonar.scanner.protocol.output.ScannerReport;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
import static org.sonar.ce.task.projectanalysis.component.Component.Type.FILE;
import static org.sonar.ce.task.projectanalysis.component.Component.Type.PROJECT;
import static org.sonar.ce.task.projectanalysis.component.ReportComponent.builder;
@@ -56,7 +61,8 @@ public class LoadDuplicationsFromReportStepTest {
builder(PROJECT, ROOT_REF)
.addChildren(
builder(FILE, FILE_1_REF).build(),
- builder(FILE, FILE_2_REF).build())
+ // status has no effect except if it's a SLB or PR
+ builder(FILE, FILE_2_REF).setStatus(Component.Status.SAME).build())
.build());
@Rule
public BatchReportReaderRule reportReader = new BatchReportReaderRule();
@@ -100,6 +106,23 @@ public class LoadDuplicationsFromReportStepTest {
}
@Test
+ public void loads_duplication_with_otherFileRef_as_InExtendedProject_duplication() {
+ Branch branch = mock(Branch.class);
+ when(branch.getType()).thenReturn(BranchType.PULL_REQUEST);
+ analysisMetadataHolder.setBranch(branch);
+
+ reportReader.putDuplications(FILE_1_REF, createDuplication(singleLineTextRange(LINE), createInProjectDuplicate(FILE_2_REF, LINE + 1)));
+
+ TestComputationStepContext context = new TestComputationStepContext();
+ underTest.execute(context);
+
+ assertDuplications(FILE_1_REF, singleLineDetailedTextBlock(1, LINE),
+ new InExtendedProjectDuplicate(treeRootHolder.getComponentByRef(FILE_2_REF), singleLineTextBlock(LINE + 1)));
+ assertNoDuplication(FILE_2_REF);
+ assertNbOfDuplications(context, 1);
+ }
+
+ @Test
public void loads_multiple_duplications_with_multiple_duplicates() {
reportReader.putDuplications(
FILE_2_REF,
diff --git a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/step/NewSizeMeasuresStepTest.java b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/step/NewSizeMeasuresStepTest.java
index 2bbd57d3c8f..e08097564cb 100644
--- a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/step/NewSizeMeasuresStepTest.java
+++ b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/step/NewSizeMeasuresStepTest.java
@@ -177,7 +177,7 @@ public class NewSizeMeasuresStepTest {
@Test
public void compute_duplicated_lines_counts_lines_from_original_and_ignores_CrossProjectDuplicate() {
TextBlock original = new TextBlock(1, 1);
- duplicationRepository.addDuplication(FILE_1_REF, original, SOME_FILE_KEY, new TextBlock(2, 2));
+ duplicationRepository.addCrossProjectDuplication(FILE_1_REF, original, SOME_FILE_KEY, new TextBlock(2, 2));
setNewLines(FILE_1);
underTest.execute(new TestComputationStepContext());
@@ -294,7 +294,7 @@ public class NewSizeMeasuresStepTest {
@Test
public void compute_duplicated_blocks_one_for_original_and_ignores_CrossProjectDuplicate() {
- duplicationRepository.addDuplication(FILE_1_REF, new TextBlock(1, 1), SOME_FILE_KEY, new TextBlock(2, 2));
+ duplicationRepository.addCrossProjectDuplication(FILE_1_REF, new TextBlock(1, 1), SOME_FILE_KEY, new TextBlock(2, 2));
setNewLines(FILE_1);
underTest.execute(new TestComputationStepContext());
diff --git a/server/sonar-server/src/main/java/org/sonar/server/duplication/ws/Duplication.java b/server/sonar-server/src/main/java/org/sonar/server/duplication/ws/Duplication.java
index 88c38fdc548..4f4b410213c 100644
--- a/server/sonar-server/src/main/java/org/sonar/server/duplication/ws/Duplication.java
+++ b/server/sonar-server/src/main/java/org/sonar/server/duplication/ws/Duplication.java
@@ -30,6 +30,14 @@ public class Duplication {
private final Integer size;
private final boolean removed;
+ private Duplication(@Nullable ComponentDto componentDto, String componentDbKey, Integer from, Integer size, boolean removed) {
+ this.componentDto = componentDto;
+ this.componentDbKey = componentDbKey;
+ this.from = from;
+ this.size = size;
+ this.removed = removed;
+ }
+
static Duplication newRemovedComponent(String componentDbKey, Integer from, Integer size) {
return new Duplication(null, componentDbKey, from, size, true);
}
@@ -42,14 +50,6 @@ public class Duplication {
return new Duplication(componentDto, componentDto.getDbKey(), from, size, false);
}
- private Duplication(@Nullable ComponentDto componentDto, String componentDbKey, Integer from, Integer size, boolean removed) {
- this.componentDto = componentDto;
- this.componentDbKey = componentDbKey;
- this.from = from;
- this.size = size;
- this.removed = removed;
- }
-
String componentDbKey() {
return componentDbKey;
}
diff --git a/server/sonar-server/src/main/java/org/sonar/server/duplication/ws/DuplicationsParser.java b/server/sonar-server/src/main/java/org/sonar/server/duplication/ws/DuplicationsParser.java
index f6cc09f3196..d51c0adab7c 100644
--- a/server/sonar-server/src/main/java/org/sonar/server/duplication/ws/DuplicationsParser.java
+++ b/server/sonar-server/src/main/java/org/sonar/server/duplication/ws/DuplicationsParser.java
@@ -24,7 +24,7 @@ import java.io.Serializable;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Comparator;
-import java.util.HashMap;
+import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.CheckForNull;
@@ -50,7 +50,7 @@ public class DuplicationsParser {
}
public List<Block> parse(DbSession session, ComponentDto component, @Nullable String branch, @Nullable String pullRequest, @Nullable String duplicationsData) {
- Map<String, ComponentDto> componentsByKey = new HashMap<>();
+ Map<String, ComponentDto> componentsByKey = new LinkedHashMap<>();
List<Block> blocks = new ArrayList<>();
if (duplicationsData == null) {
return blocks;
@@ -69,10 +69,10 @@ public class DuplicationsParser {
while (bCursor.getNext() != null) {
String from = bCursor.getAttrValue("s");
String size = bCursor.getAttrValue("l");
- boolean onlyText = Boolean.parseBoolean(bCursor.getAttrValue("t"));
+ boolean disableLink = Boolean.parseBoolean(bCursor.getAttrValue("t"));
String componentDbKey = bCursor.getAttrValue("r");
if (from != null && size != null && componentDbKey != null) {
- if (onlyText) {
+ if (disableLink) {
// flag means that the target refers to an unchanged file in SLBs/PRs that doesn't exist in DB.
// Display as text without a link or other details.
duplications.add(Duplication.newTextComponent(componentDbKey, Integer.valueOf(from), Integer.valueOf(size)));
diff --git a/server/sonar-server/src/main/java/org/sonar/server/duplication/ws/ShowResponseBuilder.java b/server/sonar-server/src/main/java/org/sonar/server/duplication/ws/ShowResponseBuilder.java
index 4212ca7be74..85362b2ac38 100644
--- a/server/sonar-server/src/main/java/org/sonar/server/duplication/ws/ShowResponseBuilder.java
+++ b/server/sonar-server/src/main/java/org/sonar/server/duplication/ws/ShowResponseBuilder.java
@@ -21,6 +21,7 @@ package org.sonar.server.duplication.ws;
import com.google.common.annotations.VisibleForTesting;
import java.util.HashMap;
+import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
@@ -40,7 +41,6 @@ import static org.sonar.core.util.Protobuf.setNullable;
public class ShowResponseBuilder {
private final ComponentDao componentDao;
- private final Map<String, Reference> refByComponentKey = new HashMap<>();
public ShowResponseBuilder(DbClient dbClient) {
this.componentDao = dbClient.componentDao();
@@ -51,25 +51,26 @@ public class ShowResponseBuilder {
}
ShowResponse build(DbSession session, List<DuplicationsParser.Block> blocks, @Nullable String branch, @Nullable String pullRequest) {
+ Map<String, Reference> refByComponentKey = new LinkedHashMap<>();
ShowResponse.Builder response = ShowResponse.newBuilder();
blocks.stream()
- .map(this::toWsDuplication)
+ .map(block -> toWsDuplication(block, refByComponentKey))
.forEach(response::addDuplications);
- writeFileRefs(session, response, branch, pullRequest);
+ writeFileRefs(session, refByComponentKey, response, branch, pullRequest);
return response.build();
}
- private Duplications.Duplication.Builder toWsDuplication(DuplicationsParser.Block block) {
+ private static Duplications.Duplication.Builder toWsDuplication(DuplicationsParser.Block block, Map<String, Reference> refByComponentKey) {
Duplications.Duplication.Builder wsDuplication = Duplications.Duplication.newBuilder();
block.getDuplications().stream()
- .map(this::toWsBlock)
+ .map(duplication -> toWsBlock(duplication, refByComponentKey))
.forEach(wsDuplication::addBlocks);
return wsDuplication;
}
- private Block.Builder toWsBlock(Duplication duplication) {
+ private static Block.Builder toWsBlock(Duplication duplication, Map<String, Reference> refByComponentKey) {
Block.Builder block = Block.newBuilder();
if (!duplication.removed()) {
@@ -77,7 +78,7 @@ public class ShowResponseBuilder {
Integer.toString(refByComponentKey.size() + 1),
duplication.componentDto(),
duplication.componentDbKey()));
- block.setRef(ref.id);
+ block.setRef(ref.getId());
}
block.setFrom(duplication.from());
@@ -86,20 +87,20 @@ public class ShowResponseBuilder {
return block;
}
- private void writeFileRefs(DbSession session, ShowResponse.Builder response, @Nullable String branch, @Nullable String pullRequest) {
+ private void writeFileRefs(DbSession session, Map<String, Reference> refByComponentKey, ShowResponse.Builder response, @Nullable String branch, @Nullable String pullRequest) {
Map<String, ComponentDto> projectsByUuid = new HashMap<>();
Map<String, ComponentDto> parentModulesByUuid = new HashMap<>();
for (Map.Entry<String, Reference> entry : refByComponentKey.entrySet()) {
Reference ref = entry.getValue();
- ComponentDto file = ref.dto();
+ ComponentDto file = ref.getDto();
if (file != null) {
ComponentDto project = getProject(file.projectUuid(), projectsByUuid, session);
ComponentDto parentModule = getParentProject(file.getRootUuid(), parentModulesByUuid, session);
- response.putFiles(ref.id(), toWsFile(file, project, parentModule, branch, pullRequest));
+ response.putFiles(ref.getId(), toWsFile(file, project, parentModule, branch, pullRequest));
} else {
- response.putFiles(ref.id(), toWsFile(ref.componentKey(), branch, pullRequest));
+ response.putFiles(ref.getId(), toWsFile(ref.getComponentKey(), branch, pullRequest));
}
}
}
@@ -173,16 +174,16 @@ public class ShowResponseBuilder {
this.componentKey = componentKey;
}
- public String id() {
+ public String getId() {
return id;
}
@CheckForNull
- public ComponentDto dto() {
+ public ComponentDto getDto() {
return dto;
}
- public String componentKey() {
+ public String getComponentKey() {
return componentKey;
}