Newer
Older
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
/*
* Copyright 2010 ETH Zuerich, CISD
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package eu.basysbio.cisd.dss;
import static eu.basysbio.cisd.dss.TimeSeriesAndTimePointDataSetHandler.HELPDESK_EMAIL;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import org.hamcrest.collection.IsArray;
import org.hamcrest.core.IsNull;
import org.jmock.Expectations;
import org.jmock.Mockery;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import ch.rinn.restrictions.Friend;
import ch.systemsx.cisd.base.tests.AbstractFileSystemTestCase;
import ch.systemsx.cisd.common.logging.LogInitializer;
import ch.systemsx.cisd.common.mail.From;
import ch.systemsx.cisd.common.mail.IMailClient;
import ch.systemsx.cisd.common.utilities.ITimeProvider;
import ch.systemsx.cisd.etlserver.IDataSetHandler;
import ch.systemsx.cisd.openbis.dss.generic.shared.dto.DataSetInformation;
import ch.systemsx.cisd.openbis.generic.shared.basic.dto.DataSetType;
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
/**
*
*
* @author Franz-Josef Elmer
*/
@Friend(toClasses=TimeSeriesAndTimePointDataSetHandler.class)
public class TimeSeriesAndTimePointDataSetHandlerTest extends AbstractFileSystemTestCase
{
private static final class StringMatcher extends BaseMatcher<String>
{
private final String expectedString;
StringMatcher(String expectedString)
{
this.expectedString = expectedString;
}
public boolean matches(Object item)
{
if (item instanceof String)
{
String string = (String) item;
assertEquals(expectedString, string);
return true;
}
return false;
}
public void describeTo(Description description)
{
description.appendText(expectedString);
}
}
private Mockery context;
private IDataSetHandler delegator;
private IDataSetHandler timePointDataSetHandler;
private IMailClient mailClient;
private IDataSetHandler handler;
private File dropBox;
private ITimeProvider timeProvider;
@BeforeMethod
public void beforeMethod() throws Exception
{
super.setUp();
LogInitializer.init();
context = new Mockery();
delegator = context.mock(IDataSetHandler.class, "delegator");
timePointDataSetHandler = context.mock(IDataSetHandler.class, "timePointDataSetHandler");
mailClient = context.mock(IMailClient.class);
timeProvider = context.mock(ITimeProvider.class);
dropBox = new File(workingDirectory, "drop-box");
timePointFolder = new File(workingDirectory, "time-point-folder");
timePointFolder.mkdirs();
handler =
new TimeSeriesAndTimePointDataSetHandler(delegator, mailClient,
timePointDataSetHandler, timePointFolder, timeProvider);
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
}
@AfterMethod
public void tearDown()
{
// To following line of code should also be called at the end of each test method.
// Otherwise one does not known which test failed.
context.assertIsSatisfied();
}
@Test
public void testFailingTimeSeriesRegistration()
{
context.checking(new Expectations()
{
{
one(delegator).handleDataSet(dropBox);
will(returnValue(Arrays.asList()));
}
});
List<DataSetInformation> dataSets = handler.handleDataSet(dropBox);
assertEquals(0, dataSets.size());
context.assertIsSatisfied();
}
@Test
public void testHandleSuccesfullyTwoTimePointDataSets() throws IOException
{
final File tp1 = new File(timePointFolder, "tp1");
tp1.createNewFile();
final DataSetInformation ds1 = new DataSetInformation();
final File tp2 = new File(timePointFolder, "tp2");
tp2.createNewFile();
final DataSetInformation ds2 = new DataSetInformation();
final DataSetInformation dataSetInformation = new DataSetInformation();
dataSetInformation.setUploadingUserEmail("john.doe@abc.de");
dataSetInformation.setDataSetType(new DataSetType(DataSetHandler.TIME_SERIES));
String expectedSubject = "BaSysBio: Successful uploading of data set 'drop-box'";
+ "has been successfully uploaded and registered in openBIS.";
prepareSendingEMail(expectedSubject, expectedMessage, dataSetInformation, false);
context.checking(new Expectations()
{
{
one(delegator).handleDataSet(dropBox);
will(returnValue(Arrays.asList(dataSetInformation)));
one(timePointDataSetHandler).handleDataSet(tp1);
will(returnValue(Arrays.asList(ds1)));
one(timePointDataSetHandler).handleDataSet(tp2);
will(returnValue(Arrays.asList(ds2)));
}
});
List<DataSetInformation> dataSets = handler.handleDataSet(dropBox);
assertEquals(3, dataSets.size());
assertSame(dataSetInformation, dataSets.get(0));
assertSame(ds1, dataSets.get(1));
assertSame(ds2, dataSets.get(2));
context.assertIsSatisfied();
}
@Test
public void testHandleNotSuccesfullyTwoTimePointDataSets() throws IOException
{
final File tp1 = new File(timePointFolder, "tp1");
final File tp2 = new File(timePointFolder, "tp2");
tp2.createNewFile();
final DataSetInformation ds2 = new DataSetInformation();
final DataSetInformation dataSetInformation = new DataSetInformation();
dataSetInformation.setUploadingUserEmail("john.doe@abc.de");
dataSetInformation.setDataSetType(new DataSetType(DataSetHandler.TIME_SERIES));
String expectedSubject = "BaSysBio: Failed uploading of data set 'drop-box'";
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
+ "because only 1 of 2 time point data sets could be registered in openBIS.\n\n"
+ "Please, contact the help desk for support: " + HELPDESK_EMAIL + "\n"
+ "(Time stamp of failure: 1970-01-01 01:01:14 +0100)";
prepareSendingEMail(expectedSubject, expectedMessage, dataSetInformation, true);
context.checking(new Expectations()
{
{
one(delegator).handleDataSet(dropBox);
will(returnValue(Arrays.asList(dataSetInformation)));
one(timePointDataSetHandler).handleDataSet(tp1);
will(returnValue(Arrays.asList()));
one(timePointDataSetHandler).handleDataSet(tp2);
will(returnValue(Arrays.asList(ds2)));
one(timeProvider).getTimeInMilliseconds();
will(returnValue(42 * 42 * 42L));
}
});
List<DataSetInformation> dataSets = handler.handleDataSet(dropBox);
assertEquals(2, dataSets.size());
assertSame(dataSetInformation, dataSets.get(0));
assertSame(ds2, dataSets.get(1));
context.assertIsSatisfied();
}
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
@Test
public void testHandleNotSuccesfullyTwoLcaMicTimeSeriesDataSets() throws IOException
{
final File tp1 = new File(workingDirectory, DataSetHandler.LCA_MIC_TIME_SERIES + "1");
tp1.createNewFile();
final File tp2 = new File(workingDirectory, DataSetHandler.LCA_MIC_TIME_SERIES + "2");
tp2.createNewFile();
final DataSetInformation ds2 = new DataSetInformation();
final DataSetInformation dataSetInformation = new DataSetInformation();
dataSetInformation.setUploadingUserEmail("john.doe@abc.de");
dataSetInformation.setDataSetType(new DataSetType(DataSetHandler.LCA_MIC));
String expectedSubject = "BaSysBio: Failed uploading of data set 'drop-box'";
String expectedMessage =
"Uploading of data set 'drop-box' failed "
+ "because only 1 of 2 LCA MIC time series data sets could be registered in openBIS.\n\n"
+ "Please, contact the help desk for support: " + HELPDESK_EMAIL + "\n"
+ "(Time stamp of failure: 1970-01-01 01:01:14 +0100)";
prepareSendingEMail(expectedSubject, expectedMessage, dataSetInformation, true);
context.checking(new Expectations()
{
{
one(delegator).handleDataSet(dropBox);
will(returnValue(Arrays.asList(dataSetInformation)));
one(delegator).handleDataSet(tp1);
will(returnValue(Arrays.asList()));
one(delegator).handleDataSet(tp2);
will(returnValue(Arrays.asList(ds2)));
one(timeProvider).getTimeInMilliseconds();
will(returnValue(42 * 42 * 42L));
}
});
List<DataSetInformation> dataSets = handler.handleDataSet(dropBox);
assertEquals(2, dataSets.size());
assertSame(dataSetInformation, dataSets.get(0));
assertSame(ds2, dataSets.get(1));
context.assertIsSatisfied();
}
@Test
public void testHandleLcaMicTimeSeriesDataSet() throws IOException
{
final DataSetInformation dataSetInformation = new DataSetInformation();
dataSetInformation.setUploadingUserEmail("john.doe@abc.de");
dataSetInformation.setDataSetType(new DataSetType(DataSetHandler.LCA_MIC));
String expectedSubject = "BaSysBio: Successful uploading of data set 'drop-box'";
String expectedMessage =
"The data set 'drop-box' "
+ "has been successfully uploaded and registered in openBIS.";
prepareSendingEMail(expectedSubject, expectedMessage, dataSetInformation, false);
context.checking(new Expectations()
{
{
one(delegator).handleDataSet(dropBox);
will(returnValue(Arrays.asList(dataSetInformation)));
}
});
List<DataSetInformation> dataSets = handler.handleDataSet(dropBox);
assertEquals(1, dataSets.size());
assertSame(dataSetInformation, dataSets.get(0));
context.assertIsSatisfied();
}
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
private void prepareSendingEMail(final String expectedSubject, final String expectedMessage,
final DataSetInformation dataSetInformation, final boolean alsoToHelpDesk)
{
context.checking(new Expectations()
{
{
StringMatcher[] recipients = new StringMatcher[alsoToHelpDesk ? 2 : 1];
recipients[0] =
new StringMatcher(dataSetInformation.tryGetUploadingUserEmail());
if (alsoToHelpDesk)
{
recipients[1] = new StringMatcher(HELPDESK_EMAIL);
}
one(mailClient).sendMessage(string(expectedSubject), string(expectedMessage),
with(new IsNull<String>()), with(new IsNull<From>()),
with(new IsArray<String>(recipients)));
}
private String string(String expectedString)
{
return with(new StringMatcher(expectedString));
}
});
}
}