aboutsummaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit.lfs.server.test/tst/org/eclipse/jgit/lfs/server/fs/DownloadTest.java
blob: 8d32f9e75a70f2586011ad1d412f804cfd979312 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
 * Copyright (C) 2015, Matthias Sohn <matthias.sohn@sap.com> and others
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Distribution License v. 1.0 which is available at
 * https://www.eclipse.org/org/documents/edl-v10.php.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */
package org.eclipse.jgit.lfs.server.fs;

import static org.apache.http.HttpStatus.SC_NOT_FOUND;
import static org.apache.http.HttpStatus.SC_UNPROCESSABLE_ENTITY;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThrows;

import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.MessageFormat;

import org.apache.http.client.ClientProtocolException;
import org.eclipse.jgit.lfs.lib.AnyLongObjectId;
import org.eclipse.jgit.lfs.test.LongObjectIdTestUtils;
import org.eclipse.jgit.util.FileUtils;
import org.junit.Test;

public class DownloadTest extends LfsServerTest {

	@Test
	public void testDownload() throws Exception {
		String TEXT = "test";
		AnyLongObjectId id = putContent(TEXT);
		Path f = Paths.get(getTempDirectory().toString(), "download");
		long len = getContent(id, f);
		assertEquals(TEXT.length(), len);
		FileUtils.delete(f.toFile(), FileUtils.RETRY);
	}

	@Test
	public void testDownloadInvalidPathInfo()
			throws ClientProtocolException, IOException {
		String TEXT = "test";
		String id = putContent(TEXT).name().substring(0, 60);
		Path f = Paths.get(getTempDirectory().toString(), "download");
		String error = String.format(
				"Invalid pathInfo: '/%s' does not match '/{SHA-256}'", id);
		assertThrows(formatErrorMessage(SC_UNPROCESSABLE_ENTITY, error),
				RuntimeException.class, () -> getContent(id, f));
	}

	@Test
	public void testDownloadInvalidId()
			throws ClientProtocolException, IOException {
		String TEXT = "test";
		String id = putContent(TEXT).name().replace('f', 'z');
		Path f = Paths.get(getTempDirectory().toString(), "download");
		String error = String.format("Invalid id: %s", id);
		assertThrows(formatErrorMessage(SC_UNPROCESSABLE_ENTITY, error),
				RuntimeException.class, () -> getContent(id, f));
	}

	@Test
	public void testDownloadNotFound() {
		String TEXT = "test";
		AnyLongObjectId id = LongObjectIdTestUtils.hash(TEXT);
		Path f = Paths.get(getTempDirectory().toString(), "download");
		String error = String.format("Object '%s' not found", id.getName());
		assertThrows(formatErrorMessage(SC_NOT_FOUND, error),
				RuntimeException.class, () -> getContent(id, f));
	}

	@SuppressWarnings("boxing")
	@Test
	public void testLargeFileDownload() throws Exception {
		Path f = Paths.get(getTempDirectory().toString(), "largeRandomFile");
		long expectedLen = createPseudoRandomContentFile(f, 5 * MiB);
		AnyLongObjectId id = putContent(f);
		Path f2 = Paths.get(getTempDirectory().toString(), "download");
		long start = System.nanoTime();
		long len = getContent(id, f2);
		System.out.println(
				MessageFormat.format("downloaded 10 MiB random data in {0}ms",
						(System.nanoTime() - start) / 1e6));
		assertEquals(expectedLen, len);
		FileUtils.delete(f.toFile(), FileUtils.RETRY);

	}

	@SuppressWarnings("boxing")
	private String formatErrorMessage(int status, String message) {
		return String.format("Status: %d {\"message\":\"%s\"}", status,
				message);
	}
}