@@ -1,53 +0,0 @@ | |||
/* | |||
* SonarQube | |||
* Copyright (C) 2009-2024 SonarSource SA | |||
* mailto:info AT sonarsource DOT com | |||
* | |||
* This program is free software; you can redistribute it and/or | |||
* modify it under the terms of the GNU Lesser General Public | |||
* License as published by the Free Software Foundation; either | |||
* version 3 of the License, or (at your option) any later version. | |||
* | |||
* This program is distributed in the hope that it will be useful, | |||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |||
* Lesser General Public License for more details. | |||
* | |||
* You should have received a copy of the GNU Lesser General Public License | |||
* along with this program; if not, write to the Free Software Foundation, | |||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |||
*/ | |||
package org.sonar.db.issue; | |||
public class HotspotGroupDto { | |||
private String status; | |||
private long count; | |||
private boolean inLeak; | |||
public String getStatus() { | |||
return status; | |||
} | |||
public HotspotGroupDto setStatus(String status) { | |||
this.status = status; | |||
return this; | |||
} | |||
public long getCount() { | |||
return count; | |||
} | |||
public HotspotGroupDto setCount(long count) { | |||
this.count = count; | |||
return this; | |||
} | |||
public boolean isInLeak() { | |||
return inLeak; | |||
} | |||
public HotspotGroupDto setInLeak(boolean inLeak) { | |||
this.inLeak = inLeak; | |||
return this; | |||
} | |||
} |
@@ -1,63 +0,0 @@ | |||
/* | |||
* SonarQube | |||
* Copyright (C) 2009-2024 SonarSource SA | |||
* mailto:info AT sonarsource DOT com | |||
* | |||
* This program is free software; you can redistribute it and/or | |||
* modify it under the terms of the GNU Lesser General Public | |||
* License as published by the Free Software Foundation; either | |||
* version 3 of the License, or (at your option) any later version. | |||
* | |||
* This program is distributed in the hope that it will be useful, | |||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |||
* Lesser General Public License for more details. | |||
* | |||
* You should have received a copy of the GNU Lesser General Public License | |||
* along with this program; if not, write to the Free Software Foundation, | |||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |||
*/ | |||
package org.sonar.server.measure.live; | |||
import java.util.Collection; | |||
import java.util.HashMap; | |||
import java.util.Map; | |||
import javax.annotation.Nullable; | |||
import org.sonar.db.issue.HotspotGroupDto; | |||
public class HotspotsCounter { | |||
private final Map<String, Count> hotspotsByStatus = new HashMap<>(); | |||
HotspotsCounter(Collection<HotspotGroupDto> groups) { | |||
for (HotspotGroupDto group : groups) { | |||
if (group.getStatus() != null) { | |||
hotspotsByStatus | |||
.computeIfAbsent(group.getStatus(), k -> new Count()) | |||
.add(group); | |||
} | |||
} | |||
} | |||
public long countHotspotsByStatus(String status, boolean onlyInLeak) { | |||
return value(hotspotsByStatus.get(status), onlyInLeak); | |||
} | |||
private static long value(@Nullable Count count, boolean onlyInLeak) { | |||
if (count == null) { | |||
return 0; | |||
} | |||
return onlyInLeak ? count.leak : count.absolute; | |||
} | |||
private static class Count { | |||
private long absolute = 0L; | |||
private long leak = 0L; | |||
void add(HotspotGroupDto group) { | |||
absolute += group.getCount(); | |||
if (group.isInLeak()) { | |||
leak += group.getCount(); | |||
} | |||
} | |||
} | |||
} |
@@ -1,49 +0,0 @@ | |||
/* | |||
* SonarQube | |||
* Copyright (C) 2009-2024 SonarSource SA | |||
* mailto:info AT sonarsource DOT com | |||
* | |||
* This program is free software; you can redistribute it and/or | |||
* modify it under the terms of the GNU Lesser General Public | |||
* License as published by the Free Software Foundation; either | |||
* version 3 of the License, or (at your option) any later version. | |||
* | |||
* This program is distributed in the hope that it will be useful, | |||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |||
* Lesser General Public License for more details. | |||
* | |||
* You should have received a copy of the GNU Lesser General Public License | |||
* along with this program; if not, write to the Free Software Foundation, | |||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |||
*/ | |||
package org.sonar.server.measure.live; | |||
import java.util.List; | |||
import org.junit.Test; | |||
import org.sonar.db.issue.HotspotGroupDto; | |||
import static java.util.Collections.emptyList; | |||
import static org.assertj.core.api.Assertions.assertThat; | |||
public class HotspotsCounterTest { | |||
@Test | |||
public void counts_hotspots() { | |||
HotspotGroupDto group1 = new HotspotGroupDto().setCount(3).setStatus("TO_REVIEW").setInLeak(false); | |||
HotspotGroupDto group2 = new HotspotGroupDto().setCount(2).setStatus("REVIEWED").setInLeak(false); | |||
HotspotGroupDto group3 = new HotspotGroupDto().setCount(1).setStatus("TO_REVIEW").setInLeak(true); | |||
HotspotGroupDto group4 = new HotspotGroupDto().setCount(1).setStatus("REVIEWED").setInLeak(true); | |||
HotspotsCounter counter = new HotspotsCounter(List.of(group1, group2, group3, group4)); | |||
assertThat(counter.countHotspotsByStatus("TO_REVIEW", true)).isEqualTo(1); | |||
assertThat(counter.countHotspotsByStatus("REVIEWED", true)).isEqualTo(1); | |||
assertThat(counter.countHotspotsByStatus("TO_REVIEW", false)).isEqualTo(4); | |||
assertThat(counter.countHotspotsByStatus("REVIEWED", false)).isEqualTo(3); | |||
} | |||
@Test | |||
public void count_empty_hotspots() { | |||
HotspotsCounter counter = new HotspotsCounter(emptyList()); | |||
assertThat(counter.countHotspotsByStatus("TO_REVIEW", true)).isZero(); | |||
} | |||
} |