View Javadoc
1   /*
2    * Copyright (C) 2015, Matthias Sohn <matthias.sohn@sap.com> and others
3    *
4    * This program and the accompanying materials are made available under the
5    * terms of the Eclipse Distribution License v. 1.0 which is available at
6    * https://www.eclipse.org/org/documents/edl-v10.php.
7    *
8    * SPDX-License-Identifier: BSD-3-Clause
9    */
10  package org.eclipse.jgit.lfs.server.fs;
11  
12  import static org.apache.http.HttpStatus.SC_NOT_FOUND;
13  import static org.apache.http.HttpStatus.SC_UNPROCESSABLE_ENTITY;
14  import static org.junit.Assert.assertEquals;
15  import static org.junit.Assert.assertThrows;
16  
17  import java.io.IOException;
18  import java.nio.file.Path;
19  import java.nio.file.Paths;
20  import java.text.MessageFormat;
21  
22  import org.apache.http.client.ClientProtocolException;
23  import org.eclipse.jgit.lfs.lib.AnyLongObjectId;
24  import org.eclipse.jgit.lfs.test.LongObjectIdTestUtils;
25  import org.eclipse.jgit.util.FileUtils;
26  import org.junit.Test;
27  
28  public class DownloadTest extends LfsServerTest {
29  
30  	@Test
31  	public void testDownload() throws Exception {
32  		String TEXT = "test";
33  		AnyLongObjectId id = putContent(TEXT);
34  		Path f = Paths.get(getTempDirectory().toString(), "download");
35  		long len = getContent(id, f);
36  		assertEquals(TEXT.length(), len);
37  		FileUtils.delete(f.toFile(), FileUtils.RETRY);
38  	}
39  
40  	@Test
41  	public void testDownloadInvalidPathInfo()
42  			throws ClientProtocolException, IOException {
43  		String TEXT = "test";
44  		String id = putContent(TEXT).name().substring(0, 60);
45  		Path f = Paths.get(getTempDirectory().toString(), "download");
46  		String error = String.format(
47  				"Invalid pathInfo: '/%s' does not match '/{SHA-256}'", id);
48  		assertThrows(formatErrorMessage(SC_UNPROCESSABLE_ENTITY, error),
49  				RuntimeException.class, () -> getContent(id, f));
50  	}
51  
52  	@Test
53  	public void testDownloadInvalidId()
54  			throws ClientProtocolException, IOException {
55  		String TEXT = "test";
56  		String id = putContent(TEXT).name().replace('f', 'z');
57  		Path f = Paths.get(getTempDirectory().toString(), "download");
58  		String error = String.format("Invalid id: %s", id);
59  		assertThrows(formatErrorMessage(SC_UNPROCESSABLE_ENTITY, error),
60  				RuntimeException.class, () -> getContent(id, f));
61  	}
62  
63  	@Test
64  	public void testDownloadNotFound() {
65  		String TEXT = "test";
66  		AnyLongObjectId id = LongObjectIdTestUtils.hash(TEXT);
67  		Path f = Paths.get(getTempDirectory().toString(), "download");
68  		String error = String.format("Object '%s' not found", id.getName());
69  		assertThrows(formatErrorMessage(SC_NOT_FOUND, error),
70  				RuntimeException.class, () -> getContent(id, f));
71  	}
72  
73  	@SuppressWarnings("boxing")
74  	@Test
75  	public void testLargeFileDownload() throws Exception {
76  		Path f = Paths.get(getTempDirectory().toString(), "largeRandomFile");
77  		long expectedLen = createPseudoRandomContentFile(f, 5 * MiB);
78  		AnyLongObjectId id = putContent(f);
79  		Path f2 = Paths.get(getTempDirectory().toString(), "download");
80  		long start = System.nanoTime();
81  		long len = getContent(id, f2);
82  		System.out.println(
83  				MessageFormat.format("downloaded 10 MiB random data in {0}ms",
84  						(System.nanoTime() - start) / 1e6));
85  		assertEquals(expectedLen, len);
86  		FileUtils.delete(f.toFile(), FileUtils.RETRY);
87  
88  	}
89  
90  	@SuppressWarnings("boxing")
91  	private String formatErrorMessage(int status, String message) {
92  		return String.format("Status: %d {\"message\":\"%s\"}", status,
93  				message);
94  	}
95  }