Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 56bcd6520670a035eac6d4081b4ffd727e17eb10 (plain) (blame)
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/******************************************************************************
 *  Copyright (c) 2011 GitHub Inc.
 *  All rights reserved. This program and the accompanying materials
 *  are made available under the terms of the Eclipse Public License 2.0
 *  which accompanies this distribution, and is available at
 *  https://www.eclipse.org/legal/epl-2.0/
 *
 *  SPDX-License-Identifier: EPL-2.0
 *
 *  Contributors:
 *    Kevin Sawicki (GitHub Inc.) - initial API and implementation
 *****************************************************************************/
package org.eclipse.egit.github.core.tests.live;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;

import java.io.IOException;
import java.util.Collections;
import java.util.List;

import org.eclipse.egit.github.core.Blob;
import org.eclipse.egit.github.core.Commit;
import org.eclipse.egit.github.core.Reference;
import org.eclipse.egit.github.core.RepositoryId;
import org.eclipse.egit.github.core.Tree;
import org.eclipse.egit.github.core.TreeEntry;
import org.eclipse.egit.github.core.client.IGitHubConstants;
import org.eclipse.egit.github.core.service.DataService;
import org.eclipse.egit.github.core.util.EncodingUtils;
import org.junit.Test;

/**
 * Unit tests of {@link DataService}
 */
public class DataTest extends LiveTest {

	/**
	 * Create and fetch blob
	 * 
	 * @throws Exception
	 */
	@Test
	public void createAndFetchBlob() throws Exception {
		checkUser();
		assertNotNull("Repo is required for test", writableRepo);

		RepositoryId repo = RepositoryId.create(client.getUser(), writableRepo);
		DataService service = new DataService(client);
		Blob blob = new Blob();
		blob.setContent("test content");
		blob.setEncoding(Blob.ENCODING_UTF8);
		String sha = service.createBlob(repo, blob);
		assertNotNull(sha);
		Blob fetch = service.getBlob(repo, sha);
		assertNotNull(fetch);
		assertEquals(blob.getContent(),
				new String(EncodingUtils.fromBase64(fetch.getContent()),
						IGitHubConstants.CHARSET_UTF8));
		assertEquals(Blob.ENCODING_BASE64, fetch.getEncoding());
	}

	/**
	 * Create and fetch tree
	 * 
	 * @throws Exception
	 */
	@Test
	public void createAndFetchTree() throws Exception {
		checkUser();
		assertNotNull("Repo is required for test", writableRepo);

		RepositoryId repo = RepositoryId.create(client.getUser(), writableRepo);
		DataService service = new DataService(client);
		TreeEntry entry = new TreeEntry();
		entry.setPath("test.txt");
		entry.setSha("0000000000000000000000000000000000000000");
		entry.setType("blob");
		Tree tree = service.createTree(repo, Collections.singleton(entry));
		assertNotNull(tree);
		assertNotNull(tree.getSha());
		assertNotNull(tree.getUrl());
		assertNotNull(tree.getTree());
		assertEquals(1, tree.getTree().size());
		assertEquals(entry.getPath(), tree.getTree().get(0).getPath());
		assertEquals(entry.getSha(), tree.getTree().get(0).getSha());
		assertEquals(entry.getType(), tree.getTree().get(0).getType());

		Tree fetched = service.getTree(repo, tree.getSha());
		assertNotNull(fetched);
		assertEquals(tree.getSha(), fetched.getSha());
		assertEquals(tree.getUrl(), fetched.getUrl());
	}

	/**
	 * Create and fetch commit
	 * 
	 * @throws Exception
	 */
	@Test
	public void createAndFetchCommit() throws Exception {
		checkUser();
		assertNotNull("Repo is required for test", writableRepo);

		RepositoryId repo = RepositoryId.create(client.getUser(), writableRepo);
		DataService service = new DataService(client);
		Commit commit = new Commit();
		commit.setMessage("commit message");
		commit.setTree(new Tree()
				.setSha("0000000000000000000000000000000000000000"));
		Commit created = service.createCommit(repo, commit);
		assertNotNull(created);
		assertNotNull(created.getSha());
		Commit fetched = service.getCommit(repo, created.getSha());
		assertNotNull(fetched);
		assertEquals(created.getSha(), fetched.getSha());
		assertEquals(created.getMessage(), fetched.getMessage());
	}

	/**
	 * Test fetching references
	 * 
	 * @throws IOException
	 */
	@Test
	public void fetchReferences() throws IOException {
		checkUser();
		assertNotNull("Repo is required for test", writableRepo);

		RepositoryId repo = RepositoryId.create(client.getUser(), writableRepo);
		DataService service = new DataService(client);
		List<Reference> refs = service.getReferences(repo);
		assertNotNull(refs);
		assertFalse(refs.isEmpty());
		for (Reference ref : refs) {
			assertNotNull(ref);
			assertNotNull(ref.getRef());
			assertNotNull(ref.getUrl());
			assertNotNull(ref.getObject());
			assertNotNull(ref.getObject().getSha());
			Reference fetched = service.getReference(repo, ref.getRef());
			assertNotNull(fetched);
			assertEquals(ref.getRef(), fetched.getRef());
			assertEquals(ref.getUrl(), fetched.getUrl());
		}
	}
}

Back to the top