Skip to content

Commit 06c9c0a

Browse files
committed
Additional tests
1 parent b18b05b commit 06c9c0a

2 files changed

Lines changed: 91 additions & 40 deletions

File tree

src/org/labkey/test/tests/TriggerScriptTest.java

Lines changed: 83 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.labkey.test.tests;
1717

18+
import org.assertj.core.api.Assertions;
1819
import org.jetbrains.annotations.Nullable;
1920
import org.junit.Assert;
2021
import org.junit.Before;
@@ -26,6 +27,7 @@
2627
import org.labkey.remoteapi.query.DeleteRowsCommand;
2728
import org.labkey.remoteapi.query.InsertRowsCommand;
2829
import org.labkey.remoteapi.query.BaseRowsCommand;
30+
import org.labkey.remoteapi.query.MoveRowsCommand;
2931
import org.labkey.remoteapi.query.RowsResponse;
3032
import org.labkey.remoteapi.query.UpdateRowsCommand;
3133
import org.labkey.test.BaseWebDriverTest;
@@ -40,6 +42,8 @@
4042
import org.labkey.test.params.FieldDefinition.ColumnType;
4143
import org.labkey.test.params.experiment.DataClassDefinition;
4244
import org.labkey.test.params.experiment.SampleTypeDefinition;
45+
import org.labkey.test.params.list.IntListDefinition;
46+
import org.labkey.test.params.list.ListDefinition;
4347
import org.labkey.test.util.DataRegionTable;
4448
import org.labkey.test.util.Maps;
4549
import org.labkey.test.util.PortalHelper;
@@ -61,6 +65,10 @@
6165
@BaseWebDriverTest.ClassTimeout(minutes = 8)
6266
public class TriggerScriptTest extends BaseWebDriverTest
6367
{
68+
private static final String PROJECT_NAME = "Test Trigger Script Project";
69+
private static final String SUBFOLDER_NAME = "SubfolderA";
70+
private static final String SUBFOLDER_PATH = "/" + PROJECT_NAME + "/" + SUBFOLDER_NAME;
71+
6472
//List constants
6573
private static final String TRIGGER_MODULE = "triggerTestModule";
6674
private static final String SIMPLE_MODULE = "simpletest";
@@ -90,6 +98,7 @@ public class TriggerScriptTest extends BaseWebDriverTest
9098
public static final String PEOPLE_LIST_NAME = "People";
9199

92100
protected final PortalHelper _portalHelper = new PortalHelper(this);
101+
private static ListDefinition EMPLOYEE_LIST;
93102

94103
@Override
95104
public List<String> getAssociatedModules()
@@ -101,7 +110,7 @@ public List<String> getAssociatedModules()
101110
@Override
102111
protected String getProjectName()
103112
{
104-
return "Test Trigger Script Project";
113+
return PROJECT_NAME;
105114
}
106115

107116
@Override
@@ -167,36 +176,42 @@ private interface GoToDataUI
167176
}
168177

169178
@BeforeClass
170-
public static void projectSetup()
179+
public static void projectSetup() throws Exception
171180
{
172181
TriggerScriptTest init = getCurrentTest();
173182
init.doSetup();
174183
}
175184

