#5297 Integrate remoting monitor cancel functionality in ui
[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.util.UUID;
13
14 import org.apache.log4j.Logger;
15 import org.eclipse.core.runtime.NullProgressMonitor;
16 import org.junit.After;
17 import org.junit.Assert;
18 import org.junit.Test;
19
20 import eu.etaxonomy.cdm.api.service.IProgressMonitorService;
21 import eu.etaxonomy.cdm.api.service.ITestService;
22 import eu.etaxonomy.cdm.common.monitor.IRemotingProgressMonitor;
23 import eu.etaxonomy.cdm.common.monitor.RemotingProgressMonitor;
24 import eu.etaxonomy.taxeditor.httpinvoker.RemotingSessionAwareTest;
25 import eu.etaxonomy.taxeditor.operation.IPostMoniteredOperationEnabled;
26 import eu.etaxonomy.taxeditor.store.CdmStore;
27
28 /**
29 * @author cmathew
30 * @date 22 Oct 2015
31 *
32 */
33 public class ProgressMonitorServiceTest extends RemotingSessionAwareTest {
34
35 private static final Logger logger = Logger.getLogger(ProgressMonitorServiceTest.class);
36
37 ITestService testService = getRemoteApplicationController().getTestService();
38
39 IProgressMonitorService progressMonitorService = getRemoteApplicationController().getProgressMonitorService();
40
41
42
43 @After
44 public void revertAuthentication() {
45 authenticateDefaultUser();
46 }
47
48 @Test
49 public void testMonitLongRunningMethod() throws InterruptedException {
50 UUID uuid = testService.monitLongRunningMethod(null);
51 int pollInterval = 1000;
52 RemotingProgressMonitor expectedMonitor = new RemotingProgressMonitor();
53 expectedMonitor.setResult("Success");
54 expectedMonitor.addReport("Report");
55 expectedMonitor.done();
56 IRemotingProgressMonitor remotingMonitor = CdmStore.getProgressMonitorClientManager().pollMonitor("Testing Progress Monitor",
57 uuid,
58 pollInterval,
59 new MockPostMoniteredOperationEnabled(expectedMonitor, uuid),
60 new NullProgressMonitor());
61 }
62
63 @Test
64 public void testMonitLongRunningMethodByChangingUser() throws InterruptedException {
65
66 IllegalStateException ise = new IllegalStateException("IllegalStateException");
67 UUID uuid = testService.monitLongRunningMethod(ise);
68 authenticateExtraUser();
69 IRemotingProgressMonitor monitor = progressMonitorService.getRemotingMonitor(uuid);
70 Assert.assertNull(monitor);
71
72 }
73
74 @Test
75 public void testMonitLongRunningMethodWithException() throws InterruptedException {
76 IllegalStateException ise = new IllegalStateException("IllegalStateException");
77 UUID uuid = testService.monitLongRunningMethod(ise);
78 int pollInterval = 1000;
79 RemotingProgressMonitor expectedMonitor = new RemotingProgressMonitor();
80 expectedMonitor.setResult(ise);
81 expectedMonitor.setIsFailed(true);
82 expectedMonitor.done();
83 IRemotingProgressMonitor remotingMonitor = CdmStore.getProgressMonitorClientManager().pollMonitor("Testing Progress Monitor",
84 uuid,
85 pollInterval,
86 new MockPostMoniteredOperationEnabled(expectedMonitor, uuid),
87 new NullProgressMonitor());
88 }
89
90 @Test
91 public void testMonitLongRunningMethodWithInterrupt() throws InterruptedException {
92 IllegalStateException ise = new IllegalStateException("Interrupted Exception");
93 final UUID uuid = testService.monitLongRunningMethod(ise);
94 final int pollInterval = 1000;
95 final RemotingProgressMonitor expectedMonitor = new RemotingProgressMonitor();
96 expectedMonitor.setResult(ise);
97 expectedMonitor.setCanceled(true);
98 expectedMonitor.setIsFailed(true);
99 expectedMonitor.done();
100
101 Thread thread = new Thread() {
102 @Override
103 public void run() {
104 try {
105 IRemotingProgressMonitor remotingMonitor = CdmStore.getProgressMonitorClientManager().pollMonitor("Testing Progress Monitor",
106 uuid,
107 pollInterval,
108 null,
109 new NullProgressMonitor());
110 } catch (InterruptedException e) {
111
112 }
113 }
114
115 };
116 thread.start();
117 while(!progressMonitorService.isMonitorThreadRunning(uuid)) {}
118
119 progressMonitorService.interrupt(uuid);
120
121 while(progressMonitorService.isMonitorThreadRunning(uuid)) {}
122
123 IRemotingProgressMonitor remotingMonitor = progressMonitorService.getRemotingMonitor(uuid);
124 new MockPostMoniteredOperationEnabled(expectedMonitor, uuid).postOperation(remotingMonitor);
125
126 }
127
128 @Test
129 public void testMonitLongRunningMethodWithCancellation() throws InterruptedException {
130
131 final UUID uuid = testService.monitLongRunningMethod(null);
132 final int pollInterval = 1000;
133 final RemotingProgressMonitor expectedMonitor = new RemotingProgressMonitor();
134 expectedMonitor.setResult("Cancelled");
135 expectedMonitor.setCanceled(true);
136 expectedMonitor.done();
137
138 Thread thread = new Thread() {
139 @Override
140 public void run() {
141 try {
142 IRemotingProgressMonitor remotingMonitor = CdmStore.getProgressMonitorClientManager().pollMonitor("Testing Progress Monitor",
143 uuid,
144 pollInterval,
145 null,
146 new NullProgressMonitor());
147 } catch (InterruptedException e) {
148
149 }
150 }
151
152 };
153 thread.start();
154 while(!progressMonitorService.isMonitorThreadRunning(uuid)) {}
155 progressMonitorService.cancel(uuid);
156 while(progressMonitorService.isMonitorThreadRunning(uuid)) {}
157 IRemotingProgressMonitor remotingMonitor = progressMonitorService.getRemotingMonitor(uuid);
158 new MockPostMoniteredOperationEnabled(expectedMonitor, uuid).postOperation(remotingMonitor);
159
160 }
161
162
163 class MockPostMoniteredOperationEnabled implements IPostMoniteredOperationEnabled {
164
165 private RemotingProgressMonitor expectedMonitor;
166 private UUID monitorUuid;
167
168 public MockPostMoniteredOperationEnabled(RemotingProgressMonitor expectedMonitor, UUID monitorUuid) {
169 this.expectedMonitor = expectedMonitor;
170 this.monitorUuid = monitorUuid;
171 }
172 /**
173 * {@inheritDoc}
174 */
175 @Override
176 public void postOperation(IRemotingProgressMonitor monitor) {
177 if(expectedMonitor.getResult() instanceof Exception) {
178 Exception expectedException = (Exception) expectedMonitor.getResult();
179 Exception exception = (Exception) monitor.getResult();
180 Assert.assertEquals(expectedException.getClass(), expectedException.getClass());
181 Assert.assertEquals(expectedException.getMessage(), expectedException.getMessage());
182 } else {
183 Assert.assertEquals(expectedMonitor.getResult(), monitor.getResult());
184 }
185 Assert.assertEquals(expectedMonitor.getReports(), monitor.getReports());
186 Assert.assertEquals(expectedMonitor.isCanceled(), monitor.isCanceled());
187 Assert.assertEquals(expectedMonitor.isFailed(), monitor.isFailed());
188 Assert.assertEquals(expectedMonitor.isDone(), monitor.isDone());
189 Assert.assertTrue(!progressMonitorService.isMonitorThreadRunning(monitorUuid));
190 }
191
192 }
193 }