INFRA-3698 | Saurabh | Resources can be modified silently post CR approval (#1187)
* INFRA-3698 | Saurabh | Resources can be modified silently post CR approval * INFRA-3698 | Saurabh | sout removed * INFRA-3698 | Saurabh | comments resolved * INFRA-3698 | Saurabh | Bulk Update Status using hibernate with tests * INFRA-3698 | Saurabh | removed redudant code * INFRA-3698 | Saurabh | removed comments * INFRA-3698 | Saurabh | refactored query with custom repository * INFRA-3698 | Saurabh | checkstyles * INFRA-3698 | Saurabh | moved in try catch
This commit is contained in:
committed by
GitHub
parent
33eecfafed
commit
315ecaf39f
@@ -0,0 +1,14 @@
|
||||
package com.navi.infra.portal.v2.approvalflow.dto;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@AllArgsConstructor
|
||||
public class ApprovalRequestData {
|
||||
private final Long requestId;
|
||||
private final Long teamId;
|
||||
private final Long approvalRequestId;
|
||||
}
|
||||
@@ -13,10 +13,12 @@ import javax.persistence.Id;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
|
||||
@Entity
|
||||
@Getter
|
||||
@Setter
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@ToString
|
||||
|
||||
@@ -18,6 +18,7 @@ import com.navi.infra.portal.repository.ManifestRepository;
|
||||
import com.navi.infra.portal.service.user.PrivilegeUtilService;
|
||||
import com.navi.infra.portal.service.user.UserService;
|
||||
import com.navi.infra.portal.util.MapUtil;
|
||||
import com.navi.infra.portal.v2.approvalflow.dto.ApprovalRequestData;
|
||||
import com.navi.infra.portal.v2.approvalflow.dto.ApprovalRequestDto;
|
||||
import com.navi.infra.portal.v2.approvalflow.entity.ApprovalRequest;
|
||||
import com.navi.infra.portal.v2.approvalflow.entity.ApprovalRequestBuilder;
|
||||
@@ -39,6 +40,7 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.context.annotation.Lazy;
|
||||
@@ -135,15 +137,27 @@ public class ApprovalRequestServiceImpl implements ApprovalRequestService {
|
||||
manifest.getEnvironment(),
|
||||
(String) ((HashMap<String, Object>) manifest.getData().get("team")).get("name"));
|
||||
|
||||
oldApprovalList.forEach(request -> request.setStatus(PENDING));
|
||||
newApprovalList.forEach(request -> request.setStatus(PENDING));
|
||||
|
||||
final var approvalList = Stream.concat(oldApprovalList.stream(), newApprovalList.stream())
|
||||
.distinct()
|
||||
.collect(toUnmodifiableList());
|
||||
|
||||
List<ApprovalRequestData> extractedData = oldApprovalList.stream()
|
||||
.map(request -> new ApprovalRequestData(request.getRequestId(), request.getTeamId(),
|
||||
request.getId()))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
var savedApprovalRequests = repository.saveAll(approvalList);
|
||||
var additionalApprovals = new ArrayList<>(approvalList);
|
||||
additionalApprovals.removeAll(oldApprovalList);
|
||||
try {
|
||||
|
||||
changeRequestSlackService.updateApprovalStatusesInBulk(PENDING.code, extractedData);
|
||||
if (!oldApprovalList.isEmpty()) {
|
||||
log.info("oldApprovalList is not empty, updating slack message post manifest update");
|
||||
log.info(
|
||||
"oldApprovalList is not empty, updating slack message post manifest update");
|
||||
changeRequestSlackService.handleChangeRequestUpdate(manifest, changeRequest);
|
||||
}
|
||||
if (!additionalApprovals.isEmpty()) {
|
||||
@@ -152,7 +166,8 @@ public class ApprovalRequestServiceImpl implements ApprovalRequestService {
|
||||
additionalApprovals);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("Error while creating/updating CR {} slack message post manifest {} update : {}",
|
||||
log.error(
|
||||
"Error while creating/updating CR {} slack message post manifest {} update : {}",
|
||||
changeRequest.getId(), manifest.fullName(), e.getMessage());
|
||||
}
|
||||
return savedApprovalRequests;
|
||||
@@ -191,8 +206,10 @@ public class ApprovalRequestServiceImpl implements ApprovalRequestService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ApprovalRequest> findAllByRequestTypeAndTeamIds(RequestType requestType,
|
||||
List<Long> teamIds) {
|
||||
public List<ApprovalRequest> findAllByRequestTypeAndTeamIds(
|
||||
RequestType requestType,
|
||||
List<Long> teamIds
|
||||
) {
|
||||
return repository.findAllByRequestTypeAndTeamIdIsIn(requestType.code, teamIds);
|
||||
}
|
||||
|
||||
@@ -232,8 +249,10 @@ public class ApprovalRequestServiceImpl implements ApprovalRequestService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ApprovalRequest> findPendingByRequestTypeAndRequestId(RequestType requestType,
|
||||
Long requestId) {
|
||||
public List<ApprovalRequest> findPendingByRequestTypeAndRequestId(
|
||||
RequestType requestType,
|
||||
Long requestId
|
||||
) {
|
||||
return repository.findAllPendingByRequestTypeAndRequestId(requestType.code, requestId);
|
||||
}
|
||||
|
||||
@@ -267,13 +286,17 @@ public class ApprovalRequestServiceImpl implements ApprovalRequestService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ApprovalRequest> findAllByRequestTypeAndRequestIds(RequestType requestType,
|
||||
List<Long> changeRequestIds) {
|
||||
public List<ApprovalRequest> findAllByRequestTypeAndRequestIds(
|
||||
RequestType requestType,
|
||||
List<Long> changeRequestIds
|
||||
) {
|
||||
return repository.findAllByRequestTypeAndRequestIdIn(requestType.code, changeRequestIds);
|
||||
}
|
||||
|
||||
private void checkPermissionToAlterApproveRequests(Long userId,
|
||||
ApprovalRequest approvalRequest) {
|
||||
private void checkPermissionToAlterApproveRequests(
|
||||
Long userId,
|
||||
ApprovalRequest approvalRequest
|
||||
) {
|
||||
if (!userHasSameTeamAsApprovalRequest(approvalRequest.getTeamId(), userId)) {
|
||||
throw new AccessDeniedException(
|
||||
format("User cannot approve this request: userId: %d, team: %d",
|
||||
|
||||
@@ -3,14 +3,21 @@ package com.navi.infra.portal.v2.jit.repository;
|
||||
import com.navi.infra.portal.v2.jit.entity.SlackChangeRequest;
|
||||
import java.util.List;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.Modifying;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
public interface ChangeRequestSlackRepository extends JpaRepository<SlackChangeRequest, Long> {
|
||||
public interface ChangeRequestSlackRepository extends JpaRepository<SlackChangeRequest, Long>,
|
||||
CustomSlackApprovalRepository {
|
||||
|
||||
@Query(value = "SELECT afs.* FROM approval_flow_slack afs "
|
||||
+ "WHERE approval_request_id = :approvalRequestId "
|
||||
+ "AND team_id = :teamId AND status = 100", nativeQuery = true)
|
||||
List<SlackChangeRequest> findAllPendingByApprovalRequestIdAndTeam(Long approvalRequestId, Long teamId);
|
||||
List<SlackChangeRequest> findAllPendingByApprovalRequestIdAndTeam(
|
||||
Long approvalRequestId,
|
||||
Long teamId
|
||||
);
|
||||
|
||||
@Query(value = "SELECT afs.* FROM approval_flow_slack afs "
|
||||
+ "WHERE change_request_id = :changeRequestId AND status != 200", nativeQuery = true)
|
||||
@@ -20,4 +27,18 @@ public interface ChangeRequestSlackRepository extends JpaRepository<SlackChangeR
|
||||
+ "WHERE change_request_id = :changeRequestId ", nativeQuery = true)
|
||||
List<SlackChangeRequest> findAllByChangeRequestId(Long changeRequestId);
|
||||
|
||||
@Modifying
|
||||
@Transactional
|
||||
@Query(value = "UPDATE approval_flow_slack SET status = :status "
|
||||
+ "WHERE change_request_id = :changeRequestId "
|
||||
+ "AND team_id = :teamId "
|
||||
+ "AND approval_request_id = :approvalRequestId", nativeQuery = true)
|
||||
void updateStatusByChangeRequestIdAndTeamIdAndApprovalRequestId(
|
||||
@Param("changeRequestId") Long changeRequestId,
|
||||
@Param("teamId") Long teamId,
|
||||
@Param("approvalRequestId") Long approvalRequestId,
|
||||
@Param("status") int status
|
||||
);
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.navi.infra.portal.v2.jit.repository;
|
||||
|
||||
import com.navi.infra.portal.v2.approvalflow.dto.ApprovalRequestData;
|
||||
import java.util.List;
|
||||
|
||||
public interface CustomSlackApprovalRepository {
|
||||
|
||||
public void updateApprovalStatusesInBulk(int status, List<ApprovalRequestData> requestParams);
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package com.navi.infra.portal.v2.jit.repository;
|
||||
|
||||
import com.navi.infra.portal.v2.approvalflow.dto.ApprovalRequestData;
|
||||
import java.util.List;
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.Query;
|
||||
|
||||
public class CustomSlackApprovalRepositoryImpl implements CustomSlackApprovalRepository {
|
||||
|
||||
private final EntityManager entityManager;
|
||||
|
||||
public CustomSlackApprovalRepositoryImpl(EntityManager entityManager) {
|
||||
this.entityManager = entityManager;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateApprovalStatusesInBulk(int status, List<ApprovalRequestData> requestParams) {
|
||||
if (requestParams.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
StringBuilder queryBuilder = new StringBuilder(
|
||||
"UPDATE approval_flow_slack SET status = ?1 WHERE ");
|
||||
queryBuilder.append("(change_request_id, team_id, approval_request_id) IN (");
|
||||
|
||||
int paramIndex = 2;
|
||||
for (int i = 0; i < requestParams.size(); i++) {
|
||||
if (i > 0) {
|
||||
queryBuilder.append(", ");
|
||||
}
|
||||
queryBuilder.append(
|
||||
"(?" + paramIndex++ + ", ?" + paramIndex++ + ", ?" + paramIndex++ + ")");
|
||||
}
|
||||
queryBuilder.append(")");
|
||||
|
||||
Query query = entityManager.createNativeQuery(queryBuilder.toString());
|
||||
query.setParameter(1, status);
|
||||
|
||||
int index = 2;
|
||||
for (ApprovalRequestData requestData : requestParams) {
|
||||
query.setParameter(index++, requestData.getRequestId());
|
||||
query.setParameter(index++, requestData.getTeamId());
|
||||
query.setParameter(index++, requestData.getApprovalRequestId());
|
||||
}
|
||||
|
||||
query.executeUpdate();
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,19 +1,25 @@
|
||||
package com.navi.infra.portal.v2.jit.service;
|
||||
|
||||
import com.navi.infra.portal.domain.manifest.Manifest;
|
||||
import com.navi.infra.portal.v2.approvalflow.dto.ApprovalRequestData;
|
||||
import com.navi.infra.portal.v2.approvalflow.entity.ApprovalRequest;
|
||||
import com.navi.infra.portal.v2.changerequest.entity.ChangeRequest;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
public interface ChangeRequestSlackService {
|
||||
ChangeRequest sendSlackApprovalMessages(Manifest manifest, ChangeRequest changeRequest,
|
||||
List<ApprovalRequest> approvalRequests) throws IOException;
|
||||
|
||||
ChangeRequest sendSlackApprovalMessages(
|
||||
Manifest manifest, ChangeRequest changeRequest,
|
||||
List<ApprovalRequest> approvalRequests
|
||||
) throws IOException;
|
||||
|
||||
ApprovalRequest approveRequest(String userEmail, Long requestId);
|
||||
|
||||
void handleApproveRequest(Long changeRequestId, Long approvalRequestId, Long userId,
|
||||
Long teamId) throws IOException;
|
||||
void handleApproveRequest(
|
||||
Long changeRequestId, Long approvalRequestId, Long userId,
|
||||
Long teamId
|
||||
) throws IOException;
|
||||
|
||||
void handleChangeRequestUpdate(Manifest manifest, ChangeRequest changeRequest)
|
||||
throws IOException;
|
||||
@@ -22,4 +28,15 @@ public interface ChangeRequestSlackService {
|
||||
|
||||
ChangeRequest closeRequest(String userEmail, Long requestId);
|
||||
|
||||
void updateStatusByChangeRequestIdAndTeamIdAndApprovalRequestId(
|
||||
Long changeRequestId,
|
||||
Long teamId,
|
||||
Long approvalRequestId,
|
||||
int status
|
||||
);
|
||||
|
||||
void updateApprovalStatusesInBulk(
|
||||
int status, List<ApprovalRequestData> requestParams
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import com.navi.infra.portal.domain.user.User;
|
||||
import com.navi.infra.portal.service.manifest.ManifestService;
|
||||
import com.navi.infra.portal.service.user.PrivilegeUtilService;
|
||||
import com.navi.infra.portal.service.user.UserService;
|
||||
import com.navi.infra.portal.v2.approvalflow.dto.ApprovalRequestData;
|
||||
import com.navi.infra.portal.v2.approvalflow.entity.ApprovalRequest;
|
||||
import com.navi.infra.portal.v2.approvalflow.entity.RequestType;
|
||||
import com.navi.infra.portal.v2.approvalflow.service.ApprovalRequestService;
|
||||
@@ -50,6 +51,7 @@ public class ChangeRequestSlackServiceImpl implements ChangeRequestSlackService
|
||||
private final String commonChannelId;
|
||||
private final ObjectMapper objectMapper;
|
||||
|
||||
|
||||
public ChangeRequestSlackServiceImpl(
|
||||
TeamService teamService,
|
||||
UserService userService,
|
||||
@@ -87,8 +89,10 @@ public class ChangeRequestSlackServiceImpl implements ChangeRequestSlackService
|
||||
return noSlackRequestExists;
|
||||
}
|
||||
|
||||
private List<SlackChangeRequest> mapToSlackApprovals(ApprovalRequest approvalRequest,
|
||||
ChangeRequest changeRequest, Long teamId, List<Long> approversList) {
|
||||
private List<SlackChangeRequest> mapToSlackApprovals(
|
||||
ApprovalRequest approvalRequest,
|
||||
ChangeRequest changeRequest, Long teamId, List<Long> approversList
|
||||
) {
|
||||
return approversList.stream()
|
||||
.map(userId -> {
|
||||
var slackApproval = new SlackChangeRequest();
|
||||
@@ -105,7 +109,8 @@ public class ChangeRequestSlackServiceImpl implements ChangeRequestSlackService
|
||||
}
|
||||
|
||||
private Map<Long, List<Long>> findApproversForEachTeam(
|
||||
Map<Long, Map<Long, List<String>>> teamUserPrivilegesMap, Manifest manifest) {
|
||||
Map<Long, Map<Long, List<String>>> teamUserPrivilegesMap, Manifest manifest
|
||||
) {
|
||||
log.info("Finding approvers for each team based on user privileges");
|
||||
return teamUserPrivilegesMap.entrySet().stream()
|
||||
.collect(Collectors.toMap(
|
||||
@@ -184,8 +189,10 @@ public class ChangeRequestSlackServiceImpl implements ChangeRequestSlackService
|
||||
}
|
||||
|
||||
|
||||
private void handleApproval(Long changeRequestId, Long approvalRequestId, Long userId,
|
||||
Long teamId) throws IOException {
|
||||
private void handleApproval(
|
||||
Long changeRequestId, Long approvalRequestId, Long userId,
|
||||
Long teamId
|
||||
) throws IOException {
|
||||
log.info("Handling approval for changeRequestId: {}, approvalRequestId: {}, userId: {}, "
|
||||
+ "teamId: {}", changeRequestId, approvalRequestId, userId, teamId);
|
||||
List<SlackChangeRequest> requests = changeRequestSlackRepository
|
||||
@@ -212,8 +219,10 @@ public class ChangeRequestSlackServiceImpl implements ChangeRequestSlackService
|
||||
updateCommonSlackChannelMessage(changeRequest, manifest, manifestDiff);
|
||||
}
|
||||
|
||||
private void updateCommonSlackChannelMessage(ChangeRequest changeRequest, Manifest manifest,
|
||||
String manifestDiff) {
|
||||
private void updateCommonSlackChannelMessage(
|
||||
ChangeRequest changeRequest, Manifest manifest,
|
||||
String manifestDiff
|
||||
) {
|
||||
var allRequests = approvalRequestService.findAllByRequestTypeAndRequestIds(
|
||||
RequestType.CHANGE_REQUEST, List.of(changeRequest.getId()));
|
||||
|
||||
@@ -249,7 +258,10 @@ public class ChangeRequestSlackServiceImpl implements ChangeRequestSlackService
|
||||
);
|
||||
}
|
||||
|
||||
private List<String> getMissingTeams(List<Long> teamList, Map<Long, List<Long>> teamApproversMap) {
|
||||
private List<String> getMissingTeams(
|
||||
List<Long> teamList,
|
||||
Map<Long, List<Long>> teamApproversMap
|
||||
) {
|
||||
log.info("Finding missing teams for approval");
|
||||
return teamList.stream()
|
||||
.filter(teamId -> !teamApproversMap.containsKey(teamId))
|
||||
@@ -263,8 +275,10 @@ public class ChangeRequestSlackServiceImpl implements ChangeRequestSlackService
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
private List<SlackChangeRequest> getRequestsToBeSentToReviewers(ChangeRequest changeRequest,
|
||||
List<ApprovalRequest> approvalRequests, Map<Long, List<Long>> teamApproversMap) {
|
||||
private List<SlackChangeRequest> getRequestsToBeSentToReviewers(
|
||||
ChangeRequest changeRequest,
|
||||
List<ApprovalRequest> approvalRequests, Map<Long, List<Long>> teamApproversMap
|
||||
) {
|
||||
log.info("Getting requests to sent for each team's reviewers");
|
||||
return approvalRequests.stream()
|
||||
.map(approvalRequest -> mapToSlackApprovals(approvalRequest, changeRequest,
|
||||
@@ -274,8 +288,10 @@ public class ChangeRequestSlackServiceImpl implements ChangeRequestSlackService
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
private void handleSendingReviewerDm(String manifestDiff, Manifest manifest,
|
||||
ChangeRequest changeRequest, SlackChangeRequest slackChangeRequest) {
|
||||
private void handleSendingReviewerDm(
|
||||
String manifestDiff, Manifest manifest,
|
||||
ChangeRequest changeRequest, SlackChangeRequest slackChangeRequest
|
||||
) {
|
||||
log.info("Sending slack DM to reviewer for CR: {} and slackRequestId: {}",
|
||||
changeRequest.getId(), slackChangeRequest.getId());
|
||||
CompletableFuture.runAsync(() -> {
|
||||
@@ -293,8 +309,10 @@ public class ChangeRequestSlackServiceImpl implements ChangeRequestSlackService
|
||||
}
|
||||
|
||||
@Override
|
||||
public ChangeRequest sendSlackApprovalMessages(Manifest manifest, ChangeRequest changeRequest,
|
||||
List<ApprovalRequest> approvalRequests) throws IOException {
|
||||
public ChangeRequest sendSlackApprovalMessages(
|
||||
Manifest manifest, ChangeRequest changeRequest,
|
||||
List<ApprovalRequest> approvalRequests
|
||||
) throws IOException {
|
||||
log.info("Sending slack approval messages for CR: {}", changeRequest.getId());
|
||||
var teamList = approvalRequests.stream().map(ApprovalRequest::getTeamId)
|
||||
.collect(Collectors.toList());
|
||||
@@ -333,7 +351,9 @@ public class ChangeRequestSlackServiceImpl implements ChangeRequestSlackService
|
||||
channelInfoMessage, changeRequest, manifest.fullName(), SlackColor.OPEN
|
||||
));
|
||||
}
|
||||
slackApprovalRequestsWithId.forEach(slackChangeRequest -> handleSendingReviewerDm(manifestDiff, manifest, changeRequest, slackChangeRequest));
|
||||
slackApprovalRequestsWithId.forEach(
|
||||
slackChangeRequest -> handleSendingReviewerDm(manifestDiff, manifest, changeRequest,
|
||||
slackChangeRequest));
|
||||
return changeRequest;
|
||||
}
|
||||
|
||||
@@ -378,9 +398,31 @@ public class ChangeRequestSlackServiceImpl implements ChangeRequestSlackService
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateStatusByChangeRequestIdAndTeamIdAndApprovalRequestId(
|
||||
Long changeRequestId,
|
||||
Long teamId,
|
||||
Long approvalRequestId,
|
||||
int status
|
||||
) {
|
||||
log.info("Updating status for changeRequestId: {}, teamId: {}, approvalRequestId: {}, "
|
||||
+ "status: {}", changeRequestId, teamId, approvalRequestId, status);
|
||||
changeRequestSlackRepository.updateStatusByChangeRequestIdAndTeamIdAndApprovalRequestId(
|
||||
changeRequestId, teamId, approvalRequestId, status
|
||||
);
|
||||
}
|
||||
|
||||
public void handleApproveRequest(Long changeRequestId, Long approvalRequestId, Long userId,
|
||||
Long teamId) throws IOException {
|
||||
@Override
|
||||
public void updateApprovalStatusesInBulk(int status, List<ApprovalRequestData> requestParams) {
|
||||
log.info("Updating approval statuses in bulk for status: {}", status);
|
||||
changeRequestSlackRepository.updateApprovalStatusesInBulk(status, requestParams);
|
||||
}
|
||||
|
||||
|
||||
public void handleApproveRequest(
|
||||
Long changeRequestId, Long approvalRequestId, Long userId,
|
||||
Long teamId
|
||||
) throws IOException {
|
||||
if (noSlackRequestsExistsForRequest(changeRequestId)) {
|
||||
throw new IllegalStateException(
|
||||
String.format("No slack messages found for CR id: %s", changeRequestId));
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
package com.navi.infra.portal.v2.jit.repository;
|
||||
|
||||
import com.navi.infra.portal.v2.approvalflow.dto.ApprovalRequestData;
|
||||
import java.util.List;
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.Query;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyInt;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class CustomSlackApprovalRepositoryImplTest {
|
||||
|
||||
@Mock
|
||||
private EntityManager entityManager;
|
||||
|
||||
@Mock
|
||||
private Query query;
|
||||
|
||||
@InjectMocks
|
||||
private CustomSlackApprovalRepositoryImpl customSlackApprovalRepositoryImpl;
|
||||
|
||||
@Test
|
||||
void testUpdateApprovalStatusesInBulk_WithEmptyRequestParams() {
|
||||
|
||||
customSlackApprovalRepositoryImpl.updateApprovalStatusesInBulk(1, List.of());
|
||||
|
||||
verify(entityManager, never()).createNativeQuery(anyString());
|
||||
verify(query, never()).executeUpdate();
|
||||
}
|
||||
|
||||
@Test
|
||||
void testUpdateApprovalStatusesInBulk_WithValidRequestParams() {
|
||||
|
||||
int status = 100;
|
||||
List<ApprovalRequestData> requestParams = List.of(
|
||||
new ApprovalRequestData(1L, 101L, 1001L),
|
||||
new ApprovalRequestData(2L, 102L, 1002L)
|
||||
);
|
||||
|
||||
when(entityManager.createNativeQuery(anyString())).thenReturn(query);
|
||||
when(query.setParameter(anyInt(), any())).thenReturn(query);
|
||||
|
||||
customSlackApprovalRepositoryImpl.updateApprovalStatusesInBulk(status, requestParams);
|
||||
|
||||
verify(entityManager).createNativeQuery(anyString());
|
||||
verify(query).setParameter(1, status);
|
||||
|
||||
verify(query).setParameter(2, 1L);
|
||||
verify(query).setParameter(3, 101L);
|
||||
verify(query).setParameter(4, 1001L);
|
||||
|
||||
verify(query).setParameter(5, 2L);
|
||||
verify(query).setParameter(6, 102L);
|
||||
verify(query).setParameter(7, 1002L);
|
||||
|
||||
verify(query).executeUpdate();
|
||||
}
|
||||
}
|
||||
@@ -89,10 +89,11 @@ class ChangeRequestSlackServiceImplTest {
|
||||
@Mock
|
||||
private ChangeRequestSlackRepository changeRequestSlackRepository;
|
||||
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
changeRequestSlackService =
|
||||
new ChangeRequestSlackServiceImpl(teamService, userService,privilegeUtilService,
|
||||
new ChangeRequestSlackServiceImpl(teamService, userService, privilegeUtilService,
|
||||
approvalRequestService, changeRequestService, manifestService, slackBotClient,
|
||||
changeRequestSlackUtil, changeRequestSlackRepository, new ObjectMapper(), "");
|
||||
|
||||
@@ -125,7 +126,7 @@ class ChangeRequestSlackServiceImplTest {
|
||||
user4.setTeams(List.of(team2));
|
||||
|
||||
mockUser = mock(User.class);
|
||||
|
||||
|
||||
Collection<? extends GrantedAuthority> authorities = mockUser.getAuthorities();
|
||||
Authentication newAuthentication = new UsernamePasswordAuthenticationToken(
|
||||
"mockUser@navi.com", "admin", authorities);
|
||||
@@ -163,8 +164,10 @@ class ChangeRequestSlackServiceImplTest {
|
||||
slackPostResponse.setChannel("channel");
|
||||
}
|
||||
|
||||
private SlackChangeRequest createSlackChangeRequest(RequestStatus status,
|
||||
ChangeRequest changeRequest, ApprovalRequest approvalRequest, User user, Team team) {
|
||||
private SlackChangeRequest createSlackChangeRequest(
|
||||
RequestStatus status,
|
||||
ChangeRequest changeRequest, ApprovalRequest approvalRequest, User user, Team team
|
||||
) {
|
||||
var slackChangeRequest = new SlackChangeRequest();
|
||||
slackChangeRequest.setStatus(status);
|
||||
slackChangeRequest.setChangeRequest(changeRequest);
|
||||
@@ -181,8 +184,8 @@ class ChangeRequestSlackServiceImplTest {
|
||||
when(mockUser.getEmail()).thenReturn("mockUser@navi.com");
|
||||
when(approvalRequestService.allowApproveRequest(1L, mockUser.getId()))
|
||||
.thenThrow(new AccessDeniedException(""));
|
||||
assertThrows(AccessDeniedException.class ,
|
||||
()-> changeRequestSlackService.approveRequest("mockUser@navi.com", 1L));
|
||||
assertThrows(AccessDeniedException.class,
|
||||
() -> changeRequestSlackService.approveRequest("mockUser@navi.com", 1L));
|
||||
verify(approvalRequestService, times(1)).allowApproveRequest(
|
||||
any(), any()
|
||||
);
|
||||
@@ -195,8 +198,8 @@ class ChangeRequestSlackServiceImplTest {
|
||||
when(mockUser.getEmail()).thenReturn("mockUser@navi.com");
|
||||
when(approvalRequestService.reject(1L, mockUser.getId()))
|
||||
.thenThrow(new AccessDeniedException(""));
|
||||
assertThrows(AccessDeniedException.class ,
|
||||
()-> changeRequestSlackService.rejectRequest("mockUser@navi.com", 1L));
|
||||
assertThrows(AccessDeniedException.class,
|
||||
() -> changeRequestSlackService.rejectRequest("mockUser@navi.com", 1L));
|
||||
verify(approvalRequestService, times(1)).reject(
|
||||
any(), any()
|
||||
);
|
||||
@@ -209,8 +212,8 @@ class ChangeRequestSlackServiceImplTest {
|
||||
when(mockUser.getEmail()).thenReturn("mockUser@navi.com");
|
||||
when(changeRequestService.close(1L, mockUser.getId()))
|
||||
.thenThrow(new NotFoundException(""));
|
||||
assertThrows(NotFoundException.class ,
|
||||
()-> changeRequestSlackService.closeRequest("mockUser@navi.com", 1L));
|
||||
assertThrows(NotFoundException.class,
|
||||
() -> changeRequestSlackService.closeRequest("mockUser@navi.com", 1L));
|
||||
verify(changeRequestService, times(1)).close(
|
||||
any(), any()
|
||||
);
|
||||
@@ -221,11 +224,11 @@ class ChangeRequestSlackServiceImplTest {
|
||||
void reviewerUserNotFound() {
|
||||
final var someRandomEmail = "someRandomEmail@random.com";
|
||||
when(userService.findUserByEmail(someRandomEmail)).thenThrow(
|
||||
new UsernameNotFoundException(someRandomEmail+" not found"));
|
||||
assertThrows(UsernameNotFoundException.class ,
|
||||
()-> changeRequestSlackService.approveRequest(someRandomEmail, 1L));
|
||||
assertThrows(UsernameNotFoundException.class ,
|
||||
()-> changeRequestSlackService.rejectRequest(someRandomEmail, 1L));
|
||||
new UsernameNotFoundException(someRandomEmail + " not found"));
|
||||
assertThrows(UsernameNotFoundException.class,
|
||||
() -> changeRequestSlackService.approveRequest(someRandomEmail, 1L));
|
||||
assertThrows(UsernameNotFoundException.class,
|
||||
() -> changeRequestSlackService.rejectRequest(someRandomEmail, 1L));
|
||||
verify(approvalRequestService, times(0)).allowApproveRequest(
|
||||
any(), any()
|
||||
);
|
||||
@@ -234,9 +237,10 @@ class ChangeRequestSlackServiceImplTest {
|
||||
@Test
|
||||
@DisplayName("should throw exception if slack message doesn't exists for the given cr")
|
||||
void slackMessageNotFound() {
|
||||
when(changeRequestSlackRepository.findAllByChangeRequestId(1L)).thenReturn(Collections.emptyList());
|
||||
assertThrows(IllegalStateException.class ,
|
||||
()-> changeRequestSlackService.handleApproveRequest(1L, 1L, 1L, 1L));
|
||||
when(changeRequestSlackRepository.findAllByChangeRequestId(1L)).thenReturn(
|
||||
Collections.emptyList());
|
||||
assertThrows(IllegalStateException.class,
|
||||
() -> changeRequestSlackService.handleApproveRequest(1L, 1L, 1L, 1L));
|
||||
}
|
||||
|
||||
|
||||
@@ -250,7 +254,8 @@ class ChangeRequestSlackServiceImplTest {
|
||||
Map.of(1L, List.of("yes"), 3L, List.of("no")),
|
||||
2L, Map.of(2L, List.of("no"), 4L, List.of("no")));
|
||||
when(teamService.findById(team2.getId())).thenReturn(team2);
|
||||
when(teamService.findTeamBasedUserPrivilegesForEachTeam(List.of(team1.getId(),team2.getId())))
|
||||
when(teamService.findTeamBasedUserPrivilegesForEachTeam(
|
||||
List.of(team1.getId(), team2.getId())))
|
||||
.thenReturn(teamUserPrivilegesMap);
|
||||
when(privilegeUtilService.canApproveChangeRequests(manifest, List.of("yes")))
|
||||
.thenReturn(true);
|
||||
@@ -277,7 +282,8 @@ class ChangeRequestSlackServiceImplTest {
|
||||
Map<Long, Map<Long, List<String>>> teamUserPrivilegesMap = Map.of(1L,
|
||||
Map.of(1L, List.of("yes"), 3L, List.of("no")));
|
||||
when(teamService.findById(team2.getId())).thenReturn(team2);
|
||||
when(teamService.findTeamBasedUserPrivilegesForEachTeam(List.of(team1.getId(),team2.getId()))).thenReturn(
|
||||
when(teamService.findTeamBasedUserPrivilegesForEachTeam(
|
||||
List.of(team1.getId(), team2.getId()))).thenReturn(
|
||||
teamUserPrivilegesMap);
|
||||
when(privilegeUtilService.canApproveChangeRequests(manifest, List.of("yes")))
|
||||
.thenReturn(true);
|
||||
@@ -326,20 +332,22 @@ class ChangeRequestSlackServiceImplTest {
|
||||
reviewerRequestsToSave.stream().anyMatch(request -> request.getUser().equals(user))
|
||||
);
|
||||
}))).thenReturn(slackRequests);
|
||||
when(teamService.findTeamBasedUserPrivilegesForEachTeam(List.of(team1.getId(),team2.getId())))
|
||||
when(teamService.findTeamBasedUserPrivilegesForEachTeam(
|
||||
List.of(team1.getId(), team2.getId())))
|
||||
.thenReturn(teamUserPrivilegesMap);
|
||||
when(privilegeUtilService.canApproveChangeRequests(manifest, List.of("yes")))
|
||||
.thenReturn(true);
|
||||
when(privilegeUtilService.canApproveChangeRequests(manifest, List.of("no")))
|
||||
.thenReturn(false);
|
||||
|
||||
when(slackBotClient.postMessage(any(),any())).thenReturn(slackPostResponse);
|
||||
changeRequest = changeRequestSlackService.sendSlackApprovalMessages(manifest, pendingChangeRequest,
|
||||
when(slackBotClient.postMessage(any(), any())).thenReturn(slackPostResponse);
|
||||
changeRequest = changeRequestSlackService.sendSlackApprovalMessages(manifest,
|
||||
pendingChangeRequest,
|
||||
List.of(approvalRequest1, approvalRequest2));
|
||||
verify(changeRequestSlackRepository, times(1)).saveAll(any());
|
||||
assertEquals(changeRequest.getSlackChannelMessageTimestamp(), slackPostResponse.getTs());
|
||||
verify(changeRequestSlackUtil, times(1))
|
||||
.getManifestDiff(changeRequest,manifest);
|
||||
.getManifestDiff(changeRequest, manifest);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -377,8 +385,10 @@ class ChangeRequestSlackServiceImplTest {
|
||||
when(changeRequestSlackRepository.findAllByChangeRequestId(changeRequest.getId()))
|
||||
.thenReturn(slackRequests);
|
||||
when(approvalRequestService.findAllByRequestTypeAndRequestIds(RequestType.CHANGE_REQUEST,
|
||||
List.of(rejectedChangeRequest.getId()))).thenReturn(List.of(approvalRequest1, approvalRequest2, approvalRequest3));
|
||||
when(changeRequestSlackUtil.getManifestDiff(rejectedChangeRequest,manifest)).thenReturn("diff");
|
||||
List.of(rejectedChangeRequest.getId()))).thenReturn(
|
||||
List.of(approvalRequest1, approvalRequest2, approvalRequest3));
|
||||
when(changeRequestSlackUtil.getManifestDiff(rejectedChangeRequest, manifest)).thenReturn(
|
||||
"diff");
|
||||
changeRequestSlackService.handleChangeRequestUpdate(manifest, rejectedChangeRequest);
|
||||
verify(changeRequestSlackUtil, times(1)).getManifestDiff(rejectedChangeRequest, manifest);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user