176-
177-
protected void doSetup()
185+
protected void doSetup() throws Exception
178186
{
179187
_containerHelper.createProject(getProjectName(), null);
188+
_containerHelper.createSubfolder(getProjectName(), SUBFOLDER_NAME);
180189
_containerHelper.enableModule(getProjectName(), "Query");
181190
_containerHelper.enableModule(getProjectName(), SIMPLE_MODULE);
182191
_containerHelper.enableModule(getProjectName(), TRIGGER_MODULE);
183192

184-
//create List
185-
FieldDefinition[] columns = new FieldDefinition[] {
193+
// Create lists
194+
{
195+
List<FieldDefinition> fields = List.of(
186196
new FieldDefinition("name", ColumnType.String).setLabel("Name"),
187197
new FieldDefinition("ssn", ColumnType.String).setLabel("SSN"),
188198
new FieldDefinition("company", ColumnType.String).setLabel("Company")
199+
);
189200

190-
};
191-
192-
_listHelper.createList(getProjectName(), LIST_NAME, "Key", columns);
201+
EMPLOYEE_LIST = new IntListDefinition(LIST_NAME, "Key").setFields(fields);
202+
EMPLOYEE_LIST.create(createDefaultConnection(), getProjectName());
193203

194-
log("Create the People list");
195-
_listHelper.createList(getProjectName(), PEOPLE_LIST_NAME, "Key",
204+
fields = List.of(
196205
new FieldDefinition("Name", ColumnType.String).setDescription("Name"),
197206
new FieldDefinition("Age", ColumnType.Integer).setDescription("Age"),
198207
new FieldDefinition("FavoriteDateTime", ColumnType.DateAndTime).setDescription("Favorite date time. Who doesn't have one?"),
199-
new FieldDefinition("Crazy", ColumnType.Boolean).setDescription("Crazy?"));
208+
new FieldDefinition("Crazy", ColumnType.Boolean).setDescription("Crazy?")
209+
);
210+
211+
new IntListDefinition(PEOPLE_LIST_NAME, "Key")
212+
.setFields(fields)
213+
.create(createDefaultConnection(), getProjectName());
214+
}
200215

201216
importFolderFromZip(TestFileUtils.getSampleData("studies/LabkeyDemoStudy.zip"));
202217

@@ -217,6 +232,7 @@ public void goToProjectStart()
217232
@Test
218233
public void testListIndividualTriggers()
219234
{
235+
cleanUpListRows();
220236
EmployeeRecord caughtAfter = new EmployeeRecord("Emp 1", "1112223333", "Test"),
221237
changedBefore = new EmployeeRecord("Emp 2", "2223334444", "Some Other");
222238

@@ -267,12 +283,12 @@ public void testListIndividualTriggers()
267283
clickButton("Back");
268284
//Verify validation error prevented delete
269285
waitForElement(Locator.tagWithText("td", "Emp 3"));
270-
cleanUpListRows();
271286
}
272287

273288
@Test
274289
public void testListImportTriggers()
275290
{
291+
cleanUpListRows();
276292
goToManagedList(LIST_NAME);
277293
_listHelper.clickImportData();
278294

@@ -302,7 +318,27 @@ public void testListImportTriggers()
302318
importDataPage.submit();
303319

304320
waitForElement(Locator.tagWithText("td","Importing TSV"));
321+
}
322+
323+
@Test
324+
public void testListMoveTriggers() throws Exception
325+
{
305326
cleanUpListRows();
327+
328+
RowsResponse response = EMPLOYEE_LIST.getTestDataGenerator(getProjectName())
329+
.addCustomRow(Map.of("name", "Emp 11", "ssn", "123-45-6789", "company", "LK"))
330+
.insertRows();
331+
332+
List<EmployeeRecord> records = response.getRows().stream().map(EmployeeRecord::fromMap).toList();
333+
334+
openServerJavaScriptConsole();
335+
336+
MoveRowsCommand command = new MoveRowsCommand(SUBFOLDER_PATH, LIST_SCHEMA, LIST_NAME);
337+
command.setRows(List.of(Map.of("Key", records.get(0).key)));
338+
command.execute(createDefaultConnection(), getProjectName());
339+
waitForConsole("init got triggered with event: move", "complete got triggered with event: move");
340+
341+
closeServerJavaScriptConsole();
306342
}
307343

308344
/** Issue 52098 - ensure trigger scripts have a chance to do custom type conversion with the incoming row */
@@ -336,9 +372,10 @@ public void testListAPITriggerTypeConversion() throws Exception
336372
Assert.assertEquals(26, updatedRow.get("Age"));
337373
}
338374

339-
@Test
375+
@Test
340376
public void testListAPITriggers() throws Exception
341377
{
378+
cleanUpListRows();
342379
String ssn1 = "111111112";
343380
String ssn2 = "222211111";
344381

@@ -408,8 +445,6 @@ public void testListAPITriggers() throws Exception
408445
delCmd = new DeleteRowsCommand(LIST_SCHEMA, LIST_NAME);
409446
delCmd.addRow(row3.toMap());
410447
assertAPIErrorMessage(delCmd, BEFORE_DELETE_ERROR, cn);
411-
412-
cleanUpListRows();
413448
}
414449

