CVCController { @GetMapping(value = "/download", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE) public ResponseEntity<byte[]> downloadMetadata() throws MetadataDownloadException { byte[] metadataArray = metadataService.getMetadata(); if (metadataArray.length != 0) { return ResponseEntity.ok() .header("Content-Disposition", "attachment; filename=Metadata.xml") .contentType(MediaType.APPLICATION_OCTET_STREAM) .body(metadataArray); } log.error("No Metadata were created. Can not download metadata."); throw new MetadataDownloadException(); } CVCController(PermissionDataHandlingMBean data,
                       MetadataService metadataService,
                       CvcTlsCheck cvcTlsCheck); @GetMapping("") void index(HttpServletResponse response); @GetMapping("login") String login(); @GetMapping("list") String list(Model model); String getMailto(Map.Entry<String, CvcTlsCheck.CvcCheckResults> entry); @GetMapping("status") String status(Model model); @GetMapping("details/{entityID}") String details(@PathVariable String entityID, Model model, HttpServletResponse response); @PostMapping("details/{entityID}/check") String check(@PathVariable String entityID, Model model); @PostMapping("details/{entityID}/initialRequest") String initialRequest(@PathVariable String entityID,
                               Model model,
                               @ModelAttribute CVCRequestModel form); @PostMapping("details/{entityID}/renewBlackList") String renewBlackList(@PathVariable String entityID, Model model); @PostMapping("details/{entityID}/renewMasterDefectList") String renewMasterDefectList(@PathVariable String entityID, Model model); @PostMapping("/details/{entityID}/renewCVC") String renewCVC(@PathVariable String entityID, Model model); @GetMapping(value = "/download", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE) ResponseEntity<byte[]> downloadMetadata(); @ExceptionHandler(MetadataDownloadException.class) String handleMetadataDownloadException(Model model);  }
@Test void metadataDownloadExceptionIsThrownWhenByteArrayIsEmpty() { byte[] bytes = new byte[0]; Mockito.when(metadataService.getMetadata()).thenReturn(bytes); Assertions.assertThrows(MetadataDownloadException.class, () -> controller.downloadMetadata()); }
@Test void downloadMetadataReturnsMetadataAsXMLFile() throws Exception { InputStream resourceAsStream = CVCControllerTest.class.getResourceAsStream("/Metadata.xml"); Mockito.when(metadataService.getMetadata()).thenReturn(ByteStreams.toByteArray(resourceAsStream)); ResponseEntity<byte[]> responseEntity = controller.downloadMetadata(); HttpStatus statusCode = responseEntity.getStatusCode(); Assertions.assertEquals(HttpStatus.OK, statusCode); HttpHeaders headers = responseEntity.getHeaders(); ContentDisposition contentDisposition = headers.getContentDisposition(); Assertions.assertEquals("attachment", contentDisposition.getType()); Assertions.assertEquals("Metadata.xml", contentDisposition.getFilename()); Assertions.assertEquals(MediaType.APPLICATION_OCTET_STREAM, headers.getContentType()); byte[] body = responseEntity.getBody(); Assertions.assertFalse(ArrayUtil.isNullOrEmpty(body)); }