Skip to content

Commit

Permalink
Replace hardcoded filenames in testQueueFiles with fetched values #50
Browse files Browse the repository at this point in the history
  • Loading branch information
patrick-austin committed Jan 15, 2025
1 parent d0a7220 commit ca41d0f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
26 changes: 23 additions & 3 deletions src/main/java/org/icatproject/topcat/IcatClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -230,18 +230,38 @@ private JsonArray submitQuery(String query) throws TopcatException {
* @throws TopcatException
*/
public JsonObject getEntity(String entityType) throws TopcatException {
return getEntities(entityType, 1L).get(0);
}


/**
* Gets multiple Entities of the specified type, without any other conditions.
*
* NOTE: This function is written and intended for getting Investigation,
* Dataset or Datafile entities as part of the tests. It does not handle casing of
* entities containing multiple words, or querying for a specific instance of an
* entity.
*
* @param entityType Type of ICAT Entity to get
* @return A ICAT Entities of the specified type as JsonObjects
* @throws TopcatException
*/
public List<JsonObject> getEntities(String entityType, long limit) throws TopcatException {
try {
String entityCapital = StringUtils.capitalize(entityType.toLowerCase());
String query = URLEncoder.encode("SELECT o FROM " + entityCapital + " o LIMIT 0, 1", "UTF8");
String query = URLEncoder.encode("SELECT o FROM " + entityCapital + " o LIMIT 0, " + limit, "UTF8");
String url = "entityManager?sessionId=" + URLEncoder.encode(sessionId, "UTF8") + "&query=" + query;
Response response = httpClient.get(url, new HashMap<String, String>());
if(response.getCode() == 404){
throw new NotFoundException("Could not run getEntity got a 404 response");
} else if(response.getCode() >= 400){
throw new BadRequestException(Utils.parseJsonObject(response.toString()).getString("message"));
}
JsonObject entity = Utils.parseJsonArray(response.toString()).getJsonObject(0);
return entity.getJsonObject(entityCapital);
List<JsonObject> entities = new ArrayList<>();
for (JsonValue entity : Utils.parseJsonArray(response.toString())) {
entities.add(((JsonObject) entity).getJsonObject(entityCapital));
}
return entities;
} catch (TopcatException e){
throw e;
} catch (Exception e) {
Expand Down
10 changes: 8 additions & 2 deletions src/test/java/org/icatproject/topcat/UserResourceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -344,17 +344,23 @@ public void testQueueFiles() throws Exception {
String facilityName = "LILS";
String transport = "http";
String email = "";
List<String> files = Arrays.asList("return/tax/free", "thus/upon/land", "hard/wife/five");

IcatClient icatClient = new IcatClient("https://localhost:8181", sessionId);
List<JsonObject> datafiles = icatClient.getEntities("datafile", 3L);
List<String> files = new ArrayList<>();
for (JsonObject datafile : datafiles) {
files.add(datafile.getString("location"));
}
Response response = userResource.queueFiles(facilityName, sessionId, transport, email, files);
assertEquals(200, response.getStatus());

JsonArray downloadIdsArray = Utils.parseJsonArray(response.getEntity().toString());
assertEquals(3, downloadIdsArray.size());
long part = 1;
for (JsonNumber downloadIdJson : downloadIdsArray.getValuesAs(JsonNumber.class)) {
long downloadId = downloadIdJson.longValueExact();
downloadIds.add(downloadId);
}
assertEquals(3, downloadIds.size());
for (long downloadId : downloadIds) {
Download download = downloadRepository.getDownload(downloadId);
assertNull(download.getPreparedId());
Expand Down

0 comments on commit ca41d0f

Please sign in to comment.