public Product buildProduct( String productAccessUrl, String downloadDirectory, ProductPriority priority) { Product product = new Product(); product.setProductAccessUrl(productAccessUrl); product.setUuid(UUID.randomUUID().toString()); /* * add a small delay before setting the creation timestamp - this ensures that each product has a unique timestamp. * The timestamp is used to determine which product should be downloaded first if priorities are the same. */ try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } product.setCreationTimestamp(new Timestamp(new Date().getTime())); product.setNotified(false); product.setDownloadDirectory(downloadDirectory); product.setPriority(priority); product.setVisible(true); ProductProgress productProgress = new ProductProgress(); productProgress.setProgressPercentage(0); productProgress.setDownloadedSize(0); productProgress.setStatus(EDownloadStatus.NOT_STARTED); productProgress.setMessage(null); product.setProductProgress(productProgress); return product; }
@Test public void getStatusTest() throws CLICommandException, ServiceException, IOException { HttpURLConnection conn = mock(HttpURLConnection.class); when(downloadManagerService.sendGetCommand( new URL("http://localhost:8082/download-manager/dataAccessRequests"))) .thenReturn(conn); // setup basic status response StatusResponse statusResponse = new StatusResponse(); List<DataAccessRequest> darList = new ArrayList<>(); DataAccessRequest dar = new DataAccessRequestBuilder().buildDAR("http://www.test.com/dar", null, true); Product product1 = new ProductBuilder().buildProduct("product 1 url"); product1.setTotalFileSize(-1); product1.getProductProgress().setDownloadedSize(21345); product1.getProductProgress().setProgressPercentage(-1); Product product2 = new ProductBuilder().buildProduct("product 2 url"); product2.setPriority(ProductPriority.HIGH); Product product3 = new ProductBuilder().buildProduct("product 3 url"); product3.setTotalFileSize(5242880); product3.getProductProgress().setDownloadedSize(5242880); product3.getProductProgress().setProgressPercentage(100); product3.getProductProgress().setStatus(EDownloadStatus.COMPLETED); Product hiddenProduct = new ProductBuilder().buildProduct("hidden product url"); hiddenProduct.setVisible(false); List<Product> productList = new ArrayList<>(); productList.add(product1); productList.add(product2); productList.add(product3); productList.add(hiddenProduct); dar.setProductList(productList); darList.add(dar); DataAccessRequest hiddenDar = new DataAccessRequestBuilder().buildDAR("hidden dar url", "hidden dar name", true); hiddenDar.setVisible(false); darList.add(hiddenDar); statusResponse.setDataAccessRequests(darList); when(downloadManagerResponseParser.parseStatusResponse(conn)).thenReturn(statusResponse); StringBuilder expectedOutput = new StringBuilder(100); expectedOutput.append("http://www.test.com/dar\nMonitoring Status: IN_PROGRESS\n\n"); expectedOutput.append(String.format("\tproduct 1 url (%s)\n", product1.getUuid())); expectedOutput.append("\t\tDownloaded: 21,345 / Unknown (Unknown %)\n"); expectedOutput.append("\t\tStatus: NOT_STARTED\n"); expectedOutput.append("\t\tPriority: Normal\n\n"); expectedOutput.append(String.format("\tproduct 2 url (%s)\n", product2.getUuid())); expectedOutput.append("\t\tDownloaded: 0 / 0 (0%)\n"); expectedOutput.append("\t\tStatus: NOT_STARTED\n"); expectedOutput.append("\t\tPriority: High\n\n"); expectedOutput.append(String.format("\tproduct 3 url (%s)\n", product3.getUuid())); expectedOutput.append("\t\tDownloaded: 5,242,880 / 5,242,880 (100%)\n"); expectedOutput.append("\t\tStatus: COMPLETED\n\n"); assertEquals(expectedOutput.toString(), getStatus.getStatus()); }