Skip to content
Merged
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
35 changes: 12 additions & 23 deletions src/org/labkey/test/util/exp/SampleTypeAPIHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.labkey.test.util.exp;

import org.json.JSONArray;
import org.junit.Assert;
import org.labkey.remoteapi.CommandException;
import org.labkey.remoteapi.Connection;
Expand All @@ -28,14 +29,12 @@
import org.labkey.test.params.experiment.SampleTypeDefinition;
import org.labkey.test.util.DomainUtils;
import org.labkey.test.util.DomainUtils.DomainKind;
import org.labkey.test.util.EscapeUtil;
import org.labkey.test.util.TestDataGenerator;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
Expand Down Expand Up @@ -149,45 +148,36 @@ public static Map<Integer, String> getSortedRowIdToSamplesMap(String containerPa
*/
public static Map<String, Integer> getRowIdsForSamples(String containerPath, String sampleTypeName, List<String> sampleNames) throws IOException, CommandException
{

// Use json for the value parameter of the filter. This allows for tricky characters like ";" to be passed to the API.
StringBuilder json = new StringBuilder("{json:[");

Iterator<String> iterator = sampleNames.iterator();

while (iterator.hasNext()) {
String sampleName = iterator.next();
json.append(EscapeUtil.toJSONStr(sampleName));
if (iterator.hasNext()) {
json.append(", ");
} else {
json.append("]}");
}
}
StringBuilder json = new StringBuilder("{json:").append(new JSONArray(sampleNames)).append("}");

// Use the details view to avoid column and filter settings on the default view
String detailsView = "~~DETAILS~~";
Connection connection = WebTestHelper.getRemoteApiConnection();
SelectRowsCommand cmd = new SelectRowsCommand("samples", sampleTypeName);
cmd.setColumns(Arrays.asList("RowId", "Name"));
cmd.setColumns(List.of("RowId", "Name"));
cmd.addFilter("Name", json, Filter.Operator.IN);
cmd.setViewName(detailsView);

SelectRowsResponse response = cmd.execute(connection, containerPath);

String errorMsg = "The sample names returned from the query do not match the sample names sent in.";

// There have been some TC failures where selectRows is not returning anything when it should. Adding this
// logging to help get more logging when/if it happens again.
if(response.getRowCount().intValue() == 0)
if (response.getRowCount().intValue() == 0)
{
cmd = new SelectRowsCommand("samples", sampleTypeName);
cmd.setColumns(Arrays.asList("Name"));
cmd.setColumns(List.of("Name"));
cmd.setViewName(detailsView);

SelectRowsResponse responseCount = cmd.execute(connection, containerPath);

errorMsg = errorMsg + "\n" + String.format("No rows were returned with filter. The sample type '%s' has %d rows.",
sampleTypeName, responseCount.getRowCount().intValue());

List<String> names = new ArrayList<>();
for(Map<String, Object> row : responseCount.getRows())
for (Map<String, Object> row : responseCount.getRows())
{
Object tempName = row.get("Name");
names.add(tempName.toString());
Expand All @@ -198,16 +188,15 @@ public static Map<String, Integer> getRowIdsForSamples(String containerPath, Str

Map<String, Integer> rowIds = new TreeMap<>();

for(Map<String, Object> row : response.getRows())
for (Map<String, Object> row : response.getRows())
{
Object name = row.get("Name");
Object value = row.get("RowId");
rowIds.put(name.toString(), Integer.parseInt(value.toString()));
}

// Check that the names returned from the query match the names sent in.
Assert.assertEquals(errorMsg,
new HashSet<>(sampleNames), rowIds.keySet());
Assert.assertEquals(errorMsg, new HashSet<>(sampleNames), rowIds.keySet());

return rowIds;
}
Expand Down