From 167d2c2ed2e0ac670380c7f923259f0d7f623e7b Mon Sep 17 00:00:00 2001 From: Ezequiel Valencia Date: Mon, 23 Dec 2024 17:28:34 -0500 Subject: [PATCH] Add Tests For The Field Data Endpoint --- .../restq/handlers/FieldData/FieldDataDB.java | 7 +- .../handlers/FieldData/FieldDataResource.java | 6 +- .../restq/apiclient/FieldDataAPITest.java | 152 ++++++++++++++++++ 3 files changed, 163 insertions(+), 2 deletions(-) create mode 100644 vcell-rest/src/test/java/org/vcell/restq/apiclient/FieldDataAPITest.java diff --git a/vcell-rest/src/main/java/org/vcell/restq/handlers/FieldData/FieldDataDB.java b/vcell-rest/src/main/java/org/vcell/restq/handlers/FieldData/FieldDataDB.java index 99efa94155..5245045590 100644 --- a/vcell-rest/src/main/java/org/vcell/restq/handlers/FieldData/FieldDataDB.java +++ b/vcell-rest/src/main/java/org/vcell/restq/handlers/FieldData/FieldDataDB.java @@ -10,6 +10,7 @@ import cbit.vcell.geometry.RegionImage; import cbit.vcell.math.VariableType; import cbit.vcell.modeldb.DatabaseServerImpl; +import cbit.vcell.resource.PropertyLoader; import cbit.vcell.simdata.DataSetControllerImpl; import cbit.vcell.solvers.CartesianMesh; import jakarta.enterprise.context.ApplicationScoped; @@ -38,7 +39,11 @@ public class FieldDataDB { @Inject public FieldDataDB(AgroalConnectionFactory agroalConnectionFactory) throws DataAccessException, FileNotFoundException { databaseServer = new DatabaseServerImpl(agroalConnectionFactory, agroalConnectionFactory.getKeyFactory()); - dataSetController = new DataSetControllerImpl(null, new File("/simdata"), new File("/simdata")); + String primarySimDataDir = PropertyLoader.getProperty(PropertyLoader.primarySimDataDirInternalProperty, "/simdata"); + String secondarySimDataDir = PropertyLoader.getProperty(PropertyLoader.secondarySimDataDirInternalProperty, "/simdata"); + dataSetController = new DataSetControllerImpl(null, + new File(primarySimDataDir), + new File(secondarySimDataDir)); } public FieldDataDBOperationResults copyNoConflict(User user, FieldDataDBOperationSpec spec) throws DataAccessException { diff --git a/vcell-rest/src/main/java/org/vcell/restq/handlers/FieldData/FieldDataResource.java b/vcell-rest/src/main/java/org/vcell/restq/handlers/FieldData/FieldDataResource.java index b16f4e4a27..de79ed47ed 100644 --- a/vcell-rest/src/main/java/org/vcell/restq/handlers/FieldData/FieldDataResource.java +++ b/vcell-rest/src/main/java/org/vcell/restq/handlers/FieldData/FieldDataResource.java @@ -26,6 +26,7 @@ import org.vcell.util.document.KeyValue; import org.vcell.util.document.User; +import java.io.FileNotFoundException; import java.io.InputStream; import java.sql.SQLException; import java.util.HashMap; @@ -73,7 +74,10 @@ public FieldDataInfo getFieldDataFromID(String fieldDataID){ FieldDataFileOperationResults results = fieldDataDB.getFieldDataFromID(userRestDB.getUserFromIdentity(securityIdentity), fieldDataID, 0); return new FieldDataInfo(results.extent, results.origin, results.iSize, results.dataIdentifierArr,results.times); } catch (DataAccessException e) { - throw new WebApplicationException(e.getMessage(), 500); + if (e.getCause() instanceof FileNotFoundException){ + throw new WebApplicationException("Field data not found.", 404); + } + throw new WebApplicationException("Problem retrieving file.", 500); } } diff --git a/vcell-rest/src/test/java/org/vcell/restq/apiclient/FieldDataAPITest.java b/vcell-rest/src/test/java/org/vcell/restq/apiclient/FieldDataAPITest.java new file mode 100644 index 0000000000..4a1a89748e --- /dev/null +++ b/vcell-rest/src/test/java/org/vcell/restq/apiclient/FieldDataAPITest.java @@ -0,0 +1,152 @@ +package org.vcell.restq.apiclient; + +import cbit.vcell.math.VariableType; +import cbit.vcell.resource.PropertyLoader; +import io.quarkus.test.junit.QuarkusTest; +import io.quarkus.test.keycloak.client.KeycloakTestClient; +import jakarta.inject.Inject; +import org.apache.commons.io.FileUtils; +import org.eclipse.microprofile.config.inject.ConfigProperty; +import org.junit.jupiter.api.*; +import org.vcell.restclient.ApiClient; +import org.vcell.restclient.ApiException; +import org.vcell.restclient.api.FieldDataResourceApi; +import org.vcell.restclient.api.UsersResourceApi; +import org.vcell.restclient.model.AnalyzedResultsFromFieldData; +import org.vcell.restclient.model.FieldDataInfo; +import org.vcell.restclient.model.FieldDataReferences; +import org.vcell.restclient.model.FieldDataSaveResults; +import org.vcell.restq.TestEndpointUtils; +import org.vcell.restq.config.CDIVCellConfigProvider; +import org.vcell.restq.db.AgroalConnectionFactory; +import org.vcell.util.*; +import org.vcell.util.document.KeyValue; + +import java.io.File; +import java.io.IOException; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; + +@QuarkusTest +public class FieldDataAPITest { + + @ConfigProperty(name = "quarkus.http.test-port") + Integer testPort; + + @Inject + AgroalConnectionFactory agroalConnectionFactory; + + KeycloakTestClient keycloakClient = new KeycloakTestClient(); + + private ApiClient aliceAPIClient; + private ApiClient bobAPIClient; + private static String previousPrimarySimDir; + private static String previousSecondarySimDir; + + private final static File temporaryFolder = new File(System.getProperty("java.io.tmpdir") + "/fieldDataTest"); + + @BeforeAll + public static void setupConfig(){ + PropertyLoader.setConfigProvider(new CDIVCellConfigProvider()); + } + + @BeforeEach + public void createClients() throws ApiException { + aliceAPIClient = TestEndpointUtils.createAuthenticatedAPIClient(keycloakClient, testPort, TestEndpointUtils.TestOIDCUsers.alice); + bobAPIClient = TestEndpointUtils.createAuthenticatedAPIClient(keycloakClient, testPort, TestEndpointUtils.TestOIDCUsers.bob); + UsersResourceApi usersResourceApi = new UsersResourceApi(aliceAPIClient); + usersResourceApi.mapUser(TestEndpointUtils.administratorUserLoginInfo); + + temporaryFolder.mkdirs(); + + previousPrimarySimDir = PropertyLoader.getProperty(PropertyLoader.primarySimDataDirInternalProperty, null); + PropertyLoader.setProperty(PropertyLoader.primarySimDataDirInternalProperty, temporaryFolder.getAbsolutePath()); + + previousSecondarySimDir = PropertyLoader.getProperty(PropertyLoader.secondarySimDataDirExternalProperty, null); + System.setProperty(PropertyLoader.secondarySimDataDirExternalProperty, temporaryFolder.getAbsolutePath()); + } + + @AfterEach + public void removeOIDCMappings() throws SQLException, DataAccessException, IOException { + TestEndpointUtils.removeAllMappings(agroalConnectionFactory); + if (previousPrimarySimDir != null){ + PropertyLoader.setProperty(PropertyLoader.primarySimDataDirInternalProperty, previousPrimarySimDir); + } + if (previousSecondarySimDir != null){ + System.setProperty(PropertyLoader.secondarySimDataDirExternalProperty, previousSecondarySimDir); + } + + FileUtils.cleanDirectory(temporaryFolder); + } + + + @Test + public void testAddAndDeleteFieldDataFromFile() throws ApiException { + FieldDataResourceApi fieldDataResourceApi = new FieldDataResourceApi(aliceAPIClient); + List>> matrix = new ArrayList<>(); + List varNames = new ArrayList<>(){{add("Variable 1");}}; + List times = new ArrayList<>(); times.add(0.0); times.add(1.0); + + double x = 5; + double y = 4; + double z = 3; + for (int t = 0; t < times.size(); t++){ + List> tArr = new ArrayList<>(); + for (int vars = 0; vars < varNames.size(); vars++){ + List vArr = new ArrayList<>(); + for (int i = 0; i < (x * y * z); i++){ + vArr.add(i); + } + tArr.add(vArr); + } + matrix.add(tArr); + } + VariableType varType = VariableType.getVariableTypeFromInteger(1); + Origin origin = new Origin(0.0, 0.0, 0.0); + Extent extent = new Extent(x, y, z); + ISize iSize = new ISize(1, 1, 1); + + ///////////////////// + // Add Field Data // + /////////////////// + AnalyzedResultsFromFieldData saveFieldDataFromFile = new AnalyzedResultsFromFieldData(); + saveFieldDataFromFile.setShortSpecData(matrix); saveFieldDataFromFile.varNames(varNames); + saveFieldDataFromFile.times(times); saveFieldDataFromFile.origin(Origin.originToDTO(origin)); saveFieldDataFromFile.extent(Extent.extentToDTO(extent)); + saveFieldDataFromFile.isize(ISize.iSizeToDTO(iSize)); saveFieldDataFromFile.annotation("test annotation"); saveFieldDataFromFile.name("TestFile"); + FieldDataSaveResults results = fieldDataResourceApi.createNewFieldDataFromFileAlreadyAnalyzed(saveFieldDataFromFile); + + // File is Saved on File System + FieldDataInfo fieldDataInfo = fieldDataResourceApi.getFieldDataFromID(results.getFieldDataID()); + Assertions.assertEquals(saveFieldDataFromFile.getName(), results.getFieldDataName()); + Assertions.assertTrue(origin.compareEqual(Origin.dtoToOrigin(fieldDataInfo.getOrigin()))); + Assertions.assertTrue(extent.compareEqual(Extent.dtoToExtent(fieldDataInfo.getExtent()))); + Assertions.assertTrue(iSize.compareEqual(ISize.dtoToISize(fieldDataInfo.getIsize()))); + Assertions.assertEquals(times, fieldDataInfo.getTimes()); + + // It's in the DB + FieldDataReferences references = fieldDataResourceApi.getAllFieldDataIDs(); + Assertions.assertEquals(saveFieldDataFromFile.getAnnotation(), references.getExternalDataAnnotations().get(0)); + Assertions.assertEquals(results.getFieldDataID(), references.getExternalDataIdentifiers().get(0).getKey().getValue().toString()); + Assertions.assertEquals(0, references.getExternalDataIDSimRefs().size()); + + /////////////////////// + // Delete Field Data // + ////////////////////// + fieldDataResourceApi.deleteFieldData(results.getFieldDataID()); + + // No Longer on File System + try{ + fieldDataResourceApi.getFieldDataFromID(results.getFieldDataID()); + } catch (ApiException e){ + Assertions.assertEquals(404, e.getCode()); + } + + // No Longer in DB + references = fieldDataResourceApi.getAllFieldDataIDs(); + Assertions.assertEquals(0, references.getExternalDataIdentifiers().size()); + Assertions.assertEquals(0, references.getExternalDataAnnotations().size()); + Assertions.assertEquals(0, references.getExternalDataIDSimRefs().size()); + } + +}