Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 08b9bc030dfdcb81381217b2c7d5a3bb8a9b0f24 (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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/*******************************************************************************
 * Copyright (c) 2017 Red Hat Inc. 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:
 *     Red Hat - Initial Contribution
 *******************************************************************************/
package org.eclipse.linuxtools.mylyn.osio.rest.test.support;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.http.Header;
import org.apache.http.HeaderElement;
import org.apache.http.NameValuePair;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.linuxtools.internal.mylyn.osio.rest.core.IOSIORestRequestProvider;
import org.eclipse.linuxtools.internal.mylyn.osio.rest.core.OSIORestClient;
import org.eclipse.linuxtools.internal.mylyn.osio.rest.core.OSIORestConfiguration;
import org.eclipse.linuxtools.internal.mylyn.osio.rest.core.OSIORestConnector;
import org.eclipse.linuxtools.internal.mylyn.osio.rest.core.OSIORestException;
import org.eclipse.linuxtools.internal.mylyn.osio.rest.core.OSIORestResourceMovedPermanentlyException;
import org.eclipse.linuxtools.internal.mylyn.osio.rest.core.OSIORestTaskSchema;
import org.eclipse.linuxtools.internal.mylyn.osio.rest.core.response.data.Identity;
import org.eclipse.linuxtools.internal.mylyn.osio.rest.core.response.data.LinkResponse;
import org.eclipse.linuxtools.internal.mylyn.osio.rest.core.response.data.RestResponse;
import org.eclipse.linuxtools.internal.mylyn.osio.rest.core.response.data.SingleRestResponse;
import org.eclipse.linuxtools.internal.mylyn.osio.rest.core.response.data.Space;
import org.eclipse.linuxtools.internal.mylyn.osio.rest.core.response.data.WorkItem;
import org.eclipse.mylyn.commons.core.operations.IOperationMonitor;
import org.eclipse.mylyn.commons.repositories.http.core.CommonHttpClient;
import org.eclipse.mylyn.tasks.core.TaskRepository;
import org.eclipse.mylyn.tasks.core.data.TaskAttribute;
import org.eclipse.mylyn.tasks.core.data.TaskData;

import com.google.gson.reflect.TypeToken;

@SuppressWarnings("restriction")
public class OSIOTestRestRequestProvider implements IOSIORestRequestProvider {
	
	private Map<String, Object> requestMap = new HashMap<>();
	private Map<String, Object> postMap = new HashMap<>();
	private Map<String, String> relocations = new HashMap<>();
	
	public void addGetRequest (String url, Object object) {
		requestMap.put(url, object);
	}
	
	public void addPostRequest (String url, Object object) {
		postMap.put(url, object);
	}

	public void addRelocation (String url, String relocation) {
		relocations.put(url, relocation);
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public <R extends RestResponse<?>> R getRequest(IOperationMonitor monitor, CommonHttpClient client,
			String urlSuffix, TypeToken<?> responseType) throws OSIORestException {
		return (R)requestMap.get(urlSuffix);
	}

	@SuppressWarnings("unchecked")
	@Override
	public <R extends SingleRestResponse<?>> R getSingleRequest(IOperationMonitor monitor, CommonHttpClient client,
			String urlSuffix, TypeToken<?> responseType) throws OSIORestException {
		return (R)requestMap.get(urlSuffix);
	}

	@SuppressWarnings("unchecked")
	@Override
	public <R extends RestResponse<?>> R getRequest(IOperationMonitor monitor, CommonHttpClient client,
			String urlSuffix, TypeToken<?> responseType, boolean authrequired) throws OSIORestException {
		return (R)requestMap.get(urlSuffix);
	}

	@SuppressWarnings("unchecked")
	@Override
	public <R extends SingleRestResponse<?>> R getSingleRequest(IOperationMonitor monitor, CommonHttpClient client,
			String urlSuffix, TypeToken<?> responseType, boolean authrequired) throws OSIORestException {
		return (R)requestMap.get(urlSuffix);
	}

	@SuppressWarnings("unchecked")
	@Override
	public <R extends RestResponse<?>> R getRequest(IOperationMonitor monitor, CommonHttpClient client,
			String urlSuffix, TypeToken<?> responseType, boolean authrequired, boolean authURLrequired)
			throws OSIORestException {
		return (R)requestMap.get(urlSuffix);
	}

	@SuppressWarnings("unchecked")
	@Override
	public <R extends SingleRestResponse<?>> R getSingleRequest(IOperationMonitor monitor, CommonHttpClient client,
			String urlSuffix, TypeToken<?> responseType, boolean authrequired, boolean authURLrequired)
			throws OSIORestException {
		return (R)requestMap.get(urlSuffix);
	}

	@Override
	public String postNewTask(IOperationMonitor monitor, CommonHttpClient client, TaskData taskData, Space space,
			OSIORestConnector connector, TaskRepository repository) throws OSIORestException, CoreException {
		TaskAttribute spaceAttr = taskData.getRoot().getAttribute(OSIORestTaskSchema.getDefault().SPACE.getKey());
		return null;
	}

	@Override
	public TaskAttribute postNewCommentTask(IOperationMonitor monitor, CommonHttpClient client, TaskData taskData,
			Set<TaskAttribute> oldAttributes) throws OSIORestException {
		return null;
	}

	@Override
	public TaskData patchUpdateTask(IOperationMonitor monitor, CommonHttpClient client, TaskData taskData,
			Set<TaskAttribute> oldAttributes, Space space) throws OSIORestException {
		return null;
	}

	@Override
	public String deleteLink(IOperationMonitor monitor, CommonHttpClient client, String wid, String id)
			throws OSIORestException {
		String query = "/workitems/" + //$NON-NLS-1$ 
				wid + 
				"/relationships/links";
		requestMap.remove(query);
		return "ok";
	}

	@Override
	public LinkResponse postNewLink(IOperationMonitor monitor, CommonHttpClient client, String linkid, String sourceid,
			String targetid, boolean isForward) throws OSIORestException {
		String query = "/workitems/" + //$NON-NLS-1$ 
				(isForward ? sourceid : targetid) + 
				"/relationships/links";
		LinkResponse response = (LinkResponse)postMap.get(query);
		WorkItem source = (WorkItem)requestMap.get("/workitems/" + sourceid);
		WorkItem target = (WorkItem)requestMap.get("/workitems/" + targetid);
		String sourceNum = (String)source.getNumber();
		String targetNum = (String)target.getNumber();
		requestMap.put(query, response);
		
//		String link += " " + workitem.getTitle() + " [" //$NON-NLS-1$ //$NON-NLS-2$ 
//				+ otherWorkItem + "]"; //$NON-NLS-1$

		return response;
	}

	public class TestHeaderElement implements HeaderElement {
		
		private String name;
		private String value;
		
		public TestHeaderElement (String name, String value) {
			this.name = name;
			this.value = value;
		}

		@Override
		public String getName() {
			return name;
		}

		@Override
		public NameValuePair getParameter(int arg0) {
			return null;
		}

		@Override
		public NameValuePair getParameterByName(String arg0) {
			return null;
		}

		@Override
		public int getParameterCount() {
			return 0;
		}

		@Override
		public NameValuePair[] getParameters() {
			return null;
		}

		@Override
		public String getValue() {
			return value;
		}
	}
	
	public class TestHeader implements Header {
		
		private String location;
		
		public TestHeader (String location) {
			this.location = location;
		}
		
		@Override
		public HeaderElement[] getElements() {
			HeaderElement h = new TestHeaderElement("Location", location);
			HeaderElement[] elements = new HeaderElement[] {h};
			return elements; 
		}

		@Override
		public String getName() {
			return null;
		}

		@Override
		public String getValue() {
			return null;
		}
		
	}
	
	
	@Override
	public String getWID(IOperationMonitor monitor, CommonHttpClient client, String query,
			TaskRepository taskRepository) throws OSIORestException {
		String location = relocations.get(query);
		TestHeader t = new TestHeader(location);
		throw new OSIORestResourceMovedPermanentlyException(t);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<TaskData> getTaskData(IOperationMonitor monitor, CommonHttpClient client, OSIORestConnector connector,
			String queryUrlSuffix, TaskRepository taskRepository) throws OSIORestException, CoreException {
		return (List<TaskData>)requestMap.get(queryUrlSuffix);
	}

	@Override
	public TaskData getSingleTaskData(IOperationMonitor monitor, CommonHttpClient client, OSIORestConnector connector,
			String workitemquery, TaskRepository taskRepository) throws OSIORestException, CoreException {
		return (TaskData)requestMap.get(workitemquery);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<TaskAttribute> getTaskComments(IOperationMonitor monitor, CommonHttpClient client, Space space,
			TaskData taskData) throws OSIORestException {
		List<TaskAttribute> comments = (List<TaskAttribute>)requestMap.get("/workitems/" + taskData.getRoot().getAttribute(OSIORestTaskSchema.getDefault().UUID.getKey()).getValue() + "/comments");
		TaskAttribute root = taskData.getRoot();
		for (TaskAttribute comment : comments) {
			root.deepAddCopy(comment);
		}
		return comments; 
	}

	@Override
	public WorkItem getWorkItem(IOperationMonitor monitor, CommonHttpClient client, String id) throws OSIORestException {
		WorkItem workitem = (WorkItem)requestMap.get("/workitems/" + id);
		return workitem; 
	}
	
	@Override
	public TaskAttribute getTaskCreator(IOperationMonitor monitor, CommonHttpClient client, TaskData taskData)
			throws OSIORestException {
		TaskAttribute creator = (TaskAttribute)requestMap.get("/users/" + taskData.getRoot().getAttribute(OSIORestTaskSchema.getDefault().CREATOR_ID.getKey()).getValue());
		taskData.getRoot().deepAddCopy(creator);
		return creator;
	}

	@Override
	public TaskAttribute getTaskLabels(IOperationMonitor monitor, CommonHttpClient client, Space space,
			TaskData taskData) throws OSIORestException {
		TaskAttribute labels = (TaskAttribute)requestMap.get("/workitems/" + taskData.getRoot().getAttribute(OSIORestTaskSchema.getDefault().UUID.getKey()).getValue() + "/labels");
		taskData.getRoot().deepAddCopy(labels);
		return labels;
	}

	@Override
	public TaskAttribute getTaskLinks(IOperationMonitor monitor, CommonHttpClient client, OSIORestClient restClient,
			Space space, TaskData taskData, OSIORestConfiguration config) throws OSIORestException {
		TaskAttribute links = (TaskAttribute)requestMap.get("/workitems/" + taskData.getRoot().getAttribute(OSIORestTaskSchema.getDefault().UUID.getKey()).getValue() + "/relationships/links");
		taskData.getRoot().deepAddCopy(links);
		return links;
	}

	@Override
	public TaskAttribute getTaskAssignee(IOperationMonitor monitor, CommonHttpClient client, String id,
			TaskData taskData) throws OSIORestException {
		TaskAttribute assignee = (TaskAttribute)requestMap.get("/users/" + id);
		TaskAttribute originalAttr = taskData.getRoot().getAttribute(OSIORestTaskSchema.getDefault().ASSIGNEES.getKey());
		originalAttr.addValue(assignee.getValue());
		return assignee;
	}

	@Override
	public Identity getAuthUser(IOperationMonitor monitor, CommonHttpClient client) throws OSIORestException {
		return (Identity)requestMap.get("/user");
	}

}

Back to the top