Added service method tests + javadocs

This commit is contained in:
BuildTools 2020-11-27 16:55:15 +01:00
parent 2545a0e682
commit 13eb8dae77
5 changed files with 56 additions and 12 deletions

View File

@ -21,11 +21,15 @@ public interface GroupRepository extends JpaRepository<Groups, Long> {
@Query("SELECT COUNT(*) AS assinged_times FROM Assignment WHERE isPastAssignment=false GROUP BY group HAVING group_id=?1")
Optional<Number> getAssignedAmount(Long groupId);
/**
* PLAIN SQL QUERY: SELECT group_id, COUNT(*) assinged_times FROM assignment
* WHERE is_past_assignment=0 GROUP BY group_id HAVING group_id IN (:ids)")
*
* @param groupIds list of groups ids
* @return list of objects arrays where first object is Groups instance and
* second is Long that is taken places value
*/
@Query("SELECT group, COUNT(*) AS assinged_times FROM Assignment a WHERE a.isPastAssignment=false GROUP BY a.group HAVING group_id IN (:ids)")
// @Query(nativeQuery = true, value = "SELECT group_id, COUNT(*) AS
// assinged_times FROM assignment WHERE is_past_assignment=0 GROUP BY group_id
// HAVING group_id IN (:ids)")
List<Object[]> getAssignedAmounts(@Param("ids") List<Long> groupIds);
}

View File

@ -54,12 +54,22 @@ public class GroupService {
}
public HashMap<Groups, Integer> getTakenPlaces(List<Groups> groups) {
HashMap<Groups, Integer> respoonse = new HashMap<>();
/**
*
* @param groups list of groups you want to get taken places ammount
* @return HashMap<Long, Integer> where Long is group id and Integer is how many
* places in gorup is already taken
*/
public HashMap<Long, Integer> getTakenPlaces(List<Groups> groups) {
HashMap<Long, Integer> response = new HashMap<>();
List<Object[]> respoonse2 = this.repo.getAssignedAmounts(
List<Object[]> respoonses = this.repo.getAssignedAmounts(
groups.stream().filter(Objects::nonNull).map(Groups::getId).collect(Collectors.toList()));
return respoonse;
for (Object[] element : respoonses) {
response.put(((Groups) element[0]).getId(), ((Long) element[1]).intValue());
}
return response;
}
}

View File

@ -9,8 +9,6 @@ import static org.junit.Assert.assertTrue;
import java.util.List;
import javax.swing.GroupLayout.Group;
import com.plannaplan.entities.Assignment;
import com.plannaplan.entities.Commision;
import com.plannaplan.entities.Groups;

View File

@ -2,7 +2,15 @@ package com.plannaplan.services;
import static org.junit.Assert.assertTrue;
import java.util.HashMap;
import java.util.List;
import com.plannaplan.entities.Assignment;
import com.plannaplan.entities.Commision;
import com.plannaplan.entities.Groups;
import com.plannaplan.entities.User;
import com.plannaplan.types.UserRoles;
import com.plannaplan.types.WeekDay;
import org.junit.Test;
import org.junit.runner.RunWith;
@ -18,6 +26,12 @@ public class GroupServiceTest {
@Autowired
private GroupService groupService;
@Autowired
private AssignmentService assignmentService;
@Autowired
private CommisionService commisionService;
@Autowired
private UserService userService;
@Test
public void createAndDeleteGroup() {
@ -33,7 +47,25 @@ public class GroupServiceTest {
}
@Test
public void shouldGetGroupsAssignmentsAmmounts() {
public void shouldGetGroupsAssignmentsAmmounts() throws InterruptedException {
final Groups testGroup = groupService.save(new Groups(43, "A-41", null, 645, WeekDay.MONDAY, null));
final Groups testGroup2 = groupService.save(new Groups(433, "A-41", null, 235, WeekDay.TUESDAY, null));
final Groups testGroup3 = groupService.save(new Groups(23, "A-41", null, 340, WeekDay.MONDAY, null));
final User user = this.userService.save(
new User("Dare", "Oc", "shouldReturnGroupAssignmentTimesList@grouprepository.test", UserRoles.STUDENT));
final Commision commision = this.commisionService.save(new Commision(user));
this.assignmentService.save(new Assignment(testGroup, commision));
this.assignmentService.save(new Assignment(testGroup2, commision));
this.assignmentService.save(new Assignment(testGroup3, commision));
Thread.sleep(1000);
HashMap<Long, Integer> response = this.groupService.getTakenPlaces(List.of(testGroup, testGroup2, testGroup3));
assertTrue(response.size() == 3);
assertTrue(response.get(testGroup.getId()) == 1);
assertTrue(response.get(testGroup2.getId()) == 1);
assertTrue(response.get(testGroup3.getId()) == 1);
}
}

View File

@ -41,7 +41,7 @@ public class GroupController {
@RequestParam(name = "capacity", defaultValue = "true") @ApiParam(value = "Boolean if we want to have capacity field in response") Boolean capacity,
@RequestParam(name = "takenPlaces", defaultValue = "false") @ApiParam(value = "Boolean if we want to have respoonse with information about taken places by other students") Boolean takenPlaces) {
List<Groups> groups = this.groupService.getGroupsByCourse(id);
HashMap<Groups, Integer> ammounts;
HashMap<Long, Integer> ammounts;
if (takenPlaces) {
ammounts = this.groupService.getTakenPlaces(groups);