Skip to content

Commit fbf928b

Browse files
Merge pull request #83 from ral-facilities/handle-null-sum
Handle null fileSize SUM, null facilityName, int download_id
2 parents ab648e4 + 41c5c95 commit fbf928b

File tree

6 files changed

+51
-12
lines changed

6 files changed

+51
-12
lines changed

src/main/java/org/icatproject/topcat/FacilityMap.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public FacilityMap(Properties injectedProperties) throws InternalException{
7171
}
7272
}
7373

74-
private String validateFacilityName(String facility) throws InternalException {
74+
public String validateFacilityName(String facility) throws InternalException {
7575
if (facility == null) {
7676
String defaultFacilityName = properties.getProperty("defaultFacilityName");
7777
if (defaultFacilityName == null) {

src/main/java/org/icatproject/topcat/IcatClient.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import org.icatproject.topcat.domain.*;
1818

1919
import jakarta.json.*;
20+
import jakarta.json.JsonValue.ValueType;
2021

2122
import org.slf4j.Logger;
2223
import org.slf4j.LoggerFactory;
@@ -246,7 +247,12 @@ public long getDatasetFileCount(long datasetId) throws TopcatException {
246247
public long getDatasetFileSize(long datasetId) throws TopcatException {
247248
String query = "SELECT SUM(datafile.fileSize) FROM Datafile datafile WHERE datafile.dataset.id = " + datasetId;
248249
JsonArray jsonArray = submitQuery(query);
249-
return jsonArray.getJsonNumber(0).longValueExact();
250+
if (jsonArray.get(0).getValueType().equals(ValueType.NUMBER)) {
251+
return jsonArray.getJsonNumber(0).longValueExact();
252+
} else {
253+
// SUM will be null if there are no matching Datafiles, so return 0
254+
return 0L;
255+
}
250256
}
251257

252258
/**

src/main/java/org/icatproject/topcat/web/rest/UserResource.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -881,6 +881,7 @@ public Response queueVisitId(@FormParam("facilityName") String facilityName,
881881
logger.info("queueVisitId called for {}", visitId);
882882
validateTransport(transport);
883883

884+
facilityName = validateFacilityName(facilityName);
884885
String icatUrl = getIcatUrl(facilityName);
885886
IcatClient icatClient = new IcatClient(icatUrl, sessionId);
886887
String transportUrl = getDownloadUrl(facilityName, transport);
@@ -1004,6 +1005,7 @@ public Response queueFiles(@FormParam("facilityName") String facilityName,
10041005
}
10051006
logger.info("queueFiles called for {} files", files.size());
10061007
validateTransport(transport);
1008+
facilityName = validateFacilityName(facilityName);
10071009
if (fileName == null) {
10081010
fileName = facilityName + "_files";
10091011
}
@@ -1178,6 +1180,14 @@ private Response emptyCart(String facilityName, String userName, Long downloadId
11781180
private Response emptyCart(String facilityName, String userName) {
11791181
return emptyCart(facilityName, userName, null);
11801182
}
1183+
1184+
private String validateFacilityName(String facilityName) throws BadRequestException {
1185+
try {
1186+
return FacilityMap.getInstance().validateFacilityName(facilityName);
1187+
} catch (InternalException ie){
1188+
throw new BadRequestException( ie.getMessage() );
1189+
}
1190+
}
11811191

11821192
private String getIcatUrl( String facilityName ) throws BadRequestException{
11831193
try {

src/test/java/org/icatproject/topcat/IcatClientTest.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import org.junit.*;
88

99
import jakarta.json.*;
10+
import jakarta.json.JsonValue.ValueType;
1011
import jakarta.ejb.EJB;
1112

1213
import org.icatproject.topcat.httpclient.HttpClient;
@@ -240,11 +241,29 @@ public void testCheckUserFound() throws Exception {
240241
}
241242
}
242243

244+
@Test
245+
public void testGetDatasets() throws TopcatException {
246+
IcatClient icatClient = new IcatClient("https://localhost:8181", sessionId);
247+
JsonArray datasets = icatClient.getDatasets("Proposal 0 - 0 0");
248+
for (JsonValue dataset : datasets) {
249+
JsonArray datasetArray = dataset.asJsonArray();
250+
assertEquals(datasetArray.get(0).getValueType(), ValueType.NUMBER);
251+
assertEquals(datasetArray.get(1).getValueType(), ValueType.NUMBER);
252+
assertEquals(datasetArray.get(2).getValueType(), ValueType.NUMBER);
253+
}
254+
}
255+
243256
@Test
244257
public void testGetDatasetFileCount() throws TopcatException {
245258
IcatClient icatClient = new IcatClient("https://localhost:8181", sessionId);
246259
long datasetId = icatClient.getEntity("Dataset").getJsonNumber("id").longValueExact();
247-
assertNotEquals(0, icatClient.getDatasetFileCount(datasetId));
260+
assertNotEquals(0L, icatClient.getDatasetFileCount(datasetId));
261+
}
262+
263+
@Test
264+
public void testGetDatasetFileCountNotFound() throws TopcatException {
265+
IcatClient icatClient = new IcatClient("https://localhost:8181", sessionId);
266+
assertEquals(0L, icatClient.getDatasetFileCount(-1L));
248267
}
249268

250269
@Test
@@ -254,6 +273,12 @@ public void testGetDatasetFileSize() throws TopcatException {
254273
assertNotEquals(0, icatClient.getDatasetFileSize(datasetId));
255274
}
256275

276+
@Test
277+
public void testGetDatasetFileSizeNotFound() throws TopcatException {
278+
IcatClient icatClient = new IcatClient("https://localhost:8181", sessionId);
279+
assertEquals(0, icatClient.getDatasetFileSize(-1L));
280+
}
281+
257282
/*
258283
* @Test public void testGetSize() throws Exception { IcatClient icatClient =
259284
* new IcatClient("https://localhost:8181", sessionId);

src/test/java/org/icatproject/topcat/UserResourceTest.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -283,11 +283,10 @@ public void testQueueVisitId() throws Exception {
283283
System.out.println("DEBUG testQueueVisitId");
284284
List<Long> downloadIds = new ArrayList<>();
285285
try {
286-
String facilityName = "LILS";
287286
String transport = "http";
288287
String email = "";
289288
String visitId = "Proposal 0 - 0 0";
290-
Response response = userResource.queueVisitId(facilityName, sessionId, transport, null, email, visitId);
289+
Response response = userResource.queueVisitId(null, sessionId, transport, null, email, visitId);
291290
assertEquals(200, response.getStatus());
292291

293292
JsonArray downloadIdsArray = Utils.parseJsonArray(response.getEntity().toString());
@@ -351,7 +350,6 @@ public void testQueueFiles() throws Exception {
351350
System.out.println("DEBUG testQueueFiles");
352351
Long downloadId = null;
353352
try {
354-
String facilityName = "LILS";
355353
String transport = "http";
356354
String email = "";
357355
String file = "abcdefghijklmnopqrstuvwxyz";
@@ -362,7 +360,7 @@ public void testQueueFiles() throws Exception {
362360
for (JsonObject datafile : datafiles) {
363361
files.add(datafile.getString("location"));
364362
}
365-
Response response = userResource.queueFiles(facilityName, sessionId, transport, null, email, files);
363+
Response response = userResource.queueFiles(null, sessionId, transport, null, email, files);
366364
assertEquals(200, response.getStatus());
367365
JsonObject responseObject = Utils.parseJsonObject(response.getEntity().toString());
368366
downloadId = responseObject.getJsonNumber("downloadId").longValueExact();

tools/datagateway_admin

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ def show_download():
114114
print(requests.get(topcat_url + "/admin/downloads", params={
115115
"facilityName": facility_name,
116116
"sessionId": session_id,
117-
"queryOffset": "where download.id = " + download_id
117+
"queryOffset": f"where download.id = {download_id}"
118118
}, verify=verifySsl).text)
119119

120120

@@ -124,7 +124,7 @@ def list_file_locations():
124124
download = json.loads(requests.get(topcat_url + "/admin/downloads", params={
125125
"facilityName": facility_name,
126126
"sessionId": session_id,
127-
"queryOffset": "where download.id = " + download_id
127+
"queryOffset": f"where download.id = {download_id}"
128128
}, verify=verifySsl).text)[0]
129129
download_items = download["downloadItems"]
130130
datafile_locations = []
@@ -175,7 +175,7 @@ def expire_download():
175175

176176
def _expire_download(download_id):
177177
response = requests.put(
178-
topcat_url + "/admin/download/" + download_id + "/status",
178+
f"{topcat_url}/admin/download/{download_id}/status",
179179
data={
180180
"facilityName": facility_name,
181181
"sessionId": session_id,
@@ -341,7 +341,7 @@ def start_download(download_id):
341341
"sessionId": session_id,
342342
"value": "PREPARING"
343343
}
344-
url = topcat_url + "/admin/download/" + download_id + "/status"
344+
url = f"{topcat_url}/admin/download/{download_id}/status"
345345
response = requests.put(url=url, data=data, verify=verifySsl)
346346
print(response.status_code, response.text)
347347

@@ -369,7 +369,7 @@ def requeue_download():
369369
"sessionId": session_id,
370370
"value": "QUEUED"
371371
}
372-
url = topcat_url + "/admin/download/" + download_id + "/status"
372+
url = f"{topcat_url}/admin/download/{download_id}/status"
373373
response = requests.put(url=url, data=data, verify=verifySsl)
374374
print(response.status_code, response.text)
375375

0 commit comments

Comments
 (0)