Skip to main content
summaryrefslogtreecommitdiffstats
blob: 8aa2c4230dcbe03896b91596d2c654ba1afc9890 (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
/******************************************************************************
 *  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 v1.0
 *  which accompanies this distribution, and is available at
 *  http://www.eclipse.org/legal/epl-v10.html
 *
 *  Contributors:
 *    Kevin Sawicki (GitHub Inc.) - initial API and implementation
 *****************************************************************************/
package org.eclipse.egit.github.core.tests;

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

import org.eclipse.egit.github.core.RequestError;
import org.eclipse.egit.github.core.client.GsonUtils;
import org.eclipse.egit.github.core.client.RequestException;
import org.junit.Test;

/**
 * Unit tests of {@link RequestException}
 */
public class RequestExceptionTest {

	/**
	 * Formatted error message for invalid field
	 */
	@Test
	public void invalidField() {
		RequestError error = GsonUtils.fromJson(
				"{\"errors\":[{\"code\":\"invalid\", \"field\":\"page\"}]}",
				RequestError.class);
		RequestException e = new RequestException(error, 400);
		String formatted = e.formatErrors();
		assertNotNull(formatted);
		assertEquals("400: Invalid value for 'page' field", formatted);
	}

	/**
	 * Formatted error message with invalid field value
	 */
	@Test
	public void invalidFieldValue() {
		RequestError error = GsonUtils
				.fromJson(
						"{\"errors\":[{\"code\":\"invalid\", \"field\":\"name\", \"value\":\"100\"}]}",
						RequestError.class);
		RequestException e = new RequestException(error, 401);
		String formatted = e.formatErrors();
		assertNotNull(formatted);
		assertEquals("401: Invalid value of '100' for 'name' field", formatted);
	}

	/**
	 * Formatted error message for missing field
	 */
	@Test
	public void missingField() {
		RequestError error = GsonUtils
				.fromJson(
						"{\"errors\":[{\"code\":\"missing_field\", \"field\":\"due\"}]}",
						RequestError.class);
		RequestException e = new RequestException(error, 422);
		String formatted = e.formatErrors();
		assertNotNull(formatted);
		assertEquals("422: Missing required 'due' field", formatted);
	}

	/**
	 * Formatted error message for existing resource with field
	 */
	@Test
	public void existentField() {
		RequestError error = GsonUtils
				.fromJson(
						"{\"errors\":[{\"code\":\"already_exists\", \"field\":\"severity\",  \"resource\":\"Issue\"}]}",
						RequestError.class);
		RequestException e = new RequestException(error, 500);
		String formatted = e.formatErrors();
		assertNotNull(formatted);
		assertEquals(
				"500: Issue resource with 'severity' field already exists",
				formatted);
	}

	/**
	 * Formatted error message for error in field
	 */
	@Test
	public void errorField() {
		RequestError error = GsonUtils
				.fromJson(
						"{\"errors\":[{\"field\":\"priority\", \"resource\":\"Gist\"}]}",
						RequestError.class);
		RequestException e = new RequestException(error, 400);
		String formatted = e.formatErrors();
		assertNotNull(formatted);
		assertEquals("400: Error with 'priority' field in Gist resource",
				formatted);
	}
}

Back to the top