import java.util.List;
import java.util.Map;
import java.util.Optional;
+import java.util.Set;
import javax.annotation.Nullable;
import org.sonar.api.batch.rule.Severity;
import org.sonar.api.batch.sensor.SensorContext;
NewIssueLocation primaryLocation = fillFileOrProjectLocation(result, newIssueLocation, firstLocation);
newExternalIssue.at(primaryLocation);
}
+
+ Set<Location> relatedLocations = result.getRelatedLocations();
+ if (relatedLocations != null && !relatedLocations.isEmpty()) {
+ relatedLocations.forEach(relatedLocation -> {
+ NewIssueLocation newRelatedLocation = newExternalIssue.newLocation();
+ fillFileOrProjectLocation(result, newRelatedLocation, relatedLocation);
+ newExternalIssue.addLocation(newRelatedLocation);
+ });
+ }
}
private NewIssueLocation fillFileOrProjectLocation(Result result, NewIssueLocation newIssueLocation, Location firstLocation) {
import com.tngtech.java.junit.dataprovider.DataProviderRunner;
import com.tngtech.java.junit.dataprovider.UseDataProvider;
import java.util.List;
+import java.util.Set;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
private SensorContext sensorContext;
@Mock
- private NewExternalIssue newExternalIssue;
+ private NewExternalIssue mockExternalIssue;
@Mock
private NewIssueLocation newExternalIssueLocation;
public void setUp() {
MockitoAnnotations.openMocks(this);
when(result.getRuleId()).thenReturn(RULE_ID);
- when(sensorContext.newExternalIssue()).thenReturn(newExternalIssue);
+ when(sensorContext.newExternalIssue()).thenReturn(mockExternalIssue);
when(locationMapper.fillIssueInFileLocation(any(), any(), any())).thenReturn(newExternalIssueLocation);
when(locationMapper.fillIssueInProjectLocation(any(), any())).thenReturn(newExternalIssueLocation);
- when(newExternalIssue.newLocation()).thenReturn(newExternalIssueLocation);
+ when(mockExternalIssue.newLocation()).thenReturn(newExternalIssueLocation);
}
@Test
verify(newExternalIssue, never()).addFlow(any());
}
+ @Test
+ public void mapResult_whenRelatedLocationExists_createsSecondaryFileLocation() {
+ Location location = mock(Location.class);
+ when(result.getRelatedLocations()).thenReturn(Set.of(location));
+ var newIssueLocationCall2 = mock(NewIssueLocation.class);
+ when(mockExternalIssue.newLocation()).thenReturn(newExternalIssueLocation, newIssueLocationCall2);
+
+ NewExternalIssue newExternalIssue = resultMapper.mapResult(DRIVER_NAME, WARNING, WARNING, result);
+
+ verify(locationMapper).fillIssueInProjectLocation(result, newExternalIssueLocation);
+ verify(locationMapper).fillIssueInFileLocation(result, newIssueLocationCall2, location);
+ verifyNoMoreInteractions(locationMapper);
+ verify(newExternalIssue).at(newExternalIssueLocation);
+ verify(newExternalIssue).addLocation(newIssueLocationCall2);
+ verify(newExternalIssue, never()).addFlow(any());
+ }
+
@Test
public void mapResult_whenLocationExistsButLocationMapperReturnsNull_createsProjectLocation() {
Location location = mock(Location.class);