diff --git a/server/src/main/java/com/objectcomputing/checkins/services/file/FileServicesBaseImpl.java b/server/src/main/java/com/objectcomputing/checkins/services/file/FileServicesBaseImpl.java new file mode 100644 index 0000000000..28e841c1a4 --- /dev/null +++ b/server/src/main/java/com/objectcomputing/checkins/services/file/FileServicesBaseImpl.java @@ -0,0 +1,177 @@ +package com.objectcomputing.checkins.services.file; + +import com.objectcomputing.checkins.services.checkindocument.CheckinDocument; +import com.objectcomputing.checkins.services.checkindocument.CheckinDocumentServices; +import com.objectcomputing.checkins.services.checkins.CheckIn; +import com.objectcomputing.checkins.services.checkins.CheckInServices; +import com.objectcomputing.checkins.services.memberprofile.MemberProfile; +import com.objectcomputing.checkins.services.memberprofile.MemberProfileServices; +import com.objectcomputing.checkins.services.memberprofile.MemberProfileUtils; +import com.objectcomputing.checkins.services.memberprofile.currentuser.CurrentUserServices; + +import io.micronaut.core.annotation.Nullable; +import io.micronaut.http.multipart.CompletedFileUpload; + +import jakarta.inject.Singleton; +import jakarta.validation.constraints.NotNull; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.FileOutputStream; +import java.io.IOException; +import java.util.Collections; +import java.util.HashSet; +import java.util.Set; +import java.util.UUID; +import java.util.function.Function; + +import static com.objectcomputing.checkins.services.validate.PermissionsValidation.NOT_AUTHORIZED_MSG; + +@Singleton +abstract public class FileServicesBaseImpl implements FileServices { + private static final Logger LOG = LoggerFactory.getLogger(FileServicesBaseImpl.class); + + protected final CheckInServices checkInServices; + protected final CheckinDocumentServices checkinDocumentServices; + protected final MemberProfileServices memberProfileServices; + protected final CurrentUserServices currentUserServices; + + public FileServicesBaseImpl(CheckInServices checkInServices, + CheckinDocumentServices checkinDocumentServices, + MemberProfileServices memberProfileServices, + CurrentUserServices currentUserServices) { + this.checkInServices = checkInServices; + this.checkinDocumentServices = checkinDocumentServices; + this.memberProfileServices = memberProfileServices; + this.currentUserServices = currentUserServices; + } + + abstract protected void getCheckinDocuments( + Set result, Set checkinDocuments) throws IOException; + abstract protected void downloadSingleFile( + String docId, FileOutputStream myWriter) throws IOException; + abstract protected FileInfoDTO uploadSingleFile( + CompletedFileUpload file, String directoryName, + Function consumer) throws IOException; + abstract protected void deleteSingleFile(String docId) throws IOException; + + @Override + public Set findFiles(@Nullable UUID checkInID) { + boolean isAdmin = currentUserServices.isAdmin(); + validate(checkInID == null && !isAdmin, NOT_AUTHORIZED_MSG); + + try { + Set result = new HashSet<>(); + if (checkInID == null && isAdmin) { + getCheckinDocuments(result, Collections.emptySet()); + } else if (checkInID != null) { + validate(!checkInServices.accessGranted(checkInID, currentUserServices.getCurrentUser().getId()), + "You are not authorized to perform this operation"); + + // If there aren't any documents, do not call + // getCheckinDocument. It assumes that an empty set means + // that it should attempt to get all documents. And, in this + // case, we just want an empty result set. + Set checkinDocuments = checkinDocumentServices.read(checkInID); + if (!checkinDocuments.isEmpty()) { + getCheckinDocuments(result, checkinDocuments); + } + } + + return result; + } catch (IOException e) { + LOG.error("Error occurred while retrieving files.", e); + throw new FileRetrievalException(e.getMessage()); + } + } + + @Override + public java.io.File downloadFiles(@NotNull String uploadDocId) { + MemberProfile currentUser = currentUserServices.getCurrentUser(); + boolean isAdmin = currentUserServices.isAdmin(); + + CheckinDocument cd = checkinDocumentServices.getFindByUploadDocId(uploadDocId); + validate(cd == null, String.format("Unable to find record with id %s", uploadDocId)); + + CheckIn associatedCheckin = checkInServices.read(cd.getCheckinsId()); + + if(!isAdmin) { + validate((!currentUser.getId().equals(associatedCheckin.getTeamMemberId()) && !currentUser.getId().equals(associatedCheckin.getPdlId())), NOT_AUTHORIZED_MSG); + } + try { + java.io.File file = java.io.File.createTempFile("tmp", ".txt"); + file.deleteOnExit(); + try( + FileOutputStream myWriter = new FileOutputStream(file) + ) { + downloadSingleFile(uploadDocId, myWriter); + return file; + } catch (IOException e) { + LOG.error("Error occurred while retrieving files.", e); + throw new FileRetrievalException(e.getMessage()); + } + } catch(IOException e) { + LOG.error("Error occurred while attempting to create a temporary file.", e); + throw new FileRetrievalException(e.getMessage()); + } + } + + @Override + public FileInfoDTO uploadFile(@NotNull UUID checkInID, @NotNull CompletedFileUpload file) { + MemberProfile currentUser = currentUserServices.getCurrentUser(); + boolean isAdmin = currentUserServices.isAdmin(); + validate((file.getFilename() == null || file.getFilename().equals("")), "Please select a valid file before uploading."); + + CheckIn checkIn = checkInServices.read(checkInID); + validate(checkIn == null, "Unable to find checkin record with id %s", checkInID); + if(!isAdmin) { + validate((!currentUser.getId().equals(checkIn.getTeamMemberId()) && !currentUser.getId().equals(checkIn.getPdlId())), "You are not authorized to perform this operation"); + validate(checkIn.isCompleted(), NOT_AUTHORIZED_MSG); + } + + // create folder for each team member + final String directoryName = MemberProfileUtils.getFullName(memberProfileServices.getById(checkIn.getTeamMemberId())); + + try { + return uploadSingleFile(file, directoryName, + (fileId) -> { + //create record in checkin-document service + CheckinDocument cd = new CheckinDocument(checkInID, fileId); + checkinDocumentServices.save(cd); + return cd; + }); + } catch (IOException e) { + LOG.error("Unexpected error processing file upload.", e); + throw new FileRetrievalException(e.getMessage()); + } + } + + @Override + public boolean deleteFile(@NotNull String uploadDocId) { + MemberProfile currentUser = currentUserServices.getCurrentUser(); + boolean isAdmin = currentUserServices.isAdmin(); + + CheckinDocument cd = checkinDocumentServices.getFindByUploadDocId(uploadDocId); + validate(cd == null, String.format("Unable to find record with id %s", uploadDocId)); + + CheckIn associatedCheckin = checkInServices.read(cd.getCheckinsId()); + if(!isAdmin) { + validate((!currentUser.getId().equals(associatedCheckin.getTeamMemberId()) && !currentUser.getId().equals(associatedCheckin.getPdlId())), NOT_AUTHORIZED_MSG); + } + + try { + deleteSingleFile(uploadDocId); + checkinDocumentServices.deleteByUploadDocId(uploadDocId); + return true; + } catch (IOException e) { + LOG.error("Error occurred while deleting files.", e); + throw new FileRetrievalException(e.getMessage()); + } + } + + protected void validate(boolean isError, String message, Object... args) { + if(isError) { + throw new FileRetrievalException(String.format(message, args)); + } + } +} diff --git a/server/src/main/java/com/objectcomputing/checkins/services/file/FileServicesImpl.java b/server/src/main/java/com/objectcomputing/checkins/services/file/FileServicesImpl.java index 505f68d56c..38654da92f 100644 --- a/server/src/main/java/com/objectcomputing/checkins/services/file/FileServicesImpl.java +++ b/server/src/main/java/com/objectcomputing/checkins/services/file/FileServicesImpl.java @@ -15,11 +15,10 @@ import com.objectcomputing.checkins.services.memberprofile.MemberProfileUtils; import com.objectcomputing.checkins.services.memberprofile.currentuser.CurrentUserServices; import com.objectcomputing.checkins.util.googleapiaccess.GoogleApiAccess; -import io.micronaut.core.annotation.Nullable; import io.micronaut.http.MediaType; import io.micronaut.http.multipart.CompletedFileUpload; + import jakarta.inject.Singleton; -import jakarta.validation.constraints.NotNull; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -29,22 +28,15 @@ import java.io.FileOutputStream; import java.io.IOException; import java.util.Collections; -import java.util.HashSet; import java.util.Set; -import java.util.UUID; - -import static com.objectcomputing.checkins.services.validate.PermissionsValidation.NOT_AUTHORIZED_MSG; +import java.util.function.Function; @Singleton -public class FileServicesImpl implements FileServices { +public class FileServicesImpl extends FileServicesBaseImpl { private static final Logger LOG = LoggerFactory.getLogger(FileServicesImpl.class); private final GoogleApiAccess googleApiAccess; - private final CheckInServices checkInServices; - private final CheckinDocumentServices checkinDocumentServices; - private final MemberProfileServices memberProfileServices; - private final CurrentUserServices currentUserServices; private final GoogleServiceConfiguration googleServiceConfiguration; public FileServicesImpl(GoogleApiAccess googleApiAccess, @@ -53,118 +45,60 @@ public FileServicesImpl(GoogleApiAccess googleApiAccess, MemberProfileServices memberProfileServices, CurrentUserServices currentUserServices, GoogleServiceConfiguration googleServiceConfiguration) { + super(checkInServices, checkinDocumentServices, memberProfileServices, + currentUserServices); this.googleApiAccess = googleApiAccess; - this.checkInServices = checkInServices; - this.checkinDocumentServices = checkinDocumentServices; - this.memberProfileServices = memberProfileServices; - this.currentUserServices = currentUserServices; this.googleServiceConfiguration = googleServiceConfiguration; } @Override - public Set findFiles(@Nullable UUID checkInID) { - - boolean isAdmin = currentUserServices.isAdmin(); - validate(checkInID == null && !isAdmin, NOT_AUTHORIZED_MSG); + protected void getCheckinDocuments( + Set result, + Set checkinDocuments) throws IOException { + Drive drive = googleApiAccess.getDrive(); + validate(drive == null, "Unable to access Google Drive"); - try { - Set result = new HashSet<>(); - Drive drive = googleApiAccess.getDrive(); - validate(drive == null, "Unable to access Google Drive"); + String rootDirId = googleServiceConfiguration.getDirectoryId(); + validate(rootDirId == null, "No destination folder has been configured. Contact your administrator for assistance."); - String rootDirId = googleServiceConfiguration.getDirectoryId(); - validate(rootDirId == null, "No destination folder has been configured. Contact your administrator for assistance."); - - if (checkInID == null && isAdmin) { - FileList driveIndex = getFoldersInRoot(drive, rootDirId); - driveIndex.getFiles().forEach(folder -> { - try { - //find all - FileList fileList = drive.files().list().setSupportsAllDrives(true) - .setIncludeItemsFromAllDrives(true) - .setQ(String.format("'%s' in parents and mimeType != 'application/vnd.google-apps.folder' and trashed != true", folder.getId())) - .setSpaces("drive") - .setFields("files(id, name, parents, size)") - .execute(); - fileList.getFiles() - .forEach(file -> result.add(setFileInfo(file, null))); - } catch (IOException ioe) { - LOG.error("Error occurred while retrieving files from Google Drive.", ioe); - throw new FileRetrievalException(ioe.getMessage()); - } - }); - } else if (checkInID != null) { - validate(!checkInServices.accessGranted(checkInID, currentUserServices.getCurrentUser().getId()), - "You are not authorized to perform this operation"); - - Set checkinDocuments = checkinDocumentServices.read(checkInID); - for (CheckinDocument cd : checkinDocuments) { - File file = drive.files().get(cd.getUploadDocId()).setSupportsAllDrives(true).execute(); - result.add(setFileInfo(file, cd)); + if (checkinDocuments.isEmpty()) { + FileList driveIndex = getFoldersInRoot(drive, rootDirId); + driveIndex.getFiles().forEach(folder -> { + try { + //find all + FileList fileList = drive.files().list().setSupportsAllDrives(true) + .setIncludeItemsFromAllDrives(true) + .setQ(String.format("'%s' in parents and mimeType != 'application/vnd.google-apps.folder' and trashed != true", folder.getId())) + .setSpaces("drive") + .setFields("files(id, name, parents, size)") + .execute(); + fileList.getFiles() + .forEach(file -> result.add(setFileInfo(file, null))); + } catch (IOException ioe) { + LOG.error("Error occurred while retrieving files from Google Drive.", ioe); + throw new FileRetrievalException(ioe.getMessage()); } + }); + } else { + for (CheckinDocument cd : checkinDocuments) { + File file = drive.files().get(cd.getUploadDocId()).setSupportsAllDrives(true).execute(); + result.add(setFileInfo(file, cd)); } - - return result; - } catch (IOException e) { - LOG.error("Error occurred while retrieving files from Google Drive.", e); - throw new FileRetrievalException(e.getMessage()); } } @Override - public java.io.File downloadFiles(@NotNull String uploadDocId) { - - MemberProfile currentUser = currentUserServices.getCurrentUser(); - boolean isAdmin = currentUserServices.isAdmin(); + protected void downloadSingleFile(String docId, FileOutputStream myWriter) throws IOException { + Drive drive = googleApiAccess.getDrive(); + validate(drive == null, "Unable to access Google Drive"); - CheckinDocument cd = checkinDocumentServices.getFindByUploadDocId(uploadDocId); - validate(cd == null, String.format("Unable to find record with id %s", uploadDocId)); - - CheckIn associatedCheckin = checkInServices.read(cd.getCheckinsId()); - - if(!isAdmin) { - validate((!currentUser.getId().equals(associatedCheckin.getTeamMemberId()) && !currentUser.getId().equals(associatedCheckin.getPdlId())), NOT_AUTHORIZED_MSG); - } - try { - java.io.File file = java.io.File.createTempFile("tmp", ".txt"); - file.deleteOnExit(); - try( - FileOutputStream myWriter = new FileOutputStream(file) - ) { - Drive drive = googleApiAccess.getDrive(); - validate(drive == null, "Unable to access Google Drive"); - - drive.files().get(uploadDocId).setSupportsAllDrives(true).executeMediaAndDownloadTo(myWriter); - myWriter.close(); - - return file; - } catch (IOException e) { - LOG.error("Error occurred while retrieving files from Google Drive.", e); - throw new FileRetrievalException(e.getMessage()); - } - } catch(IOException e) { - LOG.error("Error occurred while attempting to create a temporary file.", e); - throw new FileRetrievalException(e.getMessage()); - } + drive.files().get(docId) + .setSupportsAllDrives(true).executeMediaAndDownloadTo(myWriter); + myWriter.close(); } @Override - public FileInfoDTO uploadFile(@NotNull UUID checkInID, @NotNull CompletedFileUpload file) { - - MemberProfile currentUser = currentUserServices.getCurrentUser(); - boolean isAdmin = currentUserServices.isAdmin(); - validate((file.getFilename() == null || file.getFilename().equals("")), "Please select a valid file before uploading."); - - CheckIn checkIn = checkInServices.read(checkInID); - validate(checkIn == null, "Unable to find checkin record with id %s", checkInID); - if(!isAdmin) { - validate((!currentUser.getId().equals(checkIn.getTeamMemberId()) && !currentUser.getId().equals(checkIn.getPdlId())), "You are not authorized to perform this operation"); - validate(checkIn.isCompleted(), NOT_AUTHORIZED_MSG); - } - - // create folder for each team member - final String directoryName = MemberProfileUtils.getFullName(memberProfileServices.getById(checkIn.getTeamMemberId())); - + protected FileInfoDTO uploadSingleFile(CompletedFileUpload file, String directoryName, Function consumer) throws IOException { try { Drive drive = googleApiAccess.getDrive(); validate(drive == null, "Unable to access Google Drive"); @@ -198,17 +132,12 @@ public FileInfoDTO uploadFile(@NotNull UUID checkInID, @NotNull CompletedFileUpl .setFields("id, size, name") .execute(); - //create record in checkin-document service - CheckinDocument cd = new CheckinDocument(checkInID, uploadedFile.getId()); - checkinDocumentServices.save(cd); - + CheckinDocument cd = + consumer.apply(uploadedFile.getId()); return setFileInfo(uploadedFile, cd); } catch (GoogleJsonResponseException e) { LOG.error("Error occurred while accessing Google Drive.", e); - throw new FileRetrievalException(e.getMessage()); - } catch (IOException e) { - LOG.error("Unexpected error processing file upload.", e); - throw new FileRetrievalException(e.getMessage()); + throw new IOException(e.getMessage()); } } @@ -317,36 +246,11 @@ private FileList getFoldersInRoot(Drive drive, String rootDirId) throws IOExcept } @Override - public boolean deleteFile(@NotNull String uploadDocId) { - - MemberProfile currentUser = currentUserServices.getCurrentUser(); - boolean isAdmin = currentUserServices.isAdmin(); - - CheckinDocument cd = checkinDocumentServices.getFindByUploadDocId(uploadDocId); - validate(cd == null, String.format("Unable to find record with id %s", uploadDocId)); - - CheckIn associatedCheckin = checkInServices.read(cd.getCheckinsId()); - if(!isAdmin) { - validate((!currentUser.getId().equals(associatedCheckin.getTeamMemberId()) && !currentUser.getId().equals(associatedCheckin.getPdlId())), NOT_AUTHORIZED_MSG); - } - - try { - Drive drive = googleApiAccess.getDrive(); - validate(drive == null, "Unable to access Google Drive"); - File file = new File().setTrashed(true); - drive.files().update(uploadDocId, file).setSupportsAllDrives(true).execute(); - checkinDocumentServices.deleteByUploadDocId(uploadDocId); - return true; - } catch (IOException e) { - LOG.error("Error occurred while retrieving files from Google Drive.", e); - throw new FileRetrievalException(e.getMessage()); - } - } - - private void validate(boolean isError, String message, Object... args) { - if(isError) { - throw new FileRetrievalException(String.format(message, args)); - } + protected void deleteSingleFile(String docId) throws IOException { + Drive drive = googleApiAccess.getDrive(); + validate(drive == null, "Unable to access Google Drive"); + File file = new File().setTrashed(true); + drive.files().update(docId, file).setSupportsAllDrives(true).execute(); } private File createNewDirectoryOnDrive(Drive drive, String directoryName, String parentId) throws IOException { diff --git a/server/src/main/java/com/objectcomputing/checkins/services/memberprofile/memberphoto/GooglePhotoAccessorImpl.java b/server/src/main/java/com/objectcomputing/checkins/services/memberprofile/memberphoto/GooglePhotoAccessorImpl.java index d9f06b5c40..964b6b8fcb 100644 --- a/server/src/main/java/com/objectcomputing/checkins/services/memberprofile/memberphoto/GooglePhotoAccessorImpl.java +++ b/server/src/main/java/com/objectcomputing/checkins/services/memberprofile/memberphoto/GooglePhotoAccessorImpl.java @@ -18,7 +18,8 @@ import java.util.Optional; @Singleton -class GooglePhotoAccessorImpl implements GooglePhotoAccessor { +// Public so that this class can be replaced during testing. +public class GooglePhotoAccessorImpl implements GooglePhotoAccessor { private static final Logger LOG = LoggerFactory.getLogger(GooglePhotoAccessorImpl.class); diff --git a/server/src/main/java/com/objectcomputing/checkins/services/reports/MarkdownGeneration.java b/server/src/main/java/com/objectcomputing/checkins/services/reports/MarkdownGeneration.java index a1eea89b78..76315cc82c 100644 --- a/server/src/main/java/com/objectcomputing/checkins/services/reports/MarkdownGeneration.java +++ b/server/src/main/java/com/objectcomputing/checkins/services/reports/MarkdownGeneration.java @@ -82,7 +82,7 @@ public int compare(CompensationHistory.Compensation a, private static final Logger LOG = LoggerFactory.getLogger(MarkdownGeneration.class); private static final String noneAvailable = "None available during the period covered by this review."; - private static final String directory = "merit-reports"; + public static final String directory = "merit-reports"; private final ReportDataServices reportDataServices; private final KudosRepository kudosRepository; diff --git a/server/src/test/java/com/objectcomputing/checkins/services/CurrentUserServicesReplacement.java b/server/src/test/java/com/objectcomputing/checkins/services/CurrentUserServicesReplacement.java new file mode 100644 index 0000000000..60723b8d5e --- /dev/null +++ b/server/src/test/java/com/objectcomputing/checkins/services/CurrentUserServicesReplacement.java @@ -0,0 +1,43 @@ +package com.objectcomputing.checkins.services; + +import com.objectcomputing.checkins.services.memberprofile.MemberProfile; +import com.objectcomputing.checkins.services.memberprofile.currentuser.CurrentUserServices; +import com.objectcomputing.checkins.services.role.RoleType; + +import java.util.List; + +import jakarta.inject.Singleton; +import io.micronaut.core.util.StringUtils; +import io.micronaut.context.env.Environment; +import io.micronaut.context.annotation.Replaces; +import io.micronaut.context.annotation.Requires; + +@Singleton +@Replaces(CurrentUserServices.class) +@Requires(property = "replace.currentuserservices", value = StringUtils.TRUE) +public class CurrentUserServicesReplacement implements CurrentUserServices { + public MemberProfile currentUser; + public List roles; + + @Override + public MemberProfile findOrSaveUser(String firstName, + String lastName, + String workEmail) { + return null; + } + + @Override + public boolean hasRole(RoleType role) { + return roles == null ? false : roles.contains(role); + } + + @Override + public boolean isAdmin() { + return hasRole(RoleType.ADMIN); + } + + @Override + public MemberProfile getCurrentUser() { + return currentUser; + } +} diff --git a/server/src/test/java/com/objectcomputing/checkins/services/FileServicesImplReplacement.java b/server/src/test/java/com/objectcomputing/checkins/services/FileServicesImplReplacement.java new file mode 100644 index 0000000000..c51d6036d3 --- /dev/null +++ b/server/src/test/java/com/objectcomputing/checkins/services/FileServicesImplReplacement.java @@ -0,0 +1,142 @@ +package com.objectcomputing.checkins.services; + +import com.objectcomputing.checkins.services.file.FileInfoDTO; +import com.objectcomputing.checkins.services.file.FileServicesImpl; +import com.objectcomputing.checkins.services.file.FileServicesBaseImpl; +import com.objectcomputing.checkins.services.checkindocument.CheckinDocument; +import com.objectcomputing.checkins.services.checkindocument.CheckinDocumentServices; +import com.objectcomputing.checkins.services.checkins.CheckInServices; +import com.objectcomputing.checkins.services.memberprofile.MemberProfileServices; +import com.objectcomputing.checkins.services.memberprofile.currentuser.CurrentUserServices; + +import io.micronaut.http.multipart.CompletedFileUpload; + +import java.io.IOException; +import java.io.FileOutputStream; +import java.nio.file.Paths; +import java.util.Set; +import java.util.HashMap; +import java.util.function.Function; + +import jakarta.inject.Singleton; +import io.micronaut.core.util.StringUtils; +import io.micronaut.context.annotation.Replaces; +import io.micronaut.context.annotation.Requires; + +@Singleton +@Replaces(FileServicesImpl.class) +@Requires(property = "replace.fileservicesimpl", value = StringUtils.TRUE) +public class FileServicesImplReplacement extends FileServicesBaseImpl { + private HashMap map = new HashMap<>(); + + public FileServicesImplReplacement( + CheckInServices checkInServices, + CheckinDocumentServices checkinDocumentServices, + MemberProfileServices memberProfileServices, + CurrentUserServices currentUserServices) { + super(checkInServices, checkinDocumentServices, memberProfileServices, + currentUserServices); + } + + // ******************************************************************* + // Test Interface + // ******************************************************************* + + public boolean shouldThrow = false; + + public void reset() { + shouldThrow = false; + map.clear(); + } + + public FileInfoDTO addFile(String key, byte[] content) { + map.put(key, content); + return setFileInfo(key, null); + } + + public FileInfoDTO addFile(String key, byte[] content, + Function consumer) { + map.put(key, content); + return setFileInfo(key, consumer.apply(key)); + } + + public String getFile(String key) { + return new String(map.get(key)); + } + + // ******************************************************************* + // Overrides for FileServicesBaseImpl + // ******************************************************************* + + @Override + public FileInfoDTO uploadDocument(String directory, + String name, String text) { + return addFile(directory + "/" + name, text.getBytes()); + } + + @Override + protected void getCheckinDocuments( + Set result, Set checkinDocuments) throws IOException { + checkThrow(); + if (checkinDocuments.isEmpty()) { + for(String key : map.keySet()) { + result.add(setFileInfo(key, null)); + } + } else { + for (CheckinDocument cd : checkinDocuments) { + result.add(setFileInfo(cd.getUploadDocId(), cd)); + } + } + } + + @Override + protected void downloadSingleFile( + String docId, FileOutputStream myWriter) throws IOException { + checkThrow(); + if (map.containsKey(docId)) { + myWriter.write(map.get(docId)); + } else { + throw new IOException("File does not exist."); + } + } + + @Override + protected FileInfoDTO uploadSingleFile( + CompletedFileUpload file, String directoryName, + Function consumer) throws IOException { + checkThrow(); + String key = directoryName + "/" + file.getFilename(); + map.put(key, file.getInputStream().readAllBytes()); + return setFileInfo(key, consumer.apply(key)); + } + + @Override + protected void deleteSingleFile(String docId) throws IOException { + checkThrow(); + if (map.containsKey(docId)) { + map.remove(docId); + } else { + throw new IOException("File does not exist."); + } + } + + private FileInfoDTO setFileInfo(String key, CheckinDocument cd) { + FileInfoDTO dto = new FileInfoDTO(); + dto.setFileId(key); + dto.setName(Paths.get(key).getFileName().toString()); + dto.setSize((long)(map.get(key).length)); + if (cd != null) { + dto.setCheckInId(cd.getCheckinsId()); + } + return dto; + } + + private void checkThrow() throws IOException { + if (shouldThrow) { + // This ensures that IOExceptions thrown from these overridden + // methods are caught in FileServicesBaseImpl and converted to + // FileRetrievalException objects. + throw new IOException("Unable to access Google Drive"); + } + } +} diff --git a/server/src/test/java/com/objectcomputing/checkins/services/GooglePhotoAccessorImplReplacement.java b/server/src/test/java/com/objectcomputing/checkins/services/GooglePhotoAccessorImplReplacement.java new file mode 100644 index 0000000000..122bd9d202 --- /dev/null +++ b/server/src/test/java/com/objectcomputing/checkins/services/GooglePhotoAccessorImplReplacement.java @@ -0,0 +1,44 @@ +package com.objectcomputing.checkins.services; + +import com.google.api.services.directory.model.UserPhoto; +import com.objectcomputing.checkins.services.memberprofile.memberphoto.GooglePhotoAccessorImpl; +import com.objectcomputing.checkins.services.memberprofile.memberphoto.GooglePhotoAccessor; + +import java.util.List; +import java.util.Map; +import java.util.HashMap; +import java.util.Base64; + +import jakarta.inject.Singleton; +import io.micronaut.core.util.StringUtils; +import io.micronaut.context.annotation.Replaces; +import io.micronaut.context.annotation.Requires; + +@Singleton +@Replaces(GooglePhotoAccessorImpl.class) +@Requires(property = "replace.googlephotoaccessorimpl", value = StringUtils.TRUE) +public class GooglePhotoAccessorImplReplacement implements GooglePhotoAccessor { + Map photos = new HashMap<>(); + + public void reset() { + photos.clear(); + } + + public void setUserPhoto(String email, UserPhoto photo) { + photos.put(email, photo); + } + + public void setPhotoData(String email, byte[] photoData) { + UserPhoto photo = new UserPhoto(); + photo.setPhotoData(new String(photoData)); + photos.put(email, photo); + } + + @Override + public byte[] getPhotoData(String workEmail) { + UserPhoto photo = photos.get(workEmail); + return photo == null + ? new byte[0] + : Base64.getUrlDecoder().decode(photo.getPhotoData().getBytes()); + } +} diff --git a/server/src/test/java/com/objectcomputing/checkins/services/checkindocument/CheckinDocumentServiceImplTest.java b/server/src/test/java/com/objectcomputing/checkins/services/checkindocument/CheckinDocumentServiceImplTest.java index 595aaadc9d..1068d52b52 100644 --- a/server/src/test/java/com/objectcomputing/checkins/services/checkindocument/CheckinDocumentServiceImplTest.java +++ b/server/src/test/java/com/objectcomputing/checkins/services/checkindocument/CheckinDocumentServiceImplTest.java @@ -2,135 +2,86 @@ import com.objectcomputing.checkins.exceptions.BadArgException; import com.objectcomputing.checkins.services.TestContainersSuite; +import com.objectcomputing.checkins.services.fixture.MemberProfileFixture; +import com.objectcomputing.checkins.services.fixture.CheckInDocumentFixture; +import com.objectcomputing.checkins.services.fixture.CheckInFixture; import com.objectcomputing.checkins.services.checkins.CheckIn; -import com.objectcomputing.checkins.services.checkins.CheckInRepository; -import com.objectcomputing.checkins.services.memberprofile.currentuser.CurrentUserServices; -import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.BeforeAll; +import com.objectcomputing.checkins.services.memberprofile.MemberProfile; +import com.objectcomputing.checkins.services.role.RoleType; + import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.condition.DisabledInNativeImage; -import org.mockito.InjectMocks; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; -import java.util.Optional; +import jakarta.inject.Inject; +import io.micronaut.context.annotation.Property; +import io.micronaut.core.util.StringUtils; + import java.util.Set; import java.util.UUID; +import java.util.ArrayList; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.mockito.Mockito.any; -import static org.mockito.Mockito.eq; -import static org.mockito.Mockito.never; -import static org.mockito.Mockito.reset; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -// Disabled in nativeTest, as we get an exception from Mockito -// => org.graalvm.nativeimage.MissingReflectionRegistrationError: The program tried to reflectively access the proxy class -// inheriting [org.mockito.plugins.MockMaker] without it being registered for runtime reflection -@DisabledInNativeImage -class CheckinDocumentServiceImplTest extends TestContainersSuite { - - @Mock - private CheckInRepository checkinRepository; - - @Mock - private CheckinDocumentRepository checkinDocumentRepository; - - @Mock - private CurrentUserServices currentUserServices; - - @InjectMocks - private CheckinDocumentServicesImpl services; - private AutoCloseable mockFinalizer; +class CheckinDocumentServiceImplTest extends TestContainersSuite + implements MemberProfileFixture, CheckInFixture, CheckInDocumentFixture { + @Inject + private CheckinDocumentServicesImpl services; - @BeforeAll - void initMocks() { - mockFinalizer = MockitoAnnotations.openMocks(this); - } + private MemberProfile pdl; + private MemberProfile member; + private CheckIn checkIn; @BeforeEach - void resetMocks() { - reset(checkinRepository, checkinDocumentRepository, currentUserServices); - } - - @AfterAll - void close() throws Exception { - mockFinalizer.close(); + void reset() { + pdl = createADefaultMemberProfile(); + member = createADefaultMemberProfileForPdl(pdl); + checkIn = createADefaultCheckIn(member, pdl); } @Test void testRead() { - CheckinDocument cd = new CheckinDocument(UUID.randomUUID(), UUID.randomUUID(), "exampleDocId"); - Set checkinDocumentSet = Set.of( - new CheckinDocument(UUID.randomUUID(), UUID.randomUUID(), "doc1"), - new CheckinDocument(UUID.randomUUID(), UUID.randomUUID(), "doc2"), - new CheckinDocument(UUID.randomUUID(), UUID.randomUUID(), "doc3") + createACustomCheckInDocument(checkIn, "doc1"), + createACustomCheckInDocument(checkIn, "doc2"), + createACustomCheckInDocument(checkIn, "doc3") ); - when(checkinDocumentRepository.findByCheckinsId(cd.getCheckinsId())).thenReturn(checkinDocumentSet); - - assertEquals(checkinDocumentSet, services.read(cd.getCheckinsId())); - - verify(checkinDocumentRepository, times(1)).findByCheckinsId(any(UUID.class)); + assertEquals(checkinDocumentSet, services.read(checkIn.getId())); } @Test void testReadNullId() { assertTrue(services.read(null).isEmpty()); - - verify(checkinDocumentRepository, never()).findByCheckinsId(any(UUID.class)); } @Test void testFindByUploadDocId() { - - CheckinDocument cd = new CheckinDocument(UUID.randomUUID(), UUID.randomUUID(), "exampleDocId"); - when(checkinDocumentRepository.findByUploadDocId(any(String.class))).thenReturn(Optional.of(cd)); + CheckinDocument cd = createADefaultCheckInDocument(checkIn); assertEquals(cd, services.getFindByUploadDocId(cd.getUploadDocId())); - verify(checkinDocumentRepository, times(1)).findByUploadDocId(any(String.class)); } @Test void testFindByUploadDocIdWhenRecordDoesNotExist() { - String id = "some.id"; - when(checkinDocumentRepository.findByUploadDocId(any(String.class))).thenReturn(Optional.empty()); BadArgException exception = assertThrows(BadArgException.class, () -> services.getFindByUploadDocId(id)); assertEquals(String.format("CheckinDocument with document id %s does not exist", id), exception.getMessage()); - verify(checkinDocumentRepository, times(1)).findByUploadDocId(any(String.class)); } @Test void testSave() { - CheckinDocument cd = new CheckinDocument(UUID.randomUUID(), "docId"); - CheckIn checkin = new CheckIn(); - - when(checkinRepository.findById(cd.getCheckinsId())).thenReturn(Optional.of(checkin)); - when(checkinDocumentRepository.save(cd)).thenReturn(cd); - + CheckinDocument cd = new CheckinDocument(checkIn.getId(), "doc1"); assertEquals(cd, services.save(cd)); - - verify(checkinRepository, times(1)).findById(any(UUID.class)); - verify(checkinDocumentRepository, times(1)).save(any(CheckinDocument.class)); } @Test void testSaveWithId() { - CheckinDocument cd = new CheckinDocument(UUID.randomUUID(), UUID.randomUUID(), "docId"); + CheckinDocument cd = createADefaultCheckInDocument(checkIn); BadArgException exception = assertThrows(BadArgException.class, () -> services.save(cd)); assertEquals(String.format("Found unexpected CheckinDocument id %s, please try updating instead", cd.getId()), exception.getMessage()); - - verify(checkinDocumentRepository, never()).save(any(CheckinDocument.class)); - verify(checkinRepository, never()).findById(any(UUID.class)); } @Test @@ -139,85 +90,51 @@ void testSaveCheckinDocumentNullCheckinsId() { BadArgException exception = assertThrows(BadArgException.class, () -> services.save(cd)); assertEquals(String.format("Invalid CheckinDocument %s", cd), exception.getMessage()); - - verify(checkinDocumentRepository, never()).save(any(CheckinDocument.class)); - verify(checkinRepository, never()).findById(any(UUID.class)); } @Test void testSaveCheckinDocumentNullUploadDocId() { - CheckinDocument cd = new CheckinDocument(UUID.randomUUID(), null); + CheckinDocument cd = new CheckinDocument(checkIn.getId(), null); BadArgException exception = assertThrows(BadArgException.class, () -> services.save(cd)); assertEquals(String.format("Invalid CheckinDocument %s", cd), exception.getMessage()); - - verify(checkinDocumentRepository, never()).save(any(CheckinDocument.class)); - verify(checkinRepository, never()).findById(any(UUID.class)); } @Test void testSaveNullCheckinDocument() { assertNull(services.save(null)); - - verify(checkinDocumentRepository, never()).save(any(CheckinDocument.class)); - verify(checkinRepository, never()).findById(any(UUID.class)); } @Test void testSaveCheckinDocumentNonExistingCheckIn() { CheckinDocument cd = new CheckinDocument(UUID.randomUUID(), "docId"); - when(checkinRepository.findById(cd.getCheckinsId())).thenReturn(Optional.empty()); - BadArgException exception = assertThrows(BadArgException.class, () -> services.save(cd)); assertEquals(String.format("CheckIn %s doesn't exist", cd.getCheckinsId()), exception.getMessage()); - - verify(checkinDocumentRepository, never()).save(any(CheckinDocument.class)); - verify(checkinRepository, times(1)).findById(any(UUID.class)); } @Test void testSaveCheckinDocumentExistingUploadDocId() { - CheckinDocument cd = new CheckinDocument(UUID.randomUUID(), "docId"); - - CheckIn checkin = new CheckIn(); - - when(checkinRepository.findById(cd.getCheckinsId())).thenReturn(Optional.of(checkin)); - when(checkinDocumentRepository.findByUploadDocId(cd.getUploadDocId())).thenReturn(Optional.of(cd)); + String docId = "doc1"; + CheckinDocument existing = createACustomCheckInDocument(checkIn, docId); + CheckinDocument cd = new CheckinDocument(checkIn.getId(), docId); BadArgException exception = assertThrows(BadArgException.class, () -> services.save(cd)); assertEquals(String.format("CheckinDocument with document ID %s already exists", cd.getUploadDocId()), exception.getMessage()); - - verify(checkinDocumentRepository, never()).save(any(CheckinDocument.class)); - verify(checkinRepository, times(1)).findById(any(UUID.class)); } @Test void testUpdate() { - CheckinDocument cd = new CheckinDocument(UUID.randomUUID(), UUID.randomUUID(), "docId"); - CheckIn checkin = new CheckIn(); - - when(checkinRepository.findById(eq(cd.getCheckinsId()))).thenReturn(Optional.of(checkin)); - when(checkinDocumentRepository.findById(cd.getId())).thenReturn(Optional.of(cd)); - when(checkinDocumentRepository.update(eq(cd))).thenReturn(cd); - + CheckinDocument cd = createADefaultCheckInDocument(checkIn); assertEquals(cd, services.update(cd)); - - verify(checkinRepository, times(1)).findById(any(UUID.class)); - verify(checkinDocumentRepository, times(1)).findById(cd.getId()); - verify(checkinDocumentRepository, times(1)).update(any(CheckinDocument.class)); } @Test void testUpdateWithoutId() { - CheckinDocument cd = new CheckinDocument(UUID.randomUUID(), "docId"); + CheckinDocument cd = new CheckinDocument(checkIn.getId(), "docId"); BadArgException exception = assertThrows(BadArgException.class, () -> services.update(cd)); assertEquals(String.format("CheckinDocument id %s not found, please try inserting instead", cd.getId()), exception.getMessage()); - - verify(checkinRepository, never()).findById(any(UUID.class)); - verify(checkinDocumentRepository, never()).update(any(CheckinDocument.class)); - verify(checkinDocumentRepository, never()).findById(any(UUID.class)); } @Test @@ -226,106 +143,65 @@ void testUpdateCheckinDocumentNullCheckinsId() { BadArgException exception = assertThrows(BadArgException.class, () -> services.update(cd)); assertEquals(String.format("Invalid CheckinDocument %s", cd), exception.getMessage()); - - verify(checkinRepository, never()).findById(any(UUID.class)); - verify(checkinDocumentRepository, never()).update(any(CheckinDocument.class)); - verify(checkinDocumentRepository, never()).findById(any(UUID.class)); } @Test void testUpdateCheckinDocumentNullUploadDocId() { - CheckinDocument cd = new CheckinDocument(UUID.randomUUID(), null); + CheckinDocument cd = new CheckinDocument(checkIn.getId(), null); BadArgException exception = assertThrows(BadArgException.class, () -> services.update(cd)); assertEquals(String.format("Invalid CheckinDocument %s", cd), exception.getMessage()); - - verify(checkinRepository, never()).findById(any(UUID.class)); - verify(checkinDocumentRepository, never()).update(any(CheckinDocument.class)); - verify(checkinDocumentRepository, never()).findById(any(UUID.class)); } @Test void testUpdateCheckinDocumentDoesNotExist() { CheckinDocument cd = new CheckinDocument(UUID.randomUUID(), UUID.randomUUID(), "docId"); - when(checkinDocumentRepository.findById(cd.getCheckinsId())).thenReturn(Optional.empty()); BadArgException exception = assertThrows(BadArgException.class, () -> services.update(cd)); assertEquals(String.format("CheckinDocument id %s not found, please try inserting instead", cd.getId()), exception.getMessage()); - - verify(checkinRepository, never()).findById(any(UUID.class)); - verify(checkinDocumentRepository, never()).update(any(CheckinDocument.class)); - verify(checkinDocumentRepository, times(1)).findById(any(UUID.class)); } @Test void testUpdateCheckInDoesNotExist() { - CheckinDocument cd = new CheckinDocument(UUID.randomUUID(), UUID.randomUUID(), "docId"); - when(checkinDocumentRepository.findById(cd.getId())).thenReturn(Optional.of(cd)); - when(checkinRepository.findById(cd.getCheckinsId())).thenReturn(Optional.empty()); + CheckinDocument existing = createADefaultCheckInDocument(checkIn); + CheckinDocument cd = new CheckinDocument(existing.getId(), + UUID.randomUUID(), "docId"); BadArgException exception = assertThrows(BadArgException.class, () -> services.update(cd)); assertEquals(String.format("CheckIn %s doesn't exist", cd.getCheckinsId()), exception.getMessage()); - - verify(checkinRepository, times(1)).findById(any(UUID.class)); - verify(checkinDocumentRepository, never()).update(any(CheckinDocument.class)); - verify(checkinDocumentRepository, times(1)).findById(any(UUID.class)); } @Test void testUpdateNullCheckinDocument() { assertNull(services.update(null)); - - verify(checkinRepository, never()).findById(any(UUID.class)); - verify(checkinDocumentRepository, never()).update(any(CheckinDocument.class)); - verify(checkinDocumentRepository, never()).findById(any(UUID.class)); } @Test void testDeleteByCheckinId() { - when(checkinDocumentRepository.existsByCheckinsId(any(UUID.class))).thenReturn(true); - when(currentUserServices.isAdmin()).thenReturn(true); - - services.deleteByCheckinId(UUID.randomUUID()); - - verify(checkinDocumentRepository, times(1)).deleteByCheckinsId(any(UUID.class)); + CheckinDocument toBeDeleted = createADefaultCheckInDocument(checkIn); + services.deleteByCheckinId(checkIn.getId()); } @Test void testDeleteNonExistingCheckinsId() { UUID uuid = UUID.randomUUID(); - when(checkinDocumentRepository.existsByCheckinsId(any(UUID.class))).thenReturn(false); - when(currentUserServices.isAdmin()).thenReturn(true); - BadArgException exception = assertThrows(BadArgException.class, () -> services.deleteByCheckinId(uuid)); assertEquals(String.format("CheckinDocument with CheckinsId %s does not exist", uuid), exception.getMessage()); - - verify(checkinDocumentRepository, times(0)).deleteByCheckinsId(any(UUID.class)); } @Test void testDeleteByUploadDocId() { - - when(checkinDocumentRepository.existsByUploadDocId(any(String.class))).thenReturn(true); - when(currentUserServices.isAdmin()).thenReturn(true); - - services.deleteByUploadDocId("Test.Upload.Doc.Id"); - - verify(checkinDocumentRepository, times(1)).deleteByUploadDocId(any(String.class)); - verify(checkinDocumentRepository, times(1)).existsByUploadDocId(any(String.class)); + CheckinDocument toBeDeleted = createADefaultCheckInDocument(checkIn); + services.deleteByUploadDocId(toBeDeleted.getUploadDocId()); } @Test void testDeleteNonExistingUploadDocId() { String id = "Test.Id"; - when(checkinDocumentRepository.existsByUploadDocId(any(String.class))).thenReturn(false); - when(currentUserServices.isAdmin()).thenReturn(true); - BadArgException exception = assertThrows(BadArgException.class, () -> services.deleteByUploadDocId(id)); assertEquals(String.format("CheckinDocument with uploadDocId %s does not exist", id), exception.getMessage()); - verify(checkinDocumentRepository, times(0)).deleteByUploadDocId(any(String.class)); - verify(checkinDocumentRepository, times(1)).existsByUploadDocId(any(String.class)); } } diff --git a/server/src/test/java/com/objectcomputing/checkins/services/feedback_request/FeedbackRequestTest.java b/server/src/test/java/com/objectcomputing/checkins/services/feedback_request/FeedbackRequestTest.java index aa4420298f..0f83752c7c 100644 --- a/server/src/test/java/com/objectcomputing/checkins/services/feedback_request/FeedbackRequestTest.java +++ b/server/src/test/java/com/objectcomputing/checkins/services/feedback_request/FeedbackRequestTest.java @@ -6,6 +6,12 @@ import com.objectcomputing.checkins.notifications.email.MailJetFactory; import com.objectcomputing.checkins.services.MailJetFactoryReplacement; import com.objectcomputing.checkins.services.TestContainersSuite; +import com.objectcomputing.checkins.services.CurrentUserServicesReplacement; +import com.objectcomputing.checkins.services.role.RoleType; +import com.objectcomputing.checkins.services.fixture.FeedbackRequestFixture; +import com.objectcomputing.checkins.services.fixture.MemberProfileFixture; +import com.objectcomputing.checkins.services.fixture.ReviewPeriodFixture; +import com.objectcomputing.checkins.services.fixture.ReviewAssignmentFixture; import com.objectcomputing.checkins.services.memberprofile.MemberProfile; import com.objectcomputing.checkins.services.memberprofile.MemberProfileServices; import com.objectcomputing.checkins.services.memberprofile.currentuser.CurrentUserServices; @@ -19,14 +25,8 @@ import io.micronaut.runtime.server.EmbeddedServer; import jakarta.inject.Inject; import jakarta.inject.Named; -import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.condition.DisabledInNativeImage; -import org.mockito.Mock; -import org.mockito.Mockito; import java.time.LocalDate; import java.util.*; @@ -36,111 +36,61 @@ import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.never; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; -import static org.mockito.MockitoAnnotations.openMocks; @Property(name = "replace.mailjet.factory", value = StringUtils.TRUE) -// Disabled in nativeTest, as we get an exception from Mockito -// => java.lang.NoClassDefFoundError: Could not initialize class org.mockito.Mockito -@DisabledInNativeImage -class FeedbackRequestTest extends TestContainersSuite { - - @Mock - private FeedbackRequestRepository feedbackReqRepository; - - @Mock - private CurrentUserServices currentUserServices; - - @Mock - private MemberProfileServices memberProfileServices; - - @Mock - private ReviewPeriodRepository reviewPeriodRepository; - - @Mock - private ReviewAssignmentRepository reviewAssignmentRepository; +@Property(name = "replace.currentuserservices", value = StringUtils.TRUE) +class FeedbackRequestTest extends TestContainersSuite + implements FeedbackRequestFixture, MemberProfileFixture, ReviewPeriodFixture, ReviewAssignmentFixture { + @Inject + private CurrentUserServicesReplacement currentUserServices; + @Inject private FeedbackRequestServicesImpl feedbackRequestServices; @Inject @Named(MailJetFactory.MJML_FORMAT) private MailJetFactoryReplacement.MockEmailSender emailSender; - @Inject - private EmbeddedServer server; - - private AutoCloseable mockFinalizer; - @Inject CheckInsConfiguration checkInsConfiguration; - @BeforeAll - void initMocks() { - mockFinalizer = openMocks(this); - feedbackRequestServices = new FeedbackRequestServicesImpl(feedbackReqRepository, currentUserServices, memberProfileServices, reviewPeriodRepository, reviewAssignmentRepository, emailSender, checkInsConfiguration); - server.getApplicationContext().inject(feedbackRequestServices); - } - @BeforeEach - @Tag("mocked") void setUp() { - Mockito.reset(feedbackReqRepository); - Mockito.reset(currentUserServices); - Mockito.reset(memberProfileServices); - Mockito.reset(reviewPeriodRepository); - Mockito.reset(reviewAssignmentRepository); emailSender.reset(); } - @AfterAll - void cleanupMocks() throws Exception { - mockFinalizer.close(); - } - @Test - @Tag("mocked") void testUpdateFeedbackRequest() { + MemberProfile creator = createADefaultMemberProfile(); + MemberProfile recipient = createASecondDefaultMemberProfile(); + MemberProfile requestee = createAThirdDefaultMemberProfile(); + FeedbackRequest feedbackRequest = + saveFeedbackRequest(creator, requestee, recipient); + UUID feedbackRequestId = UUID.randomUUID(); UUID creatorId = UUID.randomUUID(); UUID recipientId = UUID.randomUUID(); UUID requesteeId = UUID.randomUUID(); - FeedbackRequest feedbackRequest = new FeedbackRequest(); - feedbackRequest.setId(feedbackRequestId); - feedbackRequest.setCreatorId(creatorId); - feedbackRequest.setRecipientId(recipientId); - feedbackRequest.setRequesteeId(requesteeId); - feedbackRequest.setSendDate(LocalDate.now()); - feedbackRequest.setStatus("sent"); + // We need the current user to be logged in and admin. + currentUserServices.currentUser = creator; + currentUserServices.roles = new ArrayList(); + currentUserServices.roles.add(RoleType.ADMIN); - MemberProfile currentUser = new MemberProfile(); - currentUser.setId(creatorId); FeedbackRequestUpdateDTO updateDTO = new FeedbackRequestUpdateDTO(); updateDTO.setId(feedbackRequest.getId()); updateDTO.setDueDate(LocalDate.now().plusDays(7)); updateDTO.setStatus("submitted"); updateDTO.setRecipientId(feedbackRequest.getRecipientId()); - when(feedbackReqRepository.findById(feedbackRequest.getId())).thenReturn(Optional.of(feedbackRequest)); - when(currentUserServices.getCurrentUser()).thenReturn(currentUser); - when(currentUserServices.isAdmin()).thenReturn(true); - when(feedbackReqRepository.update(any(FeedbackRequest.class))).thenReturn(feedbackRequest); - when(memberProfileServices.getById(any(UUID.class))).thenReturn(new MemberProfile()); - FeedbackRequest updatedFeedbackRequest = feedbackRequestServices.update(updateDTO); assertNotNull(updatedFeedbackRequest); assertEquals("submitted", updatedFeedbackRequest.getStatus()); - verify(feedbackReqRepository, times(1)).update(any(FeedbackRequest.class)); assertEquals(0, emailSender.events.size()); } @Test - @Tag("mocked") void testUpdateFeedbackRequest_NotFound() { UUID feedbackRequestId = UUID.randomUUID(); UUID creatorId = UUID.randomUUID(); @@ -158,28 +108,25 @@ void testUpdateFeedbackRequest_NotFound() { FeedbackRequestUpdateDTO updateDTO = new FeedbackRequestUpdateDTO(); updateDTO.setId(feedbackRequest.getId()); - when(feedbackReqRepository.findById(feedbackRequest.getId())).thenReturn(Optional.empty()); - assertThrows(NotFoundException.class, () -> feedbackRequestServices.update(updateDTO)); - verify(feedbackReqRepository, never()).update(any(FeedbackRequest.class)); assertEquals(0, emailSender.events.size()); } @Test - @Tag("mocked") void testUpdateFeedbackRequest_Unauthorized() { + MemberProfile creator = createADefaultMemberProfile(); + MemberProfile recipient = createASecondDefaultMemberProfile(); + MemberProfile requestee = createAThirdDefaultMemberProfile(); + FeedbackRequest feedbackRequest = + saveFeedbackRequest(creator, requestee, recipient); + UUID feedbackRequestId = UUID.randomUUID(); UUID creatorId = UUID.randomUUID(); UUID recipientId = UUID.randomUUID(); UUID requesteeId = UUID.randomUUID(); - FeedbackRequest feedbackRequest = new FeedbackRequest(); - feedbackRequest.setId(feedbackRequestId); - feedbackRequest.setCreatorId(creatorId); - feedbackRequest.setRecipientId(recipientId); - feedbackRequest.setRequesteeId(requesteeId); - feedbackRequest.setSendDate(LocalDate.now()); - feedbackRequest.setStatus("sent"); + // We need the current user to be logged in and *not* admin. + currentUserServices.currentUser = creator; FeedbackRequestUpdateDTO updateDTO = new FeedbackRequestUpdateDTO(); updateDTO.setId(feedbackRequest.getId()); @@ -187,92 +134,33 @@ void testUpdateFeedbackRequest_Unauthorized() { updateDTO.setStatus("submitted"); updateDTO.setRecipientId(feedbackRequest.getRecipientId()); - MemberProfile requestee = new MemberProfile(); - requestee.setId(requesteeId); - - MemberProfile currentMemberProfile = new MemberProfile(); - currentMemberProfile.setId(UUID.randomUUID()); - when(feedbackReqRepository.findById(feedbackRequest.getId())).thenReturn(Optional.of(feedbackRequest)); - when(currentUserServices.getCurrentUser()).thenReturn(currentMemberProfile); - when(currentUserServices.isAdmin()).thenReturn(false); - when(memberProfileServices.getById(requesteeId)).thenReturn(requestee); - assertThrows(PermissionException.class, () -> feedbackRequestServices.update(updateDTO)); - verify(feedbackReqRepository, never()).update(any(FeedbackRequest.class)); assertEquals(0, emailSender.events.size()); } @Test - @Tag("mocked") void testSendSelfReviewCompletionEmailToReviewers() { - UUID reviewAssignmentId; - ReviewAssignment reviewAssignment; - UUID creatorId = UUID.randomUUID(); - MemberProfile currentUser = new MemberProfile(); - currentUser.setId(creatorId); - - MemberProfile pdlProfile = new MemberProfile(); - pdlProfile.setId(UUID.randomUUID()); - pdlProfile.setFirstName("PDL"); - pdlProfile.setLastName("Profile"); - pdlProfile.setWorkEmail("pdl@example.com"); - - MemberProfile supervisorProfile = new MemberProfile(); - supervisorProfile.setId(UUID.randomUUID()); - supervisorProfile.setFirstName("Supervisor"); - supervisorProfile.setLastName("Profile"); - supervisorProfile.setWorkEmail("supervisor@example.com"); - - currentUser.setPdlId(pdlProfile.getId()); - currentUser.setSupervisorid(supervisorProfile.getId()); - - MemberProfile reviewer01 = new MemberProfile(); - reviewer01.setId(UUID.randomUUID()); - reviewer01.setFirstName("Reviewer01"); - reviewer01.setLastName("Profile"); - reviewer01.setWorkEmail("reviewer01@example.com"); - - MemberProfile reviewer02 = new MemberProfile(); - reviewer02.setId(UUID.randomUUID()); - reviewer02.setFirstName("Reviewer02"); - reviewer02.setLastName("Profile"); - reviewer02.setWorkEmail("reviewer02@example.com"); - - ReviewPeriod reviewPeriod = new ReviewPeriod(); - reviewPeriod.setName("Self-Review Test"); - - String firstName = "firstName"; - String lastName = "lastName"; - - currentUser.setFirstName(firstName); - currentUser.setLastName(lastName); - - UUID reviewPeriodId = UUID.randomUUID(); - FeedbackRequest feedbackRequest = new FeedbackRequest(); - feedbackRequest.setReviewPeriodId(reviewPeriodId); + MemberProfile pdlProfile = createASecondDefaultMemberProfile(); + MemberProfile supervisorProfile = createAThirdDefaultMemberProfile(); + MemberProfile currentUser = + createAProfileWithSupervisorAndPDL(supervisorProfile, pdlProfile); + + MemberProfile reviewer01 = createADefaultMemberProfile(); + MemberProfile reviewer02 = createAnUnrelatedUser(); + + ReviewPeriod reviewPeriod = createADefaultReviewPeriod(); - when(currentUserServices.getCurrentUser()).thenReturn(currentUser); - when(memberProfileServices.getById(pdlProfile.getId())).thenReturn(pdlProfile); - when(memberProfileServices.getById(supervisorProfile.getId())).thenReturn(supervisorProfile); - when(memberProfileServices.getById(reviewer01.getId())).thenReturn(reviewer01); - when(memberProfileServices.getById(reviewer02.getId())).thenReturn(reviewer02); - when(reviewPeriodRepository.findById(reviewPeriodId)).thenReturn(Optional.of(reviewPeriod)); + FeedbackRequest feedbackRequest = + saveFeedbackRequest(supervisorProfile, currentUser, + pdlProfile, reviewPeriod); + + currentUserServices.currentUser = currentUser; Set reviewAssignmentsSet = new HashSet(); - reviewAssignmentId = UUID.randomUUID(); - reviewAssignment= new ReviewAssignment(); - reviewAssignment.setId(reviewAssignmentId); - reviewAssignment.setReviewPeriodId(reviewPeriodId); - reviewAssignment.setReviewerId(reviewer01.getId()); - reviewAssignment.setRevieweeId(currentUser.getId()); - reviewAssignmentsSet.add(reviewAssignment); - reviewAssignmentId = UUID.randomUUID(); - reviewAssignment= new ReviewAssignment(); - reviewAssignment.setId(reviewAssignmentId); - reviewAssignment.setReviewPeriodId(reviewPeriodId); - reviewAssignment.setReviewerId(reviewer02.getId()); - reviewAssignment.setRevieweeId(currentUser.getId()); - reviewAssignmentsSet.add(reviewAssignment); + reviewAssignmentsSet.add( + createAReviewAssignmentBetweenMembers(currentUser, reviewer01, reviewPeriod, true)); + reviewAssignmentsSet.add( + createAReviewAssignmentBetweenMembers(currentUser, reviewer02, reviewPeriod, true)); feedbackRequestServices.sendSelfReviewCompletionEmailToReviewers(feedbackRequest, reviewAssignmentsSet); @@ -280,119 +168,78 @@ void testSendSelfReviewCompletionEmailToReviewers() { // The order in which emails are sent is random. We will not be // checking the recipient. assertEquals(2, emailSender.events.size()); - EmailHelper.validateEmail("SEND_EMAIL", "null", "null", "firstName lastName has finished their self-review for Self-Review Test.", "firstName lastName has completed their self-review", null, emailSender.events.getFirst()); + EmailHelper.validateEmail("SEND_EMAIL", "null", "null", + String.format("%s %s has finished their self-review for %s.", currentUser.getFirstName(), currentUser.getLastName(), reviewPeriod.getName()), + String.format("%s %s has completed their self-review", currentUser.getFirstName(), currentUser.getLastName()), + null, emailSender.events.getFirst()); } @Test - @Tag("mocked") void testSendSelfReviewCompletionEmailToSupervisor() { - UUID creatorId = UUID.randomUUID(); - MemberProfile currentUser = new MemberProfile(); - currentUser.setId(creatorId); - - MemberProfile pdlProfile = new MemberProfile(); - pdlProfile.setId(UUID.randomUUID()); - pdlProfile.setFirstName("PDL"); - pdlProfile.setLastName("Profile"); - pdlProfile.setWorkEmail("pdl@example.com"); + MemberProfile pdlProfile = createASecondDefaultMemberProfile(); + MemberProfile supervisorProfile = createAThirdDefaultMemberProfile(); + MemberProfile currentUser = + createAProfileWithSupervisorAndPDL(supervisorProfile, pdlProfile); - MemberProfile supervisorProfile = new MemberProfile(); - supervisorProfile.setId(UUID.randomUUID()); - supervisorProfile.setFirstName("Supervisor"); - supervisorProfile.setLastName("Profile"); - supervisorProfile.setWorkEmail("supervisor@example.com"); + ReviewPeriod reviewPeriod = createADefaultReviewPeriod(); - currentUser.setPdlId(pdlProfile.getId()); - currentUser.setSupervisorid(supervisorProfile.getId()); + FeedbackRequest feedbackRequest = + saveFeedbackRequest(supervisorProfile, currentUser, + pdlProfile, reviewPeriod); - ReviewPeriod reviewPeriod = new ReviewPeriod(); - reviewPeriod.setName("Self-Review Test"); - - String firstName = "firstName"; - String lastName = "lastName"; - - currentUser.setFirstName(firstName); - currentUser.setLastName(lastName); - - UUID reviewPeriodId = UUID.randomUUID(); - FeedbackRequest feedbackRequest = new FeedbackRequest(); - feedbackRequest.setReviewPeriodId(reviewPeriodId); - - when(currentUserServices.getCurrentUser()).thenReturn(currentUser); - when(memberProfileServices.getById(pdlProfile.getId())).thenReturn(pdlProfile); - when(memberProfileServices.getById(supervisorProfile.getId())).thenReturn(supervisorProfile); - when(reviewPeriodRepository.findById(reviewPeriodId)).thenReturn(Optional.of(reviewPeriod)); + currentUserServices.currentUser = currentUser; feedbackRequestServices.sendSelfReviewCompletionEmailToSupervisor(feedbackRequest); assertEquals(1, emailSender.events.size()); - EmailHelper.validateEmail("SEND_EMAIL", "null", "null", "firstName lastName has finished their self-review for Self-Review Test.", "firstName lastName has completed their self-review", supervisorProfile.getWorkEmail(), emailSender.events.getFirst()); + EmailHelper.validateEmail("SEND_EMAIL", "null", "null", + String.format("%s %s has finished their self-review for %s.", currentUser.getFirstName(), currentUser.getLastName(), reviewPeriod.getName()), + String.format("%s %s has completed their self-review", currentUser.getFirstName(), currentUser.getLastName()), + supervisorProfile.getWorkEmail(), + emailSender.events.getFirst()); } @Test - @Tag("mocked") void testSendSelfReviewCompletionEmailToSupervisor_MissingSupervisor() { - UUID creatorId = UUID.randomUUID(); - MemberProfile currentUser = new MemberProfile(); - currentUser.setId(creatorId); - - MemberProfile pdlProfile = new MemberProfile(); - pdlProfile.setId(UUID.randomUUID()); - pdlProfile.setFirstName("PDL"); - pdlProfile.setLastName("Profile"); - pdlProfile.setWorkEmail("pdl@example.com"); + MemberProfile pdlProfile = createASecondDefaultMemberProfile(); + MemberProfile otherProfile = createAThirdDefaultMemberProfile(); + MemberProfile currentUser = createADefaultMemberProfile(); - currentUser.setPdlId(pdlProfile.getId()); + ReviewPeriod reviewPeriod = createADefaultReviewPeriod(); - String firstName = "firstName"; - String lastName = "lastName"; + FeedbackRequest feedbackRequest = + saveFeedbackRequest(otherProfile, currentUser, + pdlProfile, reviewPeriod); - currentUser.setFirstName(firstName); - currentUser.setLastName(lastName); + currentUserServices.currentUser = currentUser; - when(currentUserServices.getCurrentUser()).thenReturn(currentUser); - when(memberProfileServices.getById(pdlProfile.getId())).thenReturn(pdlProfile); - - feedbackRequestServices.sendSelfReviewCompletionEmailToSupervisor(new FeedbackRequest()); + feedbackRequestServices.sendSelfReviewCompletionEmailToSupervisor(feedbackRequest); assertEquals(0, emailSender.events.size()); } @Test - @Tag("mocked") void testSendSelfReviewCompletionEmailToSupervisor_EmailSenderException() { - UUID creatorId = UUID.randomUUID(); - MemberProfile currentUser = new MemberProfile(); - currentUser.setId(creatorId); - - MemberProfile pdlProfile = new MemberProfile(); - pdlProfile.setId(UUID.randomUUID()); - pdlProfile.setFirstName("PDL"); - pdlProfile.setLastName("Profile"); - pdlProfile.setWorkEmail("pdl@example.com"); - - MemberProfile supervisorProfile = new MemberProfile(); - supervisorProfile.setId(UUID.randomUUID()); - supervisorProfile.setFirstName("Supervisor"); - supervisorProfile.setLastName("Profile"); - supervisorProfile.setWorkEmail("supervisor@example.com"); - - currentUser.setPdlId(pdlProfile.getId()); - currentUser.setSupervisorid(supervisorProfile.getId()); + MemberProfile pdlProfile = createASecondDefaultMemberProfile(); + MemberProfile supervisorProfile = createAThirdDefaultMemberProfile(); + MemberProfile currentUser = + createAProfileWithSupervisorAndPDL(supervisorProfile, pdlProfile); - String firstName = "firstName"; - String lastName = "lastName"; + ReviewPeriod reviewPeriod = createADefaultReviewPeriod(); - currentUser.setFirstName(firstName); - currentUser.setLastName(lastName); + FeedbackRequest feedbackRequest = + saveFeedbackRequest(supervisorProfile, currentUser, + pdlProfile, reviewPeriod); - when(currentUserServices.getCurrentUser()).thenReturn(currentUser); - when(memberProfileServices.getById(pdlProfile.getId())).thenReturn(pdlProfile); - when(memberProfileServices.getById(supervisorProfile.getId())).thenReturn(supervisorProfile); + currentUserServices.currentUser = currentUser; emailSender.setException(new RuntimeException("Email sending failed")); - assertDoesNotThrow(() -> feedbackRequestServices.sendSelfReviewCompletionEmailToSupervisor(new FeedbackRequest())); + assertDoesNotThrow(() -> feedbackRequestServices.sendSelfReviewCompletionEmailToSupervisor(feedbackRequest)); assertEquals(1, emailSender.events.size()); - EmailHelper.validateEmail("SEND_EMAIL", "null", "null", "firstName lastName has finished their self-review.", "firstName lastName has completed their self-review", supervisorProfile.getWorkEmail(), emailSender.events.getFirst()); + EmailHelper.validateEmail("SEND_EMAIL", "null", "null", + String.format("%s %s has finished their self-review for %s.", currentUser.getFirstName(), currentUser.getLastName(), reviewPeriod.getName()), + String.format("%s %s has completed their self-review", currentUser.getFirstName(), currentUser.getLastName()), + supervisorProfile.getWorkEmail(), + emailSender.events.getFirst()); } } diff --git a/server/src/test/java/com/objectcomputing/checkins/services/file/FileControllerTest.java b/server/src/test/java/com/objectcomputing/checkins/services/file/FileControllerTest.java index 314fa1f6e4..e547735ead 100644 --- a/server/src/test/java/com/objectcomputing/checkins/services/file/FileControllerTest.java +++ b/server/src/test/java/com/objectcomputing/checkins/services/file/FileControllerTest.java @@ -1,6 +1,13 @@ package com.objectcomputing.checkins.services.file; import com.objectcomputing.checkins.services.TestContainersSuite; +import com.objectcomputing.checkins.services.FileServicesImplReplacement; +import com.objectcomputing.checkins.services.fixture.MemberProfileFixture; +import com.objectcomputing.checkins.services.fixture.CheckInFixture; +import com.objectcomputing.checkins.services.fixture.CheckInDocumentFixture; +import com.objectcomputing.checkins.services.checkins.CheckIn; +import com.objectcomputing.checkins.services.checkindocument.CheckinDocument; +import com.objectcomputing.checkins.services.memberprofile.MemberProfile; import io.micronaut.core.type.Argument; import io.micronaut.http.HttpRequest; import io.micronaut.http.HttpResponse; @@ -10,12 +17,14 @@ import io.micronaut.http.client.exceptions.HttpClientResponseException; import io.micronaut.http.client.multipart.MultipartBody; import io.micronaut.http.multipart.CompletedFileUpload; -import io.micronaut.test.annotation.MockBean; +import io.micronaut.context.annotation.Property; +import io.micronaut.core.util.StringUtils; + import jakarta.inject.Inject; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.condition.DisabledInNativeImage; import java.io.File; import java.io.FileWriter; @@ -26,22 +35,16 @@ import java.util.UUID; import static com.objectcomputing.checkins.services.role.RoleType.Constants.MEMBER_ROLE; +import static com.objectcomputing.checkins.services.role.RoleType.Constants.ADMIN_ROLE; import static io.micronaut.http.MediaType.MULTIPART_FORM_DATA; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.mockito.Mockito.any; -import static org.mockito.Mockito.doThrow; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -// Disabled in nativeTest, as we get an exception from Mockito -// => java.lang.NoClassDefFoundError: Could not initialize class org.mockito.Mockito -@DisabledInNativeImage -class FileControllerTest extends TestContainersSuite { + +@Property(name = "replace.fileservicesimpl", value = StringUtils.TRUE) +class FileControllerTest extends TestContainersSuite + implements MemberProfileFixture, CheckInFixture, CheckInDocumentFixture { @Inject @Client("/services/files") @@ -51,7 +54,18 @@ class FileControllerTest extends TestContainersSuite { private final static String filePath = "testFile.txt"; @Inject - private FileServices fileServices; + private FileServicesImplReplacement fileServices; + + private CheckIn checkIn; + private MemberProfile pdl; + private MemberProfile member; + + @BeforeEach + void reset() { + pdl = createADefaultMemberProfile(); + member = createADefaultMemberProfileForPdl(pdl); + checkIn = createADefaultCheckIn(member, pdl); + } @BeforeAll void createTestFile() throws IOException { @@ -68,21 +82,12 @@ void deleteTestFile() { @Test void testFindAll() { + String fileId = "some.id"; + FileInfoDTO testFileInfoDto = fileServices.addFile(fileId, new byte[0]); - UUID testCheckinId = UUID.randomUUID(); - - FileInfoDTO testFileInfoDto = new FileInfoDTO(); - testFileInfoDto.setFileId("some.id"); - testFileInfoDto.setName(testFile.getName()); - testFileInfoDto.setCheckInId(testCheckinId); - testFileInfoDto.setSize(testFile.length()); - Set testResultFromService = new HashSet<>(); - testResultFromService.add(testFileInfoDto); - when(fileServices.findFiles(null)).thenReturn(testResultFromService); - - final HttpRequest request = HttpRequest.GET("").basicAuth("some.email.id", MEMBER_ROLE); + final HttpRequest request = HttpRequest.GET("").basicAuth(member.getWorkEmail(), ADMIN_ROLE); final HttpResponse> response = client.toBlocking().exchange(request, Argument.setOf(FileInfoDTO.class)); assertNotNull(response); @@ -92,26 +97,20 @@ void testFindAll() { assertEquals(testFileInfoDto.getFileId(), result.iterator().next().getFileId()); assertEquals(testFileInfoDto.getCheckInId(), result.iterator().next().getCheckInId()); assertEquals(testFileInfoDto.getName(), result.iterator().next().getName()); - verify(fileServices, times(1)).findFiles(null); + } + + private FileInfoDTO createUploadedDocument(String fileId) { + return fileServices.addFile(fileId, new byte[50], (id) -> { + return createACustomCheckInDocument(checkIn, id); + }); } @Test void testFindByCheckinId() { + String fileId = "some.id"; + FileInfoDTO testFileInfoDto = createUploadedDocument(fileId); - UUID testCheckinId = UUID.randomUUID(); - - FileInfoDTO testFileInfoDto = new FileInfoDTO(); - testFileInfoDto.setFileId("some.id"); - testFileInfoDto.setName(testFile.getName()); - testFileInfoDto.setCheckInId(testCheckinId); - testFileInfoDto.setSize(testFile.length()); - - Set testResultFromService = new HashSet<>(); - testResultFromService.add(testFileInfoDto); - - when(fileServices.findFiles(testCheckinId)).thenReturn(testResultFromService); - - final HttpRequest request = HttpRequest.GET(String.format("?id=%s", testCheckinId)).basicAuth("some.email.id", MEMBER_ROLE); + final HttpRequest request = HttpRequest.GET(String.format("?id=%s", checkIn.getId())).basicAuth(member.getWorkEmail(), MEMBER_ROLE); final HttpResponse> response = client.toBlocking().exchange(request, Argument.setOf(FileInfoDTO.class)); assertNotNull(response); @@ -121,61 +120,44 @@ void testFindByCheckinId() { assertEquals(testFileInfoDto.getFileId(), result.iterator().next().getFileId()); assertEquals(testFileInfoDto.getCheckInId(), result.iterator().next().getCheckInId()); assertEquals(testFileInfoDto.getName(), result.iterator().next().getName()); - verify(fileServices, times(1)).findFiles(testCheckinId); } @Test void testDownloadDocument() { String uploadDocId = "some.upload.id"; - - when(fileServices.downloadFiles(uploadDocId)).thenReturn(testFile); + FileInfoDTO testFileInfoDto = createUploadedDocument(uploadDocId); final HttpRequest request= HttpRequest.GET(String.format("/%s/download", uploadDocId)) - .basicAuth("some.email.id", MEMBER_ROLE); + .basicAuth(member.getWorkEmail(), MEMBER_ROLE); final HttpResponse response = client.toBlocking().exchange(request, File.class); assertNotNull(response); assertEquals(HttpStatus.OK, response.getStatus()); - verify(fileServices, times(1)).downloadFiles(uploadDocId); } @Test void testUploadEndpoint() { - - UUID testCheckinId = UUID.randomUUID(); - - FileInfoDTO testFileInfoDto = new FileInfoDTO(); - testFileInfoDto.setFileId("some.id"); - testFileInfoDto.setName(testFile.getName()); - testFileInfoDto.setCheckInId(testCheckinId); - testFileInfoDto.setSize(testFile.length()); - - when(fileServices.uploadFile(any(UUID.class), any(CompletedFileUpload.class))).thenReturn(testFileInfoDto); - - final HttpRequest request = HttpRequest.POST(String.format("/%s", testCheckinId), MultipartBody.builder() + final HttpRequest request = HttpRequest.POST(String.format("/%s", checkIn.getId()), MultipartBody.builder() .addPart("file", testFile).build()) - .basicAuth("some.email.id", MEMBER_ROLE) + .basicAuth(member.getWorkEmail(), MEMBER_ROLE) .contentType(MULTIPART_FORM_DATA); final HttpResponse response = client.toBlocking().exchange(request, FileInfoDTO.class); assertNotNull(response); assertEquals(HttpStatus.CREATED, response.getStatus()); FileInfoDTO result = response.getBody().get(); - assertEquals(testFileInfoDto.getSize(), result.getSize()); - assertEquals(testFileInfoDto.getFileId(), result.getFileId()); - assertEquals(testFileInfoDto.getCheckInId(), result.getCheckInId()); - assertEquals(testFileInfoDto.getName(), result.getName()); - verify(fileServices, times(1)).uploadFile(any(UUID.class), any(CompletedFileUpload.class)); + assertEquals(testFile.length(), result.getSize()); + assertEquals(checkIn.getId(), result.getCheckInId()); + assertEquals(testFile.getName(), result.getName()); } @Test void testUploadEndpointFailsForInvalidFile() { - UUID testCheckinId = UUID.randomUUID(); File badFile = new File(""); - final HttpRequest request = HttpRequest.POST(String.format("/%s", testCheckinId), MultipartBody.builder() + final HttpRequest request = HttpRequest.POST(String.format("/%s", checkIn.getId()), MultipartBody.builder() .addPart("file", badFile).build()) - .basicAuth("some.email", MEMBER_ROLE) + .basicAuth(member.getWorkEmail(), MEMBER_ROLE) .contentType(MULTIPART_FORM_DATA); final IllegalArgumentException responseException = assertThrows(IllegalArgumentException.class, () -> @@ -183,42 +165,31 @@ void testUploadEndpointFailsForInvalidFile() { // Note: exception message is different here depending on your operating system. Better to just check the type. assertTrue(responseException.getMessage().contains("java.io.FileNotFoundException")); - verify(fileServices, times(0)).uploadFile(any(UUID.class), any(CompletedFileUpload.class)); } @Test void testDeleteEndpoint() { - String uploadDocId = "some.upload.id"; - when(fileServices.deleteFile(uploadDocId)).thenReturn(true); + FileInfoDTO testFileInfoDto = createUploadedDocument(uploadDocId); final HttpRequest request = HttpRequest.DELETE(String.format("/%s", uploadDocId)) - .basicAuth("some.email.id", MEMBER_ROLE); + .basicAuth(member.getWorkEmail(), MEMBER_ROLE); final HttpResponse response = client.toBlocking().exchange(request); assertNotNull(response); assertEquals(HttpStatus.OK, response.getStatus()); - verify(fileServices, times(1)).deleteFile(uploadDocId); } @Test void testHandleBadArgs() { - String uploadDocId = "some.upload.id"; - doThrow(FileRetrievalException.class).when(fileServices).deleteFile(uploadDocId); final HttpRequest request = HttpRequest.DELETE(String.format("/%s", uploadDocId)) - .basicAuth("some.email.id", MEMBER_ROLE); + .basicAuth(member.getWorkEmail(), MEMBER_ROLE); final HttpClientResponseException responseException = assertThrows(HttpClientResponseException.class, () -> client.toBlocking().exchange(request, Map.class)); assertEquals(HttpStatus.BAD_REQUEST, responseException.getStatus()); - verify(fileServices, times(1)).deleteFile(uploadDocId); - } - - @MockBean(FileServices.class) - public FileServices fileServices() { - return mock(FileServices.class); } } diff --git a/server/src/test/java/com/objectcomputing/checkins/services/file/FileServicesImplTest.java b/server/src/test/java/com/objectcomputing/checkins/services/file/FileServicesImplTest.java index cbbfbd97c6..1a6e6e4ed6 100644 --- a/server/src/test/java/com/objectcomputing/checkins/services/file/FileServicesImplTest.java +++ b/server/src/test/java/com/objectcomputing/checkins/services/file/FileServicesImplTest.java @@ -1,13 +1,7 @@ package com.objectcomputing.checkins.services.file; -import com.google.api.client.googleapis.json.GoogleJsonResponseException; -import com.google.api.client.googleapis.testing.json.GoogleJsonResponseExceptionFactoryTesting; -import com.google.api.client.http.AbstractInputStreamContent; -import com.google.api.client.json.JsonFactory; -import com.google.api.client.testing.json.MockJsonFactory; -import com.google.api.services.drive.Drive; -import com.google.api.services.drive.model.FileList; -import com.google.common.io.ByteStreams; +import com.objectcomputing.checkins.exceptions.BadArgException; +import com.objectcomputing.checkins.exceptions.NotFoundException; import com.objectcomputing.checkins.security.GoogleServiceConfiguration; import com.objectcomputing.checkins.services.TestContainersSuite; import com.objectcomputing.checkins.services.checkindocument.CheckinDocument; @@ -17,25 +11,32 @@ import com.objectcomputing.checkins.services.memberprofile.MemberProfile; import com.objectcomputing.checkins.services.memberprofile.MemberProfileServices; import com.objectcomputing.checkins.services.memberprofile.MemberProfileUtils; -import com.objectcomputing.checkins.services.memberprofile.currentuser.CurrentUserServices; -import com.objectcomputing.checkins.util.googleapiaccess.GoogleApiAccess; +import com.objectcomputing.checkins.services.CurrentUserServicesReplacement; +import com.objectcomputing.checkins.services.FileServicesImplReplacement; +import com.objectcomputing.checkins.services.fixture.MemberProfileFixture; +import com.objectcomputing.checkins.services.fixture.CheckInFixture; +import com.objectcomputing.checkins.services.fixture.CheckInDocumentFixture; +import com.objectcomputing.checkins.services.fixture.RoleFixture; +import com.objectcomputing.checkins.services.role.RoleType; + import io.micronaut.http.multipart.CompletedFileUpload; +import io.micronaut.http.MediaType; import io.micronaut.security.authentication.Authentication; +import io.micronaut.context.annotation.Property; +import io.micronaut.core.util.StringUtils; + +import jakarta.inject.Inject; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.condition.DisabledInNativeImage; -import org.mockito.InjectMocks; -import org.mockito.Mock; -import org.mockito.invocation.InvocationOnMock; -import org.mockito.stubbing.Answer; import java.io.File; import java.io.FileInputStream; import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; +import java.io.ByteArrayInputStream; import java.io.OutputStream; import java.time.LocalDate; import java.util.ArrayList; @@ -45,173 +46,115 @@ import java.util.Map; import java.util.Set; import java.util.UUID; +import java.util.Optional; +import java.nio.ByteBuffer; import static com.objectcomputing.checkins.services.validate.PermissionsValidation.NOT_AUTHORIZED_MSG; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.doAnswer; -import static org.mockito.Mockito.eq; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.reset; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; -import static org.mockito.MockitoAnnotations.openMocks; - -// Disabled in nativeTest, as we get an exception from Mockito -// => java.lang.NoClassDefFoundError: Could not initialize class org.mockito.internal.configuration.plugins.Plugins -@DisabledInNativeImage -class FileServicesImplTest extends TestContainersSuite { - - private static File testFile; - private final static String filePath = "testFile.txt"; - final static JsonFactory jsonFactory = new MockJsonFactory(); - - @Mock - private Authentication authentication; - - @Mock - private Map mockAttributes; - - @Mock - private Drive drive; - - @Mock - private Drive.Files files; - - @Mock - private Drive.Files.List list; - - @Mock - private Drive.Files.Get get; - - @Mock - private Drive.Files.Delete delete; - - @Mock - private Drive.Files.Create create; - - @Mock - private Drive.Files.Update update; - - @Mock - private CompletedFileUpload fileToUpload; - - @Mock - private MemberProfile testMemberProfile; - - @Mock - private CheckIn testCheckIn; - @Mock - private CheckinDocument testCd; +@Property(name = "replace.fileservicesimpl", value = StringUtils.TRUE) +@Property(name = "replace.currentuserservices", value = StringUtils.TRUE) +class FileServicesImplTest extends TestContainersSuite + implements MemberProfileFixture, CheckInFixture, CheckInDocumentFixture, RoleFixture { + + private class SimpleUploadFile implements CompletedFileUpload { + String filename; + + public SimpleUploadFile(String name) { + filename = name; + } + + @Override + public boolean isComplete() { + return true; + } + + @Override + public long getDefinedSize() { + return 50; + } + + @Override + public long getSize() { + return getDefinedSize(); + } + + @Override + public String getFilename() { + return filename; + } + + @Override + public String getName() { + return getFilename(); + } + + @Override + public Optional getContentType() { + return Optional.of(MediaType.of(MediaType.TEXT_PLAIN)); + } + + @Override + public byte[] getBytes() throws IOException { + byte[] bytes = new byte[(int)getSize()]; + return bytes; + } + + @Override + public ByteBuffer getByteBuffer() throws IOException { + return ByteBuffer.wrap(getBytes()); + } + + @Override + public InputStream getInputStream() throws IOException { + return new ByteArrayInputStream(getBytes()); + } + } + + @Inject + private CurrentUserServicesReplacement currentUserServices; + + // The bulk of the functionality for FileServicesImpl has been moved to + // FileServicesBaseImpl, which this replacement extends. The Google + // related portion is the part that will not be tested (it was partially + // tested previously with the use of Mockito). + @Inject + private FileServicesImplReplacement services; + + private CheckIn checkIn; + private MemberProfile pdl; + private MemberProfile member; - @Mock - private InputStream mockInputStream; - - @Mock - private CheckInServices checkInServices; - - @Mock - private CheckinDocumentServices checkinDocumentServices; - - @Mock - private GoogleApiAccess mockGoogleApiAccess; - - @Mock - private CurrentUserServices currentUserServices; - - @Mock - private MemberProfileServices memberProfileServices; - - @Mock - private CompletedFileUpload completedFileUpload; - - @Mock - private GoogleServiceConfiguration googleServiceConfiguration; - - @InjectMocks - private FileServicesImpl services; + @BeforeEach + void reset() { + services.reset(); - private AutoCloseable mockFinalizer; + createAndAssignRoles(); - @BeforeAll - void initMocksAndInitializeFile() throws IOException { - mockFinalizer = openMocks(this); - testFile = new File(filePath); - FileWriter myWriter = new FileWriter(testFile); - myWriter.write("This.Is.A.Test.File"); - myWriter.close(); - } + pdl = createADefaultMemberProfile(); + member = createADefaultMemberProfileForPdl(pdl); - @AfterAll - void deleteTestFile() throws Exception { - testFile.deleteOnExit(); - mockFinalizer.close(); - } + currentUserServices.currentUser = createASecondDefaultMemberProfile(); + currentUserServices.roles = null; - @BeforeEach - void resetMocks() { - reset(authentication); - reset(mockAttributes); - reset(drive); - reset(files); - reset(list); - reset(get); - reset(delete); - reset(create); - reset(update); - reset(fileToUpload); - reset(testMemberProfile); - reset(testCheckIn); - reset(testCd); - reset(mockInputStream); - reset(checkInServices); - reset(checkinDocumentServices); - reset(mockGoogleApiAccess); - reset(currentUserServices); - reset(memberProfileServices); - reset(completedFileUpload); - reset(googleServiceConfiguration); - - when(authentication.getAttributes()).thenReturn(mockAttributes); - when(mockAttributes.get("email")).thenReturn(mockAttributes); - when(mockAttributes.toString()).thenReturn("test.email"); - when(currentUserServices.findOrSaveUser(any(), any(), any())).thenReturn(testMemberProfile); - when(googleServiceConfiguration.getDirectoryId()).thenReturn("testDirectoryId"); + checkIn = createADefaultCheckIn(member, pdl); } @Test - void testFindFilesForFindAll() throws IOException { - - FileList fileList = new FileList(); - List mockFiles = new ArrayList<>(); - com.google.api.services.drive.model.File file1 = new com.google.api.services.drive.model.File(); - file1.setId("some.id"); - mockFiles.add(file1); - fileList.setFiles(mockFiles); - - when(currentUserServices.isAdmin()).thenReturn(true); - when(mockGoogleApiAccess.getDrive()).thenReturn(drive); - when(drive.files()).thenReturn(files); - when(files.list()).thenReturn(list); - when(list.setFields(any(String.class))).thenReturn(list); - when(list.setSupportsAllDrives(any())).thenReturn(list); - when(list.setIncludeItemsFromAllDrives(any())).thenReturn(list); - when(list.setQ(any())).thenReturn(list); - when(list.setSpaces(any())).thenReturn(list); - when(list.execute()).thenReturn(fileList); + void testFindFilesForFindAll() { + String fileId = "some.id"; + services.addFile(fileId, new byte[0]); + + currentUserServices.roles = new ArrayList(); + currentUserServices.roles.add(RoleType.ADMIN); final Set result = services.findFiles(null); assertNotNull(result); - assertEquals(fileList.getFiles().iterator().next().getId(), result.iterator().next().getFileId()); - verify(mockGoogleApiAccess, times(1)).getDrive(); - verify(checkinDocumentServices, times(0)).read(any(UUID.class)); - verify(checkInServices, times(0)).read(any(UUID.class)); + assertEquals(fileId, result.iterator().next().getFileId()); } @Test @@ -223,752 +166,273 @@ void testFindAllFailsIfNotAdmin() { } @Test - void testFindFilesForFindByCheckinId() throws IOException { - - com.google.api.services.drive.model.File file = new com.google.api.services.drive.model.File(); - file.setId("some.id"); + void testFindFilesForFindByCheckinId() { + String fileId = "some.id"; + services.addFile(fileId, new byte[0]); + CheckinDocument cd = createACustomCheckInDocument(checkIn, fileId); - UUID testCheckinId = UUID.randomUUID(); - UUID testMemberProfileId = UUID.randomUUID(); - final Set testCheckinDocument = new HashSet<>(); - testCheckinDocument.add(testCd); - - when(mockGoogleApiAccess.getDrive()).thenReturn(drive); - when(drive.files()).thenReturn(files); - when(files.get(any(String.class))).thenReturn(get); - when(get.setSupportsAllDrives(any())).thenReturn(get); - when(get.execute()).thenReturn(file); - when(checkInServices.accessGranted(testCheckinId, testMemberProfileId)).thenReturn(true); - when(testCheckIn.getTeamMemberId()).thenReturn(testMemberProfileId); - when(currentUserServices.getCurrentUser()).thenReturn(testMemberProfile); - when(testMemberProfile.getId()).thenReturn(testMemberProfileId); - when(checkinDocumentServices.read(testCheckinId)).thenReturn(testCheckinDocument); - when(testCd.getUploadDocId()).thenReturn("some.upload.doc.id"); - - final Set result = services.findFiles(testCheckinId); + currentUserServices.currentUser = member; + final Set result = services.findFiles(checkIn.getId()); assertNotNull(result); - assertEquals(file.getId(), result.iterator().next().getFileId()); - verify(mockGoogleApiAccess, times(1)).getDrive(); - verify(checkinDocumentServices, times(1)).read(testCheckinId); - verify(checkInServices, times(1)).accessGranted(testCheckinId, testMemberProfileId); + assertEquals(fileId, result.iterator().next().getFileId()); } @Test void testFindByCheckinIdWhenNoDocsAreUploadedForCheckinId() { - - UUID testCheckinId = UUID.randomUUID(); - UUID testMemberProfileId = UUID.randomUUID(); - when(mockGoogleApiAccess.getDrive()).thenReturn(drive); - when(checkInServices.accessGranted(testCheckinId, testMemberProfileId)).thenReturn(true); - when(testCheckIn.getTeamMemberId()).thenReturn(testMemberProfileId); - when(currentUserServices.getCurrentUser()).thenReturn(testMemberProfile); - when(testMemberProfile.getId()).thenReturn(testMemberProfileId); - when(checkinDocumentServices.read(testCheckinId)).thenReturn(Collections.emptySet()); - - final Set result = services.findFiles(testCheckinId); + currentUserServices.currentUser = member; + final Set result = services.findFiles(checkIn.getId()); assertNotNull(result); assertTrue(result.isEmpty()); - verify(mockGoogleApiAccess, times(1)).getDrive(); - verify(checkinDocumentServices, times(1)).read(any(UUID.class)); - verify(checkInServices, times(1)).accessGranted(testCheckinId, testMemberProfileId); } @Test void testFindByCheckinIdForUnauthorizedUser() { - UUID testCheckinId = UUID.randomUUID(); - when(mockGoogleApiAccess.getDrive()).thenReturn(drive); - when(checkInServices.accessGranted(testCheckinId, testMemberProfile.getId())).thenReturn(false); - when(testCheckIn.getTeamMemberId()).thenReturn(UUID.randomUUID()); - when(currentUserServices.getCurrentUser()).thenReturn(testMemberProfile); - when(testMemberProfile.getId()).thenReturn(UUID.randomUUID()); - final FileRetrievalException responseException = assertThrows(FileRetrievalException.class, () -> - services.findFiles(testCheckinId)); + services.findFiles(checkIn.getId())); assertEquals("You are not authorized to perform this operation", responseException.getMessage()); - verify(mockGoogleApiAccess, times(1)).getDrive(); - verify(checkInServices, times(1)).accessGranted(testCheckinId, testMemberProfile.getId()); - verify(checkinDocumentServices, times(0)).read(any(UUID.class)); } @Test - void testFindFilesDriveCantConnect() { - when(currentUserServices.isAdmin()).thenReturn(true); - when(mockGoogleApiAccess.getDrive()).thenReturn(null); + void testFindAllFilesThrowsException() { + String fileId = "some.id"; + services.addFile(fileId, new byte[0]); - final FileRetrievalException responseException = assertThrows(FileRetrievalException.class, () -> - services.findFiles(null)); - - assertEquals("Unable to access Google Drive", responseException.getMessage()); - } + currentUserServices.roles = new ArrayList(); + currentUserServices.roles.add(RoleType.ADMIN); - @Test - void testFindAllFilesThrowsGoogleJsonResponseException() throws IOException { - - GoogleJsonResponseException testException = GoogleJsonResponseExceptionFactoryTesting.newMock(jsonFactory, 404, null); - when(currentUserServices.isAdmin()).thenReturn(true); - when(mockGoogleApiAccess.getDrive()).thenReturn(drive); - when(drive.files()).thenReturn(files); - when(files.list()).thenReturn(list); - when(list.setFields(any(String.class))).thenReturn(list); - when(list.setSupportsAllDrives(any())).thenReturn(list); - when(list.setIncludeItemsFromAllDrives(any())).thenReturn(list); - when(list.setQ(any())).thenReturn(list); - when(list.setSpaces(any())).thenReturn(list); - when(list.execute()).thenThrow(testException); + services.shouldThrow = true; final FileRetrievalException responseException = assertThrows(FileRetrievalException.class, () -> services.findFiles(null)); - - assertEquals(testException.getMessage(), responseException.getMessage()); - verify(mockGoogleApiAccess, times(1)).getDrive(); } @Test - void testFindByCheckinIdThrowsGoogleJsonResponseException() throws IOException { + void testFindByCheckinIdThrowsException() { + String fileId = "some.id"; + services.addFile(fileId, new byte[0]); + CheckinDocument cd = createACustomCheckInDocument(checkIn, fileId); - UUID testCheckinId = UUID.randomUUID(); - final Set testCheckinDocument = new HashSet<>(); - testCheckinDocument.add(testCd); - GoogleJsonResponseException testException = GoogleJsonResponseExceptionFactoryTesting.newMock(jsonFactory, 404, null); - - when(currentUserServices.isAdmin()).thenReturn(true); - when(checkInServices.read(testCheckinId)).thenReturn(testCheckIn); - when(checkInServices.accessGranted(testCheckinId, testMemberProfile.getId())).thenReturn(true); - when(currentUserServices.getCurrentUser()).thenReturn(testMemberProfile); - when(checkinDocumentServices.read(testCheckinId)).thenReturn(testCheckinDocument); - when(testCd.getUploadDocId()).thenReturn("some.upload.doc.id"); - when(mockGoogleApiAccess.getDrive()).thenReturn(drive); - when(drive.files()).thenReturn(files); - when(files.get(any(String.class))).thenReturn(get); - when(get.setSupportsAllDrives(any())).thenReturn(get); - when(get.execute()).thenThrow(testException); + services.shouldThrow = true; final FileRetrievalException responseException = assertThrows(FileRetrievalException.class, - () -> services.findFiles(testCheckinId)); - - assertEquals(testException.getMessage(), responseException.getMessage()); - verify(mockGoogleApiAccess, times(1)).getDrive(); - verify(checkinDocumentServices, times(1)).read(testCheckinId); - verify(checkInServices, times(1)).accessGranted(testCheckinId, testMemberProfile.getId()); + () -> services.findFiles(checkIn.getId())); } @Test - void testDownloadFiles() throws IOException { - String testUploadDocId = "some.test.id"; - UUID testCheckinId = UUID.randomUUID(); - UUID testMemberId = UUID.randomUUID(); - - when(checkinDocumentServices.getFindByUploadDocId(testUploadDocId)).thenReturn(testCd); - when(testCd.getCheckinsId()).thenReturn(testCheckinId); - when(checkInServices.read(testCheckinId)).thenReturn(testCheckIn); - when(testCheckIn.getTeamMemberId()).thenReturn(testMemberId); - when(currentUserServices.getCurrentUser()).thenReturn(testMemberProfile); - when(testMemberProfile.getId()).thenReturn(testMemberId); - when(mockGoogleApiAccess.getDrive()).thenReturn(drive); - when(drive.files()).thenReturn(files); - when(files.get(testUploadDocId)).thenReturn(get); - when(get.setSupportsAllDrives(any())).thenReturn(get); - doAnswer(new Answer() { - public Void answer(InvocationOnMock invocation) throws IOException { - OutputStream outputStream = invocation.getArgument(0); - InputStream inputstream = new FileInputStream(testFile); - ByteStreams.copy(inputstream, outputStream); - return null; - } - }).when(get).executeMediaAndDownloadTo(any(OutputStream.class)); - - final java.io.File resultFile = services.downloadFiles(testUploadDocId); - - assertEquals(testFile.length(), resultFile.length()); - verify(mockGoogleApiAccess, times(1)).getDrive(); - verify(checkInServices, times(1)).read(any(UUID.class)); - verify(checkinDocumentServices, times(1)).getFindByUploadDocId(testUploadDocId); - verify(get, times(1)).executeMediaAndDownloadTo(any(OutputStream.class)); + void testDownloadFiles() { + String uploadDocId = "some.test.id"; + long byteCount = 50; + services.addFile(uploadDocId, new byte[(int)byteCount]); + + currentUserServices.currentUser = member; + CheckinDocument cd = createACustomCheckInDocument(checkIn, uploadDocId); + + final java.io.File resultFile = services.downloadFiles(uploadDocId); + assertEquals(byteCount, resultFile.length()); } @Test - void testDownloadFilesAdminCanAccess() throws IOException { - String testUploadDocId = "some.test.id"; - UUID testCheckinId = UUID.randomUUID(); + void testDownloadFilesAdminCanAccess() { + String uploadDocId = "some.test.id"; + long byteCount = 50; + services.addFile(uploadDocId, new byte[(int)byteCount]); - when(checkinDocumentServices.getFindByUploadDocId(testUploadDocId)).thenReturn(testCd); - when(currentUserServices.isAdmin()).thenReturn(true); - when(testCd.getCheckinsId()).thenReturn(testCheckinId); - when(checkInServices.read(testCheckinId)).thenReturn(testCheckIn); - when(mockGoogleApiAccess.getDrive()).thenReturn(drive); - when(drive.files()).thenReturn(files); - when(files.get(testUploadDocId)).thenReturn(get); - when(get.setSupportsAllDrives(any())).thenReturn(get); - doAnswer(new Answer() { - public Void answer(InvocationOnMock invocation) throws IOException { - OutputStream outputStream = invocation.getArgument(0); - InputStream inputstream = new FileInputStream(testFile); - ByteStreams.copy(inputstream, outputStream); - return null; - } - }).when(get).executeMediaAndDownloadTo(any(OutputStream.class)); - - final java.io.File resultFile = services.downloadFiles(testUploadDocId); - - assertEquals(testFile.length(), resultFile.length()); - verify(mockGoogleApiAccess, times(1)).getDrive(); - verify(checkInServices, times(1)).read(any(UUID.class)); - verify(checkinDocumentServices, times(1)).getFindByUploadDocId(testUploadDocId); - verify(get, times(1)).executeMediaAndDownloadTo(any(OutputStream.class)); + currentUserServices.roles = new ArrayList(); + currentUserServices.roles.add(RoleType.ADMIN); + // The CheckInsServicesImpl checks with the rolePermissionServices. + assignAdminRole(currentUserServices.currentUser); + + CheckinDocument cd = createACustomCheckInDocument(checkIn, uploadDocId); + final java.io.File resultFile = services.downloadFiles(uploadDocId); + + assertEquals(byteCount, resultFile.length()); } @Test void testDownloadFilesInvalidUploadDocId() { String invalidUploadDocId = "some.test.id"; - when(checkinDocumentServices.getFindByUploadDocId(invalidUploadDocId)).thenReturn(null); - final FileRetrievalException responseException = assertThrows(FileRetrievalException.class, () -> + // This exception actually comes from the CheckinDocumentServices. + final BadArgException responseException = assertThrows(BadArgException.class, () -> services.downloadFiles(invalidUploadDocId)); - assertEquals(String.format("Unable to find record with id %s", invalidUploadDocId), responseException.getMessage()); - verify(mockGoogleApiAccess, times(0)).getDrive(); - verify(checkInServices, times(0)).read(any(UUID.class)); - verify(checkinDocumentServices, times(1)).getFindByUploadDocId(invalidUploadDocId); + assertEquals(String.format("CheckinDocument with document id %s does not exist", invalidUploadDocId), responseException.getMessage()); } @Test void testDownloadFilesUnauthorizedUser() { - String testUploadDocId = "some.test.id"; - UUID testCheckinId = UUID.randomUUID(); - when(checkinDocumentServices.getFindByUploadDocId(testUploadDocId)).thenReturn(testCd); - when(testCd.getCheckinsId()).thenReturn(testCheckinId); - when(checkInServices.read(testCheckinId)).thenReturn(testCheckIn); - when(testCheckIn.getTeamMemberId()).thenReturn(UUID.randomUUID()); - when(currentUserServices.getCurrentUser()).thenReturn(testMemberProfile); - when(testMemberProfile.getId()).thenReturn(UUID.randomUUID()); + String uploadDocId = "some.test.id"; + services.addFile(uploadDocId, new byte[0]); + CheckinDocument cd = createACustomCheckInDocument(checkIn, uploadDocId); - final FileRetrievalException responseException = assertThrows(FileRetrievalException.class, () -> - services.downloadFiles(testUploadDocId)); + // This exception actually comes from the CheckinDocumentServices. + final BadArgException responseException = assertThrows(BadArgException.class, () -> + services.downloadFiles(uploadDocId)); assertEquals(NOT_AUTHORIZED_MSG, responseException.getMessage()); - verify(mockGoogleApiAccess, times(0)).getDrive(); - verify(checkinDocumentServices, times(1)).getFindByUploadDocId(testUploadDocId); - verify(checkInServices, times(1)).read(testCheckinId); } @Test - void testDownloadFilesDriveCantConnect() { - String testUploadDocId = "some.test.id"; - UUID testMemberProfileId = UUID.randomUUID(); - when(checkinDocumentServices.getFindByUploadDocId(testUploadDocId)).thenReturn(testCd); - when(checkInServices.read(any())).thenReturn(testCheckIn); - when(testCheckIn.getTeamMemberId()).thenReturn(testMemberProfileId); - when(currentUserServices.getCurrentUser()).thenReturn(testMemberProfile); - when(testMemberProfile.getId()).thenReturn(testMemberProfileId); - when(mockGoogleApiAccess.getDrive()).thenReturn(null); + void testDownloadFilesThrowsException() { + String uploadDocId = "some.test.id"; + services.addFile(uploadDocId, new byte[0]); - final FileRetrievalException responseException = assertThrows(FileRetrievalException.class, () -> - services.downloadFiles(testUploadDocId)); + currentUserServices.currentUser = member; + CheckinDocument cd = createACustomCheckInDocument(checkIn, uploadDocId); - assertEquals("Unable to access Google Drive", responseException.getMessage()); - verify(mockGoogleApiAccess, times(1)).getDrive(); - } - - @Test - void testDownloadFilesThrowsGoogleJsonResponseException() throws IOException { - - String testUploadDocId = "some.test.id"; - UUID testMemberProfileId = UUID.randomUUID(); - GoogleJsonResponseException testException = GoogleJsonResponseExceptionFactoryTesting.newMock(jsonFactory, 404, null); - - when(currentUserServices.isAdmin()).thenReturn(true); - when(checkinDocumentServices.getFindByUploadDocId(testUploadDocId)).thenReturn(testCd); - when(checkInServices.read(any())).thenReturn(testCheckIn); - when(testCheckIn.getTeamMemberId()).thenReturn(testMemberProfileId); - when(testMemberProfile.getId()).thenReturn(testMemberProfileId); - when(mockGoogleApiAccess.getDrive()).thenReturn(drive); - when(drive.files()).thenReturn(files); - when(files.get(testUploadDocId)).thenThrow(testException); + services.shouldThrow = true; final FileRetrievalException responseException = assertThrows(FileRetrievalException.class, - () -> services.downloadFiles(testUploadDocId)); - - assertEquals(testException.getMessage(), responseException.getMessage()); - verify(mockGoogleApiAccess, times(1)).getDrive(); - verify(checkinDocumentServices, times(1)).getFindByUploadDocId(testUploadDocId); - verify(checkInServices, times(1)).read(any()); + () -> services.downloadFiles(uploadDocId)); } @Test - void testDeleteFile() throws IOException { - + void testDeleteFile() { String uploadDocId = "Some.Upload.Doc.Id"; - UUID testCheckinId = UUID.randomUUID(); - UUID testMemberId = UUID.randomUUID(); - when(checkinDocumentServices.getFindByUploadDocId(uploadDocId)).thenReturn(testCd); - when(testCd.getCheckinsId()).thenReturn(testCheckinId); - when(checkInServices.read(testCheckinId)).thenReturn(testCheckIn); - when(testCheckIn.getTeamMemberId()).thenReturn(testMemberId); - when(currentUserServices.getCurrentUser()).thenReturn(testMemberProfile); - when(testMemberProfile.getId()).thenReturn(testMemberId); - when(mockGoogleApiAccess.getDrive()).thenReturn(drive); - when(drive.files()).thenReturn(files); - when(files.update(eq(uploadDocId), any())).thenReturn(update); - when(update.setSupportsAllDrives(any())).thenReturn(update); + services.addFile(uploadDocId, new byte[0]); + CheckinDocument cd = createACustomCheckInDocument(checkIn, uploadDocId); + currentUserServices.currentUser = member; Boolean result = services.deleteFile(uploadDocId); - assertTrue(result); - verify(mockGoogleApiAccess, times(1)).getDrive(); - verify(checkinDocumentServices, times(1)).getFindByUploadDocId(uploadDocId); - verify(checkinDocumentServices, times(1)).deleteByUploadDocId(uploadDocId); - verify(checkInServices, times(1)).read(testCheckinId); } @Test - void testDeleteFilesAdminCanAccess() throws IOException { + void testDeleteFilesAdminCanAccess() { String uploadDocId = "Some.Upload.Doc.Id"; - UUID testCheckinId = UUID.randomUUID(); - when(checkinDocumentServices.getFindByUploadDocId(uploadDocId)).thenReturn(testCd); - when(testCd.getCheckinsId()).thenReturn(testCheckinId); - when(currentUserServices.isAdmin()).thenReturn(true); - when(checkInServices.read(testCheckinId)).thenReturn(testCheckIn); - when(mockGoogleApiAccess.getDrive()).thenReturn(drive); - when(drive.files()).thenReturn(files); - when(files.update(eq(uploadDocId), any())).thenReturn(update); - when(update.setSupportsAllDrives(any())).thenReturn(update); + services.addFile(uploadDocId, new byte[0]); + CheckinDocument cd = createACustomCheckInDocument(checkIn, uploadDocId); - Boolean result = services.deleteFile(uploadDocId); + currentUserServices.roles = new ArrayList(); + currentUserServices.roles.add(RoleType.ADMIN); + // The CheckInsServicesImpl checks with the rolePermissionServices. + assignAdminRole(currentUserServices.currentUser); + Boolean result = services.deleteFile(uploadDocId); assertTrue(result); - verify(mockGoogleApiAccess, times(1)).getDrive(); - verify(checkinDocumentServices, times(1)).getFindByUploadDocId(uploadDocId); - verify(checkinDocumentServices, times(1)).deleteByUploadDocId(uploadDocId); - verify(checkInServices, times(1)).read(testCheckinId); } @Test void testDeleteFileWhenCheckinDocDoesntExist() { String uploadDocId = "Some.Upload.Doc.Id"; - UUID testCheckinId = UUID.randomUUID(); - when(checkinDocumentServices.getFindByUploadDocId(uploadDocId)).thenReturn(null); - final FileRetrievalException responseException = assertThrows(FileRetrievalException.class, () -> + // This exception actually comes from the CheckinDocumentServices. + final BadArgException responseException = assertThrows(BadArgException.class, () -> services.deleteFile(uploadDocId)); - assertEquals(String.format("Unable to find record with id %s", uploadDocId), responseException.getMessage()); - verify(checkinDocumentServices, times(1)).getFindByUploadDocId(uploadDocId); - verify(checkInServices, times(0)).read(testCheckinId); - verify(mockGoogleApiAccess, times(0)).getDrive(); - verify(checkinDocumentServices, times(0)).deleteByUploadDocId(uploadDocId); + assertEquals(String.format("CheckinDocument with document id %s does not exist", uploadDocId), responseException.getMessage()); } @Test - void testDeleteFileByUnauthorizedUser() throws IOException { + void testDeleteFileByUnauthorizedUser() { String uploadDocId = "Some.Upload.Doc.Id"; - UUID testCheckinId = UUID.randomUUID(); - when(checkinDocumentServices.getFindByUploadDocId(uploadDocId)).thenReturn(testCd); - when(testCd.getCheckinsId()).thenReturn(testCheckinId); - when(checkInServices.read(testCheckinId)).thenReturn(testCheckIn); - when(testCheckIn.getTeamMemberId()).thenReturn(UUID.randomUUID()); - when(currentUserServices.getCurrentUser()).thenReturn(testMemberProfile); - when(testMemberProfile.getId()).thenReturn(UUID.randomUUID()); - when(mockGoogleApiAccess.getDrive()).thenReturn(drive); - when(drive.files()).thenReturn(files); - when(files.delete(uploadDocId)).thenReturn(delete); + services.addFile(uploadDocId, new byte[0]); + CheckinDocument cd = createACustomCheckInDocument(checkIn, uploadDocId); - final FileRetrievalException responseException = assertThrows(FileRetrievalException.class, () -> + final BadArgException responseException = assertThrows(BadArgException.class, () -> services.deleteFile(uploadDocId)); assertEquals(NOT_AUTHORIZED_MSG, responseException.getMessage()); - verify(checkinDocumentServices, times(1)).getFindByUploadDocId(uploadDocId); - verify(checkInServices, times(1)).read(testCheckinId); - verify(mockGoogleApiAccess, times(0)).getDrive(); - verify(checkinDocumentServices, times(0)).deleteByUploadDocId(uploadDocId); } @Test - void testDeleteFileDriveCantConnect() { + void testDeleteFileThrowsException() { String uploadDocId = "Some.Upload.Doc.Id"; - UUID testCheckinId = UUID.randomUUID(); - UUID testMemberId = UUID.randomUUID(); - when(checkinDocumentServices.getFindByUploadDocId(uploadDocId)).thenReturn(testCd); - when(testCd.getCheckinsId()).thenReturn(testCheckinId); - when(checkInServices.read(testCheckinId)).thenReturn(testCheckIn); - when(testCheckIn.getTeamMemberId()).thenReturn(testMemberId); - when(currentUserServices.getCurrentUser()).thenReturn(testMemberProfile); - when(testMemberProfile.getId()).thenReturn(testMemberId); - when(mockGoogleApiAccess.getDrive()).thenReturn(null); + services.addFile(uploadDocId, new byte[0]); + CheckinDocument cd = createACustomCheckInDocument(checkIn, uploadDocId); - final FileRetrievalException responseException = assertThrows(FileRetrievalException.class, () -> - services.deleteFile(uploadDocId)); - - assertEquals("Unable to access Google Drive", responseException.getMessage()); - verify(checkinDocumentServices, times(1)).getFindByUploadDocId(uploadDocId); - verify(checkInServices, times(1)).read(testCheckinId); - verify(mockGoogleApiAccess, times(1)).getDrive(); - verify(checkinDocumentServices, times(0)).deleteByUploadDocId(uploadDocId); - } - - @Test - void testDeleteFileThrowsGoogleJsonResponseException() throws IOException { - - String uploadDocId = "Some.Upload.Doc.Id"; - UUID testCheckinId = UUID.randomUUID(); - GoogleJsonResponseException testException = GoogleJsonResponseExceptionFactoryTesting.newMock(jsonFactory, 404, null); - when(currentUserServices.isAdmin()).thenReturn(true); - when(checkinDocumentServices.getFindByUploadDocId(uploadDocId)).thenReturn(testCd); - when(testCd.getCheckinsId()).thenReturn(testCheckinId); - when(checkInServices.read(testCheckinId)).thenReturn(testCheckIn); - when(mockGoogleApiAccess.getDrive()).thenReturn(drive); - when(drive.files()).thenReturn(files); - when(files.update(eq(uploadDocId), any())).thenReturn(update); - when(update.setSupportsAllDrives(any())).thenReturn(update); - when(update.execute()).thenThrow(testException); + services.shouldThrow = true; //act + currentUserServices.currentUser = member; final FileRetrievalException responseException = assertThrows(FileRetrievalException.class, () -> services.deleteFile(uploadDocId)); - - //assert - assertEquals(testException.getMessage(), responseException.getMessage()); - verify(checkinDocumentServices, times(1)).getFindByUploadDocId(uploadDocId); - verify(checkInServices, times(1)).read(testCheckinId); - verify(mockGoogleApiAccess, times(1)).getDrive(); - verify(checkinDocumentServices, times(0)).deleteByUploadDocId(uploadDocId); } @Test - void testUploadFilesByCreatingNewDirectory() throws IOException { - - //arrange - String memberName = "testName"; - UUID testCheckinId = UUID.randomUUID(); - UUID testMemberId = UUID.randomUUID(); - - FileList fileList = new FileList(); - List mockFiles = new ArrayList<>(); - com.google.api.services.drive.model.File fileInService = new com.google.api.services.drive.model.File(); - fileInService.setId("some.id"); - fileInService.setName("some.file.name"); - mockFiles.add(fileInService); - fileList.setFiles(mockFiles); - - com.google.api.services.drive.model.File newFolderCreatedOnDrive = new com.google.api.services.drive.model.File(); - newFolderCreatedOnDrive.setName("new.directory.name"); - - com.google.api.services.drive.model.File fileFromDrive = new com.google.api.services.drive.model.File(); - fileFromDrive.setName("testFile"); - - Drive.Files.Create createForFileUpload = mock(Drive.Files.Create.class); - - when(fileToUpload.getFilename()).thenReturn("testFile"); - when(fileToUpload.getInputStream()).thenReturn(mockInputStream); - - when(checkInServices.read(testCheckinId)).thenReturn(testCheckIn); - when(testCheckIn.getTeamMemberId()).thenReturn(testMemberId); - when(testCheckIn.isCompleted()).thenReturn(false); - when(memberProfileServices.getById(any(UUID.class))).thenReturn(testMemberProfile); - when(currentUserServices.getCurrentUser()).thenReturn(testMemberProfile); - when(MemberProfileUtils.getFullName(testMemberProfile)).thenReturn(memberName); - when(testMemberProfile.getId()).thenReturn(testMemberId); - - when(mockGoogleApiAccess.getDrive()).thenReturn(drive); - when(drive.files()).thenReturn(files); - when(files.list()).thenReturn(list); - when(list.setFields(any(String.class))).thenReturn(list); - when(list.setSupportsAllDrives(any())).thenReturn(list); - when(list.setIncludeItemsFromAllDrives(any())).thenReturn(list); - when(list.setQ(any())).thenReturn(list); - when(list.setSpaces(any())).thenReturn(list); - when(list.execute()).thenReturn(fileList); - - when(files.create(any(com.google.api.services.drive.model.File.class))).thenReturn(create); - when(create.setSupportsAllDrives(any())).thenReturn(create); - when(create.execute()).thenReturn(newFolderCreatedOnDrive); - - when(files.create(any(com.google.api.services.drive.model.File.class), any(AbstractInputStreamContent.class))).thenReturn(createForFileUpload); - when(createForFileUpload.setSupportsAllDrives(true)).thenReturn(createForFileUpload); - when(createForFileUpload.setFields(any(String.class))).thenReturn(createForFileUpload); - when(createForFileUpload.execute()).thenReturn(fileFromDrive); + void testUploadFilesByCreatingNewDirectory() { + CompletedFileUpload fileToUpload = new SimpleUploadFile("file.name"); //act - FileInfoDTO result = services.uploadFile(testCheckinId, fileToUpload); + currentUserServices.currentUser = member; + FileInfoDTO result = services.uploadFile(checkIn.getId(), fileToUpload); //assert assertNotNull(result); assertEquals(fileToUpload.getFilename(), result.getName()); - verify(checkInServices, times(1)).read(testCheckinId); - verify(memberProfileServices, times(1)).getById(any(UUID.class)); - verify(mockGoogleApiAccess, times(1)).getDrive(); - verify(checkinDocumentServices, times(1)).save(any(CheckinDocument.class)); } @Test - void testUploadFilesToExistingDirectory() throws IOException { - //arrange - String memberName = "testName"; - UUID testCheckinId = UUID.randomUUID(); - UUID testMemberId = UUID.randomUUID(); - - FileList fileList = new FileList(); - List mockFiles = new ArrayList<>(); - com.google.api.services.drive.model.File fileInService = new com.google.api.services.drive.model.File(); - fileInService.setId("some.id"); - fileInService.setName(memberName.concat(LocalDate.now().toString())); - mockFiles.add(fileInService); - fileList.setFiles(mockFiles); - - com.google.api.services.drive.model.File fileFromDrive = new com.google.api.services.drive.model.File(); - fileFromDrive.setName("testFile"); - - when(fileToUpload.getFilename()).thenReturn("testFile"); - when(fileToUpload.getInputStream()).thenReturn(mockInputStream); - - when(checkInServices.read(testCheckinId)).thenReturn(testCheckIn); - when(testCheckIn.getTeamMemberId()).thenReturn(testMemberId); - when(testCheckIn.isCompleted()).thenReturn(false); - when(memberProfileServices.getById(any(UUID.class))).thenReturn(testMemberProfile); - when(currentUserServices.getCurrentUser()).thenReturn(testMemberProfile); - when(MemberProfileUtils.getFullName(testMemberProfile)).thenReturn(memberName); - when(testMemberProfile.getId()).thenReturn(testMemberId); - - when(mockGoogleApiAccess.getDrive()).thenReturn(drive); - when(drive.files()).thenReturn(files); - when(files.list()).thenReturn(list); - when(files.create(any(com.google.api.services.drive.model.File.class), any(AbstractInputStreamContent.class))).thenReturn(create); - when(files.create(any(com.google.api.services.drive.model.File.class))).thenReturn(create); - when(list.setFields(any(String.class))).thenReturn(list); - when(list.setSupportsAllDrives(any())).thenReturn(list); - when(list.setIncludeItemsFromAllDrives(any())).thenReturn(list); - when(list.setQ(any())).thenReturn(list); - when(list.setSpaces(any())).thenReturn(list); - when(list.execute()).thenReturn(fileList); - when(create.setSupportsAllDrives(true)).thenReturn(create); - when(create.setFields(any(String.class))).thenReturn(create); - when(create.execute()).thenReturn(fileFromDrive); - - //act - FileInfoDTO result = services.uploadFile(testCheckinId, fileToUpload); + void testFileUploadForAdminByUploadingFileToExistingDirectory() { + CompletedFileUpload fileToUpload = new SimpleUploadFile("file.name"); - //assert - assertNotNull(result); - assertEquals(fileToUpload.getFilename(), result.getName()); - verify(checkInServices, times(1)).read(testCheckinId); - verify(memberProfileServices, times(1)).getById(any(UUID.class)); - verify(mockGoogleApiAccess, times(1)).getDrive(); - verify(checkinDocumentServices, times(1)).save(any(CheckinDocument.class)); - } - - @Test - void testFileUploadForAdminByUploadingFileToExistingDirectory() throws IOException { - //arrange - String memberName = "testName"; - UUID testCheckinId = UUID.randomUUID(); - UUID testMemberId = UUID.randomUUID(); - - FileList fileList = new FileList(); - List mockFiles = new ArrayList<>(); - com.google.api.services.drive.model.File fileInService = new com.google.api.services.drive.model.File(); - fileInService.setId("some.id"); - fileInService.setName(memberName.concat(LocalDate.now().toString())); - mockFiles.add(fileInService); - fileList.setFiles(mockFiles); - - com.google.api.services.drive.model.File fileFromDrive = new com.google.api.services.drive.model.File(); - fileFromDrive.setName("testFile"); - - when(currentUserServices.isAdmin()).thenReturn(true); - when(fileToUpload.getFilename()).thenReturn("testFile"); - when(fileToUpload.getInputStream()).thenReturn(mockInputStream); - - when(checkInServices.read(testCheckinId)).thenReturn(testCheckIn); - when(testCheckIn.getTeamMemberId()).thenReturn(testMemberId); - when(memberProfileServices.getById(any(UUID.class))).thenReturn(testMemberProfile); - when(MemberProfileUtils.getFullName(testMemberProfile)).thenReturn(memberName); - - when(mockGoogleApiAccess.getDrive()).thenReturn(drive); - when(drive.files()).thenReturn(files); - when(files.list()).thenReturn(list); - when(files.create(any(com.google.api.services.drive.model.File.class), any(AbstractInputStreamContent.class))).thenReturn(create); - when(files.create(any(com.google.api.services.drive.model.File.class))).thenReturn(create); - when(list.setFields(any(String.class))).thenReturn(list); - when(list.setSupportsAllDrives(any())).thenReturn(list); - when(list.setIncludeItemsFromAllDrives(any())).thenReturn(list); - when(list.setQ(any())).thenReturn(list); - when(list.setSpaces(any())).thenReturn(list); - when(list.execute()).thenReturn(fileList); - when(create.setSupportsAllDrives(true)).thenReturn(create); - when(create.setFields(any(String.class))).thenReturn(create); - when(create.execute()).thenReturn(fileFromDrive); + currentUserServices.roles = new ArrayList(); + currentUserServices.roles.add(RoleType.ADMIN); + // The CheckInsServicesImpl checks with the rolePermissionServices. + assignAdminRole(currentUserServices.currentUser); //act - FileInfoDTO result = services.uploadFile(testCheckinId, fileToUpload); + FileInfoDTO result = services.uploadFile(checkIn.getId(), fileToUpload); //assert assertNotNull(result); assertEquals(fileToUpload.getFilename(), result.getName()); - verify(checkInServices, times(1)).read(testCheckinId); - verify(memberProfileServices, times(1)).getById(any(UUID.class)); - verify(mockGoogleApiAccess, times(1)).getDrive(); - verify(checkinDocumentServices, times(1)).save(any(CheckinDocument.class)); } @Test void testUploadFileThrowsErrorWhenFileNameIsEmpty() { - when(fileToUpload.getFilename()).thenReturn(null); + CompletedFileUpload fileToUpload = new SimpleUploadFile(null); + currentUserServices.currentUser = member; final FileRetrievalException responseException = assertThrows(FileRetrievalException.class, () -> - services.uploadFile(UUID.randomUUID(), fileToUpload)); + services.uploadFile(checkIn.getId(), fileToUpload)); assertEquals("Please select a valid file before uploading.", responseException.getMessage()); } @Test void testUploadFileThrowsErrorForInvalidCheckinId() { + CompletedFileUpload fileToUpload = new SimpleUploadFile("file.name"); UUID testCheckinId = UUID.randomUUID(); - when(fileToUpload.getFilename()).thenReturn("test.file.name"); - when(checkInServices.read(testCheckinId)).thenReturn(null); - - final FileRetrievalException responseException = assertThrows(FileRetrievalException.class, () -> - services.uploadFile(testCheckinId, fileToUpload)); - - assertEquals(String.format("Unable to find checkin record with id %s", testCheckinId), responseException.getMessage()); - } - - @Test - void testUploadFileUnauthorizedUser() throws IOException { - UUID testCheckinId = UUID.randomUUID(); - - when(fileToUpload.getFilename()).thenReturn("testFile"); - when(fileToUpload.getInputStream()).thenReturn(mockInputStream); - - when(checkInServices.read(testCheckinId)).thenReturn(testCheckIn); - when(testCheckIn.getTeamMemberId()).thenReturn(UUID.randomUUID()); - when(currentUserServices.getCurrentUser()).thenReturn(testMemberProfile); - when(testMemberProfile.getId()).thenReturn(UUID.randomUUID()); - - final FileRetrievalException responseException = assertThrows(FileRetrievalException.class, () -> + // This exception actually comes from the CheckinDocumentServices. + final NotFoundException responseException = assertThrows(NotFoundException.class, () -> services.uploadFile(testCheckinId, fileToUpload)); - assertEquals("You are not authorized to perform this operation", responseException.getMessage()); - verify(checkInServices, times(1)).read(testCheckinId); - verify(memberProfileServices, times(0)).getById(any(UUID.class)); - verify(mockGoogleApiAccess, times(0)).getDrive(); - verify(checkinDocumentServices, times(0)).save(any()); + assertEquals(String.format("Checkin not found by Id: %s.", testCheckinId), responseException.getMessage()); } @Test - void testUploadFileThrowsErrorIfCheckinIsComplete() throws IOException { - UUID testCheckinId = UUID.randomUUID(); - UUID testMemberId = UUID.randomUUID(); + void testUploadFileUnauthorizedUser() { + CompletedFileUpload fileToUpload = new SimpleUploadFile("file.name"); - when(fileToUpload.getFilename()).thenReturn("testFile"); - when(fileToUpload.getInputStream()).thenReturn(mockInputStream); - when(checkInServices.read(testCheckinId)).thenReturn(testCheckIn); - when(currentUserServices.getCurrentUser()).thenReturn(testMemberProfile); - when(testMemberProfile.getId()).thenReturn(testMemberId); - when(testCheckIn.getTeamMemberId()).thenReturn(testMemberId); - when(testCheckIn.isCompleted()).thenReturn(true); - - final FileRetrievalException responseException = assertThrows(FileRetrievalException.class, () -> - services.uploadFile(testCheckinId, fileToUpload)); + final BadArgException responseException = assertThrows(BadArgException.class, () -> + services.uploadFile(checkIn.getId(), fileToUpload)); assertEquals(NOT_AUTHORIZED_MSG, responseException.getMessage()); - verify(checkInServices, times(1)).read(testCheckinId); - verify(memberProfileServices, times(0)).getById(any(UUID.class)); - verify(mockGoogleApiAccess, times(0)).getDrive(); - verify(checkinDocumentServices, times(0)).save(any()); } @Test - void testUploadFileDriveCantConnect() throws IOException { - UUID testCheckinId = UUID.randomUUID(); - UUID testMemberId = UUID.randomUUID(); - - when(currentUserServices.isAdmin()).thenReturn(true); - when(fileToUpload.getFilename()).thenReturn("testFile"); - when(fileToUpload.getInputStream()).thenReturn(mockInputStream); - when(checkInServices.read(testCheckinId)).thenReturn(testCheckIn); - when(testCheckIn.getTeamMemberId()).thenReturn(testMemberId); - when(memberProfileServices.getById(testMemberId)).thenReturn(testMemberProfile); - when(MemberProfileUtils.getFullName(testMemberProfile)).thenReturn("test.name"); - when(mockGoogleApiAccess.getDrive()).thenReturn(null); + void testUploadFileThrowsErrorIfCheckinIsComplete() { + CheckIn completed = createACompletedCheckIn(member, pdl); + CompletedFileUpload fileToUpload = new SimpleUploadFile("file.name"); + currentUserServices.currentUser = member; final FileRetrievalException responseException = assertThrows(FileRetrievalException.class, () -> - services.uploadFile(testCheckinId, fileToUpload)); + services.uploadFile(completed.getId(), fileToUpload)); - assertEquals("Unable to access Google Drive", responseException.getMessage()); - verify(checkInServices, times(1)).read(testCheckinId); - verify(memberProfileServices, times(1)).getById(any(UUID.class)); - verify(mockGoogleApiAccess, times(1)).getDrive(); - verify(checkinDocumentServices, times(0)).save(any()); - } - - @Test - void testUploadFileThrowsIOException() throws IOException { - UUID testCheckinId = UUID.randomUUID(); - UUID testMemberId = UUID.randomUUID(); - - when(currentUserServices.isAdmin()).thenReturn(true); - when(fileToUpload.getFilename()).thenReturn("testFile"); - when(fileToUpload.getInputStream()).thenReturn(mockInputStream); - - when(checkInServices.read(testCheckinId)).thenReturn(testCheckIn); - when(testCheckIn.getTeamMemberId()).thenReturn(testMemberId); - when(memberProfileServices.getById(testMemberId)).thenReturn(testMemberProfile); - when(MemberProfileUtils.getFullName(testMemberProfile)).thenReturn("test.name"); - when(mockGoogleApiAccess.getDrive()).thenReturn(drive); - when(drive.files()).thenReturn(files); - when(files.list()).thenReturn(list); - when(list.setFields(any(String.class))).thenReturn(list); - when(list.setSupportsAllDrives(any())).thenReturn(list); - when(list.setIncludeItemsFromAllDrives(any())).thenReturn(list); - when(list.setQ(any())).thenReturn(list); - when(list.setSpaces(any())).thenReturn(list); - when(list.execute()).thenThrow(IOException.class); - - assertThrows(FileRetrievalException.class, () -> services.uploadFile(testCheckinId, fileToUpload)); - verify(checkInServices, times(1)).read(testCheckinId); - verify(memberProfileServices, times(1)).getById(any(UUID.class)); - verify(mockGoogleApiAccess, times(1)).getDrive(); - verify(checkinDocumentServices, times(0)).save(any()); + assertEquals(NOT_AUTHORIZED_MSG, responseException.getMessage()); } @Test - void testUploadFileThrowsGoogleJsonResponseException() throws IOException { - //arrange - GoogleJsonResponseException testException = GoogleJsonResponseExceptionFactoryTesting.newMock(jsonFactory, 404, null); - String memberName = "testName"; - UUID testCheckinId = UUID.randomUUID(); + void testUploadFileThrowsException() { + services.shouldThrow = true; + CompletedFileUpload fileToUpload = new SimpleUploadFile("file.name"); - when(currentUserServices.isAdmin()).thenReturn(true); - when(fileToUpload.getFilename()).thenReturn("testFile"); - when(fileToUpload.getInputStream()).thenReturn(mockInputStream); - - when(checkInServices.read(testCheckinId)).thenReturn(testCheckIn); - when(testCheckIn.getTeamMemberId()).thenReturn(UUID.randomUUID()); - when(memberProfileServices.getById(any(UUID.class))).thenReturn(testMemberProfile); - when(MemberProfileUtils.getFullName(testMemberProfile)).thenReturn(memberName); - - when(mockGoogleApiAccess.getDrive()).thenReturn(drive); - when(drive.files()).thenReturn(files); - when(files.list()).thenReturn(list); - when(list.setFields(any(String.class))).thenReturn(list); - when(list.setSupportsAllDrives(any())).thenReturn(list); - when(list.setIncludeItemsFromAllDrives(any())).thenReturn(list); - when(list.setQ(any())).thenReturn(list); - when(list.setSpaces(any())).thenReturn(list); - when(list.execute()).thenThrow(testException); - - //act - final FileRetrievalException responseException = assertThrows(FileRetrievalException.class, - () -> services.uploadFile(testCheckinId, fileToUpload)); - - //assert - assertEquals(testException.getMessage(), responseException.getMessage()); - verify(mockGoogleApiAccess, times(1)).getDrive(); - verify(checkInServices, times(1)).read(testCheckinId); - verify(memberProfileServices, times(1)).getById(any()); - verify(checkinDocumentServices, times(0)).save(any()); + currentUserServices.currentUser = member; + assertThrows(FileRetrievalException.class, () -> services.uploadFile(checkIn.getId(), fileToUpload)); } } diff --git a/server/src/test/java/com/objectcomputing/checkins/services/guild/GuildTest.java b/server/src/test/java/com/objectcomputing/checkins/services/guild/GuildTest.java index 4a0e27b4b0..deae25f885 100644 --- a/server/src/test/java/com/objectcomputing/checkins/services/guild/GuildTest.java +++ b/server/src/test/java/com/objectcomputing/checkins/services/guild/GuildTest.java @@ -5,9 +5,13 @@ import com.objectcomputing.checkins.exceptions.PermissionException; import com.objectcomputing.checkins.notifications.email.MailJetFactory; import com.objectcomputing.checkins.services.MailJetFactoryReplacement; +import com.objectcomputing.checkins.services.role.RoleType; +import com.objectcomputing.checkins.services.CurrentUserServicesReplacement; +import com.objectcomputing.checkins.services.fixture.MemberProfileFixture; import com.objectcomputing.checkins.services.TestContainersSuite; import com.objectcomputing.checkins.services.guild.member.GuildMember; import com.objectcomputing.checkins.services.guild.member.GuildMemberHistoryRepository; +import com.objectcomputing.checkins.services.guild.member.GuildMemberResponseDTO; import com.objectcomputing.checkins.services.guild.member.GuildMemberRepository; import com.objectcomputing.checkins.services.guild.member.GuildMemberServices; import com.objectcomputing.checkins.services.memberprofile.MemberProfile; @@ -23,14 +27,13 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.condition.DisabledInNativeImage; -import org.mockito.Mockito; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.List; +import java.util.ArrayList; import java.util.Optional; import java.util.Set; import java.util.UUID; @@ -39,22 +42,18 @@ import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.mockito.Mockito.any; -import static org.mockito.Mockito.doThrow; -import static org.mockito.Mockito.never; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; @Property(name = "replace.mailjet.factory", value = StringUtils.TRUE) -// Disabled in nativeTest, as we get an exception from Mockito -// => java.lang.NoClassDefFoundError: Could not initialize class org.mockito.Mockito -@DisabledInNativeImage -class GuildTest extends TestContainersSuite { +@Property(name = "replace.currentuserservices", value = StringUtils.TRUE) +class GuildTest extends TestContainersSuite + implements MemberProfileFixture { @Inject private Validator validator; + @Inject + private CurrentUserServicesReplacement currentUserServices; + @Inject private CheckInsConfiguration checkInsConfiguration; @@ -62,39 +61,12 @@ class GuildTest extends TestContainersSuite { @Named(MailJetFactory.HTML_FORMAT) private MailJetFactoryReplacement.MockEmailSender emailSender; - private GuildRepository guildsRepo; - private GuildMemberRepository guildMemberRepo; - private GuildMemberHistoryRepository guildMemberHistoryRepo; - private CurrentUserServices currentUserServices; - private MemberProfileServices memberProfileServices; - private GuildMemberServices guildMemberServices; - private Environment environment; + @Inject private GuildServicesImpl guildServices; @BeforeEach - @Tag("mocked") void setUp() { - guildsRepo = Mockito.mock(GuildRepository.class); - guildMemberRepo = Mockito.mock(GuildMemberRepository.class); - guildMemberHistoryRepo = Mockito.mock(GuildMemberHistoryRepository.class); - currentUserServices = Mockito.mock(CurrentUserServices.class); - memberProfileServices = Mockito.mock(MemberProfileServices.class); - guildMemberServices = Mockito.mock(GuildMemberServices.class); - environment = Mockito.mock(Environment.class); - emailSender.reset(); - - guildServices = Mockito.spy(new GuildServicesImpl( - guildsRepo, - guildMemberRepo, - guildMemberHistoryRepo, - currentUserServices, - memberProfileServices, - guildMemberServices, - emailSender, - environment, - checkInsConfiguration) - ); } @Test @@ -182,8 +154,8 @@ void testToString() { assertTrue(s.contains(description)); assertTrue(s.contains(isCommunity)); } + @Test - @Tag("mocked") void testEmailGuildLeadersWithValidGuild() { Set guildLeadersEmails = new HashSet<>(); guildLeadersEmails.add("leader1@example.com"); @@ -204,7 +176,6 @@ void testEmailGuildLeadersWithValidGuild() { } @Test - @Tag("mocked") void testEmailGuildLeadersWithNullGuild() { Set guildLeadersEmails = new HashSet<>(); guildLeadersEmails.add("leader1@example.com"); @@ -215,7 +186,6 @@ void testEmailGuildLeadersWithNullGuild() { } @Test - @Tag("mocked") void testEmailGuildLeadersWithNullGuildName() { Set guildLeadersEmails = new HashSet<>(); guildLeadersEmails.add("leader1@example.com"); @@ -228,7 +198,6 @@ void testEmailGuildLeadersWithNullGuildName() { } @Test - @Tag("mocked") void testEmailGuildLeadersWithEmptyGuildName() { Set guildLeadersEmails = new HashSet<>(); guildLeadersEmails.add("leader1@example.com"); @@ -241,9 +210,7 @@ void testEmailGuildLeadersWithEmptyGuildName() { assertEquals(0, emailSender.events.size()); } - @Test - @Tag("mocked") void testSaveGuildWithValidData() { GuildCreateDTO guildDTO = new GuildCreateDTO(); guildDTO.setName("Test Guild"); @@ -251,48 +218,57 @@ void testSaveGuildWithValidData() { guildDTO.setLink(link); guildDTO.setCommunity(true); - UUID memberId = UUID.randomUUID(); + MemberProfile memberProfile = createADefaultMemberProfile(); GuildCreateDTO.GuildMemberCreateDTO guildMemberCreateDTO = new GuildCreateDTO.GuildMemberCreateDTO(); - guildMemberCreateDTO.setMemberId(memberId); + guildMemberCreateDTO.setMemberId(memberProfile.getId()); guildMemberCreateDTO.setLead(true); guildDTO.setGuildMembers(Collections.singletonList(guildMemberCreateDTO)); - MemberProfile memberProfile = new MemberProfile(); - memberProfile.setWorkEmail("test@example.com"); - - Guild guild = new Guild(UUID.randomUUID(), "test", "example", link, true, true); - when(guildsRepo.search(any(), any())).thenReturn(Collections.emptyList()); - when(guildsRepo.save(any())).thenReturn(guild); - when(memberProfileServices.getById(any())).thenReturn(memberProfile); - when(guildMemberRepo.save(any())).thenReturn(new GuildMember(UUID.randomUUID(), guild.getId(), guildMemberCreateDTO.getMemberId(), true)); GuildResponseDTO response = guildServices.save(guildDTO); - verify(guildsRepo, times(1)).save(any()); - assertEquals(1, emailSender.events.size()); assertEquals( - List.of("SEND_EMAIL", "null", "null", "You have been assigned as a guild leader of test", "Congratulations, you have been assigned as a guild leader of test", memberProfile.getWorkEmail()), + List.of("SEND_EMAIL", "null", "null", + String.format("You have been assigned as a guild leader of %s", guildDTO.getName()), + String.format("Congratulations, you have been assigned as a guild leader of %s", guildDTO.getName()), + memberProfile.getWorkEmail()), emailSender.events.getFirst() ); } + + GuildResponseDTO createGuild(String name, MemberProfile lead) { + GuildCreateDTO guildDTO = new GuildCreateDTO(); + guildDTO.setName(name); + GuildCreateDTO.GuildMemberCreateDTO guildMemberCreateDTO = new GuildCreateDTO.GuildMemberCreateDTO(); + guildMemberCreateDTO.setMemberId(lead.getId()); + guildMemberCreateDTO.setLead(true); + List members = new ArrayList<>(); + members.add(guildMemberCreateDTO); + guildDTO.setGuildMembers(members); + return guildServices.save(guildDTO); + } + @Test - @Tag("mocked") void testSaveGuildWithExistingName() { - GuildCreateDTO guildDTO = new GuildCreateDTO(); - guildDTO.setName("Existing Guild"); + MemberProfile memberProfile = createADefaultMemberProfile(); + createGuild("Existing Guild", memberProfile); + emailSender.reset(); - when(guildsRepo.search(any(), any())).thenReturn(Collections.singletonList(new Guild())); + GuildCreateDTO existingGuildDTO = new GuildCreateDTO(); + existingGuildDTO.setName("Existing Guild"); + GuildCreateDTO.GuildMemberCreateDTO guildMemberCreateDTO = new GuildCreateDTO.GuildMemberCreateDTO(); + guildMemberCreateDTO.setMemberId(memberProfile.getId()); + guildMemberCreateDTO.setLead(true); + existingGuildDTO.setGuildMembers(Collections.singletonList(guildMemberCreateDTO)); - assertThrows(BadArgException.class, () -> guildServices.save(guildDTO)); + assertThrows(BadArgException.class, () -> guildServices.save(existingGuildDTO)); - verify(guildsRepo, never()).save(any()); assertEquals(0, emailSender.events.size()); } @Test - @Tag("mocked") void testSaveGuildWithoutLead() { GuildCreateDTO guildDTO = new GuildCreateDTO(); guildDTO.setName("Test Guild"); @@ -303,86 +279,59 @@ void testSaveGuildWithoutLead() { guildDTO.setGuildMembers(Collections.singletonList(guildMemberCreateDTO)); - when(guildsRepo.search(any(), any())).thenReturn(Collections.emptyList()); - assertThrows(BadArgException.class, () -> guildServices.save(guildDTO)); - - verify(guildsRepo, never()).save(any()); assertEquals(0, emailSender.events.size()); } @Test - @Tag("mocked") void testSaveGuildWithNullDTO() { - GuildResponseDTO response = guildServices.save(null); - - verify(guildsRepo, never()).save(any()); assertEquals(0, emailSender.events.size()); } @Test - @Tag("mocked") void testUpdateGuildWithNewGuildLeaders() { - UUID guildId = UUID.randomUUID(); + // Create members involved. + MemberProfile currentUser = createADefaultMemberProfile(); + MemberProfile memberProfile1 = createASecondDefaultMemberProfile(); + MemberProfile memberProfile2 = createAThirdDefaultMemberProfile(); + + // Create the existing guild with member1 as lead. + GuildResponseDTO existing = createGuild("Test Guild", memberProfile1); + emailSender.reset(); + + // Create an update DTO. + UUID guildId = existing.getId(); GuildUpdateDTO guildDTO = new GuildUpdateDTO(); + guildDTO.setName(existing.getName()); guildDTO.setId(guildId); guildDTO.setLink("http://example.com"); - UUID currentUserId = UUID.randomUUID(); - UUID memberId1 = UUID.randomUUID(); - UUID memberId2 = UUID.randomUUID(); - + // Set the new guild members + List exMembers = existing.getGuildMembers(); GuildUpdateDTO.GuildMemberUpdateDTO guildMember1 = new GuildUpdateDTO.GuildMemberUpdateDTO(); - guildMember1.setMemberId(memberId1); - guildMember1.setLead(true); + guildMember1.setId(exMembers.get(0).getId()); + guildMember1.setMemberId(exMembers.get(0).getMemberId()); + guildMember1.setLead(exMembers.get(0).getLead()); GuildUpdateDTO.GuildMemberUpdateDTO guildMember2 = new GuildUpdateDTO.GuildMemberUpdateDTO(); - guildMember2.setMemberId(memberId2); + guildMember2.setMemberId(memberProfile2.getId()); guildMember2.setLead(true); guildDTO.setGuildMembers(Arrays.asList(guildMember1, guildMember2)); - MemberProfile currentUser = new MemberProfile(); - currentUser.setId(currentUserId); - - MemberProfile memberProfile1 = new MemberProfile(); - memberProfile1.setWorkEmail("leader1@example.com"); - - MemberProfile memberProfile2 = new MemberProfile(); - memberProfile2.setWorkEmail("leader2@example.com"); - - Guild existingGuild = new Guild(); - existingGuild.setId(guildId); - existingGuild.setName("Test Guild"); - - GuildMember existingGuildMember = new GuildMember(); - existingGuildMember.setMemberId(memberId1); - existingGuildMember.setLead(true); - - GuildMember newGuildLeader = new GuildMember(); - newGuildLeader.setMemberId(memberId2); - newGuildLeader.setLead(true); - - when(currentUserServices.getCurrentUser()).thenReturn(currentUser); - when(currentUserServices.isAdmin()).thenReturn(true); - when(guildsRepo.findById(guildId)).thenReturn(Optional.of(existingGuild)); - when(guildsRepo.update(any())).thenReturn(existingGuild); - when(memberProfileServices.getById(memberId1)).thenReturn(memberProfile1); - when(memberProfileServices.getById(memberId2)).thenReturn(memberProfile2); - - Set initialGuildLeaders = Collections.singleton(existingGuildMember); - Set updatedGuildLeaders = new HashSet<>(initialGuildLeaders); - updatedGuildLeaders.add(newGuildLeader); - when(guildMemberServices.findByFields(guildId, null, true)) - .thenReturn(initialGuildLeaders) - .thenReturn(updatedGuildLeaders); + // We need the current user to be logged in and admin. + currentUserServices.currentUser = currentUser; + currentUserServices.roles = new ArrayList(); + currentUserServices.roles.add(RoleType.ADMIN); GuildResponseDTO response = guildServices.update(guildDTO); - assertEquals(2, emailSender.events.size()); assertEquals( - List.of("SEND_EMAIL", "null", "null", "Membership Changes have been made to the Test Guild guild", "

Changes have been made to the Test Guild guild.

The following members have been added:

  • null null
  • null null
Click here to view the changes in the Check-Ins app.", memberProfile1.getWorkEmail()), + List.of("SEND_EMAIL", "null", "null", + "Membership Changes have been made to the Test Guild guild", + String.format("

Changes have been made to the Test Guild guild.

The following members have been added:

  • %s %s
Click here to view the changes in the Check-Ins app.", memberProfile2.getFirstName(), memberProfile2.getLastName()), + memberProfile1.getWorkEmail()), emailSender.events.get(0) ); assertEquals( @@ -392,69 +341,62 @@ void testUpdateGuildWithNewGuildLeaders() { } @Test - @Tag("mocked") void testUpdateGuildWithNoNewGuildLeaders() { - UUID guildId = UUID.randomUUID(); + // Create members involved. + MemberProfile currentUser = createADefaultMemberProfile(); + MemberProfile memberProfile1 = createASecondDefaultMemberProfile(); + MemberProfile memberProfile2 = createAThirdDefaultMemberProfile(); + + // Create the existing guild with member1 as lead. + GuildResponseDTO existing = createGuild("Test Guild", memberProfile1); + emailSender.reset(); + + // Create an update DTO. + UUID guildId = existing.getId(); GuildUpdateDTO guildDTO = new GuildUpdateDTO(); + guildDTO.setName(existing.getName()); guildDTO.setId(guildId); guildDTO.setLink("http://example.com"); - UUID currentUserId = UUID.randomUUID(); - UUID memberId1 = UUID.randomUUID(); - UUID memberId2 = UUID.randomUUID(); - + // Set the new guild members + List exMembers = existing.getGuildMembers(); GuildUpdateDTO.GuildMemberUpdateDTO guildMember1 = new GuildUpdateDTO.GuildMemberUpdateDTO(); - guildMember1.setMemberId(memberId1); - guildMember1.setLead(true); + guildMember1.setId(exMembers.get(0).getId()); + guildMember1.setMemberId(exMembers.get(0).getMemberId()); + guildMember1.setLead(exMembers.get(0).getLead()); GuildUpdateDTO.GuildMemberUpdateDTO guildMember2 = new GuildUpdateDTO.GuildMemberUpdateDTO(); - guildMember2.setMemberId(memberId2); - guildMember2.setLead(true); + guildMember2.setMemberId(memberProfile2.getId()); + guildMember2.setLead(false); guildDTO.setGuildMembers(Arrays.asList(guildMember1, guildMember2)); - MemberProfile currentUser = new MemberProfile(); - currentUser.setId(currentUserId); - - MemberProfile memberProfile1 = new MemberProfile(); - memberProfile1.setWorkEmail("leader1@example.com"); - - MemberProfile memberProfile2 = new MemberProfile(); - memberProfile2.setWorkEmail("leader2@example.com"); - - Guild existingGuild = new Guild(); - existingGuild.setId(guildId); - existingGuild.setName("Test Guild"); - - GuildMember existingGuildMember = new GuildMember(); - existingGuildMember.setMemberId(memberId1); - existingGuildMember.setLead(true); - - when(currentUserServices.getCurrentUser()).thenReturn(currentUser); - when(currentUserServices.isAdmin()).thenReturn(true); - when(guildsRepo.findById(guildId)).thenReturn(Optional.of(existingGuild)); - when(guildsRepo.update(any())).thenReturn(existingGuild); - when(memberProfileServices.getById(memberId1)).thenReturn(memberProfile1); - when(memberProfileServices.getById(memberId2)).thenReturn(memberProfile2); - when(guildMemberServices.findByFields(guildId, null, true)).thenReturn(Collections.singleton(existingGuildMember)); - - guildServices.update(guildDTO); + // We need the current user to be logged in and admin. + currentUserServices.currentUser = currentUser; + currentUserServices.roles = new ArrayList(); + currentUserServices.roles.add(RoleType.ADMIN); + GuildResponseDTO response = guildServices.update(guildDTO); assertEquals(1, emailSender.events.size()); assertEquals( - List.of("SEND_EMAIL", "null", "null", "Membership Changes have been made to the Test Guild guild", "

Changes have been made to the Test Guild guild.

The following members have been added:

  • null null
  • null null
Click here to view the changes in the Check-Ins app.", memberProfile1.getWorkEmail()), + List.of("SEND_EMAIL", "null", "null", + "Membership Changes have been made to the Test Guild guild", + String.format("

Changes have been made to the Test Guild guild.

The following members have been added:

  • %s %s
Click here to view the changes in the Check-Ins app.", memberProfile2.getFirstName(), memberProfile2.getLastName()), + memberProfile1.getWorkEmail()), emailSender.events.getFirst() ); } @Test - @Tag("mocked") void testUpdateGuildWithNonExistentGuildId() { GuildUpdateDTO guildDTO = new GuildUpdateDTO(); guildDTO.setId(UUID.randomUUID()); // Non-existent Guild ID - when(currentUserServices.isAdmin()).thenReturn(true); - when(guildsRepo.findById(guildDTO.getId())).thenReturn(Optional.empty()); + // We need the current user to be logged in and admin. + MemberProfile currentUser = createADefaultMemberProfile(); + currentUserServices.currentUser = currentUser; + currentUserServices.roles = new ArrayList(); + currentUserServices.roles.add(RoleType.ADMIN); assertThrows(BadArgException.class, () -> guildServices.update(guildDTO)); @@ -462,18 +404,21 @@ void testUpdateGuildWithNonExistentGuildId() { } @Test - @Tag("mocked") void testUpdateGuildWithNoGuildLeads() { + // Create the existing guild with member1 as lead. + MemberProfile memberProfile1 = createASecondDefaultMemberProfile(); + GuildResponseDTO existing = createGuild("Test Guild", memberProfile1); + emailSender.reset(); + GuildUpdateDTO guildDTO = new GuildUpdateDTO(); - guildDTO.setId(UUID.randomUUID()); + guildDTO.setId(existing.getId()); guildDTO.setGuildMembers(Collections.emptyList()); // No guild members - Guild existingGuild = new Guild(); - existingGuild.setId(guildDTO.getId()); - existingGuild.setName("Test Guild"); - - when(currentUserServices.isAdmin()).thenReturn(true); - when(guildsRepo.findById(guildDTO.getId())).thenReturn(Optional.of(existingGuild)); + // We need the current user to be logged in and admin. + MemberProfile currentUser = createADefaultMemberProfile(); + currentUserServices.currentUser = currentUser; + currentUserServices.roles = new ArrayList(); + currentUserServices.roles.add(RoleType.ADMIN); assertThrows(BadArgException.class, () -> guildServices.update(guildDTO)); @@ -481,17 +426,19 @@ void testUpdateGuildWithNoGuildLeads() { } @Test - @Tag("mocked") void testUpdateGuildWithUnauthorizedUser() { - GuildUpdateDTO guildDTO = new GuildUpdateDTO(); - guildDTO.setId(UUID.randomUUID()); + // Create the existing guild with member1 as lead. + MemberProfile memberProfile1 = createASecondDefaultMemberProfile(); + GuildResponseDTO existing = createGuild("Test Guild", memberProfile1); + emailSender.reset(); - MemberProfile currentUser = new MemberProfile(); - currentUser.setId(UUID.randomUUID()); + GuildUpdateDTO guildDTO = new GuildUpdateDTO(); + guildDTO.setId(existing.getId()); + guildDTO.setGuildMembers(Collections.emptyList()); // No guild members - when(currentUserServices.getCurrentUser()).thenReturn(currentUser); - when(currentUserServices.isAdmin()).thenReturn(false); - when(guildMemberServices.findByFields(guildDTO.getId(), currentUser.getId(), true)).thenReturn(new HashSet<>()); + // We need the current user to be logged in and *not* admin. + MemberProfile currentUser = createADefaultMemberProfile(); + currentUserServices.currentUser = currentUser; assertThrows(PermissionException.class, () -> { guildServices.update(guildDTO); @@ -501,20 +448,21 @@ void testUpdateGuildWithUnauthorizedUser() { } @Test - @Tag("mocked") void testUpdateGuildWithInvalidLink() { + // Create the existing guild with member1 as lead. + MemberProfile memberProfile1 = createASecondDefaultMemberProfile(); + GuildResponseDTO existing = createGuild("Test Guild", memberProfile1); + emailSender.reset(); + GuildUpdateDTO guildDTO = new GuildUpdateDTO(); - guildDTO.setId(UUID.randomUUID()); + guildDTO.setId(existing.getId()); guildDTO.setLink("invalid-link"); // Invalid link - Guild existingGuild = new Guild(); - existingGuild.setId(guildDTO.getId()); - existingGuild.setName("Test Guild"); - - when(currentUserServices.isAdmin()).thenReturn(true); - when(guildsRepo.findById(guildDTO.getId())).thenReturn(Optional.of(existingGuild)); - - doThrow(new BadArgException("Invalid link")).when(guildServices).validateLink("invalid-link"); + // We need the current user to be logged in and admin. + MemberProfile currentUser = createADefaultMemberProfile(); + currentUserServices.currentUser = currentUser; + currentUserServices.roles = new ArrayList(); + currentUserServices.roles.add(RoleType.ADMIN); assertThrows(BadArgException.class, () -> guildServices.update(guildDTO)); diff --git a/server/src/test/java/com/objectcomputing/checkins/services/member_skill/MemberSkillServiceImplTest.java b/server/src/test/java/com/objectcomputing/checkins/services/member_skill/MemberSkillServiceImplTest.java index ea5ef37802..153914a566 100644 --- a/server/src/test/java/com/objectcomputing/checkins/services/member_skill/MemberSkillServiceImplTest.java +++ b/server/src/test/java/com/objectcomputing/checkins/services/member_skill/MemberSkillServiceImplTest.java @@ -3,102 +3,54 @@ import com.objectcomputing.checkins.exceptions.AlreadyExistsException; import com.objectcomputing.checkins.exceptions.BadArgException; import com.objectcomputing.checkins.services.TestContainersSuite; +import com.objectcomputing.checkins.services.fixture.SkillFixture; +import com.objectcomputing.checkins.services.fixture.MemberProfileFixture; +import com.objectcomputing.checkins.services.fixture.MemberSkillFixture; import com.objectcomputing.checkins.services.memberprofile.MemberProfile; import com.objectcomputing.checkins.services.memberprofile.MemberProfileRepository; import com.objectcomputing.checkins.services.skills.Skill; +import com.objectcomputing.checkins.services.member_skill.skillsreport.SkillLevel; import com.objectcomputing.checkins.services.skills.SkillRepository; -import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.condition.DisabledInNativeImage; -import org.mockito.InjectMocks; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; +import jakarta.inject.Inject; +import jakarta.validation.ConstraintViolationException; import java.util.HashSet; import java.util.List; import java.util.Optional; import java.util.Set; import java.util.UUID; +import java.time.LocalDate; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.doAnswer; -import static org.mockito.Mockito.never; -import static org.mockito.Mockito.reset; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; -// Disabled in nativeTest, as we get an exception from Mockito -// => java.lang.NoClassDefFoundError: Could not initialize class org.mockito.internal.configuration.plugins.Plugins -@DisabledInNativeImage -class MemberSkillServiceImplTest extends TestContainersSuite { - - @Mock - private MemberSkillRepository memberSkillRepository; - - @Mock - private MemberProfileRepository memberProfileRepository; - - @Mock - private SkillRepository skillRepository; - - @InjectMocks +class MemberSkillServiceImplTest extends TestContainersSuite + implements MemberProfileFixture, MemberSkillFixture, SkillFixture { + @Inject private MemberSkillServiceImpl memberSkillsServices; - private AutoCloseable mockFinalizer; - - @BeforeAll - void initMocks() { - mockFinalizer = MockitoAnnotations.openMocks(this); - } - - @BeforeEach - void resetMocks() { - reset(memberSkillRepository, skillRepository, memberProfileRepository); - } - - @AfterAll - void closeMocks() throws Exception { - mockFinalizer.close(); - } - @Test void testRead() { - MemberSkill memberSkill = new MemberSkill(UUID.randomUUID(), UUID.randomUUID(), UUID.randomUUID()); - - when(memberSkillRepository.findById(memberSkill.getId())).thenReturn(Optional.of(memberSkill)); - + Skill skill = createSkill("Skill1", false, "First", false); + MemberProfile member = createADefaultMemberProfile(); + MemberSkill memberSkill = createMemberSkill(member, skill, SkillLevel.INTERMEDIATE_LEVEL, LocalDate.now()); assertEquals(memberSkill, memberSkillsServices.read(memberSkill.getId())); - - verify(memberSkillRepository, times(1)).findById(any(UUID.class)); } @Test void testReadNullId() { - assertNull(memberSkillsServices.read(null)); - - verify(memberSkillRepository, never()).findById(any(UUID.class)); + assertThrows(ConstraintViolationException.class, () -> memberSkillsServices.read(null)); } @Test void testSave() { - MemberSkill memberSkill = new MemberSkill(UUID.randomUUID(), UUID.randomUUID()); - Skill skill = new Skill(); - - when(skillRepository.findById(memberSkill.getSkillid())).thenReturn(Optional.of(skill)); - when(memberProfileRepository.findById(memberSkill.getMemberid())).thenReturn(Optional.of(new MemberProfile())); - when(memberSkillRepository.save(memberSkill)).thenReturn(memberSkill); - + Skill skill = createSkill("Skill1", false, "First", false); + MemberProfile member = createADefaultMemberProfile(); + MemberSkill memberSkill = new MemberSkill(member.getId(), skill.getId()); assertEquals(memberSkill, memberSkillsServices.save(memberSkill)); - - verify(skillRepository, times(1)).findById(any(UUID.class)); - verify(memberProfileRepository, times(1)).findById(any(UUID.class)); - verify(memberSkillRepository, times(1)).save(any(MemberSkill.class)); } @Test @@ -107,10 +59,6 @@ void testSaveWithId() { BadArgException exception = assertThrows(BadArgException.class, () -> memberSkillsServices.save(memberSkill)); assertEquals(String.format("Found unexpected id %s for member skill", memberSkill.getId()), exception.getMessage()); - - verify(memberSkillRepository, never()).save(any(MemberSkill.class)); - verify(skillRepository, never()).findById(any(UUID.class)); - verify(memberProfileRepository, never()).findById(any(UUID.class)); } @Test @@ -119,10 +67,6 @@ void testSaveActionItemNullMemberId() { BadArgException exception = assertThrows(BadArgException.class, () -> memberSkillsServices.save(memberSkill)); assertEquals(String.format("Invalid member skill %s", memberSkill), exception.getMessage()); - - verify(memberSkillRepository, never()).save(any(MemberSkill.class)); - verify(skillRepository, never()).findById(any(UUID.class)); - verify(memberProfileRepository, never()).findById(any(UUID.class)); } @Test @@ -131,178 +75,135 @@ void testSaveActionItemNullSkillId() { BadArgException exception = assertThrows(BadArgException.class, () -> memberSkillsServices.save(memberSkill)); assertEquals(String.format("Invalid member skill %s", memberSkill), exception.getMessage()); - - verify(memberSkillRepository, never()).save(any(MemberSkill.class)); - verify(skillRepository, never()).findById(any(UUID.class)); - verify(memberProfileRepository, never()).findById(any(UUID.class)); } @Test void testSaveNullMemberSkill() { assertNull(memberSkillsServices.save(null)); - - verify(memberSkillRepository, never()).save(any(MemberSkill.class)); - verify(skillRepository, never()).findById(any(UUID.class)); - verify(memberProfileRepository, never()).findById(any(UUID.class)); } @Test void testSaveMemberSkillAlreadyExistingSkill() { - MemberSkill memberSkill = new MemberSkill(UUID.randomUUID(), UUID.randomUUID()); - - when(skillRepository.findById(memberSkill.getSkillid())).thenReturn(Optional.of(new Skill())); - when(memberProfileRepository.findById(memberSkill.getMemberid())).thenReturn(Optional.of(new MemberProfile())); - when(memberSkillRepository.findByMemberidAndSkillid(memberSkill.getMemberid(), memberSkill.getSkillid())) - .thenReturn(Optional.of(memberSkill)); - + Skill skill = createSkill("Skill1", false, "First", false); + MemberProfile member = createADefaultMemberProfile(); + MemberSkill savedSkill = createMemberSkill(member, skill, SkillLevel.INTERMEDIATE_LEVEL, LocalDate.now()); + MemberSkill memberSkill = new MemberSkill(member.getId(), skill.getId(), SkillLevel.INTERMEDIATE_LEVEL, LocalDate.now()); AlreadyExistsException exception = assertThrows(AlreadyExistsException.class, () -> memberSkillsServices.save(memberSkill)); assertEquals(String.format("Member %s already has this skill %s", memberSkill.getMemberid(), memberSkill.getSkillid()), exception.getMessage()); - - verify(memberSkillRepository, never()).save(any(MemberSkill.class)); - verify(skillRepository, times(1)).findById(any(UUID.class)); - verify(memberProfileRepository, times(1)).findById(any(UUID.class)); - verify(memberSkillRepository, times(1)).findByMemberidAndSkillid(any(UUID.class), any(UUID.class)); } @Test void testSaveMemberSkillNonExistingSkill() { - MemberSkill memberSkill = new MemberSkill(UUID.randomUUID(), UUID.randomUUID()); - - when(skillRepository.findById(memberSkill.getSkillid())).thenReturn(Optional.empty()); - when(memberProfileRepository.findById(memberSkill.getMemberid())).thenReturn(Optional.of(new MemberProfile())); + MemberProfile member = createADefaultMemberProfile(); + MemberSkill memberSkill = new MemberSkill(member.getId(), UUID.randomUUID()); BadArgException exception = assertThrows(BadArgException.class, () -> memberSkillsServices.save(memberSkill)); assertEquals(String.format("Skill %s doesn't exist", memberSkill.getSkillid()), exception.getMessage()); - - verify(memberSkillRepository, never()).save(any(MemberSkill.class)); - verify(skillRepository, times(1)).findById(any(UUID.class)); - verify(memberProfileRepository, times(1)).findById(any(UUID.class)); } @Test void testSaveMemberSkillNonExistingMember() { - MemberSkill memberSkill = new MemberSkill(UUID.randomUUID(), UUID.randomUUID()); - - when(skillRepository.findById(memberSkill.getSkillid())).thenReturn(Optional.of(new Skill())); - when(memberProfileRepository.findById(memberSkill.getMemberid())).thenReturn(Optional.empty()); + Skill skill = createSkill("Skill1", false, "First", false); + MemberSkill memberSkill = new MemberSkill(UUID.randomUUID(), skill.getId()); BadArgException exception = assertThrows(BadArgException.class, () -> memberSkillsServices.save(memberSkill)); assertEquals(String.format("Member Profile %s doesn't exist", memberSkill.getMemberid()), exception.getMessage()); - - verify(memberSkillRepository, never()).save(any(MemberSkill.class)); - verify(skillRepository, never()).findById(any(UUID.class)); - verify(memberProfileRepository, times(1)).findById(any(UUID.class)); } @Test void testFindByFieldsNullParams() { - Set memberSkillSet = Set.of( - new MemberSkill(UUID.randomUUID(), UUID.randomUUID()), - new MemberSkill(UUID.randomUUID(), UUID.randomUUID()), - new MemberSkill(UUID.randomUUID(), UUID.randomUUID()) - ); - - when(memberSkillRepository.findAll()).thenReturn(memberSkillSet.stream().toList()); - + Skill skill1 = createSkill("Skill1", false, "First", false); + Skill skill2 = createSkill("Skill2", false, "Second", false); + Skill skill3 = createSkill("Skill3", false, "Third", false); + MemberProfile member1 = createADefaultMemberProfile(); + MemberSkill ms1 = createMemberSkill(member1, skill1, SkillLevel.INTERMEDIATE_LEVEL, LocalDate.now()); + MemberSkill ms2 = createMemberSkill(member1, skill2, SkillLevel.ADVANCED_LEVEL, LocalDate.now()); + MemberSkill ms3 = createMemberSkill(member1, skill3, SkillLevel.NOVICE_LEVEL, LocalDate.now()); + + Set memberSkillSet = Set.of(ms1, ms2, ms3); assertEquals(memberSkillSet, memberSkillsServices.findByFields(null, null)); - - verify(memberSkillRepository, times(1)).findAll(); - verify(memberSkillRepository, never()).findByMemberid(any(UUID.class)); - verify(memberSkillRepository, never()).findBySkillid(any(UUID.class)); } @Test void testFindByFieldsMemberId() { - List memberSkillSet = List.of( - new MemberSkill(UUID.randomUUID(), UUID.randomUUID()), - new MemberSkill(UUID.randomUUID(), UUID.randomUUID()), - new MemberSkill(UUID.randomUUID(), UUID.randomUUID()) - ); - + Skill skill1 = createSkill("Skill1", false, "First", false); + Skill skill2 = createSkill("Skill2", false, "Second", false); + Skill skill3 = createSkill("Skill3", false, "Third", false); + MemberProfile member1 = createADefaultMemberProfile(); + MemberProfile member2 = createASecondDefaultMemberProfile(); + MemberSkill ms1 = createMemberSkill(member1, skill1, SkillLevel.INTERMEDIATE_LEVEL, LocalDate.now()); + MemberSkill ms2 = createMemberSkill(member2, skill2, SkillLevel.ADVANCED_LEVEL, LocalDate.now()); + MemberSkill ms3 = createMemberSkill(member1, skill3, SkillLevel.NOVICE_LEVEL, LocalDate.now()); + + List memberSkillSet = List.of(ms1, ms2, ms3); List memberSkillsToFind = List.of(memberSkillSet.get(1)); MemberSkill memberSkill = memberSkillsToFind.get(0); - when(memberSkillRepository.findAll()).thenReturn(memberSkillSet); - when(memberSkillRepository.findByMemberid(memberSkill.getMemberid())).thenReturn(memberSkillsToFind); - assertEquals(new HashSet<>(memberSkillsToFind), memberSkillsServices.findByFields(memberSkill.getMemberid(), null)); - - verify(memberSkillRepository, times(1)).findAll(); - verify(memberSkillRepository, times(1)).findByMemberid(any(UUID.class)); - verify(memberSkillRepository, never()).findBySkillid(any(UUID.class)); } @Test void testFindByFieldsSkillId() { - List memberSkillSet = List.of( - new MemberSkill(UUID.randomUUID(), UUID.randomUUID()), - new MemberSkill(UUID.randomUUID(), UUID.randomUUID()), - new MemberSkill(UUID.randomUUID(), UUID.randomUUID()) - ); + Skill skill1 = createSkill("Skill1", false, "First", false); + Skill skill2 = createSkill("Skill2", false, "Second", false); + Skill skill3 = createSkill("Skill3", false, "Third", false); + MemberProfile member1 = createADefaultMemberProfile(); + MemberProfile member2 = createASecondDefaultMemberProfile(); + MemberSkill ms1 = createMemberSkill(member1, skill1, SkillLevel.INTERMEDIATE_LEVEL, LocalDate.now()); + MemberSkill ms2 = createMemberSkill(member2, skill2, SkillLevel.ADVANCED_LEVEL, LocalDate.now()); + MemberSkill ms3 = createMemberSkill(member1, skill3, SkillLevel.NOVICE_LEVEL, LocalDate.now()); + + List memberSkillSet = List.of(ms1, ms2, ms3); List memberSkillsToFind = List.of(memberSkillSet.get(1)); MemberSkill memberSkill = memberSkillsToFind.get(0); - when(memberSkillRepository.findAll()).thenReturn(memberSkillSet); - when(memberSkillRepository.findBySkillid(memberSkill.getSkillid())).thenReturn(memberSkillsToFind); - assertEquals(new HashSet<>(memberSkillsToFind), memberSkillsServices.findByFields(null, memberSkill.getSkillid())); - - verify(memberSkillRepository, times(1)).findAll(); - verify(memberSkillRepository, times(1)).findBySkillid(any(UUID.class)); - verify(memberSkillRepository, never()).findByMemberid(any(UUID.class)); } @Test void testFindByFieldsAll() { - List memberSkillSet = List.of( - new MemberSkill(UUID.randomUUID(), UUID.randomUUID()), - new MemberSkill(UUID.randomUUID(), UUID.randomUUID()), - new MemberSkill(UUID.randomUUID(), UUID.randomUUID()) - ); + Skill skill1 = createSkill("Skill1", false, "First", false); + Skill skill2 = createSkill("Skill2", false, "Second", false); + Skill skill3 = createSkill("Skill3", false, "Third", false); + MemberProfile member1 = createADefaultMemberProfile(); + MemberProfile member2 = createASecondDefaultMemberProfile(); + MemberSkill ms1 = createMemberSkill(member1, skill1, SkillLevel.INTERMEDIATE_LEVEL, LocalDate.now()); + MemberSkill ms2 = createMemberSkill(member2, skill2, SkillLevel.ADVANCED_LEVEL, LocalDate.now()); + MemberSkill ms3 = createMemberSkill(member1, skill3, SkillLevel.NOVICE_LEVEL, LocalDate.now()); + + List memberSkillSet = List.of(ms1, ms2, ms3); List memberSkillsToFind = List.of(memberSkillSet.get(1)); MemberSkill memberSkill = memberSkillsToFind.get(0); - when(memberSkillRepository.findAll()).thenReturn(memberSkillSet); - when(memberSkillRepository.findBySkillid(memberSkill.getSkillid())).thenReturn(memberSkillsToFind); - when(memberSkillRepository.findByMemberid(memberSkill.getMemberid())).thenReturn(memberSkillsToFind); assertEquals(new HashSet<>(memberSkillsToFind), memberSkillsServices .findByFields(memberSkill.getMemberid(), memberSkill.getSkillid())); - - verify(memberSkillRepository, times(1)).findAll(); - verify(memberSkillRepository, times(1)).findByMemberid(any(UUID.class)); - verify(memberSkillRepository, times(1)).findBySkillid(any(UUID.class)); } @Test void testReadAll() { - Set memberSkillSet = Set.of( - new MemberSkill(UUID.randomUUID(), UUID.randomUUID()), - new MemberSkill(UUID.randomUUID(), UUID.randomUUID()), - new MemberSkill(UUID.randomUUID(), UUID.randomUUID()) - ); + Skill skill1 = createSkill("Skill1", false, "First", false); + Skill skill2 = createSkill("Skill2", false, "Second", false); + Skill skill3 = createSkill("Skill3", false, "Third", false); + MemberProfile member1 = createADefaultMemberProfile(); + MemberProfile member2 = createASecondDefaultMemberProfile(); + MemberSkill ms1 = createMemberSkill(member1, skill1, SkillLevel.INTERMEDIATE_LEVEL, LocalDate.now()); + MemberSkill ms2 = createMemberSkill(member2, skill2, SkillLevel.ADVANCED_LEVEL, LocalDate.now()); + MemberSkill ms3 = createMemberSkill(member1, skill3, SkillLevel.NOVICE_LEVEL, LocalDate.now()); - when(memberSkillRepository.findAll()).thenReturn(memberSkillSet.stream().toList()); + Set memberSkillSet = Set.of(ms1, ms2, ms3); assertEquals(memberSkillSet, memberSkillsServices.findByFields(null,null)); - - verify(memberSkillRepository, times(1)).findAll(); } @Test void testDelete() { - UUID uuid = UUID.randomUUID(); - - doAnswer(an -> { - assertEquals(uuid, an.getArgument(0)); - return null; - }).when(memberSkillRepository).deleteById(any(UUID.class)); - - memberSkillsServices.delete(uuid); - - verify(memberSkillRepository, times(1)).deleteById(any(UUID.class)); + Skill skill = createSkill("Skill1", false, "First", false); + memberSkillsServices.delete(skill.getId()); + assertFalse(getMemberSkillRepository().findById(skill.getId()) + .isPresent()); } } diff --git a/server/src/test/java/com/objectcomputing/checkins/services/member_skill/skillsreport/SkillsReportServicesImplTest.java b/server/src/test/java/com/objectcomputing/checkins/services/member_skill/skillsreport/SkillsReportServicesImplTest.java index 17e64ee385..fb2e77cfff 100644 --- a/server/src/test/java/com/objectcomputing/checkins/services/member_skill/skillsreport/SkillsReportServicesImplTest.java +++ b/server/src/test/java/com/objectcomputing/checkins/services/member_skill/skillsreport/SkillsReportServicesImplTest.java @@ -2,6 +2,11 @@ import com.objectcomputing.checkins.exceptions.BadArgException; import com.objectcomputing.checkins.services.TestContainersSuite; +import com.objectcomputing.checkins.services.fixture.SkillFixture; +import com.objectcomputing.checkins.services.fixture.MemberProfileFixture; +import com.objectcomputing.checkins.services.fixture.MemberSkillFixture; +import com.objectcomputing.checkins.services.skills.Skill; +import com.objectcomputing.checkins.services.member_skill.skillsreport.SkillLevel; import com.objectcomputing.checkins.services.member_skill.MemberSkill; import com.objectcomputing.checkins.services.member_skill.MemberSkillRepository; import com.objectcomputing.checkins.services.memberprofile.MemberProfile; @@ -12,10 +17,7 @@ import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.condition.DisabledInNativeImage; -import org.mockito.InjectMocks; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; +import jakarta.inject.Inject; import java.time.LocalDate; import java.util.ArrayList; @@ -29,50 +31,12 @@ import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.mockito.Mockito.any; -import static org.mockito.Mockito.never; -import static org.mockito.Mockito.reset; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; -// Disabled in nativeTest, as we get an exception from Mockito -// => java.lang.NoClassDefFoundError: Could not initialize class org.mockito.internal.configuration.plugins.Plugins -@DisabledInNativeImage -class SkillsReportServicesImplTest extends TestContainersSuite { - - @Mock - private MemberSkillRepository memberSkillRepository; - - @Mock - private MemberProfileRepository memberProfileRepository; - - @Mock - private MemberProfileServices memberProfileServices; - - @Mock - private SkillRepository skillRepository; - - @InjectMocks +class SkillsReportServicesImplTest extends TestContainersSuite + implements MemberProfileFixture, MemberSkillFixture, SkillFixture { + @Inject private SkillsReportServicesImpl skillsReportServices; - private AutoCloseable mockFinalizer; - - @BeforeAll - void initMocks() { - mockFinalizer = MockitoAnnotations.openMocks(this); - } - - @BeforeEach - void resetMocks() { - reset(memberSkillRepository, memberProfileRepository, skillRepository); - } - - @AfterAll - void finalizeMocks() throws Exception { - mockFinalizer.close(); - } - @Test void testReportSkillNotExist() { final SkillLevelDTO dto = new SkillLevelDTO(); @@ -89,13 +53,10 @@ void testReportSkillNotExist() { @Test void testReportMemberProfileNotExist() { - final SkillLevelDTO dto = new SkillLevelDTO(); - final UUID skillId = UUID.randomUUID(); - dto.setId(skillId); - when(skillRepository.existsById(skillId)).thenReturn(true); + final Skill skill = createADefaultSkill(); final List skills = new ArrayList<>(); - skills.add(dto); + skills.add(new SkillLevelDTO(skill.getId(), SkillLevel.NOVICE)); final SkillsReportRequestDTO request = new SkillsReportRequestDTO(); request.setSkills(skills); @@ -111,72 +72,39 @@ void testReportEmptyRequestedSkillsList() { request.setSkills(new ArrayList<>()); final SkillsReportResponseDTO response = skillsReportServices.report(request); assertNotNull(response); - - verify(memberSkillRepository, never()).findBySkillid(any(UUID.class)); - verify(memberProfileServices, never()).getById(any(UUID.class)); + assertEquals(0, response.getTeamMembers().size()); } @Test void testReport() { - final UUID skillId1 = UUID.randomUUID(); - final UUID skillId2 = UUID.randomUUID(); - final UUID skillId3 = UUID.randomUUID(); - final UUID skillId4 = UUID.randomUUID(); - final UUID memberId1 = UUID.randomUUID(); - final UUID memberId2 = UUID.randomUUID(); - final UUID memberId3 = UUID.randomUUID(); - final UUID memberId4 = UUID.randomUUID(); - - final MemberSkill ms1 = new MemberSkill(memberId1, skillId1, SkillLevel.INTERMEDIATE_LEVEL, LocalDate.now()); - final MemberSkill ms2 = new MemberSkill(memberId1, skillId2, SkillLevel.ADVANCED_LEVEL, LocalDate.now()); - final MemberSkill ms3 = new MemberSkill(memberId2, skillId3, SkillLevel.NOVICE_LEVEL, LocalDate.now()); - final MemberSkill ms4 = new MemberSkill(memberId2, skillId4, SkillLevel.EXPERT_LEVEL, LocalDate.now()); - final MemberSkill ms5 = new MemberSkill(memberId3, skillId2, SkillLevel.INTERESTED_LEVEL, LocalDate.now()); - final MemberSkill ms6 = new MemberSkill(memberId3, skillId3, SkillLevel.ADVANCED_LEVEL, LocalDate.now()); - final MemberSkill ms7 = new MemberSkill(memberId4, skillId1, SkillLevel.ADVANCED_LEVEL, LocalDate.now()); - final MemberSkill ms8 = new MemberSkill(memberId4, skillId2, SkillLevel.INTERMEDIATE_LEVEL, LocalDate.now()); - final MemberSkill ms9 = new MemberSkill(memberId4, skillId4, SkillLevel.EXPERT_LEVEL, LocalDate.now()); - - final List skillList1 = new ArrayList<>(); - skillList1.add(ms1); - skillList1.add(ms7); - final List skillList2 = new ArrayList<>(); - skillList2.add(ms2); - skillList2.add(ms5); - skillList2.add(ms8); - final List skillList3 = new ArrayList<>(); - skillList3.add(ms3); - skillList3.add(ms6); - final List skillList4 = new ArrayList<>(); - skillList4.add(ms4); - skillList4.add(ms9); - - when(memberSkillRepository.findBySkillid(skillId1)).thenReturn(skillList1); - when(memberSkillRepository.findBySkillid(skillId2)).thenReturn(skillList2); - when(memberSkillRepository.findBySkillid(skillId3)).thenReturn(skillList3); - when(memberSkillRepository.findBySkillid(skillId4)).thenReturn(skillList4); - MemberProfile joey = new MemberProfile("Joey", null, "Tribbiani", null, - null, null, null, null, null, null, null, - null, null, null, null, null, null); - MemberProfile chandler = new MemberProfile("Chandler", null, "Bing", null, - null, null, null, null, null, null, null, - null, null,null, null, null, null); - MemberProfile ross = new MemberProfile("Ross", null, "Geller", null, - null, null, null, null, null, null, null, - null, null,null, null, null, null); - when(memberProfileServices.getById(memberId1)).thenReturn(joey); - when(memberProfileServices.getById(memberId2)).thenReturn(chandler); - when(memberProfileServices.getById(memberId3)).thenReturn(null); - when(memberProfileServices.getById(memberId4)).thenReturn(ross); - - when(skillRepository.existsById(skillId1)).thenReturn(true); - when(skillRepository.existsById(skillId2)).thenReturn(true); - when(skillRepository.existsById(skillId3)).thenReturn(true); - when(skillRepository.existsById(skillId4)).thenReturn(true); - when(memberProfileRepository.existsById(memberId1)).thenReturn(true); - when(memberProfileRepository.existsById(memberId2)).thenReturn(true); - when(memberProfileRepository.existsById(memberId3)).thenReturn(true); - when(memberProfileRepository.existsById(memberId4)).thenReturn(true); + Skill skill1 = createSkill("Skill1", false, "First", false); + Skill skill2 = createSkill("Skill2", false, "Second", false); + Skill skill3 = createSkill("Skill3", false, "Third", false); + Skill skill4 = createSkill("Skill4", false, "Fourth", false); + + MemberProfile member1 = createADefaultMemberProfile(); + MemberProfile member2 = createASecondDefaultMemberProfile(); + MemberProfile member3 = createAThirdDefaultMemberProfile(); + MemberProfile member4 = createADefaultMemberProfileForPdl(member1); + + final UUID skillId1 = skill1.getId(); + final UUID skillId2 = skill2.getId(); + final UUID skillId3 = skill3.getId(); + final UUID skillId4 = skill4.getId(); + final UUID memberId1 = member1.getId(); + final UUID memberId2 = member2.getId(); + final UUID memberId3 = member3.getId(); + final UUID memberId4 = member4.getId(); + + final MemberSkill ms1 = createMemberSkill(member1, skill1, SkillLevel.INTERMEDIATE_LEVEL, LocalDate.now()); + final MemberSkill ms2 = createMemberSkill(member1, skill2, SkillLevel.ADVANCED_LEVEL, LocalDate.now()); + final MemberSkill ms3 = createMemberSkill(member2, skill3, SkillLevel.NOVICE_LEVEL, LocalDate.now()); + final MemberSkill ms4 = createMemberSkill(member2, skill4, SkillLevel.EXPERT_LEVEL, LocalDate.now()); + final MemberSkill ms5 = createMemberSkill(member3, skill2, SkillLevel.INTERESTED_LEVEL, LocalDate.now()); + final MemberSkill ms6 = createMemberSkill(member3, skill3, SkillLevel.ADVANCED_LEVEL, LocalDate.now()); + final MemberSkill ms7 = createMemberSkill(member4, skill1, SkillLevel.ADVANCED_LEVEL, LocalDate.now()); + final MemberSkill ms8 = createMemberSkill(member4, skill2, SkillLevel.INTERMEDIATE_LEVEL, LocalDate.now()); + final MemberSkill ms9 = createMemberSkill(member4, skill4, SkillLevel.EXPERT_LEVEL, LocalDate.now()); // List of skills required in first request final SkillLevelDTO dto1 = new SkillLevelDTO(); @@ -203,17 +131,13 @@ void testReport() { assertTrue(elem.getId().equals(memberId1) || elem.getId().equals(memberId3) || elem.getId().equals(memberId4)); if (elem.getId().equals(memberId1)) { - assertReturnedMember1(elem, skillId1, skillId2); + assertReturnedMember1(elem, skillId1, skillId2, member1.getFirstName() + " " + member1.getLastName()); } else if (elem.getId().equals(memberId3)) { - assertReturnedMember3(elem, skillId2, skillId3); + assertReturnedMember3(elem, skillId2, skillId3, member3.getFirstName() + " " + member3.getLastName()); } else { - assertReturnedMember4(elem, skillId1, skillId2); + assertReturnedMember4(elem, skillId1, skillId2, member4.getFirstName() + " " + member4.getLastName()); } } - verify(memberSkillRepository, times(3)).findBySkillid(any(UUID.class)); - verify(memberProfileServices, times(3)).getById(any(UUID.class)); - verify(skillRepository, times(3)).existsById(any(UUID.class)); - verify(memberProfileRepository, never()).existsById(any(UUID.class)); // Specify a list of members final Set members = new HashSet<>(); @@ -227,24 +151,16 @@ void testReport() { for (TeamMemberSkillDTO elem : response2.getTeamMembers()) { assertTrue(elem.getId().equals(memberId3) || elem.getId().equals(memberId4)); if (elem.getId().equals(memberId3)) { - assertReturnedMember3(elem, skillId2, skillId3); + assertReturnedMember3(elem, skillId2, skillId3, member3.getFirstName() + " " + member3.getLastName()); } else { - assertReturnedMember4(elem, skillId1, skillId2); + assertReturnedMember4(elem, skillId1, skillId2, member4.getFirstName() + " " + member4.getLastName()); } } - verify(memberSkillRepository, times(6)).findBySkillid(any(UUID.class)); - verify(memberProfileServices, times(6)).getById(any(UUID.class)); - verify(skillRepository, times(6)).existsById(any(UUID.class)); - verify(memberProfileRepository, times(3)).existsById(any(UUID.class)); // Each returned member must satisfy all requested skills request1.setInclusive(true); final SkillsReportResponseDTO response3 = skillsReportServices.report(request1); assertTrue(response3.getTeamMembers().isEmpty()); - verify(memberSkillRepository, times(9)).findBySkillid(any(UUID.class)); - verify(memberProfileServices, times(9)).getById(any(UUID.class)); - verify(skillRepository, times(9)).existsById(any(UUID.class)); - verify(memberProfileRepository, times(6)).existsById(any(UUID.class)); // Another request final SkillLevelDTO dto4 = new SkillLevelDTO(); @@ -265,7 +181,7 @@ void testReport() { assertEquals(1, response4.getTeamMembers().size()); assertEquals(memberId4, response4.getTeamMembers().get(0).getId()); - assertEquals("Ross Geller", response4.getTeamMembers().get(0).getName()); + assertEquals(member4.getFirstName() + " " + member4.getLastName(), response4.getTeamMembers().get(0).getName()); assertEquals(2, response4.getTeamMembers().get(0).getSkills().size()); for (SkillLevelDTO skill : response4.getTeamMembers().get(0).getSkills()) { assertTrue(skill.getId().equals(skillId2) || skill.getId().equals(skillId4)); @@ -275,14 +191,10 @@ void testReport() { assertEquals(SkillLevel.convertFromString(SkillLevel.EXPERT_LEVEL), skill.getLevel()); } } - verify(memberSkillRepository, times(11)).findBySkillid(any(UUID.class)); - verify(memberProfileServices, times(12)).getById(any(UUID.class)); - verify(skillRepository, times(11)).existsById(any(UUID.class)); - verify(memberProfileRepository, times(6)).existsById(any(UUID.class)); } - private void assertReturnedMember1(TeamMemberSkillDTO elem, UUID skillId1, UUID skillId2) { - assertEquals("Joey Tribbiani", elem.getName()); + private void assertReturnedMember1(TeamMemberSkillDTO elem, UUID skillId1, UUID skillId2, String fullName) { + assertEquals(fullName, elem.getName()); assertEquals(2, elem.getSkills().size()); for (SkillLevelDTO skill : elem.getSkills()) { assertTrue(skill.getId().equals(skillId1) || skill.getId().equals(skillId2)); @@ -294,8 +206,8 @@ private void assertReturnedMember1(TeamMemberSkillDTO elem, UUID skillId1, UUID } } - private void assertReturnedMember3(TeamMemberSkillDTO elem, UUID skillId2, UUID skillId3) { - assertNull(elem.getName()); + private void assertReturnedMember3(TeamMemberSkillDTO elem, UUID skillId2, UUID skillId3, String fullName) { + assertEquals(fullName, elem.getName()); assertEquals(2, elem.getSkills().size()); for (SkillLevelDTO skill : elem.getSkills()) { assertTrue(skill.getId().equals(skillId2) || skill.getId().equals(skillId3)); @@ -307,8 +219,8 @@ private void assertReturnedMember3(TeamMemberSkillDTO elem, UUID skillId2, UUID } } - private void assertReturnedMember4(TeamMemberSkillDTO elem, UUID skillId1, UUID skillId2) { - assertEquals("Ross Geller", elem.getName()); + private void assertReturnedMember4(TeamMemberSkillDTO elem, UUID skillId1, UUID skillId2, String fullName) { + assertEquals(fullName, elem.getName()); assertEquals(2, elem.getSkills().size()); for (SkillLevelDTO skill : elem.getSkills()) { assertTrue(skill.getId().equals(skillId1) || skill.getId().equals(skillId2)); diff --git a/server/src/test/java/com/objectcomputing/checkins/services/memberprofile/MemberProfileTest.java b/server/src/test/java/com/objectcomputing/checkins/services/memberprofile/MemberProfileTest.java index aa70d850eb..cc43ab26a8 100644 --- a/server/src/test/java/com/objectcomputing/checkins/services/memberprofile/MemberProfileTest.java +++ b/server/src/test/java/com/objectcomputing/checkins/services/memberprofile/MemberProfileTest.java @@ -1,5 +1,6 @@ package com.objectcomputing.checkins.services.memberprofile; +import com.objectcomputing.checkins.services.fixture.MemberProfileFixture; import com.objectcomputing.checkins.exceptions.AlreadyExistsException; import com.objectcomputing.checkins.notifications.email.MailJetFactory; import com.objectcomputing.checkins.services.MailJetFactoryReplacement; @@ -18,8 +19,6 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.condition.DisabledInNativeImage; -import org.mockito.Mockito; import java.time.LocalDate; import java.util.List; @@ -31,13 +30,10 @@ import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.mockito.Mockito.when; @Property(name = "replace.mailjet.factory", value = StringUtils.TRUE) -// Disabled in nativeTest, as we get an exception from Mockito -// => java.lang.NoClassDefFoundError: Could not initialize class org.mockito.Mockito -@DisabledInNativeImage -class MemberProfileTest extends TestContainersSuite { +class MemberProfileTest extends TestContainersSuite + implements MemberProfileFixture { @Inject protected Validator validator; @@ -46,29 +42,12 @@ class MemberProfileTest extends TestContainersSuite { @Named(MailJetFactory.HTML_FORMAT) private MailJetFactoryReplacement.MockEmailSender emailSender; - private MemberProfileRepository memberRepo; - + @Inject private MemberProfileServicesImpl memberProfileServices; @BeforeEach - @Tag("mocked") void setUp() { - memberRepo = Mockito.mock(MemberProfileRepository.class); - CurrentUserServices currentUserServices = Mockito.mock(CurrentUserServices.class); - RoleServices roleServices = Mockito.mock(RoleServices.class); - CheckInServices checkinServices = Mockito.mock(CheckInServices.class); - MemberSkillServices memberSkillServices = Mockito.mock(MemberSkillServices.class); - TeamMemberServices teamMemberServices = Mockito.mock(TeamMemberServices.class); emailSender.reset(); - - memberProfileServices = new MemberProfileServicesImpl( - memberRepo, - currentUserServices, - roleServices, - checkinServices, - memberSkillServices, - teamMemberServices, - emailSender); } @Test @@ -195,38 +174,26 @@ void testToString() { } @Test - @Tag("mocked") void testSaveProfileWithExistingEmail() { - String workEmail = "existing@example.com"; - MemberProfile existingProfile = new MemberProfile(UUID.randomUUID(), "Jane", null, "Doe", null, null, null, null, workEmail, null, null, null, null, null, null, null, null, null); + MemberProfile existingProfile = createADefaultMemberProfile(); + String workEmail = existingProfile.getWorkEmail(); MemberProfile newProfile = new MemberProfile(UUID.randomUUID(), "John", null, "Smith", null, null, null, null, workEmail, null, null, null, null, null, null, null, null, null); - when(memberRepo.findByWorkEmail(workEmail)).thenReturn(Optional.of(existingProfile)); - assertThrows(AlreadyExistsException.class, () -> memberProfileServices.saveProfile(newProfile)); } @Test - @Tag("mocked") void testSaveProfileWithNewEmail() { - UUID pdlId = UUID.randomUUID(); - UUID supervisorId = UUID.randomUUID(); - - MemberProfile pdlProfile = new MemberProfile(pdlId,"Jane", null, "Smith", null, null, null, null, "jane.smith@example.com", null, null, null, null, null, null, null, null, null); - MemberProfile supervisorProfile = new MemberProfile(supervisorId, "Janine", null, "Smith", null, null, null, null, "janine.smith@example.com", null, null, null, null, null, null, null, null, null); - MemberProfile newProfile = new MemberProfile("John", null, "Smith", null, null, pdlId, null, "john.smith@example.com", null, null, null, supervisorId, null, null, null, null, null); - - - // Mocking findByWorkEmail to return an empty Optional indicating no existing profile with the email - when(memberRepo.findByWorkEmail(newProfile.getWorkEmail())).thenReturn(Optional.empty()); - - // Mocking save to return the profile that is being saved - when(memberRepo.save(newProfile)).thenReturn(newProfile); - - // Mocking findById to return the profile after saving - when(memberRepo.findById(newProfile.getId())).thenReturn(Optional.of(newProfile)); - when(memberRepo.findById(pdlId)).thenReturn(Optional.of(pdlProfile)); - when(memberRepo.findById(supervisorId)).thenReturn(Optional.of(supervisorProfile)); + MemberProfile pdlProfile = createADefaultMemberProfile(); + MemberProfile supervisorProfile = createADefaultSupervisor(); + MemberProfile newProfile = new MemberProfile("Charizard", null, "Char", + null, "Local fire hazard", pdlProfile.getId(), + "New York, New York", + "charizard@objectcomputing.com", "local-kaiju", + LocalDate.now().minusDays(3).minusYears(5), + "Needs supervision due to building being ultra flammable", + supervisorProfile.getId(), null, null, null, null, + LocalDate.now()); MemberProfile savedProfile = memberProfileServices.saveProfile(newProfile); @@ -236,26 +203,26 @@ void testSaveProfileWithNewEmail() { assertEquals(2, emailSender.events.size()); assertEquals(List.of( - List.of("SEND_EMAIL", "null", "null", "You have been assigned as the PDL of John Smith", "John Smith will now report to you as their PDL. Please engage with them: john.smith@example.com", pdlProfile.getWorkEmail()), - List.of("SEND_EMAIL", "null", "null", "You have been assigned as the supervisor of John Smith", "John Smith will now report to you as their supervisor. Please engage with them: john.smith@example.com", supervisorProfile.getWorkEmail()) + List.of("SEND_EMAIL", "null", "null", + String.format("You have been assigned as the PDL of %s %s", newProfile.getFirstName(), newProfile.getLastName()), + String.format("%s %s will now report to you as their PDL. Please engage with them: %s", newProfile.getFirstName(), newProfile.getLastName(), newProfile.getWorkEmail()), + pdlProfile.getWorkEmail()), + List.of("SEND_EMAIL", "null", "null", + String.format("You have been assigned as the supervisor of %s %s", newProfile.getFirstName(), newProfile.getLastName()), + String.format("%s %s will now report to you as their supervisor. Please engage with them: %s", newProfile.getFirstName(), newProfile.getLastName(), newProfile.getWorkEmail()), + supervisorProfile.getWorkEmail()) ), emailSender.events ); } @Test - @Tag("mocked") void testUpdateProfileWithChangedPDL() { - UUID id = UUID.randomUUID(); - UUID pdlId = UUID.randomUUID(); - MemberProfile existingProfile = new MemberProfile(id, "John", null, "Smith", null, null, null, null, "john.smith@example.com", null, null, null, null, null, null, null, null, null); - MemberProfile updatedProfile = new MemberProfile(id, "John", null, "Smith", null, null, pdlId, null, "john.smith@example.com", null, null, null, null, null, null, null, null, null); - MemberProfile pdlProfile = new MemberProfile(pdlId, "Jane", null, "Doe", null, null, null, null, "jane.doe@example.com", null, null, null, null, null, null, null, null, null); - - when(memberRepo.findById(id)).thenReturn(Optional.of(existingProfile)); - when(memberRepo.findByWorkEmail(updatedProfile.getWorkEmail())).thenReturn(Optional.of(updatedProfile)); - when(memberRepo.findById(pdlId)).thenReturn(Optional.of(pdlProfile)); - when(memberRepo.update(updatedProfile)).thenReturn(updatedProfile); + MemberProfile existingProfile = createADefaultMemberProfile(); + MemberProfile pdlProfile = createASecondDefaultMemberProfile(); + UUID id = existingProfile.getId(); + UUID pdlId = pdlProfile.getId(); + MemberProfile updatedProfile = new MemberProfile(id, existingProfile.getFirstName(), null, existingProfile.getLastName(), null, null, pdlId, null, existingProfile.getWorkEmail(), null, null, null, null, null, null, null, null, null); MemberProfile result = memberProfileServices.saveProfile(updatedProfile); @@ -263,44 +230,38 @@ void testUpdateProfileWithChangedPDL() { assertEquals(1, emailSender.events.size()); assertEquals( - List.of("SEND_EMAIL", "null", "null", "You have been assigned as the PDL of John Smith", "John Smith will now report to you as their PDL. Please engage with them: " + existingProfile.getWorkEmail(), pdlProfile.getWorkEmail()), + List.of("SEND_EMAIL", "null", "null", + String.format("You have been assigned as the PDL of %s %s", existingProfile.getFirstName(), existingProfile.getLastName()), + String.format("%s %s will now report to you as their PDL. Please engage with them: %s", existingProfile.getFirstName(), existingProfile.getLastName(), existingProfile.getWorkEmail()), + pdlProfile.getWorkEmail()), emailSender.events.getFirst() ); } @Test - @Tag("mocked") void testUpdateProfileWithChangedSupervisor() { - UUID id = UUID.randomUUID(); - UUID supervisorId = UUID.randomUUID(); - MemberProfile existingProfile = new MemberProfile(id, "John", null, "Smith", null, null, null, null, "john.smith@example.com", null, null, null, null, null, null, null, null, null); - MemberProfile updatedProfile = new MemberProfile(id, "John", null, "Smith", null, null, null, null, "john.smith@example.com", null, null, null, supervisorId, null, null, null, null, null); - MemberProfile supervisorProfile = new MemberProfile(supervisorId, "Jane", null, "Doe", null, null, null, null, "jane.doe@example.com", null, null, null, null, null, null, null, null, null); - - when(memberRepo.findById(id)).thenReturn(Optional.of(existingProfile)); - when(memberRepo.findByWorkEmail(updatedProfile.getWorkEmail())).thenReturn(Optional.of(updatedProfile)); - when(memberRepo.findById(supervisorId)).thenReturn(Optional.of(supervisorProfile)); - when(memberRepo.update(updatedProfile)).thenReturn(updatedProfile); + MemberProfile existingProfile = createADefaultMemberProfile(); + MemberProfile supervisorProfile = createASecondDefaultMemberProfile(); + UUID id = existingProfile.getId(); + UUID supervisorId = supervisorProfile.getId(); + MemberProfile updatedProfile = new MemberProfile(id, existingProfile.getFirstName(), null, existingProfile.getLastName(), null, null, null, null, existingProfile.getWorkEmail(), null, null, null, supervisorId, null, null, null, null, null); MemberProfile result = memberProfileServices.saveProfile(updatedProfile); assertEquals(updatedProfile, result); assertEquals(1, emailSender.events.size()); assertEquals( - List.of("SEND_EMAIL", "null", "null", "You have been assigned as the supervisor of John Smith", "John Smith will now report to you as their supervisor. Please engage with them: " + existingProfile.getWorkEmail(), supervisorProfile.getWorkEmail()), + List.of("SEND_EMAIL", "null", "null", + String.format("You have been assigned as the supervisor of %s %s", existingProfile.getFirstName(), existingProfile.getLastName()), + String.format("%s %s will now report to you as their supervisor. Please engage with them: %s", existingProfile.getFirstName(), existingProfile.getLastName(), existingProfile.getWorkEmail()), + supervisorProfile.getWorkEmail()), emailSender.events.getFirst() ); } @Test - @Tag("mocked") void testUpdateProfileWithNoChange() { - UUID id = UUID.randomUUID(); - MemberProfile existingProfile = new MemberProfile(id, "John", null, "Smith", null, null, null, null, "john.smith@example.com", null, null, null, null, null, null, null, null, null); - - when(memberRepo.findById(id)).thenReturn(Optional.of(existingProfile)); - when(memberRepo.findByWorkEmail(existingProfile.getWorkEmail())).thenReturn(Optional.of(existingProfile)); - when(memberRepo.update(existingProfile)).thenReturn(existingProfile); + MemberProfile existingProfile = createADefaultMemberProfile(); MemberProfile result = memberProfileServices.saveProfile(existingProfile); @@ -309,81 +270,71 @@ void testUpdateProfileWithNoChange() { } @Test - @Tag("mocked") void testEmailAssignmentWithValidPDL() { - UUID pdlId = UUID.randomUUID(); - MemberProfile member = new MemberProfile(UUID.randomUUID(), "John", null, "Smith", null, null, pdlId, null, "john.smith@example.com", - null, null, null, null, null, null, null, null, null); - MemberProfile pdlProfile = new MemberProfile(UUID.randomUUID(), "Jane", null, "Doe", null, null, null, null, "jane.doe@example.com", - null, null, null, null, null, null, null, null, null); - - when(memberRepo.findById(pdlId)).thenReturn(Optional.of(pdlProfile)); + MemberProfile pdlProfile = createADefaultMemberProfile(); + MemberProfile member = createADefaultMemberProfileForPdl(pdlProfile); memberProfileServices.emailAssignment(member, true); assertEquals(1, emailSender.events.size()); assertEquals( - List.of("SEND_EMAIL", "null", "null", "You have been assigned as the PDL of John Smith", "John Smith will now report to you as their PDL. Please engage with them: john.smith@example.com", pdlProfile.getWorkEmail()), + List.of("SEND_EMAIL", "null", "null", + String.format("You have been assigned as the PDL of %s %s", member.getFirstName(), member.getLastName()), + String.format("%s %s will now report to you as their PDL. Please engage with them: %s", member.getFirstName(), member.getLastName(), member.getWorkEmail()), + pdlProfile.getWorkEmail()), emailSender.events.getFirst() ); } @Test - @Tag("mocked") void testEmailAssignmentWithValidSupervisor() { - UUID supervisorId = UUID.randomUUID(); - MemberProfile member = new MemberProfile(UUID.randomUUID(), "John", null, "Smith", null, null, null, null, "john.smith@example.com", - null, null, null, supervisorId, null, null, null, null, null); - MemberProfile supervisorProfile = new MemberProfile(UUID.randomUUID(), "Jane", null, "Doe", null, null, null, null, "jane.doe@example.com", - null, null, null, null, null, null, null, null, null); - - when(memberRepo.findById(supervisorId)).thenReturn(Optional.of(supervisorProfile)); + MemberProfile pdlProfile = createADefaultMemberProfile(); + MemberProfile supervisorProfile = createADefaultSupervisor(); + MemberProfile member = createAProfileWithSupervisorAndPDL( + supervisorProfile, pdlProfile); memberProfileServices.emailAssignment(member, false); assertEquals(1, emailSender.events.size()); assertEquals( - List.of("SEND_EMAIL", "null", "null", "You have been assigned as the supervisor of John Smith", "John Smith will now report to you as their supervisor. Please engage with them: john.smith@example.com", supervisorProfile.getWorkEmail()), + List.of("SEND_EMAIL", "null", "null", + String.format("You have been assigned as the supervisor of %s %s", member.getFirstName(), member.getLastName()), + String.format("%s %s will now report to you as their supervisor. Please engage with them: %s", member.getFirstName(), member.getLastName(), member.getWorkEmail()), + supervisorProfile.getWorkEmail()), emailSender.events.getFirst() ); } @Test - @Tag("mocked") void testEmailAssignmentWithValidPdlAndSupervisor() { - UUID pdlId = UUID.randomUUID(); - UUID supervisorId = UUID.randomUUID(); - MemberProfile member = new MemberProfile(UUID.randomUUID(), "John", null, "Smith", null, null, pdlId, null, "john.smith@example.com", - null, null, null, supervisorId, null, null, null, null, null); - MemberProfile pdlProfile = new MemberProfile(UUID.randomUUID(), "Jane", null, "Doe", null, null, null, null, "jane.doe@example.com", - null, null, null, null, null, null, null, null, null); - - MemberProfile supervisorProfile = new MemberProfile(UUID.randomUUID(), "Janine", null, "Doe", null, null, null, null, "janine.doe@example.com", - null, null, null, null, null, null, null, null, null); - - when(memberRepo.findById(pdlId)).thenReturn(Optional.of(pdlProfile)); - when(memberRepo.findById(supervisorId)).thenReturn(Optional.of(supervisorProfile)); + MemberProfile pdlProfile = createADefaultMemberProfile(); + MemberProfile supervisorProfile = createADefaultSupervisor(); + MemberProfile member = createAProfileWithSupervisorAndPDL( + supervisorProfile, pdlProfile); memberProfileServices.emailAssignment(member, true); // for PDL memberProfileServices.emailAssignment(member, false); // for supervisor assertEquals(2, emailSender.events.size()); assertEquals(List.of( - List.of("SEND_EMAIL", "null", "null", "You have been assigned as the PDL of John Smith", "John Smith will now report to you as their PDL. Please engage with them: john.smith@example.com", pdlProfile.getWorkEmail()), - List.of("SEND_EMAIL", "null", "null", "You have been assigned as the supervisor of John Smith", "John Smith will now report to you as their supervisor. Please engage with them: john.smith@example.com", supervisorProfile.getWorkEmail()) + List.of("SEND_EMAIL", "null", "null", + String.format("You have been assigned as the PDL of %s %s", member.getFirstName(), member.getLastName()), + String.format("%s %s will now report to you as their PDL. Please engage with them: %s", member.getFirstName(), member.getLastName(), member.getWorkEmail()), + pdlProfile.getWorkEmail()), + List.of("SEND_EMAIL", "null", "null", + String.format("You have been assigned as the supervisor of %s %s", member.getFirstName(), member.getLastName()), + String.format("%s %s will now report to you as their supervisor. Please engage with them: %s", member.getFirstName(), member.getLastName(), member.getWorkEmail()), + supervisorProfile.getWorkEmail()) ), emailSender.events ); } @Test - @Tag("mocked") void testEmailAssignmentWithInvalidPDL() { + MemberProfile existingProfile = createADefaultMemberProfile(); UUID pdlId = UUID.randomUUID(); - MemberProfile member = new MemberProfile(UUID.randomUUID(), "John", null, "Smith", null, null, pdlId, null, "john.smith@example.com", - null, null, null, null, null, null, null, null, null); - - when(memberRepo.findById(pdlId)).thenReturn(Optional.empty()); + MemberProfile member = new MemberProfile(existingProfile.getId(), existingProfile.getFirstName(), null, existingProfile.getLastName(), null, null, pdlId, null, existingProfile.getWorkEmail(), null, null, null, null, null, null, null, null, null); memberProfileServices.emailAssignment(member, true); @@ -391,13 +342,10 @@ void testEmailAssignmentWithInvalidPDL() { } @Test - @Tag("mocked") void testEmailAssignmentWithInvalidSupervisor() { + MemberProfile existingProfile = createADefaultMemberProfile(); UUID supervisorId = UUID.randomUUID(); - MemberProfile member = new MemberProfile(UUID.randomUUID(), "John", null, "Smith", null, null, null, null, "john.smith@example.com", - null, null, null, supervisorId, null, null, null, null, null); - - when(memberRepo.findById(supervisorId)).thenReturn(Optional.empty()); + MemberProfile member = new MemberProfile(existingProfile.getId(), existingProfile.getFirstName(), null, existingProfile.getLastName(), null, null, null, null, existingProfile.getWorkEmail(), null, null, null, supervisorId, null, null, null, null, null); memberProfileServices.emailAssignment(member, true); @@ -405,7 +353,6 @@ void testEmailAssignmentWithInvalidSupervisor() { } @Test - @Tag("mocked") void testEmailAssignmentWithInvalidMember() { MemberProfile member = new MemberProfile(UUID.randomUUID(), "John", null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null); @@ -416,7 +363,6 @@ void testEmailAssignmentWithInvalidMember() { } @Test - @Tag("mocked") void testEmailAssignmentWithNullRoleId() { MemberProfile member = new MemberProfile(UUID.randomUUID(), "John", null, "Smith", null, null, null, null, "john.smith@example.com", null, null, null, null, null, null, null, null, null); diff --git a/server/src/test/java/com/objectcomputing/checkins/services/memberprofile/csvreport/MemberProfileReportServicesImplTest.java b/server/src/test/java/com/objectcomputing/checkins/services/memberprofile/csvreport/MemberProfileReportServicesImplTest.java index 244d78cf4e..f8310b8fcb 100644 --- a/server/src/test/java/com/objectcomputing/checkins/services/memberprofile/csvreport/MemberProfileReportServicesImplTest.java +++ b/server/src/test/java/com/objectcomputing/checkins/services/memberprofile/csvreport/MemberProfileReportServicesImplTest.java @@ -1,6 +1,9 @@ package com.objectcomputing.checkins.services.memberprofile.csvreport; import com.objectcomputing.checkins.services.TestContainersSuite; +import com.objectcomputing.checkins.services.fixture.MemberProfileFixture; +import com.objectcomputing.checkins.services.memberprofile.MemberProfile; +import com.objectcomputing.checkins.services.memberprofile.MemberProfileServices; import org.apache.commons.csv.CSVFormat; import org.apache.commons.csv.CSVParser; import org.apache.commons.csv.CSVRecord; @@ -8,11 +11,8 @@ import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.condition.DisabledInNativeImage; -import org.mockito.InjectMocks; -import org.mockito.Mock; -import org.mockito.Mockito; -import org.mockito.MockitoAnnotations; + +import jakarta.inject.Inject; import java.io.File; import java.io.FileReader; @@ -25,46 +25,20 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.mockito.Mockito.when; - -// Disabled in nativeTest, as we get an exception from Mockito -// => java.lang.NoClassDefFoundError: Could not initialize class org.mockito.internal.configuration.plugins.Plugins -@DisabledInNativeImage -class MemberProfileReportServicesImplTest extends TestContainersSuite { - @Mock - private MemberProfileReportRepository memberProfileReportRepository; +class MemberProfileReportServicesImplTest extends TestContainersSuite + implements MemberProfileFixture { + @Inject + private MemberProfileServices memberProfileServices; - @Mock - private MemberProfileFileProvider memberProfileFileProvider; - - @InjectMocks + @Inject private MemberProfileReportServicesImpl memberProfileReportServices; - private AutoCloseable mockFinalizer; - - @BeforeAll - void initMocks() { - mockFinalizer = MockitoAnnotations.openMocks(this); - } - - @BeforeEach - void resetMocks() { - Mockito.reset(memberProfileReportRepository); - } - - @AfterAll - void close() throws Exception { - mockFinalizer.close(); - } - @Test void testGenerateFileWithAllMemberProfiles() throws IOException { List expectedRecords = createSampleRecords(); - when(memberProfileReportRepository.findAll()).thenReturn(expectedRecords); File tmpFile = File.createTempFile("member",".csv"); tmpFile.deleteOnExit(); - when(memberProfileFileProvider.provideFile()).thenReturn(tmpFile); // Generate a file with all members File file = memberProfileReportServices.generateFile(null); @@ -85,12 +59,9 @@ void testGenerateFileWithAllMemberProfiles() throws IOException { void testGenerateFileWithSelectedMemberProfiles() throws IOException { List allRecords = createSampleRecords(); MemberProfileRecord expectedRecord = allRecords.get(1); - when(memberProfileReportRepository - .findAllByMemberIds(List.of(expectedRecord.getId().toString()))) - .thenReturn(List.of(expectedRecord)); File tmpFile = File.createTempFile("member",".csv"); tmpFile.deleteOnExit(); - when(memberProfileFileProvider.provideFile()).thenReturn(tmpFile); + // Generate a file with selected members MemberProfileReportQueryDTO dto = new MemberProfileReportQueryDTO(); dto.setMemberIds(List.of(expectedRecord.getId())); @@ -104,26 +75,6 @@ void testGenerateFileWithSelectedMemberProfiles() throws IOException { assertRecordEquals(expectedRecord, csvRecord1); } - @Test - void testGenerateFileNotGenerated() throws IOException { - List allRecords = createSampleRecords(); - MemberProfileRecord expectedRecord = allRecords.get(1); - when(memberProfileReportRepository - .findAllByMemberIds(List.of(expectedRecord.getId().toString()))) - .thenReturn(List.of(expectedRecord)); - - when(memberProfileFileProvider.provideFile()).thenThrow(new RuntimeException()); - // Generate a file with selected members - MemberProfileReportQueryDTO dto = new MemberProfileReportQueryDTO(); - dto.setMemberIds(List.of(expectedRecord.getId())); - - assertThrows(RuntimeException.class, () -> { - memberProfileReportServices.generateFile(dto); - }); - } - - - private static void assertRecordEquals(MemberProfileRecord record, CSVRecord csvRecord) { assertEquals(record.getFirstName(), csvRecord.get("First Name")); assertEquals(record.getLastName(), csvRecord.get("Last Name")); @@ -146,45 +97,43 @@ static List parseRecordsFromFile(File file) throws IOException { return parser.getRecords(); } - private static List createSampleRecords() { + MemberProfileRecord from(MemberProfile profile) { MemberProfileRecord record1 = new MemberProfileRecord(); - record1.setId(UUID.randomUUID()); - record1.setFirstName("John"); - record1.setLastName("Doe"); - record1.setTitle("Software Engineer"); - record1.setLocation("St. Louis"); - record1.setWorkEmail("johndoe@objectcomputing.com"); - record1.setStartDate(LocalDate.of(2024, 1, 1)); - record1.setPdlName("Jane Miller"); - record1.setPdlEmail("janemiller@objectcomputing.com"); - record1.setSupervisorName("Tom Smith"); - record1.setSupervisorEmail("tomsmith@objectcomputing.com"); - - MemberProfileRecord record2 = new MemberProfileRecord(); - record2.setId(UUID.randomUUID()); - record2.setFirstName("Jane"); - record2.setLastName("Miller"); - record2.setTitle("Principal Software Engineer"); - record2.setLocation("St. Louis"); - record2.setWorkEmail("janemiller@objectcomputing.com"); - record2.setStartDate(LocalDate.of(2023, 1, 1)); - record2.setPdlName("Eve Williams"); - record2.setPdlEmail("evewilliams@objectcomputing.com"); - record2.setSupervisorName("Tom Smith"); - record2.setSupervisorEmail("tomsmith@objectcomputing.com"); - - MemberProfileRecord record3 = new MemberProfileRecord(); - record3.setId(UUID.randomUUID()); - record3.setFirstName("Tom"); - record3.setLastName("Smith"); - record3.setTitle("Manager, HR, and Head of Sales"); - record3.setLocation("New York City, New York"); - record3.setWorkEmail("tomsmith@objectcomputing.com"); - record3.setStartDate(LocalDate.of(2022, 1, 1)); - record3.setPdlName(null); - record3.setPdlEmail(null); - record3.setSupervisorName(null); - record3.setSupervisorEmail(null); + record1.setId(profile.getId()); + record1.setFirstName(profile.getFirstName()); + record1.setLastName(profile.getLastName()); + record1.setTitle(profile.getTitle()); + record1.setLocation(profile.getLocation()); + record1.setWorkEmail(profile.getWorkEmail()); + record1.setStartDate(profile.getStartDate()); + UUID pdlId = profile.getPdlId(); + if (pdlId != null) { + MemberProfile pdl = memberProfileServices.getById(pdlId); + if (pdl != null) { + record1.setPdlName(pdl.getFirstName() + " " + pdl.getLastName()); + record1.setPdlEmail(pdl.getWorkEmail()); + } + } + UUID supervisorId = profile.getSupervisorid(); + if (supervisorId != null) { + MemberProfile supervisor = + memberProfileServices.getById(supervisorId); + if (supervisor != null) { + record1.setSupervisorName(supervisor.getFirstName() + " " + + supervisor.getLastName()); + record1.setSupervisorEmail(supervisor.getWorkEmail()); + } + } + return record1; + } + + private List createSampleRecords() { + // The createADefaultMemberProfileForPdl() method actually sets both + // the PDL and Supervisor to the id of the member profile passed in. + MemberProfile pdl = createADefaultMemberProfile(); + MemberProfileRecord record1 = from(pdl); + MemberProfileRecord record2 = from(createADefaultMemberProfileForPdl(pdl)); + MemberProfileRecord record3 = from(createAThirdDefaultMemberProfile()); return List.of(record1, record2, record3); } diff --git a/server/src/test/java/com/objectcomputing/checkins/services/memberprofile/currentuser/CurrentUserControllerTest.java b/server/src/test/java/com/objectcomputing/checkins/services/memberprofile/currentuser/CurrentUserControllerTest.java index 0f8107236b..69737baef6 100644 --- a/server/src/test/java/com/objectcomputing/checkins/services/memberprofile/currentuser/CurrentUserControllerTest.java +++ b/server/src/test/java/com/objectcomputing/checkins/services/memberprofile/currentuser/CurrentUserControllerTest.java @@ -14,48 +14,22 @@ import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.condition.DisabledInNativeImage; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; import java.util.HashMap; import java.util.List; import java.util.Map; -import static com.objectcomputing.checkins.services.memberprofile.MemberProfileTestUtil.mkMemberProfile; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.mockito.Mockito.when; -// Disabled in nativeTest, as we get an exception from Mockito -// => java.lang.NoClassDefFoundError: Could not initialize class org.mockito.internal.configuration.plugins.Plugins -@DisabledInNativeImage class CurrentUserControllerTest extends TestContainersSuite implements MemberProfileFixture, RoleFixture { private static final Map userAttributes = new HashMap<>(); - private static final String firstName = "some.first.name"; - private static final String lastName = "some.last.name"; - private static final String userEmail = "some.email.address"; private static final String imageUrl = "some.picture.url"; - @Mock - CurrentUserServices currentUserServices; - @Inject CurrentUserController currentUserController; - private AutoCloseable mockFinalizer; - - @BeforeAll - void setupMocks() { - mockFinalizer = MockitoAnnotations.openMocks(this); - } - - @AfterAll - void close() throws Exception { - mockFinalizer.close(); - } - @Test void testCurrentUserReturnsUnauthorizedWhenAuthenticationFails() { HttpResponse response = currentUserController.currentUser(null); @@ -64,12 +38,14 @@ void testCurrentUserReturnsUnauthorizedWhenAuthenticationFails() { @Test void testCurrentUserReturnsValidDTO() { + final MemberProfile expected = createADefaultMemberProfile(); Authentication auth = new Authentication() { @NonNull @Override public Map getAttributes() { - userAttributes.put("name", firstName + ' ' + lastName); - userAttributes.put("email", userEmail); + userAttributes.put("name", expected.getFirstName() + ' ' + + expected.getLastName()); + userAttributes.put("email", expected.getWorkEmail()); userAttributes.put("picture", imageUrl); return userAttributes; } @@ -80,21 +56,14 @@ public String getName() { } }; - MemberProfile expected = mkMemberProfile(); - expected.setWorkEmail(userEmail); - expected.setFirstName(firstName); - expected.setLastName(lastName); - - when(currentUserServices.findOrSaveUser(firstName, lastName, userEmail)).thenReturn(expected); - HttpResponse actual = currentUserController.currentUser(auth); assertEquals(HttpStatus.OK, actual.getStatus()); CurrentUserDTO currentUserDTO = actual.body(); assertNotNull(currentUserDTO); - assertEquals(userEmail, currentUserDTO.getMemberProfile().getWorkEmail()); - assertEquals(firstName, currentUserDTO.getFirstName()); - assertEquals(lastName, currentUserDTO.getLastName()); + assertEquals(expected.getWorkEmail(), currentUserDTO.getMemberProfile().getWorkEmail()); + assertEquals(expected.getFirstName(), currentUserDTO.getFirstName()); + assertEquals(expected.getLastName(), currentUserDTO.getLastName()); assertEquals(imageUrl, currentUserDTO.getImageUrl()); assertNotNull(actual.getHeaders().get("location")); } diff --git a/server/src/test/java/com/objectcomputing/checkins/services/memberprofile/currentuser/CurrentUserServicesImplTest.java b/server/src/test/java/com/objectcomputing/checkins/services/memberprofile/currentuser/CurrentUserServicesImplTest.java index 47abbcbe6e..6ee5bc2541 100644 --- a/server/src/test/java/com/objectcomputing/checkins/services/memberprofile/currentuser/CurrentUserServicesImplTest.java +++ b/server/src/test/java/com/objectcomputing/checkins/services/memberprofile/currentuser/CurrentUserServicesImplTest.java @@ -1,64 +1,37 @@ package com.objectcomputing.checkins.services.memberprofile.currentuser; import com.objectcomputing.checkins.services.TestContainersSuite; +import com.objectcomputing.checkins.services.fixture.RoleFixture; +import com.objectcomputing.checkins.services.fixture.MemberProfileFixture; import com.objectcomputing.checkins.services.memberprofile.MemberProfile; import com.objectcomputing.checkins.services.memberprofile.MemberProfileRepository; import com.objectcomputing.checkins.services.role.Role; import com.objectcomputing.checkins.services.role.RoleServices; import com.objectcomputing.checkins.services.role.RoleType; import com.objectcomputing.checkins.services.role.member_roles.MemberRoleServices; -import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.condition.DisabledInNativeImage; -import org.mockito.InjectMocks; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; + +import jakarta.inject.Inject; import java.util.UUID; -import static com.objectcomputing.checkins.services.memberprofile.MemberProfileTestUtil.mkMemberProfile; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.mockito.Mockito.any; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -// Disabled in nativeTest, as we get an exception from Mockito -// => java.lang.NoClassDefFoundError: Could not initialize class org.mockito.internal.configuration.plugins.Plugins -@DisabledInNativeImage -class CurrentUserServicesImplTest extends TestContainersSuite { - - @Mock - MemberProfileRepository memberProfileRepo; - @Mock - RoleServices roleServices; +class CurrentUserServicesImplTest extends TestContainersSuite + implements MemberProfileFixture, RoleFixture { - @Mock - MemberRoleServices memberRoleServices; - - @InjectMocks + @Inject CurrentUserServicesImpl testObject; - private AutoCloseable mockFinalizer; - - @BeforeAll - public void before() { - mockFinalizer = MockitoAnnotations.openMocks(this); - } - - @AfterAll - public void after() throws Exception { - mockFinalizer.close(); + @BeforeEach + void createRolesAndPermissions() { + createAndAssignRoles(); } @Test void testFindOrSaveUserForNewUser() { - MemberProfile expected = mkMemberProfile(); - expected.setWorkEmail("test.email"); - - when(memberProfileRepo.findByWorkEmail(expected.getWorkEmail())).thenReturn(java.util.Optional.of(expected)); + MemberProfile expected = createADefaultMemberProfile(); MemberProfile actual = testObject.findOrSaveUser(expected.getFirstName(), expected.getLastName(), expected.getWorkEmail()); @@ -67,18 +40,11 @@ void testFindOrSaveUserForNewUser() { @Test void testFindOrSaveUserForExistingUser() { - MemberProfile expected = mkMemberProfile(); - expected.setId(UUID.randomUUID()); - expected.setWorkEmail("test.email"); - Role mockRole = new Role(RoleType.MEMBER.name(), "role description"); - - when(memberProfileRepo.findByWorkEmail(expected.getWorkEmail())).thenReturn(java.util.Optional.empty()); - when(memberProfileRepo.save(any())).thenReturn(expected); - when(roleServices.save(mockRole)).thenReturn(mockRole); + MemberProfile expected = createADefaultMemberProfile(); + assignMemberRole(expected); MemberProfile actual = testObject.findOrSaveUser(expected.getFirstName(), expected.getLastName(), expected.getWorkEmail()); assertEquals(expected, actual); - verify(roleServices, times(1)).save(any(Role.class)); } } diff --git a/server/src/test/java/com/objectcomputing/checkins/services/memberprofile/memberphoto/MemberPhotoControllerTest.java b/server/src/test/java/com/objectcomputing/checkins/services/memberprofile/memberphoto/MemberPhotoControllerTest.java index a16bddb9e5..43384f4635 100644 --- a/server/src/test/java/com/objectcomputing/checkins/services/memberprofile/memberphoto/MemberPhotoControllerTest.java +++ b/server/src/test/java/com/objectcomputing/checkins/services/memberprofile/memberphoto/MemberPhotoControllerTest.java @@ -1,15 +1,16 @@ package com.objectcomputing.checkins.services.memberprofile.memberphoto; import com.objectcomputing.checkins.services.TestContainersSuite; +import com.objectcomputing.checkins.services.GooglePhotoAccessorImplReplacement; +import io.micronaut.core.util.StringUtils; +import io.micronaut.context.annotation.Property; import io.micronaut.http.HttpRequest; import io.micronaut.http.HttpResponse; import io.micronaut.http.HttpStatus; import io.micronaut.http.client.HttpClient; import io.micronaut.http.client.annotation.Client; -import io.micronaut.test.annotation.MockBean; import jakarta.inject.Inject; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.condition.DisabledInNativeImage; import java.util.Base64; @@ -17,14 +18,8 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; -// Disabled in nativeTest, as we get an exception from Mockito -// => Message: Could not initialize class org.mockito.Mockito -@DisabledInNativeImage +@Property(name = "replace.googlephotoaccessorimpl", value = StringUtils.TRUE) class MemberPhotoControllerTest extends TestContainersSuite { @Inject @@ -32,15 +27,15 @@ class MemberPhotoControllerTest extends TestContainersSuite { private HttpClient client; @Inject - GooglePhotoAccessor googlePhotoAccessor; + GooglePhotoAccessorImplReplacement googlePhotoAccessor; @Test void testGetForValidInput() { String testEmail = "test@test.com"; - byte[] testData = Base64.getUrlEncoder().encode("test.photo.data".getBytes()); - - when(googlePhotoAccessor.getPhotoData(testEmail)).thenReturn(testData); + String testPhotoData = "test.photo.data"; + byte[] testData = Base64.getUrlEncoder().encode(testPhotoData.getBytes()); + googlePhotoAccessor.setPhotoData(testEmail, testData); final HttpRequest request = HttpRequest.GET(String.format("/%s", testEmail)).basicAuth(MEMBER_ROLE, MEMBER_ROLE); final HttpResponse response = client.toBlocking().exchange(request, byte[].class); @@ -51,14 +46,6 @@ void testGetForValidInput() { assertEquals(HttpStatus.OK, response.getStatus()); assertTrue(response.getBody().isPresent()); byte[] result = response.getBody().get(); - assertEquals(new String(testData), new String(result)); - - // Only called once due to the cache - verify(googlePhotoAccessor, times(1)).getPhotoData(testEmail); - } - - @MockBean(GooglePhotoAccessorImpl.class) - public GooglePhotoAccessor googlePhotoAccessor() { - return mock(GooglePhotoAccessor.class); + assertEquals(new String(testPhotoData), new String(result)); } } diff --git a/server/src/test/java/com/objectcomputing/checkins/services/memberprofile/memberphoto/MemberPhotoServiceImplTest.java b/server/src/test/java/com/objectcomputing/checkins/services/memberprofile/memberphoto/MemberPhotoServiceImplTest.java index 2108e86bc0..680d30428c 100644 --- a/server/src/test/java/com/objectcomputing/checkins/services/memberprofile/memberphoto/MemberPhotoServiceImplTest.java +++ b/server/src/test/java/com/objectcomputing/checkins/services/memberprofile/memberphoto/MemberPhotoServiceImplTest.java @@ -1,20 +1,15 @@ package com.objectcomputing.checkins.services.memberprofile.memberphoto; import com.google.api.client.googleapis.json.GoogleJsonResponseException; -import com.google.api.services.directory.Directory; import com.google.api.services.directory.model.UserPhoto; import com.objectcomputing.checkins.services.TestContainersSuite; +import com.objectcomputing.checkins.services.GooglePhotoAccessorImplReplacement; import com.objectcomputing.checkins.services.memberprofile.MemberProfileServices; -import com.objectcomputing.checkins.util.googleapiaccess.GoogleApiAccess; -import io.micronaut.context.env.Environment; -import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.BeforeAll; +import io.micronaut.core.util.StringUtils; +import io.micronaut.context.annotation.Property; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.condition.DisabledInNativeImage; -import org.mockito.InjectMocks; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; +import jakarta.inject.Inject; import java.io.IOException; import java.nio.charset.StandardCharsets; @@ -22,64 +17,19 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.mockito.Mockito.reset; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; -// Disabled in nativeTest, as we get an exception from Mockito -// => java.lang.NoClassDefFoundError: Could not initialize class org.mockito.internal.configuration.plugins.Plugins -@DisabledInNativeImage +@Property(name = "replace.googlephotoaccessorimpl", value = StringUtils.TRUE) class MemberPhotoServiceImplTest extends TestContainersSuite { - @Mock - private MemberProfileServices mockMemberProfileServices; - - @Mock - private GoogleApiAccess mockGoogleApiAccess; - - @Mock - private Directory mockDirectory; - - @Mock - private Directory.Users mockUsers; - - @Mock - private Directory.Users.Photos mockPhotos; - - @Mock - private Directory.Users.Photos.Get mockGet; - - @Mock - private Environment mockEnvironment; - - @InjectMocks - private GooglePhotoAccessorImpl accessor; + @Inject + private GooglePhotoAccessorImplReplacement googlePhotoAccessorImpl; + @Inject private MemberPhotoServiceImpl service; - private AutoCloseable mockFinalizer; - - @BeforeAll - void initMocks() { - mockFinalizer = MockitoAnnotations.openMocks(this); - } - - @AfterAll - void close() throws Exception { - mockFinalizer.close(); - } - @BeforeEach - void resetMocks() { - reset(mockMemberProfileServices); - reset(mockGoogleApiAccess); - reset(mockDirectory); - reset(mockUsers); - reset(mockPhotos); - reset(mockGet); - reset(mockEnvironment); - service = new MemberPhotoServiceImpl(accessor); + void reset() { + googlePhotoAccessorImpl.reset(); } // happy path @@ -96,36 +46,20 @@ void testGetImageByEmailAddress() throws IOException { testUserPhoto.setKind("test.kind"); testUserPhoto.setMimeType("test.mime.type"); testUserPhoto.setPhotoData(new String(testData)); - - when(mockGoogleApiAccess.getDirectory()).thenReturn(mockDirectory); - when(mockDirectory.users()).thenReturn(mockUsers); - when(mockUsers.photos()).thenReturn(mockPhotos); - when(mockPhotos.get(testEmail)).thenReturn(mockGet); - when(mockGet.execute()).thenReturn(testUserPhoto); - + googlePhotoAccessorImpl.setUserPhoto(testEmail, testUserPhoto); final byte[] result = service.getImageByEmailAddress(testEmail); assertNotNull(result); assertEquals(testPhotoData, new String(result, StandardCharsets.UTF_8)); - verify(mockGoogleApiAccess, times(1)).getDirectory(); - verify(mockGet, times(1)).execute(); } @Test void testDirectoryServiceThrowsGoogleJsonResponseException() throws IOException { - String testEmail = "test@test.com"; - - when(mockGoogleApiAccess.getDirectory()).thenReturn(mockDirectory); - when(mockDirectory.users()).thenReturn(mockUsers); - when(mockUsers.photos()).thenReturn(mockPhotos); - when(mockPhotos.get(testEmail)).thenReturn(mockGet); - when(mockGet.execute()).thenThrow(GoogleJsonResponseException.class); + String testEmail = "notcached@test.com"; final byte[] result = service.getImageByEmailAddress(testEmail); assertNotNull(result); assertEquals("", new String(result, StandardCharsets.UTF_8)); - verify(mockGoogleApiAccess, times(1)).getDirectory(); - verify(mockGet, times(1)).execute(); } } diff --git a/server/src/test/java/com/objectcomputing/checkins/services/pulseresponse/PulseResponseTest.java b/server/src/test/java/com/objectcomputing/checkins/services/pulseresponse/PulseResponseTest.java index e56aa79a57..e2a55bf163 100644 --- a/server/src/test/java/com/objectcomputing/checkins/services/pulseresponse/PulseResponseTest.java +++ b/server/src/test/java/com/objectcomputing/checkins/services/pulseresponse/PulseResponseTest.java @@ -1,8 +1,11 @@ package com.objectcomputing.checkins.services.pulseresponse; +import com.objectcomputing.checkins.services.fixture.RoleFixture; +import com.objectcomputing.checkins.services.fixture.MemberProfileFixture; import com.objectcomputing.checkins.exceptions.BadArgException; import com.objectcomputing.checkins.notifications.email.MailJetFactory; import com.objectcomputing.checkins.services.MailJetFactoryReplacement; +import com.objectcomputing.checkins.services.CurrentUserServicesReplacement; import com.objectcomputing.checkins.services.TestContainersSuite; import com.objectcomputing.checkins.services.memberprofile.MemberProfile; import com.objectcomputing.checkins.services.memberprofile.MemberProfileRepository; @@ -19,8 +22,6 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.condition.DisabledInNativeImage; -import org.mockito.Mockito; import java.time.LocalDate; import java.util.Collections; @@ -33,16 +34,10 @@ import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; @Property(name = "replace.mailjet.factory", value = StringUtils.TRUE) -// Disabled in nativeTest, as we get an exception from Mockito -// => java.lang.NoClassDefFoundError: Could not initialize class org.mockito.Mockito -@DisabledInNativeImage -class PulseResponseTest extends TestContainersSuite { +@Property(name = "replace.currentuserservices", value = StringUtils.TRUE) +class PulseResponseTest extends TestContainersSuite implements MemberProfileFixture, RoleFixture { @Inject protected Validator validator; @@ -51,41 +46,34 @@ class PulseResponseTest extends TestContainersSuite { @Named(MailJetFactory.HTML_FORMAT) private MailJetFactoryReplacement.MockEmailSender emailSender; - private MemberProfileServices memberProfileServices; - private MemberProfileRepository memberRepo; - private CurrentUserServices currentUserServices; - private RolePermissionServices rolePermissionServices; + @Inject + CurrentUserServicesReplacement currentUserServices; + + @Inject private PulseResponseServicesImpl pulseResponseService; - private PulseResponseRepository pulseResponseRepo; + + private MemberProfile regular; + private MemberProfile another; + private MemberProfile pdlProfile; + private MemberProfile supervisorProfile; @BeforeEach - @Tag("mocked") void setUp() { - pulseResponseRepo = Mockito.mock(PulseResponseRepository.class); - memberProfileServices = Mockito.mock(MemberProfileServices.class); - memberRepo = Mockito.mock(MemberProfileRepository.class); - currentUserServices = Mockito.mock(CurrentUserServices.class); - rolePermissionServices = Mockito.mock(RolePermissionServices.class); + createAndAssignRoles(); + + pdlProfile = createADefaultMemberProfile(); + regular = createADefaultMemberProfileForPdl(pdlProfile); + supervisorProfile = createADefaultSupervisor(); + another = createAProfileWithSupervisorAndPDL(supervisorProfile, + pdlProfile); + currentUserServices.currentUser = regular; emailSender.reset(); - - pulseResponseService = Mockito.spy(new PulseResponseServicesImpl(pulseResponseRepo, - memberProfileServices, - memberRepo, - currentUserServices, - rolePermissionServices, - emailSender)); - } - - @AfterEach - @Tag("mocked") - void tearDown() { - Mockito.reset(memberRepo, memberProfileServices); } @Test void testPulseResponseInstantiation() { LocalDate submissionDate = LocalDate.of(2019, 1, 1); - final UUID teamMemberId = UUID.randomUUID(); + final UUID teamMemberId = regular.getId(); final String internalFeelings = "exampleId"; final String externalFeelings = "exampleId2"; PulseResponse pulseResponse = new PulseResponse(1, 2, submissionDate, teamMemberId, internalFeelings, externalFeelings); @@ -97,7 +85,7 @@ void testPulseResponseInstantiation() { @Test void testConstraintViolation() { LocalDate submissionDate = LocalDate.of(2019, 1, 1); - final UUID teamMemberId = UUID.randomUUID(); + final UUID teamMemberId = regular.getId(); final String internalFeelings = "exampleId"; final String externalFeelings = "exampleId2"; final Integer internalScore = 1; @@ -120,7 +108,7 @@ void testEquals() { final Integer internalScore = 1; final Integer externalScore = 2; LocalDate submissionDate = LocalDate.of(2019, 1, 1); - final UUID teamMemberId = UUID.randomUUID(); + final UUID teamMemberId = regular.getId(); final String internalFeelings = "exampleId"; final String externalFeelings = "exampleId2"; @@ -153,7 +141,7 @@ void testEquals() { void testToString() { final UUID id = UUID.randomUUID(); LocalDate submissionDate = LocalDate.of(2019, 1, 1); - final UUID teamMemberId = UUID.randomUUID(); + final UUID teamMemberId = regular.getId(); final Integer internalScore = 1; final Integer externalScore = 2; final String internalFeelings = "exampleId"; @@ -168,56 +156,31 @@ void testToString() { } @Test - @Tag("mocked") void testSaveWithValidPulseResponse() { - UUID currentUserId = UUID.randomUUID(); - UUID memberId = UUID.randomUUID(); + UUID memberId = regular.getId(); LocalDate pulseSubDate = LocalDate.now(); PulseResponse pulseResponse = new PulseResponse(); pulseResponse.setTeamMemberId(memberId); pulseResponse.setSubmissionDate(pulseSubDate); - MemberProfile memberProfile = new MemberProfile(currentUserId, "John", null, "Doe", - null, null, null, null, "john@oci.com", - null, null, null, null, - null, null, null, null, null); - - MemberProfile memberProfile2 = new MemberProfile(memberId, "Jane", null, "Doe", - null, null, null, null, "jane@oci.com", - null, null, null, currentUserId, - null, null, null, null, null); - - when(currentUserServices.getCurrentUser()).thenReturn(memberProfile); - when(memberRepo.findById(memberId)).thenReturn(Optional.of(memberProfile2)); - when(memberProfileServices.getSubordinatesForId(currentUserId)).thenReturn(Collections.singletonList(memberProfile2)); - - PulseResponse savedResponse = new PulseResponse(); // Assuming this is a valid saved response - when(pulseResponseRepo.save(pulseResponse)).thenReturn(savedResponse); - + PulseResponse savedResponse = new PulseResponse(); PulseResponse result = pulseResponseService.save(pulseResponse); - assertEquals(savedResponse, result); - verify(pulseResponseRepo, times(1)).save(pulseResponse); - verify(pulseResponseService, times(1)).sendPulseLowScoreEmail(savedResponse); + assertTrue(result.getId() != null); + assertEquals(memberId, result.getTeamMemberId()); + assertEquals(LocalDate.now(), result.getSubmissionDate()); } @Test - @Tag("mocked") void testSaveWithNonNullId() { - UUID currentUserId = UUID.randomUUID(); - UUID memberId = UUID.randomUUID(); + UUID memberId = regular.getId(); LocalDate pulseSubDate = LocalDate.now(); PulseResponse pulseResponse = new PulseResponse(); pulseResponse.setId(UUID.randomUUID()); // Non-null ID pulseResponse.setTeamMemberId(memberId); pulseResponse.setSubmissionDate(pulseSubDate); - MemberProfile memberProfile = new MemberProfile(currentUserId, "John", null, "Doe", - null, null, null, null, "john@oci.com", - null, null, null, null, - null, null, null, null, null); - when(currentUserServices.getCurrentUser()).thenReturn(memberProfile); BadArgException exception = assertThrows(BadArgException.class, () -> { pulseResponseService.save(pulseResponse); @@ -228,9 +191,7 @@ void testSaveWithNonNullId() { } @Test - @Tag("mocked") void testSaveWithNonExistentMember() { - UUID currentUserId = UUID.randomUUID(); UUID memberId = UUID.randomUUID(); LocalDate pulseSubDate = LocalDate.now(); @@ -238,13 +199,6 @@ void testSaveWithNonExistentMember() { pulseResponse.setTeamMemberId(memberId); pulseResponse.setSubmissionDate(pulseSubDate); - MemberProfile memberProfile = new MemberProfile(currentUserId, "John", null, "Doe", - null, null, null, null, "john@oci.com", - null, null, null, null, - null, null, null, null, null); - when(currentUserServices.getCurrentUser()).thenReturn(memberProfile); - when(memberRepo.findById(memberId)).thenReturn(Optional.empty()); // Member doesn't exist - BadArgException exception = assertThrows(BadArgException.class, () -> { pulseResponseService.save(pulseResponse); }); @@ -254,29 +208,14 @@ void testSaveWithNonExistentMember() { } @Test - @Tag("mocked") void testSaveWithInvalidDate() { - UUID currentUserId = UUID.randomUUID(); - UUID memberId = UUID.randomUUID(); + UUID memberId = regular.getId(); LocalDate pulseSubDate = LocalDate.of(0000,1,1); PulseResponse pulseResponse = new PulseResponse(); pulseResponse.setTeamMemberId(memberId); pulseResponse.setSubmissionDate(pulseSubDate); - MemberProfile memberProfile = new MemberProfile(currentUserId, "John", null, "Doe", - null, null, null, null, "john@oci.com", - null, null, null, null, - null, null, null, null, null); - - MemberProfile memberProfile2 = new MemberProfile(memberId, "Jane", null, "Doe", - null, null, null, null, "jane@oci.com", - null, null, null, currentUserId, - null, null, null, null, null); - when(currentUserServices.getCurrentUser()).thenReturn(memberProfile); - when(memberRepo.findById(memberId)).thenReturn(Optional.of(memberProfile2)); - when(memberProfileServices.getSubordinatesForId(currentUserId)).thenReturn(Collections.singletonList(memberProfile2)); - BadArgException exception = assertThrows(BadArgException.class, () -> { pulseResponseService.save(pulseResponse); }); @@ -286,29 +225,15 @@ void testSaveWithInvalidDate() { } @Test - @Tag("mocked") void testSaveWithoutPermission() { - UUID currentUserId = UUID.randomUUID(); - UUID memberId = UUID.randomUUID(); + UUID currentUserId = regular.getId(); + UUID memberId = another.getId(); LocalDate pulseSubDate = LocalDate.now(); PulseResponse pulseResponse = new PulseResponse(); pulseResponse.setTeamMemberId(memberId); pulseResponse.setSubmissionDate(pulseSubDate); - MemberProfile memberProfile = new MemberProfile(currentUserId, "John", null, "Doe", - null, null, null, null, "john@oci.com", - null, null, null, null, - null, null, null, null, null); - - MemberProfile memberProfile2 = new MemberProfile(memberId, "Jane", null, "Doe", - null, null, null, null, "jane@oci.com", - null, null, null, currentUserId, - null, null, null, null, null); - when(currentUserServices.getCurrentUser()).thenReturn(memberProfile); - when(memberRepo.findById(memberId)).thenReturn(Optional.of(memberProfile2)); - when(memberProfileServices.getSubordinatesForId(currentUserId)).thenReturn(Collections.emptyList()); // No subordinates - BadArgException exception = assertThrows(BadArgException.class, () -> { pulseResponseService.save(pulseResponse); }); @@ -318,111 +243,76 @@ void testSaveWithoutPermission() { } @Test - @Tag("mocked") void testSendPulseLowScoreEmail_NullPulseResponse() { pulseResponseService.sendPulseLowScoreEmail(null); assertEquals(0, emailSender.events.size()); } @Test - @Tag("mocked") void testSendPulseLowScoreEmail_NoLowScores() { - PulseResponse pulseResponse = new PulseResponse(3, 4, LocalDate.now(), UUID.randomUUID(), "Good", "Great"); + PulseResponse pulseResponse = new PulseResponse(3, 4, LocalDate.now(), regular.getId(), "Good", "Great"); pulseResponseService.sendPulseLowScoreEmail(pulseResponse); assertEquals(0, emailSender.events.size()); } @Test - @Tag("mocked") void testSendPulseLowScoreEmail_LowInternalScore() { - UUID teamMemberId = UUID.randomUUID(); - UUID pdlId = UUID.randomUUID(); + UUID teamMemberId = regular.getId(); + UUID pdlId = pdlProfile.getId(); PulseResponse pulseResponse = new PulseResponse(1, 3, LocalDate.now(), teamMemberId, "Sad", "Neutral"); - MemberProfile surveyTakerProfile = mock(MemberProfile.class); - MemberProfile pdlProfile = mock(MemberProfile.class); - - when(memberRepo.existsById(teamMemberId)).thenReturn(true); - when(memberProfileServices.getById(teamMemberId)).thenReturn(surveyTakerProfile); - when(surveyTakerProfile.getFirstName()).thenReturn("John"); - when(surveyTakerProfile.getLastName()).thenReturn("Doe"); - - when(surveyTakerProfile.getPdlId()).thenReturn(pdlId); - when(memberRepo.existsById(pdlId)).thenReturn(true); - when(memberProfileServices.getById(pdlId)).thenReturn(pdlProfile); - when(pdlProfile.getWorkEmail()).thenReturn("pdl@example.com"); pulseResponseService.sendPulseLowScoreEmail(pulseResponse); assertEquals(1, emailSender.events.size()); assertEquals( - List.of("SEND_EMAIL", "null", "null", "Internal pulse scores are low for team member John Doe", "Team member John Doe has left low internal pulse scores. Please consider reaching out to this employee at null
Internal Feelings: Sad
", pdlProfile.getWorkEmail()), + List.of("SEND_EMAIL", "null", "null", + String.format("Internal pulse scores are low for team member %s %s", regular.getFirstName(), regular.getLastName()), + String.format("Team member %s %s has left low internal pulse scores. Please consider reaching out to this employee at %s
Internal Feelings: Sad
", regular.getFirstName(), regular.getLastName(), regular.getWorkEmail()), + pdlProfile.getWorkEmail()), emailSender.events.getFirst() ); } @Test - @Tag("mocked") void testSendPulseLowScoreEmail_LowExternalScore() { - UUID teamMemberId = UUID.randomUUID(); - UUID pdlId = UUID.randomUUID(); + UUID teamMemberId = regular.getId(); + UUID pdlId = pdlProfile.getId(); PulseResponse pulseResponse = new PulseResponse(3, 1, LocalDate.now(), teamMemberId, "Neutral", "Sad"); - MemberProfile surveyTakerProfile = mock(MemberProfile.class); - MemberProfile pdlProfile = mock(MemberProfile.class); - - when(memberRepo.existsById(teamMemberId)).thenReturn(true); - when(memberProfileServices.getById(teamMemberId)).thenReturn(surveyTakerProfile); - when(surveyTakerProfile.getFirstName()).thenReturn("John"); - when(surveyTakerProfile.getLastName()).thenReturn("Doe"); - - when(surveyTakerProfile.getPdlId()).thenReturn(pdlId); - when(memberRepo.existsById(pdlId)).thenReturn(true); - when(memberProfileServices.getById(pdlId)).thenReturn(pdlProfile); - when(pdlProfile.getWorkEmail()).thenReturn("pdl@example.com"); - pulseResponseService.sendPulseLowScoreEmail(pulseResponse); assertEquals(1, emailSender.events.size()); assertEquals( - List.of("SEND_EMAIL", "null", "null", "External pulse scores are low for team member John Doe", "Team member John Doe has left low external pulse scores. Please consider reaching out to this employee at null
External Feelings: Sad
", pdlProfile.getWorkEmail()), + List.of("SEND_EMAIL", "null", "null", + String.format("External pulse scores are low for team member %s %s", regular.getFirstName(), regular.getLastName()), + String.format("Team member %s %s has left low external pulse scores. Please consider reaching out to this employee at %s
External Feelings: Sad
", regular.getFirstName(), regular.getLastName(), regular.getWorkEmail()), + pdlProfile.getWorkEmail()), emailSender.events.getFirst() ); } @Test - @Tag("mocked") void testSendPulseLowScoreEmail_LowInternalAndExternalScore() { - UUID teamMemberId = UUID.randomUUID(); - UUID pdlId = UUID.randomUUID(); + UUID teamMemberId = regular.getId(); + UUID pdlId = pdlProfile.getId(); PulseResponse pulseResponse = new PulseResponse(1, 1, LocalDate.now(), teamMemberId, "Very Sad", "Very Sad"); - MemberProfile surveyTakerProfile = mock(MemberProfile.class); - MemberProfile pdlProfile = mock(MemberProfile.class); - - when(memberRepo.existsById(teamMemberId)).thenReturn(true); - when(memberProfileServices.getById(teamMemberId)).thenReturn(surveyTakerProfile); - when(surveyTakerProfile.getFirstName()).thenReturn("John"); - when(surveyTakerProfile.getLastName()).thenReturn("Doe"); - - when(surveyTakerProfile.getPdlId()).thenReturn(pdlId); - when(memberRepo.existsById(pdlId)).thenReturn(true); - when(memberProfileServices.getById(pdlId)).thenReturn(pdlProfile); - when(pdlProfile.getWorkEmail()).thenReturn("pdl@example.com"); - pulseResponseService.sendPulseLowScoreEmail(pulseResponse); assertEquals(1, emailSender.events.size()); assertEquals( - List.of("SEND_EMAIL", "null", "null", "Internal and external pulse scores are low for team member John Doe", "Team member John Doe has left low internal and external pulse scores. Please consider reaching out to this employee at null
Internal Feelings: Very Sad
External Feelings: Very Sad
", pdlProfile.getWorkEmail()), + List.of("SEND_EMAIL", "null", "null", + String.format("Internal and external pulse scores are low for team member %s %s", regular.getFirstName(), regular.getLastName()), + String.format("Team member %s %s has left low internal and external pulse scores. Please consider reaching out to this employee at %s
Internal Feelings: Very Sad
External Feelings: Very Sad
", regular.getFirstName(), regular.getLastName(), regular.getWorkEmail()), + pdlProfile.getWorkEmail()), emailSender.events.getFirst() ); } @Test - @Tag("mocked") void testSendPulseLowScoreEmail_NoTeamMemberId() { PulseResponse pulseResponse = new PulseResponse(1, 1, LocalDate.now(), null, "Very Sad", "Very Sad"); @@ -432,73 +322,49 @@ void testSendPulseLowScoreEmail_NoTeamMemberId() { } @Test - @Tag("mocked") void testSendPulseLowScoreEmail_InvalidTeamMemberId() { UUID teamMemberId = UUID.randomUUID(); PulseResponse pulseResponse = new PulseResponse(1, 1, LocalDate.now(), teamMemberId, "Very Sad", "Very Sad"); - when(memberRepo.existsById(teamMemberId)).thenReturn(false); - pulseResponseService.sendPulseLowScoreEmail(pulseResponse); assertEquals(0, emailSender.events.size()); } @Test - @Tag("mocked") void testSendPulseLowScoreEmail_WithPdlId() { - UUID teamMemberId = UUID.randomUUID(); - UUID pdlId = UUID.randomUUID(); + UUID teamMemberId = regular.getId(); + UUID pdlId = pdlProfile.getId(); PulseResponse pulseResponse = new PulseResponse(1, 1, LocalDate.now(), teamMemberId, "Very Sad", "Very Sad"); - MemberProfile surveyTakerProfile = mock(MemberProfile.class); - MemberProfile pdlProfile = mock(MemberProfile.class); - - when(memberRepo.existsById(teamMemberId)).thenReturn(true); - when(memberProfileServices.getById(teamMemberId)).thenReturn(surveyTakerProfile); - when(surveyTakerProfile.getFirstName()).thenReturn("John"); - when(surveyTakerProfile.getLastName()).thenReturn("Doe"); - - when(surveyTakerProfile.getPdlId()).thenReturn(pdlId); - when(memberRepo.existsById(pdlId)).thenReturn(true); - when(memberProfileServices.getById(pdlId)).thenReturn(pdlProfile); - when(pdlProfile.getWorkEmail()).thenReturn("pdl@example.com"); - pulseResponseService.sendPulseLowScoreEmail(pulseResponse); assertEquals(1, emailSender.events.size()); assertEquals( - List.of("SEND_EMAIL", "null", "null", "Internal and external pulse scores are low for team member John Doe", "Team member John Doe has left low internal and external pulse scores. Please consider reaching out to this employee at null
Internal Feelings: Very Sad
External Feelings: Very Sad
", pdlProfile.getWorkEmail()), + List.of("SEND_EMAIL", "null", "null", + String.format("Internal and external pulse scores are low for team member %s %s", regular.getFirstName(), regular.getLastName()), + String.format("Team member %s %s has left low internal and external pulse scores. Please consider reaching out to this employee at %s
Internal Feelings: Very Sad
External Feelings: Very Sad
", regular.getFirstName(), regular.getLastName(), regular.getWorkEmail()), + pdlProfile.getWorkEmail()), emailSender.events.getFirst() ); } @Test - @Tag("mocked") void testSendPulseLowScoreEmail_WithSupervisorId() { - UUID teamMemberId = UUID.randomUUID(); - UUID supervisorId = UUID.randomUUID(); + UUID teamMemberId = another.getId(); + UUID supervisorId = supervisorProfile.getId(); PulseResponse pulseResponse = new PulseResponse(1, 1, LocalDate.now(), teamMemberId, "Very Sad", "Very Sad"); - MemberProfile surveyTakerProfile = mock(MemberProfile.class); - MemberProfile supervisorProfile = mock(MemberProfile.class); - - when(memberRepo.existsById(teamMemberId)).thenReturn(true); - when(memberProfileServices.getById(teamMemberId)).thenReturn(surveyTakerProfile); - when(surveyTakerProfile.getFirstName()).thenReturn("John"); - when(surveyTakerProfile.getLastName()).thenReturn("Doe"); - - when(surveyTakerProfile.getSupervisorid()).thenReturn(supervisorId); - when(memberRepo.existsById(supervisorId)).thenReturn(true); - when(memberProfileServices.getById(supervisorId)).thenReturn(supervisorProfile); - when(supervisorProfile.getWorkEmail()).thenReturn("supervisor@example.com"); - pulseResponseService.sendPulseLowScoreEmail(pulseResponse); assertEquals(1, emailSender.events.size()); assertEquals( - List.of("SEND_EMAIL", "null", "null", "Internal and external pulse scores are low for team member John Doe", "Team member John Doe has left low internal and external pulse scores. Please consider reaching out to this employee at null
Internal Feelings: Very Sad
External Feelings: Very Sad
", supervisorProfile.getWorkEmail()), + List.of("SEND_EMAIL", "null", "null", + String.format("Internal and external pulse scores are low for team member %s %s", another.getFirstName(), another.getLastName()), + String.format("Team member %s %s has left low internal and external pulse scores. Please consider reaching out to this employee at %s
Internal Feelings: Very Sad
External Feelings: Very Sad
", another.getFirstName(), another.getLastName(), another.getWorkEmail()), + supervisorProfile.getWorkEmail() + + "," + pdlProfile.getWorkEmail()), emailSender.events.getFirst() ); } diff --git a/server/src/test/java/com/objectcomputing/checkins/services/reports/FileServicesImplReplacement.java b/server/src/test/java/com/objectcomputing/checkins/services/reports/FileServicesImplReplacement.java deleted file mode 100644 index 68b6ec4ea1..0000000000 --- a/server/src/test/java/com/objectcomputing/checkins/services/reports/FileServicesImplReplacement.java +++ /dev/null @@ -1,59 +0,0 @@ -package com.objectcomputing.checkins.services.reports; - -/************************************************************************* - * - * This class is here due to the fact that the ReportDataController now - * references the FileServices. The real FileServicesImpl requires the - * GoogleApiAccess class that does not exist during testing. - * - * This replacement class does not require that and can help us test the - * output of the MarkdownGeneration class. - * - ************************************************************************/ - -import com.objectcomputing.checkins.services.file.FileInfoDTO; -import com.objectcomputing.checkins.services.file.FileServices; -import com.objectcomputing.checkins.services.file.FileServicesImpl; - -import io.micronaut.http.multipart.CompletedFileUpload; - -import java.io.File; -import java.util.Set; -import java.util.HashSet; -import java.util.UUID; - -import jakarta.inject.Singleton; -import io.micronaut.context.env.Environment; -import io.micronaut.context.annotation.Replaces; -import io.micronaut.context.annotation.Requires; - -@Singleton -@Replaces(FileServicesImpl.class) -@Requires(env = Environment.TEST) -public class FileServicesImplReplacement implements FileServices { - public String documentName = ""; - public String documentText = ""; - - public Set findFiles(UUID checkInId) { - return new HashSet(); - } - - public File downloadFiles(String uploadDocId) { - return null; - } - - public FileInfoDTO uploadFile(UUID checkInID, CompletedFileUpload file) { - return new FileInfoDTO(); - } - - public FileInfoDTO uploadDocument(String directory, - String name, String text) { - documentName = name; - documentText = text; - return new FileInfoDTO(); - } - - public boolean deleteFile(String uploadDocId) { - return true; - } -} diff --git a/server/src/test/java/com/objectcomputing/checkins/services/reports/ReportDataControllerTest.java b/server/src/test/java/com/objectcomputing/checkins/services/reports/ReportDataControllerTest.java index 06c9cb7fa5..6a5010ff71 100644 --- a/server/src/test/java/com/objectcomputing/checkins/services/reports/ReportDataControllerTest.java +++ b/server/src/test/java/com/objectcomputing/checkins/services/reports/ReportDataControllerTest.java @@ -2,6 +2,7 @@ import com.objectcomputing.checkins.services.kudos.Kudos; import com.objectcomputing.checkins.services.TestContainersSuite; +import com.objectcomputing.checkins.services.FileServicesImplReplacement; import com.objectcomputing.checkins.services.fixture.KudosFixture; import com.objectcomputing.checkins.services.fixture.RoleFixture; import com.objectcomputing.checkins.services.fixture.MemberProfileFixture; @@ -127,8 +128,10 @@ void processReportData() { .basicAuth(admin.getWorkEmail(), ADMIN_ROLE); client.toBlocking().exchange(request); - validateReportData(fileServices.documentName, - fileServices.documentText, target); + String documentName = target.getWorkEmail(); + String documentText = fileServices.getFile(MarkdownGeneration.directory + + "/" + documentName); + validateReportData(documentName, documentText, target); } @Test diff --git a/server/src/test/java/com/objectcomputing/checkins/services/request_notifications/CheckServicesImplTest.java b/server/src/test/java/com/objectcomputing/checkins/services/request_notifications/CheckServicesImplTest.java index b0ac32188e..debee294cf 100644 --- a/server/src/test/java/com/objectcomputing/checkins/services/request_notifications/CheckServicesImplTest.java +++ b/server/src/test/java/com/objectcomputing/checkins/services/request_notifications/CheckServicesImplTest.java @@ -2,67 +2,68 @@ import com.objectcomputing.checkins.services.TestContainersSuite; import com.objectcomputing.checkins.services.feedback_request.FeedbackRequest; -import com.objectcomputing.checkins.services.feedback_request.FeedbackRequestRepository; -import com.objectcomputing.checkins.services.feedback_request.FeedbackRequestServicesImpl; +import com.objectcomputing.checkins.services.feedback_template.FeedbackTemplate; +import com.objectcomputing.checkins.services.fixture.FeedbackRequestFixture; +import com.objectcomputing.checkins.services.fixture.FeedbackTemplateFixture; import com.objectcomputing.checkins.services.pulse.PulseServices; import com.objectcomputing.checkins.services.reviews.ReviewPeriodServices; +import com.objectcomputing.checkins.notifications.email.MailJetFactory; +import com.objectcomputing.checkins.services.MailJetFactoryReplacement; +import com.objectcomputing.checkins.services.fixture.MemberProfileFixture; +import com.objectcomputing.checkins.services.memberprofile.MemberProfile; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.condition.DisabledInNativeImage; -import org.mockito.InjectMocks; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; + +import io.micronaut.context.annotation.Property; +import io.micronaut.core.util.StringUtils; +import jakarta.inject.Inject; +import jakarta.inject.Named; import java.util.Collections; import java.util.List; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.eq; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -// Disabled in nativeTest, as we get an exception from Mockito -// => java.lang.NoClassDefFoundError: Could not initialize class org.mockito.internal.configuration.plugins.Plugins -@DisabledInNativeImage -class CheckServicesImplTest extends TestContainersSuite { - - @Mock - private FeedbackRequestServicesImpl feedbackRequestServices; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; - @Mock - private FeedbackRequestRepository feedbackRequestRepository; +@Property(name = "replace.mailjet.factory", value = StringUtils.TRUE) +class CheckServicesImplTest extends TestContainersSuite + implements FeedbackTemplateFixture, FeedbackRequestFixture, MemberProfileFixture { - @Mock - private PulseServices pulseServices; + @Inject + @Named(MailJetFactory.MJML_FORMAT) + private MailJetFactoryReplacement.MockEmailSender emailSender; - @Mock - private ReviewPeriodServices reviewPeriodServices; - - @InjectMocks + @Inject private CheckServicesImpl checkServices; - AutoCloseable openMocks; - @BeforeEach - void initMocks() { - openMocks = MockitoAnnotations.openMocks(this); - } - - @AfterEach - void resetMocks() throws Exception { - openMocks.close(); + void resetTest() { + emailSender.reset(); } @Test void sendScheduledEmails() { - FeedbackRequest retrievedRequest = new FeedbackRequest(); - retrievedRequest.setStatus("pending"); - List list = Collections.singletonList(retrievedRequest); - when(feedbackRequestRepository.findBySendDateNotAfterAndStatusEqual(any(),eq("pending"))).thenReturn(list); + // Create a pending feedback request. + MemberProfile pdlMemberProfile = createADefaultMemberProfile(); + MemberProfile employeeMemberProfile = createADefaultMemberProfileForPdl(pdlMemberProfile); + MemberProfile recipient = createADefaultRecipient(); + + FeedbackTemplate template = createFeedbackTemplate(pdlMemberProfile.getId()); + getFeedbackTemplateRepository().save(template); + FeedbackRequest retrievedRequest = + saveSampleFeedbackRequest(pdlMemberProfile, + employeeMemberProfile, + recipient, template.getId()); + + // Send emails for today checkServices.sendScheduledEmails(); - verify(feedbackRequestServices).sendNewRequestEmail(retrievedRequest); - retrievedRequest.setStatus("sent"); - verify(feedbackRequestRepository).update(retrievedRequest); + + // One for the feedback request and, possibly, one for the pulse email. + assertTrue(emailSender.events.size() > 0); + assertEquals(pdlMemberProfile.getWorkEmail(), + emailSender.events.get(0).get(2)); + assertEquals("Feedback request", + emailSender.events.get(0).get(3)); } }