@Test public void createNewPrintJobSaveAllNonExistentData() { DataResponse<BasicPrintJob> response = new DataResponse<BasicPrintJob>(); byte[] a = hexStringToByteArray("e04fd020ea3a6910a2d808002b30309d"); MultipartFile file = new MockMultipartFile("something.stl", "something.stl", null ,a); PrintJobNewRequest request = new PrintJobNewRequest(); request.setColor("RED"); request.setComments("COMMENTS"); request.setCustomerFirstName("test"); request.setCustomerLastName("testLast"); request.setEmail("test@email.com"); request.setFile(file); CustomerInfo customerInfo = new CustomerInfo(); customerInfo.setFirstName("test"); ColorType color = new ColorType(); color.setColor("RED"); color.setAvailable(true); Principal mockPrincipal = Mockito.mock(Principal.class); Mockito.when(mockPrincipal.getName()).thenReturn("me"); Employee e = new Employee(); e.setId(999); Map<String, String> data = new HashMap<>(); data.put("filePath", "DROPBOX_PATH"); data.put("sharableLink", "http: PrintJob printJob = new PrintJob(); printJob.setColorType(color); printJob.setComments("COMMENTS"); printJob.setCreatedAt(LocalDateTime.now()); printJob.setFilePath("DROPBOX_PATH"); printJob.setFileSharableLink("http: printJob.setId(1); printJob.setUpdatedAt(LocalDateTime.now()); printJob.setStatus(Status.PENDING_REVIEW); printJob.setEmployee(e); printJob.setQueueId(new Queue(printJob, new Long(1))); printJob.setUpdatedAt(LocalDateTime.now()); Set<PrintJob> printJobData = new HashSet<>(); printJobData.add(printJob); customerInfo.setPrintJobs(printJobData); List<PrintJob> printJobs = new ArrayList<>(); printJobs.addAll(printJobData); List<BasicPrintJob> basicPrintJobs = new BasicPrintJob().ConvertPrintJobs(printJobs); response.setSuccess(true); response.setMessage("Successfully saved new file to database!"); response.setData(basicPrintJobs); response.setHttpStatus(HttpStatus.ACCEPTED); when(customerInfoRepo.findByEmail(any())).thenReturn(null); when(customerInfoRepo.save(any())).thenReturn(customerInfo); when(colorTypeRepo.findByColor(any())).thenReturn(color); when(employeeRepo.findEmployeeByUsername(any())).thenReturn(e); when(printJobRepo.save(any())).thenReturn(printJob); when(fileService.uploadFile(anyLong(), any())).thenReturn(data); DataResponse<BasicPrintJob> opResponse = operations.newPrintJob(request, mockPrincipal); assert(opResponse.equals(response)); }
public DataResponse<BasicPrintJob> newPrintJob(PrintJobNewRequest printJobNewRequest, Principal principal) { printJobNewRequest.validate(); DataResponse<BasicPrintJob> response = new DataResponse<BasicPrintJob>("File could not be uploaded"); if (printJobNewRequest.getFile() == null) { throw new IdeaLabApiException(DROPBOX_UPLOAD_FILE_ERROR); } String email = printJobNewRequest.getEmail(); String emailHash = emailHashUtil.MD5Hash(email); String customerFirstName = printJobNewRequest.getCustomerFirstName(); String customerLastName = printJobNewRequest.getCustomerLastName(); String color = printJobNewRequest.getColor(); String comments = printJobNewRequest.getComments(); LocalDateTime currentTime = LocalDateTime.now(); CustomerInfo customer = customerInfoRepo.findByEmail(email); if (customer == null) { customer = new CustomerInfo(null, customerFirstName, customerLastName, email); customer = customerInfoRepo.save(customer); } if(customer.getPrintJobs() != null && customer.getPrintJobs().size() > 5) { int countForDay = 0; for(PrintJob p : customer.getPrintJobs()) { if(p.getCreatedAt().plusHours(24).isAfter(LocalDateTime.now())) { countForDay++; } } if(countForDay > 5) { throw new IdeaLabApiException(GENERAL_ERROR, "Customer has already created 5 print jobs in the last 24 hours"); } } ColorType databaseColor = colorTypeRepo.findByColor(color); if (databaseColor == null) { throw new IdeaLabApiException(COLOR_CANT_FIND_BY_TYPE); } Employee databaseEmployee = employeeRepo.findEmployeeByUsername(principal.getName()); PrintJob printJob = new PrintJob(customer, databaseColor, databaseEmployee, Status.PENDING_REVIEW, comments, emailHash); Map<String, String> data = fileService.uploadFile(currentTime.toLocalTime().toNanoOfDay(), printJobNewRequest.getFile()); printJob.setFilePath(data.get("filePath")); printJob.setFileSharableLink(data.get("sharableLink")); Set<PrintJob> printJobs; if(customer.getPrintJobs() == null) { printJobs = new HashSet<>(); } else { printJobs = customer.getPrintJobs(); } printJobs.add(printJob); customer.setPrintJobs(printJobs); printJob = printJobRepo.save(printJob); if (printJobRepo.findByCustomerInfo(customer).size() == 1) { Queue queue = new Queue(printJob, queueRepo.getMaximumRank()+1); queue = queueRepo.save(queue); } return getPrintJobResponse(response, printJob, data, "Successfully saved new file to database!"); }
PrintJobOperations { public DataResponse<BasicPrintJob> newPrintJob(PrintJobNewRequest printJobNewRequest, Principal principal) { printJobNewRequest.validate(); DataResponse<BasicPrintJob> response = new DataResponse<BasicPrintJob>("File could not be uploaded"); if (printJobNewRequest.getFile() == null) { throw new IdeaLabApiException(DROPBOX_UPLOAD_FILE_ERROR); } String email = printJobNewRequest.getEmail(); String emailHash = emailHashUtil.MD5Hash(email); String customerFirstName = printJobNewRequest.getCustomerFirstName(); String customerLastName = printJobNewRequest.getCustomerLastName(); String color = printJobNewRequest.getColor(); String comments = printJobNewRequest.getComments(); LocalDateTime currentTime = LocalDateTime.now(); CustomerInfo customer = customerInfoRepo.findByEmail(email); if (customer == null) { customer = new CustomerInfo(null, customerFirstName, customerLastName, email); customer = customerInfoRepo.save(customer); } if(customer.getPrintJobs() != null && customer.getPrintJobs().size() > 5) { int countForDay = 0; for(PrintJob p : customer.getPrintJobs()) { if(p.getCreatedAt().plusHours(24).isAfter(LocalDateTime.now())) { countForDay++; } } if(countForDay > 5) { throw new IdeaLabApiException(GENERAL_ERROR, "Customer has already created 5 print jobs in the last 24 hours"); } } ColorType databaseColor = colorTypeRepo.findByColor(color); if (databaseColor == null) { throw new IdeaLabApiException(COLOR_CANT_FIND_BY_TYPE); } Employee databaseEmployee = employeeRepo.findEmployeeByUsername(principal.getName()); PrintJob printJob = new PrintJob(customer, databaseColor, databaseEmployee, Status.PENDING_REVIEW, comments, emailHash); Map<String, String> data = fileService.uploadFile(currentTime.toLocalTime().toNanoOfDay(), printJobNewRequest.getFile()); printJob.setFilePath(data.get("filePath")); printJob.setFileSharableLink(data.get("sharableLink")); Set<PrintJob> printJobs; if(customer.getPrintJobs() == null) { printJobs = new HashSet<>(); } else { printJobs = customer.getPrintJobs(); } printJobs.add(printJob); customer.setPrintJobs(printJobs); printJob = printJobRepo.save(printJob); if (printJobRepo.findByCustomerInfo(customer).size() == 1) { Queue queue = new Queue(printJob, queueRepo.getMaximumRank()+1); queue = queueRepo.save(queue); } return getPrintJobResponse(response, printJob, data, "Successfully saved new file to database!"); } }
PrintJobOperations { public DataResponse<BasicPrintJob> newPrintJob(PrintJobNewRequest printJobNewRequest, Principal principal) { printJobNewRequest.validate(); DataResponse<BasicPrintJob> response = new DataResponse<BasicPrintJob>("File could not be uploaded"); if (printJobNewRequest.getFile() == null) { throw new IdeaLabApiException(DROPBOX_UPLOAD_FILE_ERROR); } String email = printJobNewRequest.getEmail(); String emailHash = emailHashUtil.MD5Hash(email); String customerFirstName = printJobNewRequest.getCustomerFirstName(); String customerLastName = printJobNewRequest.getCustomerLastName(); String color = printJobNewRequest.getColor(); String comments = printJobNewRequest.getComments(); LocalDateTime currentTime = LocalDateTime.now(); CustomerInfo customer = customerInfoRepo.findByEmail(email); if (customer == null) { customer = new CustomerInfo(null, customerFirstName, customerLastName, email); customer = customerInfoRepo.save(customer); } if(customer.getPrintJobs() != null && customer.getPrintJobs().size() > 5) { int countForDay = 0; for(PrintJob p : customer.getPrintJobs()) { if(p.getCreatedAt().plusHours(24).isAfter(LocalDateTime.now())) { countForDay++; } } if(countForDay > 5) { throw new IdeaLabApiException(GENERAL_ERROR, "Customer has already created 5 print jobs in the last 24 hours"); } } ColorType databaseColor = colorTypeRepo.findByColor(color); if (databaseColor == null) { throw new IdeaLabApiException(COLOR_CANT_FIND_BY_TYPE); } Employee databaseEmployee = employeeRepo.findEmployeeByUsername(principal.getName()); PrintJob printJob = new PrintJob(customer, databaseColor, databaseEmployee, Status.PENDING_REVIEW, comments, emailHash); Map<String, String> data = fileService.uploadFile(currentTime.toLocalTime().toNanoOfDay(), printJobNewRequest.getFile()); printJob.setFilePath(data.get("filePath")); printJob.setFileSharableLink(data.get("sharableLink")); Set<PrintJob> printJobs; if(customer.getPrintJobs() == null) { printJobs = new HashSet<>(); } else { printJobs = customer.getPrintJobs(); } printJobs.add(printJob); customer.setPrintJobs(printJobs); printJob = printJobRepo.save(printJob); if (printJobRepo.findByCustomerInfo(customer).size() == 1) { Queue queue = new Queue(printJob, queueRepo.getMaximumRank()+1); queue = queueRepo.save(queue); } return getPrintJobResponse(response, printJob, data, "Successfully saved new file to database!"); } PrintJobOperations(FileService fileService, PrintJobRepo printJobRepo,
                              ColorTypeRepo colorTypeRepo, CustomerInfoRepo customerInfoRepo,
                              EmployeeRepo employeeRepo, EmailHashUtil emailHashUtil, QueueRepo queueRepo); }
PrintJobOperations { public DataResponse<BasicPrintJob> newPrintJob(PrintJobNewRequest printJobNewRequest, Principal principal) { printJobNewRequest.validate(); DataResponse<BasicPrintJob> response = new DataResponse<BasicPrintJob>("File could not be uploaded"); if (printJobNewRequest.getFile() == null) { throw new IdeaLabApiException(DROPBOX_UPLOAD_FILE_ERROR); } String email = printJobNewRequest.getEmail(); String emailHash = emailHashUtil.MD5Hash(email); String customerFirstName = printJobNewRequest.getCustomerFirstName(); String customerLastName = printJobNewRequest.getCustomerLastName(); String color = printJobNewRequest.getColor(); String comments = printJobNewRequest.getComments(); LocalDateTime currentTime = LocalDateTime.now(); CustomerInfo customer = customerInfoRepo.findByEmail(email); if (customer == null) { customer = new CustomerInfo(null, customerFirstName, customerLastName, email); customer = customerInfoRepo.save(customer); } if(customer.getPrintJobs() != null && customer.getPrintJobs().size() > 5) { int countForDay = 0; for(PrintJob p : customer.getPrintJobs()) { if(p.getCreatedAt().plusHours(24).isAfter(LocalDateTime.now())) { countForDay++; } } if(countForDay > 5) { throw new IdeaLabApiException(GENERAL_ERROR, "Customer has already created 5 print jobs in the last 24 hours"); } } ColorType databaseColor = colorTypeRepo.findByColor(color); if (databaseColor == null) { throw new IdeaLabApiException(COLOR_CANT_FIND_BY_TYPE); } Employee databaseEmployee = employeeRepo.findEmployeeByUsername(principal.getName()); PrintJob printJob = new PrintJob(customer, databaseColor, databaseEmployee, Status.PENDING_REVIEW, comments, emailHash); Map<String, String> data = fileService.uploadFile(currentTime.toLocalTime().toNanoOfDay(), printJobNewRequest.getFile()); printJob.setFilePath(data.get("filePath")); printJob.setFileSharableLink(data.get("sharableLink")); Set<PrintJob> printJobs; if(customer.getPrintJobs() == null) { printJobs = new HashSet<>(); } else { printJobs = customer.getPrintJobs(); } printJobs.add(printJob); customer.setPrintJobs(printJobs); printJob = printJobRepo.save(printJob); if (printJobRepo.findByCustomerInfo(customer).size() == 1) { Queue queue = new Queue(printJob, queueRepo.getMaximumRank()+1); queue = queueRepo.save(queue); } return getPrintJobResponse(response, printJob, data, "Successfully saved new file to database!"); } PrintJobOperations(FileService fileService, PrintJobRepo printJobRepo,
                              ColorTypeRepo colorTypeRepo, CustomerInfoRepo customerInfoRepo,
                              EmployeeRepo employeeRepo, EmailHashUtil emailHashUtil, QueueRepo queueRepo); DataResponse<BasicPrintJob> newPrintJob(PrintJobNewRequest printJobNewRequest, Principal principal); DataResponse<BasicPrintJob> updateModel(Integer printId, PrintModelUpdateRequest model); GenericResponse deleteModel(Integer printId); GenericResponse updatePrintJobStatus(Integer printId, PrintJobUpdateRequest dto); GenericResponse deletePrintJob(PrintJobDeleteRequest dto); DataResponse<BasicPrintJob> getAllPrintJobs(String status); DataResponse<BasicPrintJob> getDeletablePrintJobs(); DataResponse<BasicPrintJob> updatePrintJobProps(Integer printJobId, UpdatePrintJobPropertiesRequest request); DataResponse<BasicPrintJob> getPrintJobById(Integer printJobId); }
PrintJobOperations { public DataResponse<BasicPrintJob> newPrintJob(PrintJobNewRequest printJobNewRequest, Principal principal) { printJobNewRequest.validate(); DataResponse<BasicPrintJob> response = new DataResponse<BasicPrintJob>("File could not be uploaded"); if (printJobNewRequest.getFile() == null) { throw new IdeaLabApiException(DROPBOX_UPLOAD_FILE_ERROR); } String email = printJobNewRequest.getEmail(); String emailHash = emailHashUtil.MD5Hash(email); String customerFirstName = printJobNewRequest.getCustomerFirstName(); String customerLastName = printJobNewRequest.getCustomerLastName(); String color = printJobNewRequest.getColor(); String comments = printJobNewRequest.getComments(); LocalDateTime currentTime = LocalDateTime.now(); CustomerInfo customer = customerInfoRepo.findByEmail(email); if (customer == null) { customer = new CustomerInfo(null, customerFirstName, customerLastName, email); customer = customerInfoRepo.save(customer); } if(customer.getPrintJobs() != null && customer.getPrintJobs().size() > 5) { int countForDay = 0; for(PrintJob p : customer.getPrintJobs()) { if(p.getCreatedAt().plusHours(24).isAfter(LocalDateTime.now())) { countForDay++; } } if(countForDay > 5) { throw new IdeaLabApiException(GENERAL_ERROR, "Customer has already created 5 print jobs in the last 24 hours"); } } ColorType databaseColor = colorTypeRepo.findByColor(color); if (databaseColor == null) { throw new IdeaLabApiException(COLOR_CANT_FIND_BY_TYPE); } Employee databaseEmployee = employeeRepo.findEmployeeByUsername(principal.getName()); PrintJob printJob = new PrintJob(customer, databaseColor, databaseEmployee, Status.PENDING_REVIEW, comments, emailHash); Map<String, String> data = fileService.uploadFile(currentTime.toLocalTime().toNanoOfDay(), printJobNewRequest.getFile()); printJob.setFilePath(data.get("filePath")); printJob.setFileSharableLink(data.get("sharableLink")); Set<PrintJob> printJobs; if(customer.getPrintJobs() == null) { printJobs = new HashSet<>(); } else { printJobs = customer.getPrintJobs(); } printJobs.add(printJob); customer.setPrintJobs(printJobs); printJob = printJobRepo.save(printJob); if (printJobRepo.findByCustomerInfo(customer).size() == 1) { Queue queue = new Queue(printJob, queueRepo.getMaximumRank()+1); queue = queueRepo.save(queue); } return getPrintJobResponse(response, printJob, data, "Successfully saved new file to database!"); } PrintJobOperations(FileService fileService, PrintJobRepo printJobRepo,
                              ColorTypeRepo colorTypeRepo, CustomerInfoRepo customerInfoRepo,
                              EmployeeRepo employeeRepo, EmailHashUtil emailHashUtil, QueueRepo queueRepo); DataResponse<BasicPrintJob> newPrintJob(PrintJobNewRequest printJobNewRequest, Principal principal); DataResponse<BasicPrintJob> updateModel(Integer printId, PrintModelUpdateRequest model); GenericResponse deleteModel(Integer printId); GenericResponse updatePrintJobStatus(Integer printId, PrintJobUpdateRequest dto); GenericResponse deletePrintJob(PrintJobDeleteRequest dto); DataResponse<BasicPrintJob> getAllPrintJobs(String status); DataResponse<BasicPrintJob> getDeletablePrintJobs(); DataResponse<BasicPrintJob> updatePrintJobProps(Integer printJobId, UpdatePrintJobPropertiesRequest request); DataResponse<BasicPrintJob> getPrintJobById(Integer printJobId);  }