Skip to content

Commit

Permalink
Use timeout, actual ICAT ids and Long.equals in UserResourceTest #35
Browse files Browse the repository at this point in the history
  • Loading branch information
patrick-austin committed Nov 21, 2024
1 parent ff347ae commit 1a6df75
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 7 deletions.
28 changes: 28 additions & 0 deletions src/main/java/org/icatproject/topcat/IcatClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import org.icatproject.topcat.httpclient.*;
import org.icatproject.topcat.exceptions.*;
import org.apache.commons.lang3.StringUtils;
import org.icatproject.topcat.domain.*;

import jakarta.json.*;
Expand Down Expand Up @@ -93,6 +94,33 @@ public String getFullName() throws TopcatException {
}
}

/**
* Gets a single Entity of the specified type, without any other conditions.
*
* @param entityType Type of ICAT Entity to get
* @return A single ICAT Entity of the specified type as a JsonObject
* @throws TopcatException
*/
public JsonObject getEntity(String entityType) throws TopcatException {
try {
String entityCapital = StringUtils.capitalize(entityType.toLowerCase());
String query = URLEncoder.encode("SELECT o FROM " + entityCapital + " o LIMIT 0, 1", "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);
} catch (TopcatException e){
throw e;
} catch (Exception e) {
throw new BadRequestException(e.getMessage());
}
}

public List<JsonObject> getEntities(String entityType, List<Long> entityIds) throws TopcatException {
List<JsonObject> out = new ArrayList<JsonObject>();
try {
Expand Down
19 changes: 12 additions & 7 deletions src/test/java/org/icatproject/topcat/UserResourceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,9 @@ public void setup() throws Exception {
public void testGetSize() throws Exception {
String facilityName = "LILS";
String entityType = "investigation";
Long entityId = (long) 1;
IcatClient icatClient = new IcatClient("https://localhost:8181", sessionId);

List<Long> emptyIds = new ArrayList<Long>();
JsonObject investigation = icatClient.getEntity(entityType);
long entityId = investigation.getInt("id");

Response response = userResource.getSize(facilityName, sessionId, entityType, entityId);

Expand All @@ -105,6 +104,9 @@ public void testGetSize() throws Exception {
@Test
public void testCart() throws Exception {
String facilityName = "LILS";
IcatClient icatClient = new IcatClient("https://localhost:8181", sessionId);
JsonObject dataset = icatClient.getEntity("dataset");
long entityId = dataset.getInt("id");

Response response;

Expand All @@ -127,7 +129,7 @@ public void testCart() throws Exception {
// We assume that there is a dataset with id = 1, and that simple/root can see
// it.

response = userResource.addCartItems(facilityName, sessionId, "dataset 1", false);
response = userResource.addCartItems(facilityName, sessionId, "dataset " + entityId, false);
assertEquals(200, response.getStatus());

response = userResource.getCart(facilityName, sessionId);
Expand All @@ -138,7 +140,7 @@ public void testCart() throws Exception {
// Again, this ought to be done directly, rather than using the methods we
// should be testing independently!

response = userResource.deleteCartItems(facilityName, sessionId, "dataset 1");
response = userResource.deleteCartItems(facilityName, sessionId, "dataset " + entityId);
assertEquals(200, response.getStatus());
assertEquals(0, getCartSize(response));
}
Expand All @@ -149,6 +151,9 @@ public void testSubmitCart() throws Exception {
Response response;
JsonObject json;
List<Download> downloads;
IcatClient icatClient = new IcatClient("https://localhost:8181", sessionId);
JsonObject dataset = icatClient.getEntity("dataset");
long entityId = dataset.getInt("id");

// Get the initial state of the downloads - may not be empty
// It appears queryOffset cannot be empty!
Expand All @@ -163,7 +168,7 @@ public void testSubmitCart() throws Exception {
System.out.println("DEBUG testSubmitCart: initial downloads size: " + initialDownloadsSize);

// Put something into the Cart, so we have something to submit
response = userResource.addCartItems(facilityName, sessionId, "dataset 1", false);
response = userResource.addCartItems(facilityName, sessionId, "dataset " + entityId, false);
assertEquals(200, response.getStatus());

// Now submit it
Expand Down Expand Up @@ -318,7 +323,7 @@ private int getCartSize(Response response) throws Exception {
private Download findDownload(List<Download> downloads, Long downloadId) {

for (Download download : downloads) {
if (download.getId() == downloadId)
if (download.getId().equals(downloadId))
return download;
}
return null;
Expand Down
1 change: 1 addition & 0 deletions src/test/resources/run.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ facility.LILS.icatUrl = https://localhost:8181
facility.LILS.idsUrl = https://localhost:8181
adminUserNames=simple/root
anonUserName=anon/anon
ids.timeout=10s

# Disable scheduled Download status checks (DO THIS FOR TESTS ONLY!)
test.disableDownloadStatusChecks = true

0 comments on commit 1a6df75

Please sign in to comment.