Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,9 @@ public BrAPITrial getTrialDataByUUID(UUID programId, UUID trialId, boolean stats
//Remove the [program key] from the trial name
trial.setTrialName( Utilities.removeUnknownProgramKey( trial.getTrialName()) );
if( stats ){
log.debug("fetching experiment: " + trialId + " stats");
int environmentsCount = 1; // For now this is hardcoded to 1, because we are only supporting one environment per experiment
log.debug("fetching observation units for experiment: " + trialId);
long germplasmCount = countGermplasm(programId, trial.getTrialDbId());
trial.putAdditionalInfoItem("environmentsCount", environmentsCount);
trial.putAdditionalInfoItem("germplasmCount", germplasmCount);
Expand Down Expand Up @@ -296,11 +298,16 @@ private StreamedFile zipFiles(List<DownloadFile> files) throws IOException {
}

public Dataset getDatasetData(Program program, UUID experimentId, UUID datsetId, Boolean stats) throws ApiException, DoesNotExistException {
log.debug("fetching dataset: " + datsetId + " for experiment: " + experimentId + ". including stats: " + stats);
log.debug("fetching observationUnits for dataset: " + datsetId);
List<BrAPIObservationUnit> datasetOUs = ouDAO.getObservationUnitsForDataset(datsetId.toString(), program);
log.debug("fetching dataset variables dataset: " + datsetId);
List<BrAPIObservationVariable> datasetObsVars = getDatasetObsVars(datsetId.toString(), program);
List<String> ouDbIds = datasetOUs.stream().map(BrAPIObservationUnit::getObservationUnitDbId).collect(Collectors.toList());
List<String> obsVarDbIds = datasetObsVars.stream().map(BrAPIObservationVariable::getObservationVariableDbId).collect(Collectors.toList());
log.debug("fetching observations for dataset: " + datsetId);
List<BrAPIObservation> data = observationDAO.getObservationsByObservationUnitsAndVariables(ouDbIds, obsVarDbIds, program);
log.debug("building dataset object for dataset: " + datsetId);
Dataset dataset = new Dataset(experimentId.toString(), data, datasetOUs, datasetObsVars);
if (stats) {
Integer ouCount = datasetOUs.size();
Expand Down
81 changes: 54 additions & 27 deletions src/test/java/org/breedinginsight/DatabaseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,12 @@
import org.testcontainers.images.PullPolicy;

import javax.annotation.Nonnull;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.time.Duration;
import java.time.temporal.ChronoUnit;
import java.util.HashMap;
import java.util.Map;
import java.util.*;

@Slf4j
public class DatabaseTest implements TestPropertyProvider {
Expand All @@ -43,36 +45,42 @@ public class DatabaseTest implements TestPropertyProvider {
private static final String dbPassword = "postgres";

@Getter
private GenericContainer dbContainer;
private static GenericContainer dbContainer;

@Getter
private Network network;
private static Network network;

@Getter
private GenericContainer redisContainer;
private static GenericContainer redisContainer;

@Getter
private RedissonClient redisConnection;

@SneakyThrows
public DatabaseTest() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this capitalized and not databaseTest()?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a constructor, so it has to match the class name

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤦 thanks.

network = Network.newNetwork();
dbContainer = new GenericContainer<>("postgres:11.4")
.withNetwork(network)
.withNetworkAliases("testdb")
.withImagePullPolicy(PullPolicy.defaultPolicy())
.withExposedPorts(5432)
.withEnv("POSTGRES_DB", dbName)
.withEnv("POSTGRES_PASSWORD", dbPassword)
.waitingFor(Wait.forLogMessage(".*LOG: database system is ready to accept connections.*", 1).withStartupTimeout(Duration.of(2, ChronoUnit.MINUTES)));
dbContainer.start();
redisContainer = new GenericContainer<>("redis")
.withNetwork(network)
.withNetworkAliases("redis")
.withImagePullPolicy(PullPolicy.defaultPolicy())
.withExposedPorts(6379)
.waitingFor(Wait.forListeningPort());
redisContainer.start();
if(network == null) {
network = Network.newNetwork();
}
if(dbContainer == null) {
dbContainer = new GenericContainer<>("postgres:11.4")
.withNetwork(network)
.withNetworkAliases("testdb")
.withImagePullPolicy(PullPolicy.defaultPolicy())
.withExposedPorts(5432)
.withEnv("POSTGRES_DB", dbName)
.withEnv("POSTGRES_PASSWORD", dbPassword)
.waitingFor(Wait.forLogMessage(".*LOG: database system is ready to accept connections.*", 1)
.withStartupTimeout(Duration.of(2, ChronoUnit.MINUTES)));
dbContainer.start();
}
if(redisContainer == null) {
redisContainer = new GenericContainer<>("redis")
.withNetwork(network)
.withNetworkAliases("redis")
.withImagePullPolicy(PullPolicy.defaultPolicy())
.withExposedPorts(6379)
.waitingFor(Wait.forListeningPort());
redisContainer.start();
}

Integer redisContainerPort = redisContainer.getMappedPort(6379);
String redisContainerIp = redisContainer.getContainerIpAddress();
Expand Down Expand Up @@ -110,9 +118,28 @@ public Map<String, String> getProperties() {

@SneakyThrows
@AfterAll
public void stopContainers() {
redisContainer.stop();
dbContainer.stop();
network.close();
public void resetContainers() {
if(redisContainer != null) {
redisConnection.getKeys()
.flushall();
}
if(dbContainer != null) {
resetDb(dbName);
resetDb("postgres");
}
}

private void resetDb(String name) {
log.debug("resetting db: " + name);
try (Connection con = DriverManager.
getConnection(String.format("jdbc:postgresql://%s:%s/"+name,
dbContainer.getContainerIpAddress(), dbContainer.getMappedPort(5432)),
"postgres", dbPassword)) {

con.prepareStatement("drop schema public CASCADE").execute();
con.prepareStatement("create schema public").execute();
} catch (SQLException e) {
log.error("Error during reset", e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1104,7 +1104,9 @@ public void checkTraitFullResponse(JsonObject traitJson, Trait trait) {
assertEquals(trait.getScale().getDataType().toString(), scaleJson.get("dataType").getAsString(), "Scale data types don't match");

List<JsonObject> jsonCategories = new ArrayList<>();
scaleJson.get("categories").getAsJsonArray().iterator().forEachRemaining(element -> jsonCategories.add(element.getAsJsonObject()));
if(scaleJson.has("categories")) {
scaleJson.get("categories").getAsJsonArray().iterator().forEachRemaining(element -> jsonCategories.add(element.getAsJsonObject()));
}
Collections.sort(jsonCategories, Comparator.comparing((x) -> x.get("label").getAsString()));
List<BrAPIScaleValidValuesCategories> brApiScaleCategories = new ArrayList<>(trait.getScale().getCategories());
Collections.sort(brApiScaleCategories, Comparator.comparing(BrAPIScaleValidValuesCategories::getLabel));
Expand Down