Skip to content

Commit

Permalink
Refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
lkleisa committed Jun 6, 2024
1 parent 691783d commit e226479
Show file tree
Hide file tree
Showing 11 changed files with 108 additions and 109 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import ch.puzzle.okr.dto.alignment.AlignmentLists;
import ch.puzzle.okr.dto.alignment.AlignmentObjectiveDto;
import ch.puzzle.okr.mapper.AlignmentSelectionMapper;
import ch.puzzle.okr.models.alignment.AlignmentView;
import ch.puzzle.okr.service.business.AlignmentBusinessService;
import ch.puzzle.okr.service.business.AlignmentSelectionBusinessService;
import io.swagger.v3.oas.annotations.Operation;
Expand Down Expand Up @@ -60,6 +59,6 @@ public ResponseEntity<AlignmentLists> getAlignments(
@RequestParam(required = false, defaultValue = "", name = "quarterFilter") Long quarterFilter,
@RequestParam(required = false, defaultValue = "", name = "objectiveQuery") String objectiveQuery) {
return ResponseEntity.status(HttpStatus.OK)
.body(alignmentBusinessService.getAlignmentsByFilters(quarterFilter, teamFilter, objectiveQuery));
.body(alignmentBusinessService.getAlignmentListsByFilters(quarterFilter, teamFilter, objectiveQuery));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ public AlignmentBusinessService(AlignmentPersistenceService alignmentPersistence
this.quarterBusinessService = quarterBusinessService;
}

protected record DividedAlignmentViewLists(List<AlignmentView> correctAlignments,
List<AlignmentView> wrongAlignments) {
protected record DividedAlignmentViewLists(List<AlignmentView> filterMatchingAlignments,
List<AlignmentView> nonMatchingAlignments) {
}

public String getTargetIdByAlignedObjectiveId(Long alignedObjectiveId) {
Expand Down Expand Up @@ -151,7 +151,8 @@ public void deleteAlignmentByKeyResultId(Long keyResultId) {
});
}

public AlignmentLists getAlignmentsByFilters(Long quarterFilter, List<Long> teamFilter, String objectiveFilter) {
public AlignmentLists getAlignmentListsByFilters(Long quarterFilter, List<Long> teamFilter,
String objectiveFilter) {
if (Objects.isNull(quarterFilter)) {
quarterFilter = quarterBusinessService.getCurrentQuarter().getId();
}
Expand All @@ -168,13 +169,13 @@ public AlignmentLists getAlignmentsByFilters(Long quarterFilter, List<Long> team
DividedAlignmentViewLists dividedAlignmentViewLists = filterAlignmentViews(alignmentViewList, teamFilter,
objectiveFilter);

List<AlignmentView> finalList = getAlignmentCounterpart(dividedAlignmentViewLists);
validateFinalList(finalList, quarterFilter, teamFilter, objectiveFilter);
List<AlignmentView> correctAlignmentViewList = getAlignmentCounterpart(dividedAlignmentViewLists);
validateAlignmentList(correctAlignmentViewList, quarterFilter, teamFilter, objectiveFilter);

return generateAlignmentLists(finalList);
return generateAlignmentLists(correctAlignmentViewList);
}

protected void validateFinalList(List<AlignmentView> finalList, Long quarterFilter, List<Long> teamFilter,
protected void validateAlignmentList(List<AlignmentView> finalList, Long quarterFilter, List<Long> teamFilter,
String objectiveFilter) {
List<AlignmentView> sourceList = finalList.stream()
.filter(alignmentView -> Objects.equals(alignmentView.getConnectionItem(), "source")).toList();
Expand Down Expand Up @@ -212,13 +213,13 @@ protected AlignmentLists generateAlignmentLists(List<AlignmentView> alignmentVie
}

protected List<AlignmentView> getAlignmentCounterpart(DividedAlignmentViewLists alignmentViewLists) {
List<AlignmentView> correctAlignments = alignmentViewLists.correctAlignments();
List<AlignmentView> wrongAlignments = alignmentViewLists.wrongAlignments();
List<AlignmentView> targetAlignmentList = new ArrayList<>();
List<AlignmentView> filterMatchingAlignments = alignmentViewLists.filterMatchingAlignments();
List<AlignmentView> nonMatchingAlignments = alignmentViewLists.nonMatchingAlignments();
List<AlignmentView> correctAlignmentViewList = new ArrayList<>();

// If counterpart of the correct Alignment is in wrongAlignmentList, take it back
correctAlignments.forEach(alignment -> {
Optional<AlignmentView> matchingObject = wrongAlignments.stream()
filterMatchingAlignments.forEach(alignment -> {
Optional<AlignmentView> matchingObject = nonMatchingAlignments.stream()
.filter(view -> Objects.equals(view.getId(), alignment.getRefId())
&& Objects.equals(view.getObjectType(), alignment.getRefType())
&& Objects.equals(view.getRefId(), alignment.getId())
Expand All @@ -227,16 +228,15 @@ protected List<AlignmentView> getAlignmentCounterpart(DividedAlignmentViewLists

if (matchingObject.isPresent()) {
AlignmentView alignmentView = matchingObject.get();
targetAlignmentList.add(alignmentView);
correctAlignmentViewList.add(alignmentView);
}
});

// Create a new list because correctAlignments has a fixed length and targetAlignmentList can't be added
List<AlignmentView> finalList = new ArrayList<>(correctAlignments);
if (!targetAlignmentList.isEmpty()) {
finalList.addAll(targetAlignmentList);
List<AlignmentView> finalAlignmentViewList = new ArrayList<>(filterMatchingAlignments);
if (!correctAlignmentViewList.isEmpty()) {
finalAlignmentViewList.addAll(correctAlignmentViewList);
}
return finalList;
return finalAlignmentViewList;
}

protected DividedAlignmentViewLists filterAlignmentViews(List<AlignmentView> alignmentViewList,
Expand All @@ -251,9 +251,9 @@ protected DividedAlignmentViewLists filterAlignmentViews(List<AlignmentView> ali
&& alignmentView.getTitle().toLowerCase().contains(objectiveFilter.toLowerCase()))
.toList();
}
List<AlignmentView> correctAlignments = filteredList;
List<AlignmentView> wrongAlignments = alignmentViewList.stream()
.filter(alignmentView -> !correctAlignments.contains(alignmentView)).toList();
return new DividedAlignmentViewLists(correctAlignments, wrongAlignments);
List<AlignmentView> filterMatchingAlignments = filteredList;
List<AlignmentView> nonMatchingAlignments = alignmentViewList.stream()
.filter(alignmentView -> !filterMatchingAlignments.contains(alignmentView)).toList();
return new DividedAlignmentViewLists(filterMatchingAlignments, nonMatchingAlignments);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ void shouldReturnObjectiveWithEmptyKeyResultListWhenNoKeyResultsInFilteredQuarte

@Test
void shouldReturnCorrectAlignmentData() throws Exception {
BDDMockito.given(alignmentBusinessService.getAlignmentsByFilters(2L, List.of(4L, 5L, 8L), ""))
BDDMockito.given(alignmentBusinessService.getAlignmentListsByFilters(2L, List.of(4L, 5L, 8L), ""))
.willReturn(alignmentLists);

mvc.perform(get("/api/v2/alignments/alignmentLists?quarterFilter=2&teamFilter=4,5,8")
Expand All @@ -125,7 +125,7 @@ void shouldReturnCorrectAlignmentData() throws Exception {

@Test
void shouldReturnCorrectAlignmentDataWithObjectiveSearch() throws Exception {
BDDMockito.given(alignmentBusinessService.getAlignmentsByFilters(2L, List.of(4L, 5L, 8L), "secon"))
BDDMockito.given(alignmentBusinessService.getAlignmentListsByFilters(2L, List.of(4L, 5L, 8L), "secon"))
.willReturn(alignmentLists);

mvc.perform(get("/api/v2/alignments/alignmentLists?quarterFilter=2&teamFilter=4,5,8&objectiveQuery=secon")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class AlignmentBusinessServiceIT {

@Test
void shouldReturnCorrectAlignmentData() {
AlignmentLists alignmentLists = alignmentBusinessService.getAlignmentsByFilters(9L, List.of(5L, 6L), "");
AlignmentLists alignmentLists = alignmentBusinessService.getAlignmentListsByFilters(9L, List.of(5L, 6L), "");

assertEquals(6, alignmentLists.alignmentObjectDtoList().size());
assertEquals(4, alignmentLists.alignmentConnectionDtoList().size());
Expand Down Expand Up @@ -47,7 +47,7 @@ void shouldReturnCorrectAlignmentData() {

@Test
void shouldReturnCorrectAlignmentDataWhenLimitedTeamMatching() {
AlignmentLists alignmentLists = alignmentBusinessService.getAlignmentsByFilters(9L, List.of(6L), "");
AlignmentLists alignmentLists = alignmentBusinessService.getAlignmentListsByFilters(9L, List.of(6L), "");

assertEquals(2, alignmentLists.alignmentObjectDtoList().size());
assertEquals(1, alignmentLists.alignmentConnectionDtoList().size());
Expand All @@ -62,7 +62,7 @@ void shouldReturnCorrectAlignmentDataWhenLimitedTeamMatching() {

@Test
void shouldReturnCorrectAlignmentDataWithObjectiveSearch() {
AlignmentLists alignmentLists = alignmentBusinessService.getAlignmentsByFilters(9L, List.of(4L, 5L, 6L, 8L),
AlignmentLists alignmentLists = alignmentBusinessService.getAlignmentListsByFilters(9L, List.of(4L, 5L, 6L, 8L),
"lehrling");

assertEquals(3, alignmentLists.alignmentObjectDtoList().size());
Expand All @@ -83,7 +83,7 @@ void shouldReturnCorrectAlignmentDataWithObjectiveSearch() {

@Test
void shouldReturnCorrectAlignmentDataWithKeyResultWhenMatchingObjectiveSearch() {
AlignmentLists alignmentLists = alignmentBusinessService.getAlignmentsByFilters(9L, List.of(4L, 5L, 6L, 8L),
AlignmentLists alignmentLists = alignmentBusinessService.getAlignmentListsByFilters(9L, List.of(4L, 5L, 6L, 8L),
"firmenums");

assertEquals(2, alignmentLists.alignmentObjectDtoList().size());
Expand All @@ -99,15 +99,15 @@ void shouldReturnCorrectAlignmentDataWithKeyResultWhenMatchingObjectiveSearch()

@Test
void shouldReturnEmptyAlignmentDataWhenNoAlignments() {
AlignmentLists alignmentLists = alignmentBusinessService.getAlignmentsByFilters(3L, List.of(5L, 6L), "");
AlignmentLists alignmentLists = alignmentBusinessService.getAlignmentListsByFilters(3L, List.of(5L, 6L), "");

assertEquals(0, alignmentLists.alignmentObjectDtoList().size());
assertEquals(0, alignmentLists.alignmentConnectionDtoList().size());
}

@Test
void shouldReturnEmptyAlignmentDataWhenNoMatchingObjectiveSearch() {
AlignmentLists alignmentLists = alignmentBusinessService.getAlignmentsByFilters(9L, List.of(4L, 5L, 6L, 8L),
AlignmentLists alignmentLists = alignmentBusinessService.getAlignmentListsByFilters(9L, List.of(4L, 5L, 6L, 8L),
"spass");

assertEquals(0, alignmentLists.alignmentObjectDtoList().size());
Expand All @@ -116,16 +116,16 @@ void shouldReturnEmptyAlignmentDataWhenNoMatchingObjectiveSearch() {

@Test
void shouldReturnCorrectAlignmentDataWhenEmptyQuarterFilter() {
AlignmentLists alignmentLists = alignmentBusinessService.getAlignmentsByFilters(null, List.of(4L, 5L, 6L, 8L),
"");
AlignmentLists alignmentLists = alignmentBusinessService.getAlignmentListsByFilters(null,
List.of(4L, 5L, 6L, 8L), "");

assertEquals(8, alignmentLists.alignmentObjectDtoList().size());
assertEquals(5, alignmentLists.alignmentConnectionDtoList().size());
}

@Test
void shouldReturnEmptyAlignmentDataWhenEmptyTeamFilter() {
AlignmentLists alignmentLists = alignmentBusinessService.getAlignmentsByFilters(2L, null, "");
AlignmentLists alignmentLists = alignmentBusinessService.getAlignmentListsByFilters(2L, null, "");

assertEquals(0, alignmentLists.alignmentObjectDtoList().size());
assertEquals(0, alignmentLists.alignmentConnectionDtoList().size());
Expand Down
Loading

0 comments on commit e226479

Please sign in to comment.