-
Notifications
You must be signed in to change notification settings - Fork 4
/
GetChecksumForFileStepTest.java
192 lines (165 loc) · 10.3 KB
/
GetChecksumForFileStepTest.java
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
/*
* #%L
* Bitrepository Integrity Service
* %%
* Copyright (C) 2010 - 2012 The State and University Library, The Royal Library and The State Archives, Denmark
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 2.1 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Lesser Public License for more details.
*
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/lgpl-2.1.html>.
* #L%
*/
package org.bitrepository.integrityservice.workflow.step;
import org.bitrepository.access.getchecksums.conversation.ChecksumsCompletePillarEvent;
import org.bitrepository.bitrepositoryelements.ChecksumDataForChecksumSpecTYPE;
import org.bitrepository.bitrepositoryelements.ChecksumSpecTYPE;
import org.bitrepository.bitrepositoryelements.ResponseCode;
import org.bitrepository.bitrepositoryelements.ResultingChecksums;
import org.bitrepository.client.eventhandler.CompleteEvent;
import org.bitrepository.client.eventhandler.ContributorEvent;
import org.bitrepository.client.eventhandler.ContributorFailedEvent;
import org.bitrepository.client.eventhandler.EventHandler;
import org.bitrepository.client.eventhandler.OperationFailedEvent;
import org.bitrepository.common.utils.Base16Utils;
import org.bitrepository.common.utils.CalendarUtils;
import org.bitrepository.common.utils.ChecksumUtils;
import org.bitrepository.integrityservice.workflow.IntegrityContributors;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.testng.Assert;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import java.util.Arrays;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.verifyNoMoreInteractions;
/**
* Performs the validation of the integrity for the checksums.
*/
public class GetChecksumForFileStepTest extends WorkflowstepTest {
/** The settings for the tests. Should be instantiated in the setup.*/
public static final String TEST_PILLAR_1 = "test-pillar-1";
public static final String TEST_PILLAR_2 = "test-pillar-2";
public static final String TEST_PILLAR_3 = "test-pillar-3";
public static final String FILE_1 = "test-file-1";
public static final String FILE_2 = "test-file-2";
String TEST_COLLECTION = "test-collection";
@BeforeMethod(alwaysRun = true)
public void setup() {
super.setup();
settings.getRepositorySettings().getCollections().getCollection().get(0).getPillarIDs().getPillarID().clear();
settings.getRepositorySettings().getCollections().getCollection().get(0).getPillarIDs().getPillarID().add(TEST_PILLAR_1);
settings.getRepositorySettings().getCollections().getCollection().get(0).getPillarIDs().getPillarID().add(TEST_PILLAR_2);
settings.getRepositorySettings().getCollections().getCollection().get(0).getPillarIDs().getPillarID().add(TEST_PILLAR_3);
integrityContributors = new IntegrityContributors(Arrays.asList(TEST_PILLAR_1, TEST_PILLAR_2, TEST_PILLAR_3), 0);
}
@Test(groups = {"regressiontest", "integritytest"})
public void testNoResults() throws Exception {
addDescription("Test step for retrieving the checksum of a single file, when no results are delivered.");
ChecksumSpecTYPE checksumType = ChecksumUtils.getDefault(settings);
addStep("Setup mock answers", "");
doAnswer(new Answer<Void>() {
public Void answer(InvocationOnMock invocation) {
EventHandler eventHandler = (EventHandler) invocation.getArguments()[6];
eventHandler.handleEvent(new CompleteEvent(TEST_COLLECTION, null));
return null;
}
}).when(collector).getChecksums(
eq(TEST_COLLECTION), any(), any(ChecksumSpecTYPE.class), anyString(),
anyString(), any(), any(EventHandler.class));
GetChecksumForFileStep step = new GetChecksumForFileStep(collector, alerter, checksumType, FILE_1, settings, TEST_COLLECTION, integrityContributors);
addStep("Validate the checksum results", "Should not have any results");
step.performStep();
Assert.assertTrue(step.getResults().isEmpty());
verifyNoInteractions(alerter);
verify(collector).getChecksums(anyString(), any(), eq(checksumType), eq(FILE_1), anyString(), any(), any(EventHandler.class));
verifyNoMoreInteractions(collector);
}
@Test(groups = {"regressiontest", "integritytest"})
public void testFullData() throws Exception {
addDescription("Test step for retrieving the checksum of a single file, when all three pillars deliver results.");
ChecksumSpecTYPE checksumType = ChecksumUtils.getDefault(settings);
addStep("Setup mock answers", "");
doAnswer(new Answer<Void>() {
public Void answer(InvocationOnMock invocation) {
EventHandler eventHandler = (EventHandler) invocation.getArguments()[6];
ResultingChecksums res = createResultingChecksums((String) invocation.getArguments()[3], "checksum");
eventHandler.handleEvent(new ChecksumsCompletePillarEvent(TEST_PILLAR_1, TEST_COLLECTION, res, (ChecksumSpecTYPE) invocation.getArguments()[2], false));
eventHandler.handleEvent(new ChecksumsCompletePillarEvent(TEST_PILLAR_2, TEST_COLLECTION, res, (ChecksumSpecTYPE) invocation.getArguments()[2], false));
eventHandler.handleEvent(new ChecksumsCompletePillarEvent(TEST_PILLAR_3, TEST_COLLECTION, res, (ChecksumSpecTYPE) invocation.getArguments()[2], false));
eventHandler.handleEvent(new CompleteEvent(TEST_COLLECTION, null));
return null;
}
}).when(collector).getChecksums(
eq(TEST_COLLECTION), any(), any(ChecksumSpecTYPE.class), anyString(),
anyString(), any(), any(EventHandler.class));
GetChecksumForFileStep step = new GetChecksumForFileStep(collector, alerter, checksumType, FILE_1, settings, TEST_COLLECTION, integrityContributors);
addStep("Validate the checksum results", "Should have checksum for each pillar.");
step.performStep();
Assert.assertFalse(step.getResults().isEmpty());
Assert.assertEquals(step.getResults().size(), 3);
Assert.assertTrue(step.getResults().containsKey(TEST_PILLAR_1));
Assert.assertTrue(step.getResults().containsKey(TEST_PILLAR_2));
Assert.assertTrue(step.getResults().containsKey(TEST_PILLAR_3));
verifyNoInteractions(alerter);
verify(collector).getChecksums(anyString(), any(), eq(checksumType), eq(FILE_1), anyString(), any(), any(EventHandler.class));
verifyNoMoreInteractions(collector);
}
@Test(groups = {"regressiontest", "integritytest"})
public void testComponentFailure() throws Exception {
addDescription("Test step for retrieving the checksum of a single file, when one pillar fails.");
ChecksumSpecTYPE checksumType = ChecksumUtils.getDefault(settings);
addStep("Setup mock answers", "");
doAnswer(new Answer<Void>() {
public Void answer(InvocationOnMock invocation) {
EventHandler eventHandler = (EventHandler) invocation.getArguments()[6];
ResultingChecksums res = createResultingChecksums((String) invocation.getArguments()[3], "checksum");
ContributorEvent e1 = new ChecksumsCompletePillarEvent(TEST_PILLAR_1, TEST_COLLECTION, res, (ChecksumSpecTYPE) invocation.getArguments()[2], false);
ContributorEvent e2 = new ChecksumsCompletePillarEvent(TEST_PILLAR_2, TEST_COLLECTION, res, (ChecksumSpecTYPE) invocation.getArguments()[2], false);
ContributorEvent e3 = new ContributorFailedEvent(TEST_PILLAR_3, TEST_COLLECTION, ResponseCode.REQUEST_NOT_UNDERSTOOD_FAILURE);
eventHandler.handleEvent(e1);
eventHandler.handleEvent(e2);
eventHandler.handleEvent(e3);
eventHandler.handleEvent(new OperationFailedEvent(TEST_COLLECTION, "COMPONENT FAILED", Arrays.asList(e1, e2, e3)));
return null;
}
}).when(collector).getChecksums(
eq(TEST_COLLECTION), any(), any(ChecksumSpecTYPE.class), anyString(),
anyString(), any(), any(EventHandler.class));
GetChecksumForFileStep step = new GetChecksumForFileStep(collector, alerter, checksumType, FILE_1, settings, TEST_COLLECTION, integrityContributors);
addStep("Validate the file ids", "Should not have integrity issues.");
step.performStep();
Assert.assertFalse(step.getResults().isEmpty());
Assert.assertEquals(step.getResults().size(), 2);
Assert.assertTrue(step.getResults().containsKey(TEST_PILLAR_1));
Assert.assertTrue(step.getResults().containsKey(TEST_PILLAR_2));
Assert.assertFalse(step.getResults().containsKey(TEST_PILLAR_3));
verify(alerter).integrityFailed(anyString(), eq(TEST_COLLECTION));
verifyNoMoreInteractions(alerter);
verify(collector).getChecksums(anyString(), any(), eq(checksumType), eq(FILE_1), anyString(), any(), any(EventHandler.class));
verifyNoMoreInteractions(collector);
}
private ResultingChecksums createResultingChecksums(String fileId, String checksum) {
ChecksumDataForChecksumSpecTYPE csData = new ChecksumDataForChecksumSpecTYPE();
csData.setCalculationTimestamp(CalendarUtils.getNow());
csData.setFileID(fileId);
csData.setChecksumValue(Base16Utils.encodeBase16(checksum));
ResultingChecksums res = new ResultingChecksums();
res.getChecksumDataItems().add(csData);
return res;
}
}