415450
/********************************
@@ -470,7 +505,7 @@ public void testDatasetImportTriggers()
470505
@Test
471506
public void testDatasetAPITriggers() throws Exception
472507
{
473-
doAPITriggerTest(STUDY_SCHEMA,DATASET_NAME,"ParticipantId", true);
508+
doAPITriggerTest(STUDY_SCHEMA, DATASET_NAME, "ParticipantId", true);
474509
}
475510

476511
/********************************
@@ -484,22 +519,14 @@ public void testDataClassIndividualTriggers() throws Exception
484519
GoToDataUI goToDataClass = () -> goTo("Data Classes", DATA_CLASSES_NAME);
485520

486521
setupDataClass();
522+
openServerJavaScriptConsole();
487523

488-
// Go to the log view to start capturing messages
489-
new SiteNavBar(getDriver()).clickAdminMenuItem(false, "Developer Links", "Server JavaScript Console");
490-
switchToWindow(1);
491-
waitForText("Message");
492-
493-
switchToMainWindow();
494524
doIndividualTriggerTest("query", goToDataClass, "Name", false, "Yes, Delete", false);
495525

496-
// Go back to the console window
497-
switchToWindow(1);
498-
waitForText("init got triggered with event: delete",
526+
waitForConsole("init got triggered with event: delete",
499527
"exp.data: this is from the shared function",
500528
"complete got triggered with event: delete");
501-
getDriver().close();
502-
switchToMainWindow();
529+
closeServerJavaScriptConsole();
503530
}
504531

505532

@@ -728,7 +755,9 @@ private void assertAPIErrorMessage(BaseRowsCommand cmd, String expected, Connect
728755
}
729756
catch (CommandException e)
730757
{
731-
Assert.assertTrue("Trigger script error message was wrong", e.getMessage().contains(expected));
758+
Assertions.assertThat(e.getMessage())
759+
.as("Trigger script error should contain expected text")
760+
.contains(expected);
732761
}
733762
}
734763

@@ -834,7 +863,7 @@ private void goTo(String webPartName, String tableName)
834863
/**
835864
* Generate delimited string of keys from a map
836865
*/
837-
private String joinMapValues(Map<String,String> data, String delimiter )
866+
private String joinMapValues(Map<String,String> data, String delimiter)
838867
{
839868
StringBuilder sb = new StringBuilder();
840869
data.values().forEach(val -> sb.append(val).append(delimiter));
@@ -844,7 +873,7 @@ private String joinMapValues(Map<String,String> data, String delimiter )
844873
/**
845874
* Generate delimited string of keys from a map
846875
*/
847-
private String joinMapKeys(Map<String,String> data, String delimiter )
876+
private String joinMapKeys(Map<String,String> data, String delimiter)
848877
{
849878
StringBuilder sb = new StringBuilder();
850879
data.keySet().forEach(val -> sb.append(val).append(delimiter));
@@ -885,4 +914,27 @@ private void setupSampleType()
885914
new FieldDefinition(COUNTRY_FIELD, ColumnType.String)));
886915
SampleTypeAPIHelper.createEmptySampleType(getProjectName(), sampleType);
887916
}
917+
918+
private void closeServerJavaScriptConsole()
919+
{
920+
switchToWindow(1);
921+
getDriver().close();
922+
switchToMainWindow();
923+
}
924+
925+
private void openServerJavaScriptConsole()
926+
{
927+
// Go to the log view to start capturing messages
928+
new SiteNavBar(getDriver()).clickAdminMenuItem(false, "Developer Links", "Server JavaScript Console");
929+
switchToWindow(1);
930+
waitForText("Message");
931+
switchToMainWindow();
932+
}
933+
934+
private void waitForConsole(String... text)
935+
{
936+
switchToWindow(1);
937+
waitForText(text);
938+
switchToMainWindow();
939+
}
888940
}

