Skip to content

Commit bdeadab

Browse files
authored
Merge pull request #11713 from DANS-KNAW-jp/stable-unit-test
avoid external resource for a unit test
2 parents 42e70aa + 90d7d59 commit bdeadab

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed

src/test/java/edu/harvard/iq/dataverse/dataaccess/RemoteOverlayAccessIOTest.java

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,22 @@
1111
import edu.harvard.iq.dataverse.pidproviders.doi.AbstractDOIProvider;
1212
import edu.harvard.iq.dataverse.util.UrlSignerUtil;
1313

14+
import org.apache.http.ProtocolVersion;
15+
import org.apache.http.client.methods.CloseableHttpResponse;
16+
import org.apache.http.client.methods.HttpUriRequest;
17+
import org.apache.http.client.protocol.HttpClientContext;
18+
import org.apache.http.impl.client.CloseableHttpClient;
19+
import org.apache.http.message.BasicHeader;
20+
import org.apache.http.message.BasicStatusLine;
21+
import org.apache.http.protocol.HTTP;
22+
import org.jetbrains.annotations.NotNull;
1423
import org.junit.jupiter.api.AfterEach;
1524
import org.junit.jupiter.api.BeforeEach;
1625
import org.junit.jupiter.api.Test;
1726
import org.junit.jupiter.api.extension.ExtendWith;
1827
import static org.junit.jupiter.api.Assertions.*;
1928
import org.mockito.Mock;
29+
import org.mockito.Mockito;
2030
import org.mockito.junit.jupiter.MockitoExtension;
2131
import org.mockito.junit.jupiter.MockitoSettings;
2232
import org.mockito.quality.Strictness;
@@ -41,7 +51,7 @@ public class RemoteOverlayAccessIOTest {
4151
public void setUp() {
4252
System.setProperty("dataverse.files.test.type", "remote");
4353
System.setProperty("dataverse.files.test.label", "testOverlay");
44-
System.setProperty("dataverse.files.test.base-url", "https://github.com/IQSS/dataverse");
54+
System.setProperty("dataverse.files.test.base-url", "https://github.com/IQSS/dataverseXX");
4555
System.setProperty("dataverse.files.test.base-store", "file");
4656
System.setProperty("dataverse.files.test.download-redirect", "true");
4757
System.setProperty("dataverse.files.test.remote-store-name", "DemoDataCorp");
@@ -74,7 +84,7 @@ public void tearDown() {
7484
}
7585

7686
@Test
77-
void testRemoteOverlayFiles() throws IOException {
87+
void testRemoteOverlayFiles() throws IOException, NoSuchFieldException, IllegalAccessException {
7888
// We can read the storageIdentifier and get the driver
7989
assertTrue(datafile.getStorageIdentifier()
8090
.startsWith(DataAccess.getStorageDriverFromIdentifier(datafile.getStorageIdentifier())));
@@ -99,6 +109,13 @@ void testRemoteOverlayFiles() throws IOException {
99109
assertTrue(unsignedURL.equals(System.getProperty("dataverse.files.test.base-url") + "/" + filePath));
100110
// Once we've opened, we can get the file size (only works if the HEAD call to
101111
// the file URL works
112+
var mockResponse = mockResponseWithContentLength();
113+
var mockClient = Mockito.mock(CloseableHttpClient.class);
114+
Mockito.when(mockClient.execute(Mockito.any(HttpUriRequest.class), Mockito.any(HttpClientContext.class))).thenReturn(mockResponse);
115+
var httpClientField = remoteIO.getClass().getSuperclass().getDeclaredField("httpclient");
116+
httpClientField.setAccessible(true);
117+
httpClientField.set(remoteIO, mockClient);
118+
102119
remoteIO.open(DataAccessOption.READ_ACCESS);
103120
assertTrue(remoteIO.getSize() > 0);
104121
// If we ask for the path for an aux file, it is correct
@@ -115,6 +132,16 @@ void testRemoteOverlayFiles() throws IOException {
115132

116133
}
117134

135+
private @NotNull CloseableHttpResponse mockResponseWithContentLength() {
136+
var headers = new BasicHeader[] { new BasicHeader(HTTP.CONTENT_LEN, "123") };
137+
var version = new ProtocolVersion("HTTP", 1, 1);
138+
var statusLine = new BasicStatusLine(version, 200, "OK");
139+
var mockResponse = Mockito.mock(CloseableHttpResponse.class);
140+
Mockito.when(mockResponse.getStatusLine()).thenReturn(statusLine);
141+
Mockito.when(mockResponse.getHeaders(HTTP.CONTENT_LEN)).thenReturn(headers);
142+
return mockResponse;
143+
}
144+
118145
@Test
119146
void testRemoteOverlayIdentifierFormats() throws IOException {
120147

0 commit comments

Comments
 (0)