-
Notifications
You must be signed in to change notification settings - Fork 334
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8503fbf
commit fa32023
Showing
3 changed files
with
91 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
jplag/src/main/java/de/jplag/reporting/reportobject/mapper/ClusteringResultMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package de.jplag.reporting.reportobject.mapper; | ||
|
||
import de.jplag.JPlagResult; | ||
import de.jplag.Submission; | ||
import de.jplag.clustering.ClusteringResult; | ||
import de.jplag.reporting.reportobject.model.Cluster; | ||
|
||
import java.util.Collection; | ||
import java.util.List; | ||
|
||
/** | ||
* Extracts and maps the clusters from the JPlagResult to the corresponding JSON DTO | ||
*/ | ||
public class ClusteringResultMapper { | ||
public List<Cluster> map(JPlagResult result) { | ||
var clusteringResult = result.getClusteringResult(); | ||
return clusteringResult.stream() | ||
.map(ClusteringResult::getClusters) | ||
.flatMap(Collection::stream) | ||
.map(this::convertCluster) | ||
.toList(); | ||
} | ||
|
||
private Cluster convertCluster(de.jplag.clustering.Cluster<Submission> from) { | ||
var strength = from.getCommunityStrength(); | ||
var avgSimilarity = from.getAverageSimilarity(); | ||
var member = from.getMembers().stream().map(Submission::getName).toList(); | ||
return new Cluster(avgSimilarity, strength, member); | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
jplag/src/test/java/de/jplag/reporting/reportobject/mapper/ClusteringResultMapperTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package de.jplag.reporting.reportobject.mapper; | ||
|
||
import de.jplag.JPlagResult; | ||
import de.jplag.Submission; | ||
import de.jplag.clustering.Cluster; | ||
import de.jplag.clustering.ClusteringResult; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
public class ClusteringResultMapperTest { | ||
private final ClusteringResultMapper clusteringResultMapper = new ClusteringResultMapper(); | ||
|
||
@Test | ||
public void test() { | ||
// given | ||
JPlagResult resultMock = mock(JPlagResult.class); | ||
Cluster<Submission> cluster1 = createClusterWith(0.2f, 0.4f, "1", "2"); | ||
Cluster<Submission> cluster2 = createClusterWith(0.3f, 0.6f, "3", "4", "5"); | ||
when(resultMock.getClusteringResult()).thenReturn(List.of(new ClusteringResult<>(List.of(cluster1, cluster2), 0.3f))); | ||
|
||
// when | ||
var result = clusteringResultMapper.map(resultMock); | ||
|
||
// then | ||
assertEquals(List.of( | ||
new de.jplag.reporting.reportobject.model.Cluster(0.4f, 0.2f, List.of("1", "2")), | ||
new de.jplag.reporting.reportobject.model.Cluster(0.6f, 0.3f, List.of("3", "4", "5")) | ||
|
||
), result); | ||
} | ||
|
||
private Cluster<Submission> createClusterWith(Float communityStrength, Float averageSimilarity, String... ids) { | ||
var submissions = Arrays.stream(ids).map(this::submissionWithId).toList(); | ||
return new Cluster<>(submissions, communityStrength, averageSimilarity); | ||
} | ||
|
||
private Submission submissionWithId(String id) { | ||
Submission submission = mock(Submission.class); | ||
when(submission.getName()).thenReturn(id); | ||
return submission; | ||
} | ||
} |