b4d2c3344691d8907fc41f0b30286a1f0fc22526
[taxeditor.git] / eu.etaxonomy.taxeditor.test / src / test / java / eu / etaxonomy / taxeditor / service / ProgressMonitorServiceTest.java
1 // $Id$
2 /**
3 * Copyright (C) 2015 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
9 */
10 package eu.etaxonomy.taxeditor.service;
11
12 import java.io.Serializable;
13 import java.util.ArrayList;
14 import java.util.Arrays;
15 import java.util.List;
16 import java.util.UUID;
17
18 import org.apache.log4j.Logger;
19 import org.eclipse.core.runtime.NullProgressMonitor;
20 import org.junit.After;
21 import org.junit.Assert;
22 import org.junit.Test;
23
24 import eu.etaxonomy.cdm.api.service.IProgressMonitorService;
25 import eu.etaxonomy.cdm.api.service.ITestService;
26 import eu.etaxonomy.cdm.common.monitor.IRemotingProgressMonitor;
27 import eu.etaxonomy.cdm.common.monitor.RemotingProgressMonitor;
28 import eu.etaxonomy.taxeditor.httpinvoker.RemotingSessionAwareTest;
29 import eu.etaxonomy.taxeditor.operation.IFeedbackGenerator;
30 import eu.etaxonomy.taxeditor.operation.IPostMoniteredOperationEnabled;
31 import eu.etaxonomy.taxeditor.store.CdmStore;
32
33 /**
34 * @author cmathew
35 * @date 22 Oct 2015
36 *
37 */
38 public class ProgressMonitorServiceTest extends RemotingSessionAwareTest {
39
40 private static final Logger logger = Logger.getLogger(ProgressMonitorServiceTest.class);
41
42 ITestService testService = getRemoteApplicationController().getTestService();
43
44 IProgressMonitorService progressMonitorService = getRemoteApplicationController().getProgressMonitorService();
45
46
47
48 @After
49 public void revertAuthentication() {
50 authenticateDefaultUser();
51 }
52
53 @Test
54 public void testMonitLongRunningMethod() throws InterruptedException {
55 UUID uuid = testService.monitLongRunningMethod(null, null);
56 int pollInterval = 1000;
57 RemotingProgressMonitor expectedMonitor = new RemotingProgressMonitor();
58 expectedMonitor.setResult("Success");
59 expectedMonitor.addReport("Report");
60 expectedMonitor.done();
61 CdmStore.getProgressMonitorClientManager().pollMonitor("Testing Progress Monitor",
62 uuid,
63 pollInterval,
64 new MockPostMoniteredOperationEnabled(expectedMonitor, uuid),
65 (IFeedbackGenerator)null,
66 new NullProgressMonitor());
67 }
68
69 @Test
70 public void testMonitLongRunningMethodByChangingUser() throws InterruptedException {
71
72 IllegalStateException ise = new IllegalStateException("IllegalStateException");
73 UUID uuid = testService.monitLongRunningMethod(ise, null);
74 authenticateExtraUser();
75 IRemotingProgressMonitor monitor = progressMonitorService.getRemotingMonitor(uuid);
76 Assert.assertNull(monitor);
77
78 }
79
80 @Test
81 public void testMonitLongRunningMethodWithException() throws InterruptedException {
82 IllegalStateException ise = new IllegalStateException("IllegalStateException");
83 UUID uuid = testService.monitLongRunningMethod(ise, null);
84 int pollInterval = 1000;
85 RemotingProgressMonitor expectedMonitor = new RemotingProgressMonitor();
86 expectedMonitor.setResult(ise);
87 expectedMonitor.setIsFailed(true);
88 expectedMonitor.done();
89 CdmStore.getProgressMonitorClientManager().pollMonitor("Testing Progress Monitor",
90 uuid,
91 pollInterval,
92 new MockPostMoniteredOperationEnabled(expectedMonitor, uuid),
93 (IFeedbackGenerator)null,
94 new NullProgressMonitor());
95 }
96
97 @Test
98 public void testMonitLongRunningMethodWithInterrupt() {
99 IllegalStateException ise = new IllegalStateException("Interrupted Exception");
100 final UUID uuid = testService.monitLongRunningMethod(ise, null);
101 final int pollInterval = 1000;
102 final RemotingProgressMonitor expectedMonitor = new RemotingProgressMonitor();
103 expectedMonitor.setResult(ise);
104 expectedMonitor.setCanceled(true);
105 expectedMonitor.setIsFailed(true);
106 expectedMonitor.done();
107
108 Thread thread = new Thread() {
109 @Override
110 public void run() {
111 try {
112 CdmStore.getProgressMonitorClientManager().pollMonitor("Testing Progress Monitor",
113 uuid,
114 pollInterval,
115 null,
116 (IFeedbackGenerator)null,
117 new NullProgressMonitor());
118 } catch (InterruptedException e) {
119
120 }
121 }
122 };
123 thread.start();
124 while(!progressMonitorService.isMonitorThreadRunning(uuid)) {}
125
126 progressMonitorService.interrupt(uuid);
127
128 while(progressMonitorService.isMonitorThreadRunning(uuid)) {}
129
130 IRemotingProgressMonitor remotingMonitor = progressMonitorService.getRemotingMonitor(uuid);
131 new MockPostMoniteredOperationEnabled(expectedMonitor, uuid).postOperation(remotingMonitor);
132
133 }
134
135 @Test
136 public void testMonitLongRunningMethodWithCancellation() throws InterruptedException {
137
138 final UUID uuid = testService.monitLongRunningMethod(null, null);
139 final int pollInterval = 1000;
140 final RemotingProgressMonitor expectedMonitor = new RemotingProgressMonitor();
141 expectedMonitor.setResult("Cancelled");
142 expectedMonitor.setCanceled(true);
143 expectedMonitor.done();
144
145 Thread thread = new Thread() {
146 @Override
147 public void run() {
148 try {
149 CdmStore.getProgressMonitorClientManager().pollMonitor("Testing Progress Monitor",
150 uuid,
151 pollInterval,
152 null,
153 (IFeedbackGenerator)null,
154 new NullProgressMonitor());
155 } catch (InterruptedException e) {
156
157 }
158 }
159
160 };
161 thread.start();
162 while(!progressMonitorService.isMonitorThreadRunning(uuid)) {}
163 progressMonitorService.cancel(uuid);
164 while(progressMonitorService.isMonitorThreadRunning(uuid)) {}
165 IRemotingProgressMonitor remotingMonitor = progressMonitorService.getRemotingMonitor(uuid);
166 new MockPostMoniteredOperationEnabled(expectedMonitor, uuid).postOperation(remotingMonitor);
167
168 }
169
170 @Test
171 public void testMonitLongRunningMethodWithWaitForFeedback() throws InterruptedException {
172
173 List<String> feedbacks = Arrays.asList("feedback1", "feedback2");
174 List<IFeedbackGenerator> feebackGenerators = new ArrayList<IFeedbackGenerator>();
175 final RemotingProgressMonitor expectedMonitor = new RemotingProgressMonitor();
176 expectedMonitor.setResult("Success");
177 for(String feedback : feedbacks) {
178 feebackGenerators.add(new MockFeedbackGenerator(feedback));
179 expectedMonitor.addReport(feedback);
180 }
181 expectedMonitor.addReport("Report");
182 expectedMonitor.done();
183
184 final UUID uuid = testService.monitLongRunningMethod(null, feedbacks);
185 final int pollInterval = 1000;
186
187 CdmStore.getProgressMonitorClientManager().pollMonitor("Testing Progress Monitor",
188 uuid,
189 pollInterval,
190 new MockPostMoniteredOperationEnabled(expectedMonitor, uuid),
191 feebackGenerators,
192 new NullProgressMonitor());
193
194 feebackGenerators.remove(1);
195
196 final UUID newUuid = testService.monitLongRunningMethod(null, feedbacks);
197
198 try {
199 CdmStore.getProgressMonitorClientManager().pollMonitor("Testing Progress Monitor",
200 newUuid,
201 pollInterval,
202 new MockPostMoniteredOperationEnabled(expectedMonitor, newUuid),
203 feebackGenerators,
204 new NullProgressMonitor());
205 Assert.fail("Exception due to inconsistent number of feedback generators not thrown");
206 } catch(IllegalStateException ise) {
207
208 }
209
210
211 }
212
213
214 class MockPostMoniteredOperationEnabled implements IPostMoniteredOperationEnabled {
215
216 private RemotingProgressMonitor expectedMonitor;
217 private UUID monitorUuid;
218
219 public MockPostMoniteredOperationEnabled(RemotingProgressMonitor expectedMonitor, UUID monitorUuid) {
220 this.expectedMonitor = expectedMonitor;
221 this.monitorUuid = monitorUuid;
222 }
223 /**
224 * {@inheritDoc}
225 */
226 @Override
227 public void postOperation(IRemotingProgressMonitor monitor) {
228 if(expectedMonitor.getResult() instanceof Exception) {
229 Exception expectedException = (Exception) expectedMonitor.getResult();
230 Exception exception = (Exception) monitor.getResult();
231 Assert.assertEquals(expectedException.getClass(), expectedException.getClass());
232 Assert.assertEquals(expectedException.getMessage(), expectedException.getMessage());
233 } else {
234 Assert.assertEquals(expectedMonitor.getResult(), monitor.getResult());
235 }
236 Assert.assertEquals(expectedMonitor.getReports(), monitor.getReports());
237 Assert.assertEquals(expectedMonitor.isCanceled(), monitor.isCanceled());
238 Assert.assertEquals(expectedMonitor.isFailed(), monitor.isFailed());
239 Assert.assertEquals(expectedMonitor.isDone(), monitor.isDone());
240 Assert.assertTrue(!progressMonitorService.isMonitorThreadRunning(monitorUuid));
241 }
242 }
243
244 class MockFeedbackGenerator implements IFeedbackGenerator {
245
246 private String feedback;
247
248 public MockFeedbackGenerator(String feedback) {
249 this.feedback = feedback;
250 }
251 /**
252 * {@inheritDoc}
253 */
254 @Override
255 public Serializable generateFeedback() {
256 return feedback;
257 }
258
259 }
260 }