Skip to content

Commit

Permalink
Add Tests For The Field Data Endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
AvocadoMoon committed Dec 23, 2024
1 parent 5cb8b12 commit 167d2c2
Show file tree
Hide file tree
Showing 3 changed files with 163 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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<List<List<Integer>>> matrix = new ArrayList<>();
List<String> varNames = new ArrayList<>(){{add("Variable 1");}};
List<Double> 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<List<Integer>> tArr = new ArrayList<>();
for (int vars = 0; vars < varNames.size(); vars++){
List<Integer> 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());
}

}

0 comments on commit 167d2c2

Please sign in to comment.