Skip to main content
summaryrefslogtreecommitdiffstats
blob: ab297bb4a441e9d0b64e8971bcbf749f41bf980c (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
/*******************************************************************************
 * Copyright (c) 2013, 2015 Tasktop Technologies and others.
 * 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:
 *     Tasktop Technologies - initial API and implementation
 *******************************************************************************/

package org.eclipse.mylyn.gerrit.tests.core.client.compat;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import org.eclipse.mylyn.commons.sdk.util.CommonTestUtil;
import org.eclipse.mylyn.internal.gerrit.core.client.JSonSupport;
import org.eclipse.mylyn.internal.gerrit.core.client.compat.ChangeDetailX;
import org.eclipse.mylyn.internal.gerrit.core.client.compat.GerritConfigX;
import org.eclipse.mylyn.internal.gerrit.core.client.compat.SubmitRecord;
import org.eclipse.mylyn.internal.gerrit.core.client.compat.SubmitRecord.Label;
import org.eclipse.mylyn.internal.gerrit.core.client.rest.ApprovalUtil;
import org.junit.Test;

import com.google.gerrit.common.data.ApprovalType;
import com.google.gerrit.common.data.ApprovalTypes;
import com.google.gerrit.reviewdb.ChangeMessage;

import junit.framework.TestCase;

public class ChangeDetailXTest extends TestCase {
	@Test
	public void testCustomLabel() throws Exception {
		ChangeDetailXAsResult result = parseFile("testdata/ChangeDetailX.json");

		assertEquals("2.0", result.jsonrpc);
		assertEquals(1, result.id);
		assertNotNull(result.result);

		ChangeDetailX changeDetailX = result.result;

		assertNull(changeDetailX.getApprovalTypes());
		assertEquals(1, changeDetailX.getSubmitRecords().size());
		SubmitRecord submitRecord = changeDetailX.getSubmitRecords().get(0);
		assertEquals("NOT_READY", submitRecord.getStatus());
		assertEquals(3, submitRecord.getLabels().size());

		Iterator<Label> labels = submitRecord.getLabels().iterator();
		assertLabelEqual("Non-Author-Code-Review", labels.next());
		assertLabelEqual("Verified", labels.next());
		assertLabelEqual("Code-Review", labels.next());

		changeDetailX.convertSubmitRecordsToApprovalTypes(getTestConfig().getApprovalTypes());
		assertNotNull(changeDetailX.getApprovalTypes());
		assertEquals(3, changeDetailX.getApprovalTypes().size());
		Iterator<ApprovalType> approvalTypes = changeDetailX.getApprovalTypes().iterator();
		ApprovalType custom = approvalTypes.next();
		assertEquals("Non-Author-Code-Review", custom.getCategory().getName());
		// that's all we know about the approval, the rest is void
		assertNull(custom.getCategory().getAbbreviatedName());
		assertTrue(custom.getValues().isEmpty());
		assertCategoriesEqual(ApprovalUtil.VRIF, approvalTypes.next());
		assertCategoriesEqual(ApprovalUtil.CRVW, approvalTypes.next());
	}

	@Test
	public void testConvertWhenApprovalTypesNotNull() throws Exception {
		ChangeDetailX changeDetailX = new ChangeDetailX();
		changeDetailX.setApprovalTypes(Collections.singleton(ApprovalUtil.CRVW));

		try {
			changeDetailX.convertSubmitRecordsToApprovalTypes(getTestConfig().getApprovalTypes());
			fail();
		} catch (IllegalStateException e) {
			// expected
		}
	}

	@Test
	public void testAbandoned() throws Exception {
		ChangeDetailXAsResult result = parseFile("testdata/ChangeDetailX_abandoned.json");

		assertEquals("2.0", result.jsonrpc);
		assertEquals(3, result.id);
		assertNotNull(result.result);

		ChangeDetailX changeDetailX = result.result;

		assertNull(changeDetailX.getApprovalTypes());
		assertNull(changeDetailX.getSubmitRecords());

		changeDetailX.convertSubmitRecordsToApprovalTypes(getTestConfig().getApprovalTypes());
		assertNull(changeDetailX.getApprovalTypes()); // nothing has changed
	}

	@Test
	public void testContinousIntegrationCommentsAreRemoved() throws Exception {
		ChangeDetailXAsResult result = parseFile("testdata/ChangeDetailX_hudson.json");
		ChangeDetailX changeDetailX = result.result;

		assertEquals(1, changeDetailX.getMessages().size());

		// Make sure the message with the Hudson message was removed
		ChangeMessage lastMessageLeft = changeDetailX.getMessages().get(0);
		assertFalse(lastMessageLeft.getMessage().contains("hudson.eclipse.org"));
	}

	private void assertLabelEqual(String expectedLabel, Label label) {
		assertEquals(expectedLabel, label.getLabel());
		assertEquals("NEED", label.getStatus());
		assertNull(label.getAppliedBy());
	}

	private static void assertCategoriesEqual(ApprovalType expected, ApprovalType actual) {
		assertEquals(expected.getCategory().getId().get(), actual.getCategory().getId().get());
	}

	private static GerritConfigX getTestConfig() {
		GerritConfigX result = new GerritConfigX();
		List<ApprovalType> approvals = new ArrayList<ApprovalType>(3);
		approvals.add(ApprovalUtil.CRVW);
		approvals.add(ApprovalUtil.VRIF);
		approvals.add(ApprovalUtil.IPCL);
		result.setApprovalTypes(new ApprovalTypes(approvals, null));
		return result;
	}

	private ChangeDetailXAsResult parseFile(String path) throws IOException {
		File file = CommonTestUtil.getFile(this, path);
		String content = CommonTestUtil.read(file);
		return new JSonSupport().parseResponse(content, ChangeDetailXAsResult.class);
	}

	private class ChangeDetailXAsResult {
		private String jsonrpc;

		private int id;

		private ChangeDetailX result;
	}
}

Back to the top