14
14
import java .io .File ;
15
15
import java .io .IOException ;
16
16
import java .net .URI ;
17
- import java .nio .file .DirectoryStream ;
18
- import java .nio .file .Files ;
19
- import java .nio .file .Path ;
20
- import java .nio .file .Paths ;
21
17
import java .time .MonthDay ;
22
18
import java .util .Arrays ;
19
+ import java .util .LinkedList ;
23
20
import java .util .List ;
24
21
import java .util .Objects ;
25
22
import java .util .stream .Collectors ;
26
23
27
- import org .apache .commons .io .FileUtils ;
28
24
import org .apache .commons .lang3 .SystemUtils ;
29
25
import org .awaitility .Awaitility ;
30
26
import org .junit .After ;
27
+ import org .junit .AfterClass ;
31
28
import org .junit .Assert ;
32
29
import org .junit .Before ;
30
+ import org .junit .BeforeClass ;
33
31
import org .junit .Rule ;
34
32
import org .junit .Test ;
35
33
import org .junit .rules .ExpectedException ;
38
36
import org .kitodo .MockDatabase ;
39
37
import org .kitodo .NewspaperCourse ;
40
38
import org .kitodo .SecurityTestUtils ;
41
- import org .kitodo .TreeDeleter ;
42
39
import org .kitodo .api .MetadataEntry ;
43
40
import org .kitodo .api .dataformat .LogicalDivision ;
44
41
import org .kitodo .api .dataformat .Workpiece ;
48
45
import org .kitodo .data .database .beans .Process ;
49
46
import org .kitodo .data .database .beans .User ;
50
47
import org .kitodo .data .database .exceptions .DAOException ;
48
+ import org .kitodo .data .elasticsearch .exceptions .CustomResponseException ;
51
49
import org .kitodo .data .exceptions .DataException ;
52
50
import org .kitodo .production .dto .ProcessDTO ;
53
51
import org .kitodo .production .helper .tasks .GeneratesNewspaperProcessesThread ;
56
54
import org .kitodo .production .services .ServiceManager ;
57
55
import org .kitodo .production .services .data .ProcessService ;
58
56
import org .kitodo .production .services .dataformat .MetsService ;
57
+ import org .kitodo .utils .ProcessTestUtils ;
59
58
60
59
public class NewspaperProcessesGeneratorIT {
61
60
private static final ProcessService processService = ServiceManager .getProcessService ();
62
61
private static final MetsService metsService = ServiceManager .getMetsService ();
63
62
64
63
private static final String firstProcess = "First process" ;
65
64
private static final File script = new File (ConfigCore .getParameter (ParameterCore .SCRIPT_CREATE_DIR_META ));
65
+ private static List <Integer > dummyProcessIds = new LinkedList <>();
66
+ private static int newspaperTestProcessId = -1 ;
67
+ private static int rulesetId = -1 ;
68
+ private static final String NEWSPAPER_TEST_METADATA_FILE = "testmetaNewspaper.xml" ;
69
+ private static final String NEWSPAPER_TEST_PROCESS_TITLE = "NewspaperOverallProcess" ;
66
70
67
71
@ Rule
68
72
public ExpectedException expectedEx = ExpectedException .none ();
@@ -73,16 +77,17 @@ public class NewspaperProcessesGeneratorIT {
73
77
* @throws Exception
74
78
* if that does not work
75
79
*/
76
- @ Before
77
- public void setUp () throws Exception {
80
+ @ BeforeClass
81
+ public static void setUp () throws Exception {
78
82
if (!SystemUtils .IS_OS_WINDOWS ) {
79
83
ExecutionPermission .setExecutePermission (script );
80
84
}
81
85
FileLoader .createConfigProjectsFileForCalendarHierarchyTests ();
82
86
MockDatabase .startNode ();
83
87
MockDatabase .insertProcessesFull ();
84
- MockDatabase .insertProcessForCalendarHierarchyTests ();
88
+ MockDatabase .insertFoldersForSecondProject ();
85
89
MockDatabase .setUpAwaitility ();
90
+ rulesetId = MockDatabase .insertRuleset ("Newspaper" , "newspaper.xml" , 1 );
86
91
User userOne = ServiceManager .getUserService ().getById (1 );
87
92
SecurityTestUtils .addUserDataToSecurityContext (userOne , 1 );
88
93
Awaitility .await ().until (() -> {
@@ -94,8 +99,8 @@ public void setUp() throws Exception {
94
99
/**
95
100
* The test environment is cleaned up and the database is closed.
96
101
*/
97
- @ After
98
- public void tearDown () throws Exception {
102
+ @ AfterClass
103
+ public static void tearDown () throws Exception {
99
104
MockDatabase .stopNode ();
100
105
MockDatabase .cleanDatabase ();
101
106
KitodoConfigFile .PROJECT_CONFIGURATION .getFile ().delete ();
@@ -105,37 +110,74 @@ public void tearDown() throws Exception {
105
110
}
106
111
}
107
112
113
+ /**
114
+ * Create dummy processes and newspaper test process and copy corresponding meta.xml file.
115
+ * @throws DAOException when inserting test or dummy processes fails
116
+ * @throws DataException when inserting test or dummy processes fails
117
+ * @throws IOException when copying test metadata file fails
118
+ */
119
+ @ Before
120
+ public void prepareNewspaperProcess () throws DAOException , DataException , IOException {
121
+ dummyProcessIds = ProcessTestUtils .insertDummyProcesses (2 );
122
+ newspaperTestProcessId = MockDatabase .insertTestProcess (NEWSPAPER_TEST_PROCESS_TITLE , 1 , 1 , rulesetId );
123
+ ProcessTestUtils .copyTestFiles (newspaperTestProcessId , NEWSPAPER_TEST_METADATA_FILE );
124
+ }
125
+
126
+ /**
127
+ * Remove dummy and newspaper test processes.
128
+ * @throws DAOException when removing dummy processes from database fails
129
+ * @throws CustomResponseException when removing dummy processes from index fails
130
+ * @throws DataException when deleting newspaper test processes fails
131
+ * @throws IOException when deleting metadata test files fails
132
+ */
133
+ @ After
134
+ public void cleanupNewspaperProcess () throws DAOException , CustomResponseException , DataException , IOException {
135
+ for (int processId : dummyProcessIds ) {
136
+ ServiceManager .getProcessService ().removeFromDatabase (processId );
137
+ ServiceManager .getProcessService ().removeFromIndex (processId , false );
138
+ }
139
+ if (newspaperTestProcessId > 0 ) {
140
+ deleteProcessHierarchy (ServiceManager .getProcessService ().getById (newspaperTestProcessId ));
141
+ }
142
+ }
143
+
144
+ private static void deleteProcessHierarchy (Process process ) throws DAOException , DataException , IOException {
145
+ for (Process childProcess : process .getChildren ()) {
146
+ deleteProcessHierarchy (childProcess );
147
+ }
148
+ ProcessService .deleteProcess (process .getId ());
149
+ }
150
+
108
151
/**
109
152
* Perform the test. A long-running task is simulated: Progress and number
110
153
* of steps are queried and the next step is performed.
111
154
*/
112
155
@ Test
113
156
public void shouldGenerateNewspaperProcesses () throws Exception {
114
- // create backup of meta data file as this file is modified inside test
115
- File metaFile = new File ("src/test/resources/metadata/10/meta.xml" );
116
- File backupFile = new File ("src/test/resources/metadata/10/meta.xml.1" );
117
- FileUtils .copyFile (metaFile , backupFile );
118
-
119
- Process completeEdition = ServiceManager .getProcessService ().getById (10 );
157
+ Process completeEdition = ServiceManager .getProcessService ().getById (newspaperTestProcessId );
120
158
Course course = NewspaperCourse .getCourse ();
121
159
course .splitInto (Granularity .DAYS );
122
160
NewspaperProcessesGenerator underTest = new NewspaperProcessesGenerator (completeEdition , course );
123
161
while (underTest .getProgress () < underTest .getNumberOfSteps ()) {
124
162
underTest .nextStep ();
125
163
}
164
+ int maxId = getChildProcessWithLargestId (completeEdition , 0 );
126
165
Assert .assertEquals ("The newspaper processes generator has not been completed!" , underTest .getNumberOfSteps (),
127
166
underTest .getProgress ());
128
167
Assert .assertEquals ("Process title missing in newspaper's meta.xml" , "NewspaperOverallProcess" ,
129
- readProcessTitleFromMetadata (10 , false ));
168
+ readProcessTitleFromMetadata (newspaperTestProcessId , false ));
130
169
Assert .assertEquals ("Process title missing in year's meta.xml" , "NewspaperOverallProcess_1703" ,
131
- readProcessTitleFromMetadata (11 , false ));
170
+ readProcessTitleFromMetadata (newspaperTestProcessId + 1 , false ));
132
171
Assert .assertEquals ("Process title missing in issue's meta.xml" , "NewspaperOverallProcess_17050127" ,
133
- readProcessTitleFromMetadata (28 , true ));
172
+ readProcessTitleFromMetadata (maxId , true ));
173
+ }
134
174
135
- // restore backuped meta data file
136
- FileUtils .deleteQuietly (metaFile );
137
- FileUtils .moveFile (backupFile , metaFile );
138
- cleanUp ();
175
+ private int getChildProcessWithLargestId (Process process , int maxId ) {
176
+ maxId = Math .max (maxId , process .getId ());
177
+ for (Process childProcess : process .getChildren ()) {
178
+ maxId = getChildProcessWithLargestId (childProcess , maxId );
179
+ }
180
+ return maxId ;
139
181
}
140
182
141
183
/*
@@ -162,13 +204,7 @@ private String readProcessTitleFromMetadata(int processId, boolean issue) throws
162
204
*/
163
205
@ Test
164
206
public void shouldGenerateSeasonProcesses () throws Exception {
165
- // create backup of meta data file as this file is modified inside test
166
- File metaFile = new File ("src/test/resources/metadata/10/meta.xml" );
167
- File backupFile = new File ("src/test/resources/metadata/10/meta.xml.1" );
168
- FileUtils .copyFile (metaFile , backupFile );
169
-
170
- // Set base type in metadata/10/meta.xml to "Season"
171
- Process seasonProcess = ServiceManager .getProcessService ().getById (10 );
207
+ Process seasonProcess = ServiceManager .getProcessService ().getById (newspaperTestProcessId );
172
208
URI seasonUri = processService .getMetadataFileUri (seasonProcess );
173
209
Workpiece seasonMets = metsService .loadWorkpiece (seasonUri );
174
210
seasonMets .getLogicalStructure ().setType ("Season" );
@@ -197,32 +233,26 @@ public void shouldGenerateSeasonProcesses() throws Exception {
197
233
*/
198
234
String twoYears = workpiece .getLogicalStructure ().getOrderlabel ();
199
235
List <String > years = Arrays .asList (twoYears .split ("/" , 2 ));
200
- Assert .assertTrue ("Bad season-year in " + seasonProcess + ": " + twoYears ,
201
- Integer .parseInt (years .get (0 )) + 1 == Integer .parseInt (years .get (1 )));
236
+ Assert .assertEquals ("Bad season-year in " + seasonProcess + ": " + twoYears ,
237
+ Integer .parseInt (years .get (0 )) + 1 , Integer .parseInt (years .get (1 )));
202
238
203
239
// more tests
204
240
monthChecksOfShouldGenerateSeasonProcesses (seasonProcess , workpiece , twoYears , years );
205
241
dayChecksOfShouldGenerateSeasonProcesses (seasonProcess , workpiece );
206
242
}
207
243
}
208
-
209
- // restore backed-up meta data file
210
- FileUtils .deleteQuietly (metaFile );
211
- FileUtils .moveFile (backupFile , metaFile );
212
- cleanUp ();
213
244
}
214
245
215
246
@ Test
216
247
public void shouldNotGenerateDuplicateProcessTitle () throws DAOException , DataException {
217
- Process completeEdition = ServiceManager .getProcessService ().getById (10 );
248
+ Process completeEdition = ServiceManager .getProcessService ().getById (newspaperTestProcessId );
218
249
Course course = NewspaperCourse .getDuplicatedCourse ();
219
250
course .splitInto (Granularity .DAYS );
220
251
GeneratesNewspaperProcessesThread generatesNewspaperProcessesThread = new GeneratesNewspaperProcessesThread (completeEdition , course );
221
- generatesNewspaperProcessesThread .run ();
252
+ generatesNewspaperProcessesThread .start ();
222
253
223
254
ProcessDTO byId = ServiceManager .getProcessService ().findById (11 );
224
255
Assert .assertNull ("Process should not have been created" , byId .getTitle ());
225
-
226
256
}
227
257
228
258
private void dayChecksOfShouldGenerateSeasonProcesses (Process seasonProcess , Workpiece seasonYearWorkpiece ) {
@@ -291,19 +321,4 @@ private void monthChecksOfShouldGenerateSeasonProcesses(Process seasonProcess, W
291
321
previousMonthValue = monthValue ;
292
322
}
293
323
}
294
-
295
- /**
296
- * To clean up after the end of the test. All metadata directories >10 will
297
- * be deleted.
298
- */
299
- private static void cleanUp () throws IOException {
300
- Path dirProcesses = Paths .get (ConfigCore .getParameter (ParameterCore .DIR_PROCESSES ));
301
- DirectoryStream <Path > directoryStream = Files .newDirectoryStream (dirProcesses );
302
- for (Path path : directoryStream ) {
303
- String fileName = path .getFileName ().toString ();
304
- if (fileName .matches ("\\ d+" ) && Integer .valueOf (fileName ) > 10 ) {
305
- TreeDeleter .deltree (path );
306
- }
307
- }
308
- }
309
324
}
0 commit comments