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
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
/*
* 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;
/**
*
*
* @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 File tinePointFolder;
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");
tinePointFolder = new File(workingDirectory, "time-point-folder");
tinePointFolder.mkdirs();
handler =
new TimeSeriesAndTimePointDataSetHandler(delegator, mailClient,
timePointDataSetHandler, tinePointFolder, timeProvider);
}
@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(tinePointFolder, "tp1");
tp1.createNewFile();
final DataSetInformation ds1 = new DataSetInformation();
final File tp2 = new File(tinePointFolder, "tp2");
tp2.createNewFile();
final DataSetInformation ds2 = new DataSetInformation();
final DataSetInformation dataSetInformation = new DataSetInformation();
dataSetInformation.setUploadingUserEmail("john.doe@abc.de");
String expectedSubject =
"BaSysBio: Successful uploading of " + "time series data set 'drop-box'";
String expectedMessage =
"The time series 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(tinePointFolder, "tp1");
tp1.createNewFile();
final File tp2 = new File(tinePointFolder, "tp2");
tp2.createNewFile();
final DataSetInformation ds2 = new DataSetInformation();
final DataSetInformation dataSetInformation = new DataSetInformation();
dataSetInformation.setUploadingUserEmail("john.doe@abc.de");
String expectedSubject =
"BaSysBio: Failed uploading of " + "time series data set 'drop-box'";
String expectedMessage =
"Uploading of time series data set 'drop-box' failed "
+ "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();
}
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));
}
});
}
}