src/org/labkey/test/tests/list/ListMoveRowsTest.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
import java.io.File;
2929
import java.io.IOException;
30+
import java.text.DecimalFormat;
3031
import java.util.ArrayList;
3132
import java.util.List;
3233
import java.util.Map;
@@ -159,7 +160,6 @@ protected void doCleanup(boolean afterTest)
159160
@Test
160161
public void testAutoIntListMove() throws Exception
161162
{
162-
// Arrange
163163
truncateList(AUTO_INCREMENT_LIST);
164164

165165
int totalRows = 100;
@@ -172,7 +172,6 @@ public void testAutoIntListMove() throws Exception
172172
@Test
173173
public void testIntListMove() throws Exception
174174
{
175-
// Arrange
176175
truncateList(INTEGER_KEY_LIST);
177176

178177
int totalRows = 100;
@@ -185,7 +184,6 @@ public void testIntListMove() throws Exception
185184
@Test
186185
public void testVarListMove() throws Exception
187186
{
188-
// Arrange
189187
truncateList(STRING_KEY_LIST);
190188

191189
int totalRows = 100;
@@ -283,21 +281,21 @@ public void testListInSubfolder() throws Exception
283281

284282
private void successfullyMoveRows(ListDefinition list, List<Map<String, Object>> rows) throws Exception
285283
{
286-
var rowCount = rows.size();
284+
var rowsSize = rows.size();
287285
var rowAttachmentSize = attachmentCount(rows);
288286

289-
var mid = rowCount / 2;
287+
var mid = rowsSize / 2;
290288
var firstRows = rows.subList(0, mid);
291289
var firstRowsSize = firstRows.size();
292290
var firstRowAttachmentSize = attachmentCount(firstRows);
293291

294-
var secondRows = rows.subList(mid, rowCount);
292+
var secondRows = rows.subList(mid, rowsSize);
295293
var secondRowsSize = secondRows.size();
296294
var secondRowAttachmentSize = attachmentCount(secondRows);
297295

298296
// Act
299297
var moveResponse = moveRows(list, getProjectName(), SUBFOLDER_A_PATH, rows);
300-
var expectedCounts = new UpdateCounts(rowAttachmentSize, rowCount + 2, rowCount, rowCount, rowCount - rowAttachmentSize);
298+
var expectedCounts = new UpdateCounts(rowAttachmentSize, rowsSize + 2, rowsSize, rowsSize, rowsSize - rowAttachmentSize);
301299
verifySuccessfulMove(list, moveResponse, getProjectName(), SUBFOLDER_A_PATH, expectedCounts);
302300
if (checker().errorsSinceMark() > 0)
303301
return;
@@ -366,17 +364,18 @@ private void verifySuccessfulMove(
366364
checker().verifyEquals("Unexpected number of query audit events moved", expectedCounts.queryAuditEventsMoved, response.getUpdateCounts().get("queryAuditEventsMoved"));
367365

368366
// Verify audit logs
367+
var decimalFormat = new DecimalFormat("#,##0");
369368
var hasUpdates = expectedCounts != NO_UPDATE;
370369
var listAuditEvents = _auditLogHelper.getAuditLogsForTransactionId(getProjectName(), LIST_AUDIT_EVENT, List.of("Comment", "Container/Path", "ListId"), response.getTransactionAuditId(), ContainerFilter.CurrentAndSubfolders);
371370
checker().verifyEquals("Unexpected number of list audit events", expectedCounts.listAuditEventsCreated, listAuditEvents.size());
372371
checker().wrapAssertion(() -> {
373-
var matches = auditEventMatches(listAuditEvents, targetPath, list.getListId(), String.format("Moved %d rows from %s", expectedCounts.listRecords, sourcePath));
372+
var matches = auditEventMatches(listAuditEvents, targetPath, list.getListId(), String.format("Moved %s rows from %s", decimalFormat.format(expectedCounts.listRecords), sourcePath));
374373
Assertions.assertThat(matches)
375374
.as("Expected one event recording move in target container")
376375
.hasSize(hasUpdates ? 1 : 0);
377376
});
378377
checker().wrapAssertion(() -> {
379-
var matches = auditEventMatches(listAuditEvents, sourcePath, list.getListId(), String.format("Moved %d rows to %s", expectedCounts.listRecords, targetPath));
378+
var matches = auditEventMatches(listAuditEvents, sourcePath, list.getListId(), String.format("Moved %s rows to %s", decimalFormat.format(expectedCounts.listRecords), targetPath));
380379
Assertions.assertThat(matches)
381380
.as("Expected one event recording move in source container")
382381
.hasSize(hasUpdates ? 1 : 0);

0 commit comments

Comments
 (0)