-
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 ae68a2f
Showing
8 changed files
with
92 additions
and
32 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
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
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
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
26 changes: 26 additions & 0 deletions
26
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,26 @@ | ||
package de.jplag.reporting.reportobject.mapper; | ||
|
||
import java.util.Collection; | ||
import java.util.List; | ||
|
||
import de.jplag.JPlagResult; | ||
import de.jplag.Submission; | ||
import de.jplag.clustering.ClusteringResult; | ||
import de.jplag.reporting.reportobject.model.Cluster; | ||
|
||
/** | ||
* 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); | ||
} | ||
} |
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
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
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 static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import de.jplag.JPlagResult; | ||
import de.jplag.Submission; | ||
import de.jplag.clustering.Cluster; | ||
import de.jplag.clustering.ClusteringResult; | ||
|
||
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; | ||
} | ||
} |