@Test public void shouldGetCountOfRecordsForRespectiveModelTable() throws Exception { byte[] content = new byte[1]; MultipartFile mockMultiPartFile = spy(new MockMultipartFile("csvFile", "mock.csv", null, content)); InputStream mockInputStream = mock(InputStream.class); when(mockMultiPartFile.getInputStream()).thenReturn(mockInputStream); when(csvParser.process(eq(mockMultiPartFile.getInputStream()), argThat(modelMatcher(Product.class)), eq(handler), eq(auditFields))).thenReturn(20); String uploadSuccessMessage = "File uploaded successfully. " + "'Number of records processed: 20'"; when(messageService.message(UPLOAD_FILE_SUCCESS, 20)).thenReturn(uploadSuccessMessage); ResponseEntity<OpenLmisResponse> uploadResponse = controller.upload(mockMultiPartFile, "product", request); assertThat(uploadResponse.getBody().getSuccessMsg(), is(uploadSuccessMessage)); }
@RequestMapping(value = "/upload", method = POST) @PreAuthorize("@permissionEvaluator.hasPermission(principal,'UPLOADS')") public ResponseEntity<OpenLmisResponse> upload(MultipartFile csvFile, String model, HttpServletRequest request) { try { OpenLmisMessage errorMessage = validateFile(model, csvFile); if (errorMessage != null) { return errorResponse(errorMessage); } Date currentTimestamp = dbService.getCurrentTimestamp(); RecordHandler recordHandler = uploadBeansMap.get(model).getRecordHandler(); ModelClass modelClass = new ModelClass(uploadBeansMap.get(model).getImportableClass()); AuditFields auditFields = new AuditFields(loggedInUserId(request), currentTimestamp); int recordsToBeUploaded = csvParser.process(csvFile.getInputStream(), modelClass, recordHandler, auditFields); return successPage(recordsToBeUploaded); } catch (DataException dataException) { return errorResponse(dataException.getOpenLmisMessage()); } catch (UploadException e) { return errorResponse(new OpenLmisMessage(messageService.message(e.getCode(), (Object[])e.getParams()))); } catch (IOException e) { return errorResponse(new OpenLmisMessage(e.getMessage())); } }
UploadController extends BaseController { @RequestMapping(value = "/upload", method = POST) @PreAuthorize("@permissionEvaluator.hasPermission(principal,'UPLOADS')") public ResponseEntity<OpenLmisResponse> upload(MultipartFile csvFile, String model, HttpServletRequest request) { try { OpenLmisMessage errorMessage = validateFile(model, csvFile); if (errorMessage != null) { return errorResponse(errorMessage); } Date currentTimestamp = dbService.getCurrentTimestamp(); RecordHandler recordHandler = uploadBeansMap.get(model).getRecordHandler(); ModelClass modelClass = new ModelClass(uploadBeansMap.get(model).getImportableClass()); AuditFields auditFields = new AuditFields(loggedInUserId(request), currentTimestamp); int recordsToBeUploaded = csvParser.process(csvFile.getInputStream(), modelClass, recordHandler, auditFields); return successPage(recordsToBeUploaded); } catch (DataException dataException) { return errorResponse(dataException.getOpenLmisMessage()); } catch (UploadException e) { return errorResponse(new OpenLmisMessage(messageService.message(e.getCode(), (Object[])e.getParams()))); } catch (IOException e) { return errorResponse(new OpenLmisMessage(e.getMessage())); } } }
UploadController extends BaseController { @RequestMapping(value = "/upload", method = POST) @PreAuthorize("@permissionEvaluator.hasPermission(principal,'UPLOADS')") public ResponseEntity<OpenLmisResponse> upload(MultipartFile csvFile, String model, HttpServletRequest request) { try { OpenLmisMessage errorMessage = validateFile(model, csvFile); if (errorMessage != null) { return errorResponse(errorMessage); } Date currentTimestamp = dbService.getCurrentTimestamp(); RecordHandler recordHandler = uploadBeansMap.get(model).getRecordHandler(); ModelClass modelClass = new ModelClass(uploadBeansMap.get(model).getImportableClass()); AuditFields auditFields = new AuditFields(loggedInUserId(request), currentTimestamp); int recordsToBeUploaded = csvParser.process(csvFile.getInputStream(), modelClass, recordHandler, auditFields); return successPage(recordsToBeUploaded); } catch (DataException dataException) { return errorResponse(dataException.getOpenLmisMessage()); } catch (UploadException e) { return errorResponse(new OpenLmisMessage(messageService.message(e.getCode(), (Object[])e.getParams()))); } catch (IOException e) { return errorResponse(new OpenLmisMessage(e.getMessage())); } }  }
UploadController extends BaseController { @RequestMapping(value = "/upload", method = POST) @PreAuthorize("@permissionEvaluator.hasPermission(principal,'UPLOADS')") public ResponseEntity<OpenLmisResponse> upload(MultipartFile csvFile, String model, HttpServletRequest request) { try { OpenLmisMessage errorMessage = validateFile(model, csvFile); if (errorMessage != null) { return errorResponse(errorMessage); } Date currentTimestamp = dbService.getCurrentTimestamp(); RecordHandler recordHandler = uploadBeansMap.get(model).getRecordHandler(); ModelClass modelClass = new ModelClass(uploadBeansMap.get(model).getImportableClass()); AuditFields auditFields = new AuditFields(loggedInUserId(request), currentTimestamp); int recordsToBeUploaded = csvParser.process(csvFile.getInputStream(), modelClass, recordHandler, auditFields); return successPage(recordsToBeUploaded); } catch (DataException dataException) { return errorResponse(dataException.getOpenLmisMessage()); } catch (UploadException e) { return errorResponse(new OpenLmisMessage(messageService.message(e.getCode(), (Object[])e.getParams()))); } catch (IOException e) { return errorResponse(new OpenLmisMessage(e.getMessage())); } }  @RequestMapping(value = "/upload", method = POST) @PreAuthorize("@permissionEvaluator.hasPermission(principal,'UPLOADS')") ResponseEntity<OpenLmisResponse> upload(MultipartFile csvFile, String model, HttpServletRequest request); @RequestMapping(value = "/supported-uploads", method = GET, headers = ACCEPT_JSON) @PreAuthorize("@permissionEvaluator.hasPermission(principal,'UPLOADS')") ResponseEntity<OpenLmisResponse> getSupportedUploads(); }
UploadController extends BaseController { @RequestMapping(value = "/upload", method = POST) @PreAuthorize("@permissionEvaluator.hasPermission(principal,'UPLOADS')") public ResponseEntity<OpenLmisResponse> upload(MultipartFile csvFile, String model, HttpServletRequest request) { try { OpenLmisMessage errorMessage = validateFile(model, csvFile); if (errorMessage != null) { return errorResponse(errorMessage); } Date currentTimestamp = dbService.getCurrentTimestamp(); RecordHandler recordHandler = uploadBeansMap.get(model).getRecordHandler(); ModelClass modelClass = new ModelClass(uploadBeansMap.get(model).getImportableClass()); AuditFields auditFields = new AuditFields(loggedInUserId(request), currentTimestamp); int recordsToBeUploaded = csvParser.process(csvFile.getInputStream(), modelClass, recordHandler, auditFields); return successPage(recordsToBeUploaded); } catch (DataException dataException) { return errorResponse(dataException.getOpenLmisMessage()); } catch (UploadException e) { return errorResponse(new OpenLmisMessage(messageService.message(e.getCode(), (Object[])e.getParams()))); } catch (IOException e) { return errorResponse(new OpenLmisMessage(e.getMessage())); } }  @RequestMapping(value = "/upload", method = POST) @PreAuthorize("@permissionEvaluator.hasPermission(principal,'UPLOADS')") ResponseEntity<OpenLmisResponse> upload(MultipartFile csvFile, String model, HttpServletRequest request); @RequestMapping(value = "/supported-uploads", method = GET, headers = ACCEPT_JSON) @PreAuthorize("@permissionEvaluator.hasPermission(principal,'UPLOADS')") ResponseEntity<OpenLmisResponse> getSupportedUploads(); static final String SELECT_UPLOAD_TYPE; static final String INCORRECT_FILE; static final String FILE_IS_EMPTY; static final String INCORRECT_FILE_FORMAT; static final String UPLOAD_FILE_SUCCESS; static final String SUCCESS; static final String ERROR; static final String SUPPORTED_UPLOADS; }