|
| 1 | +package by.andd3dfx.stream; |
| 2 | + |
| 3 | +import org.junit.Test; |
| 4 | + |
| 5 | +import java.util.List; |
| 6 | +import java.util.Map; |
| 7 | +import java.util.Set; |
| 8 | + |
| 9 | +import static org.assertj.core.api.Assertions.assertThat; |
| 10 | + |
| 11 | +public class GroupingByExamplesTest { |
| 12 | + |
| 13 | + private static final GroupingByExamples.Worker WORKER1 = new GroupingByExamples.Worker("Иван", 30, 50000, "Разработчик"); |
| 14 | + private static final GroupingByExamples.Worker WORKER2 = new GroupingByExamples.Worker("Петр", 25, 45000, "Разработчик"); |
| 15 | + private static final GroupingByExamples.Worker WORKER3 = new GroupingByExamples.Worker("Мария", 28, 60000, "Менеджер"); |
| 16 | + private static final GroupingByExamples.Worker WORKER4 = new GroupingByExamples.Worker("Анна", 32, 55000, "Менеджер"); |
| 17 | + private static final GroupingByExamples.Worker WORKER5 = new GroupingByExamples.Worker("Сергей", 27, 40000, "Тестировщик"); |
| 18 | + private static final GroupingByExamples.Worker WORKER6 = new GroupingByExamples.Worker("Иван", 35, 70000, "Разработчик"); |
| 19 | + |
| 20 | + private List<GroupingByExamples.Worker> buildWorkersList() { |
| 21 | + return List.of(WORKER1, WORKER2, WORKER3, WORKER4, WORKER5, WORKER6); |
| 22 | + } |
| 23 | + |
| 24 | + @Test |
| 25 | + public void groupWorkersByPositionLists() { |
| 26 | + List<GroupingByExamples.Worker> workers = buildWorkersList(); |
| 27 | + |
| 28 | + Map<String, List<GroupingByExamples.Worker>> result = GroupingByExamples.groupWorkersByPositionLists(workers); |
| 29 | + |
| 30 | + assertThat(result).hasSize(3); |
| 31 | + assertThat(result.get("Разработчик")).hasSize(3); |
| 32 | + assertThat(result.get("Разработчик")).containsExactlyInAnyOrder(WORKER1, WORKER2, WORKER6); |
| 33 | + assertThat(result.get("Менеджер")).hasSize(2); |
| 34 | + assertThat(result.get("Менеджер")).containsExactlyInAnyOrder(WORKER3, WORKER4); |
| 35 | + assertThat(result.get("Тестировщик")).hasSize(1); |
| 36 | + assertThat(result.get("Тестировщик")).containsExactly(WORKER5); |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + public void groupWorkersByPositionListsForEmptyList() { |
| 41 | + List<GroupingByExamples.Worker> workers = List.of(); |
| 42 | + |
| 43 | + Map<String, List<GroupingByExamples.Worker>> result = GroupingByExamples.groupWorkersByPositionLists(workers); |
| 44 | + |
| 45 | + assertThat(result).isEmpty(); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + public void groupWorkersByPositionSets() { |
| 50 | + List<GroupingByExamples.Worker> workers = buildWorkersList(); |
| 51 | + |
| 52 | + Map<String, Set<GroupingByExamples.Worker>> result = GroupingByExamples.groupWorkersByPositionSets(workers); |
| 53 | + |
| 54 | + assertThat(result).hasSize(3); |
| 55 | + assertThat(result.get("Разработчик")).hasSize(3); |
| 56 | + assertThat(result.get("Разработчик")).containsExactlyInAnyOrder(WORKER1, WORKER2, WORKER6); |
| 57 | + assertThat(result.get("Менеджер")).hasSize(2); |
| 58 | + assertThat(result.get("Менеджер")).containsExactlyInAnyOrder(WORKER3, WORKER4); |
| 59 | + assertThat(result.get("Тестировщик")).hasSize(1); |
| 60 | + assertThat(result.get("Тестировщик")).containsExactly(WORKER5); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + public void groupWorkersByPositionSetsForEmptyList() { |
| 65 | + List<GroupingByExamples.Worker> workers = List.of(); |
| 66 | + |
| 67 | + Map<String, Set<GroupingByExamples.Worker>> result = GroupingByExamples.groupWorkersByPositionSets(workers); |
| 68 | + |
| 69 | + assertThat(result).isEmpty(); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void calculateAmountOfWorkersForEachPosition() { |
| 74 | + List<GroupingByExamples.Worker> workers = buildWorkersList(); |
| 75 | + |
| 76 | + Map<String, Long> result = GroupingByExamples.calculateAmountOfWorkersForEachPosition(workers); |
| 77 | + |
| 78 | + assertThat(result).hasSize(3); |
| 79 | + assertThat(result.get("Разработчик")).isEqualTo(3L); |
| 80 | + assertThat(result.get("Менеджер")).isEqualTo(2L); |
| 81 | + assertThat(result.get("Тестировщик")).isEqualTo(1L); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + public void calculateAmountOfWorkersForEachPositionForEmptyList() { |
| 86 | + List<GroupingByExamples.Worker> workers = List.of(); |
| 87 | + |
| 88 | + Map<String, Long> result = GroupingByExamples.calculateAmountOfWorkersForEachPosition(workers); |
| 89 | + |
| 90 | + assertThat(result).isEmpty(); |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + public void groupWorkersNamesByPosition() { |
| 95 | + List<GroupingByExamples.Worker> workers = buildWorkersList(); |
| 96 | + |
| 97 | + Map<String, Set<String>> result = GroupingByExamples.groupWorkersNamesByPosition(workers); |
| 98 | + |
| 99 | + assertThat(result).hasSize(3); |
| 100 | + // Set не содержит дубликатов, поэтому два "Иван" становятся одним |
| 101 | + assertThat(result.get("Разработчик")).hasSize(2); |
| 102 | + assertThat(result.get("Разработчик")).containsExactlyInAnyOrder("Иван", "Петр"); |
| 103 | + assertThat(result.get("Менеджер")).hasSize(2); |
| 104 | + assertThat(result.get("Менеджер")).containsExactlyInAnyOrder("Мария", "Анна"); |
| 105 | + assertThat(result.get("Тестировщик")).hasSize(1); |
| 106 | + assertThat(result.get("Тестировщик")).containsExactly("Сергей"); |
| 107 | + } |
| 108 | + |
| 109 | + @Test |
| 110 | + public void groupWorkersNamesByPositionForEmptyList() { |
| 111 | + List<GroupingByExamples.Worker> workers = List.of(); |
| 112 | + |
| 113 | + Map<String, Set<String>> result = GroupingByExamples.groupWorkersNamesByPosition(workers); |
| 114 | + |
| 115 | + assertThat(result).isEmpty(); |
| 116 | + } |
| 117 | + |
| 118 | + @Test |
| 119 | + public void calculateAverageSalaryForEachPosition() { |
| 120 | + List<GroupingByExamples.Worker> workers = buildWorkersList(); |
| 121 | + |
| 122 | + Map<String, Double> result = GroupingByExamples.calculateAverageSalaryForEachPosition(workers); |
| 123 | + |
| 124 | + assertThat(result).hasSize(3); |
| 125 | + // Разработчики: (50000 + 45000 + 70000) / 3 = 55000.0 |
| 126 | + assertThat(result.get("Разработчик")).isEqualTo(55000.0); |
| 127 | + // Менеджеры: (60000 + 55000) / 2 = 57500.0 |
| 128 | + assertThat(result.get("Менеджер")).isEqualTo(57500.0); |
| 129 | + // Тестировщик: 40000 / 1 = 40000.0 |
| 130 | + assertThat(result.get("Тестировщик")).isEqualTo(40000.0); |
| 131 | + } |
| 132 | + |
| 133 | + @Test |
| 134 | + public void calculateAverageSalaryForEachPositionForEmptyList() { |
| 135 | + List<GroupingByExamples.Worker> workers = List.of(); |
| 136 | + |
| 137 | + Map<String, Double> result = GroupingByExamples.calculateAverageSalaryForEachPosition(workers); |
| 138 | + |
| 139 | + assertThat(result).isEmpty(); |
| 140 | + } |
| 141 | + |
| 142 | + @Test |
| 143 | + public void groupWorkersByPositionWhenWorkerRepresentedByString() { |
| 144 | + List<GroupingByExamples.Worker> workers = buildWorkersList(); |
| 145 | + |
| 146 | + Map<String, String> result = GroupingByExamples.groupWorkersByPositionWhenWorkerRepresentedByString(workers); |
| 147 | + |
| 148 | + assertThat(result).hasSize(3); |
| 149 | + assertThat(result.get("Разработчик")).isEqualTo("{Иван, Петр, Иван}"); |
| 150 | + assertThat(result.get("Менеджер")).isEqualTo("{Мария, Анна}"); |
| 151 | + assertThat(result.get("Тестировщик")).isEqualTo("{Сергей}"); |
| 152 | + } |
| 153 | + |
| 154 | + @Test |
| 155 | + public void groupWorkersByPositionWhenWorkerRepresentedByStringForEmptyList() { |
| 156 | + List<GroupingByExamples.Worker> workers = List.of(); |
| 157 | + |
| 158 | + Map<String, String> result = GroupingByExamples.groupWorkersByPositionWhenWorkerRepresentedByString(workers); |
| 159 | + |
| 160 | + assertThat(result).isEmpty(); |
| 161 | + } |
| 162 | + |
| 163 | + @Test |
| 164 | + public void groupWorkersByPositionAndAge() { |
| 165 | + List<GroupingByExamples.Worker> workers = buildWorkersList(); |
| 166 | + |
| 167 | + Map<String, Map<Integer, List<GroupingByExamples.Worker>>> result = GroupingByExamples.groupWorkersByPositionAndAge(workers); |
| 168 | + |
| 169 | + assertThat(result).hasSize(3); |
| 170 | + |
| 171 | + // Проверяем группировку разработчиков по возрасту |
| 172 | + Map<Integer, List<GroupingByExamples.Worker>> developers = result.get("Разработчик"); |
| 173 | + assertThat(developers).hasSize(3); |
| 174 | + assertThat(developers.get(30)).hasSize(1).containsExactly(WORKER1); |
| 175 | + assertThat(developers.get(25)).hasSize(1).containsExactly(WORKER2); |
| 176 | + assertThat(developers.get(35)).hasSize(1).containsExactly(WORKER6); |
| 177 | + |
| 178 | + // Проверяем группировку менеджеров по возрасту |
| 179 | + Map<Integer, List<GroupingByExamples.Worker>> managers = result.get("Менеджер"); |
| 180 | + assertThat(managers).hasSize(2); |
| 181 | + assertThat(managers.get(28)).hasSize(1).containsExactly(WORKER3); |
| 182 | + assertThat(managers.get(32)).hasSize(1).containsExactly(WORKER4); |
| 183 | + |
| 184 | + // Проверяем группировку тестировщиков по возрасту |
| 185 | + Map<Integer, List<GroupingByExamples.Worker>> testers = result.get("Тестировщик"); |
| 186 | + assertThat(testers).hasSize(1); |
| 187 | + assertThat(testers.get(27)).hasSize(1).containsExactly(WORKER5); |
| 188 | + } |
| 189 | + |
| 190 | + @Test |
| 191 | + public void groupWorkersByPositionAndAgeForEmptyList() { |
| 192 | + List<GroupingByExamples.Worker> workers = List.of(); |
| 193 | + |
| 194 | + Map<String, Map<Integer, List<GroupingByExamples.Worker>>> result = GroupingByExamples.groupWorkersByPositionAndAge(workers); |
| 195 | + |
| 196 | + assertThat(result).isEmpty(); |
| 197 | + } |
| 198 | + |
| 199 | + @Test |
| 200 | + public void groupWorkersByPositionAndAgeWithSameAge() { |
| 201 | + GroupingByExamples.Worker worker1 = new GroupingByExamples.Worker("Иван", 30, 50000, "Разработчик"); |
| 202 | + GroupingByExamples.Worker worker2 = new GroupingByExamples.Worker("Петр", 30, 45000, "Разработчик"); |
| 203 | + List<GroupingByExamples.Worker> workers = List.of(worker1, worker2); |
| 204 | + |
| 205 | + Map<String, Map<Integer, List<GroupingByExamples.Worker>>> result = GroupingByExamples.groupWorkersByPositionAndAge(workers); |
| 206 | + |
| 207 | + assertThat(result).hasSize(1); |
| 208 | + Map<Integer, List<GroupingByExamples.Worker>> developers = result.get("Разработчик"); |
| 209 | + assertThat(developers).hasSize(1); |
| 210 | + assertThat(developers.get(30)).hasSize(2); |
| 211 | + assertThat(developers.get(30)).containsExactlyInAnyOrder(worker1, worker2); |
| 212 | + } |
| 213 | +} |
0 commit comments