diff --git a/qqq-backend-core/src/test/java/com/kingsrook/qqq/backend/core/processes/implementations/bulk/insert/BulkInsertPrepareFileMappingStepTest.java b/qqq-backend-core/src/test/java/com/kingsrook/qqq/backend/core/processes/implementations/bulk/insert/BulkInsertPrepareFileMappingStepTest.java index c633c920..3388e6d4 100644 --- a/qqq-backend-core/src/test/java/com/kingsrook/qqq/backend/core/processes/implementations/bulk/insert/BulkInsertPrepareFileMappingStepTest.java +++ b/qqq-backend-core/src/test/java/com/kingsrook/qqq/backend/core/processes/implementations/bulk/insert/BulkInsertPrepareFileMappingStepTest.java @@ -42,7 +42,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; /******************************************************************************* - ** Unit test for BulkInsertPrepareMappingStep + ** Unit test for BulkInsertPrepareMappingStep *******************************************************************************/ class BulkInsertPrepareFileMappingStepTest extends BaseTest { @@ -90,8 +90,8 @@ class BulkInsertPrepareFileMappingStepTest extends BaseTest { String fileName = "personFile.csv"; - StorageInput storageInput = new StorageInput(TestUtils.TABLE_NAME_MEMORY_STORAGE).withReference(fileName); - OutputStream outputStream = new StorageAction().createOutputStream(storageInput); + StorageInput storageInput = new StorageInput(TestUtils.TABLE_NAME_MEMORY_STORAGE).withReference(fileName); + OutputStream outputStream = new StorageAction().createOutputStream(storageInput); outputStream.write(""" name,noOfShoes John,2 @@ -104,15 +104,51 @@ class BulkInsertPrepareFileMappingStepTest extends BaseTest runProcessInput.addValue("tableName", TestUtils.TABLE_NAME_PERSON_MEMORY); runProcessInput.addValue("prepopulatedValues", JsonUtils.toJson(Map.of("homeStateId", 1))); - RunBackendStepInput runBackendStepInput = new RunBackendStepInput(runProcessInput.getProcessState()); + RunBackendStepInput runBackendStepInput = new RunBackendStepInput(runProcessInput.getProcessState()); RunBackendStepOutput runBackendStepOutput = new RunBackendStepOutput(); new BulkInsertPrepareFileMappingStep().run(runBackendStepInput, runBackendStepOutput); - BulkLoadProfile bulkLoadProfile = (BulkLoadProfile) runBackendStepOutput.getValue("suggestedBulkLoadProfile"); - Optional homeStateId = bulkLoadProfile.getFieldList().stream().filter(f -> f.getFieldName().equals("homeStateId")).findFirst(); + BulkLoadProfile bulkLoadProfile = (BulkLoadProfile) runBackendStepOutput.getValue("suggestedBulkLoadProfile"); + Optional homeStateId = bulkLoadProfile.getFieldList().stream().filter(f -> f.getFieldName().equals("homeStateId")).findFirst(); assertThat(homeStateId).isPresent(); assertEquals("1", homeStateId.get().getDefaultValue()); } -} \ No newline at end of file + + + /******************************************************************************* + ** + *******************************************************************************/ + @Test + void testUpdateWithFile() throws Exception + { + String fileName = "personFile.csv"; + + StorageInput storageInput = new StorageInput(TestUtils.TABLE_NAME_MEMORY_STORAGE).withReference(fileName); + OutputStream outputStream = new StorageAction().createOutputStream(storageInput); + outputStream.write(""" + name,noOfShoes + John,2 + Jane,4 + """.getBytes(StandardCharsets.UTF_8)); + outputStream.close(); + + RunProcessInput runProcessInput = new RunProcessInput(); + BulkInsertStepUtils.setStorageInputForTheFile(runProcessInput, storageInput); + runProcessInput.addValue("isBulkEdit", "true"); + runProcessInput.addValue("tableName", TestUtils.TABLE_NAME_PERSON_MEMORY); + runProcessInput.addValue("prepopulatedValues", JsonUtils.toJson(Map.of("homeStateId", 1))); + + RunBackendStepInput runBackendStepInput = new RunBackendStepInput(runProcessInput.getProcessState()); + RunBackendStepOutput runBackendStepOutput = new RunBackendStepOutput(); + + new BulkInsertPrepareFileMappingStep().run(runBackendStepInput, runBackendStepOutput); + + BulkLoadProfile bulkLoadProfile = (BulkLoadProfile) runBackendStepOutput.getValue("suggestedBulkLoadProfile"); + Optional homeStateId = bulkLoadProfile.getFieldList().stream().filter(f -> f.getFieldName().equals("homeStateId")).findFirst(); + assertThat(homeStateId).isPresent(); + assertEquals("1", homeStateId.get().getDefaultValue()); + } + +} diff --git a/qqq-backend-core/src/test/java/com/kingsrook/qqq/backend/core/processes/implementations/bulk/insert/BulkInsertPrepareFileUploadStepTest.java b/qqq-backend-core/src/test/java/com/kingsrook/qqq/backend/core/processes/implementations/bulk/insert/BulkInsertPrepareFileUploadStepTest.java new file mode 100644 index 00000000..c028e375 --- /dev/null +++ b/qqq-backend-core/src/test/java/com/kingsrook/qqq/backend/core/processes/implementations/bulk/insert/BulkInsertPrepareFileUploadStepTest.java @@ -0,0 +1,113 @@ +/* + * QQQ - Low-code Application Framework for Engineers. + * Copyright (C) 2021-2024. Kingsrook, LLC + * 651 N Broad St Ste 205 # 6917 | Middletown DE 19709 | United States + * contact@kingsrook.com + * https://github.com/Kingsrook/ + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +package com.kingsrook.qqq.backend.core.processes.implementations.bulk.insert; + + +import com.kingsrook.qqq.backend.core.BaseTest; +import com.kingsrook.qqq.backend.core.exceptions.QException; +import com.kingsrook.qqq.backend.core.model.actions.processes.RunBackendStepInput; +import com.kingsrook.qqq.backend.core.model.actions.processes.RunBackendStepOutput; +import com.kingsrook.qqq.backend.core.processes.implementations.bulk.insert.model.BulkLoadTableStructure; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + + +/******************************************************************************* + ** Unit test for BulkInsertPrepareFileUploadStepTest + *******************************************************************************/ +public class BulkInsertPrepareFileUploadStepTest extends BaseTest +{ + private BulkInsertPrepareFileUploadStep step; + private RunBackendStepInput input; + private RunBackendStepOutput output; + + + + /******************************************************************************* + ** + *******************************************************************************/ + @BeforeEach + void setup() + { + step = new BulkInsertPrepareFileUploadStep(); + input = new RunBackendStepInput(); + output = new RunBackendStepOutput(); + + input.setProcessName("person.bulkInsertWithFile"); + input.addValue("tableName", "person"); + } + + + + /******************************************************************************* + ** + *******************************************************************************/ + @Test + void testRun_bulkInsert_setsExpectedOutput() throws QException + { + step.run(input, output); + + assertNotNull(output.getValue("tableStructure")); + assertTrue(output.getValue("tableStructure") instanceof BulkLoadTableStructure); + assertEquals(false, output.getValue("isBulkEdit")); + assertNotNull(output.getValue("upload.html")); + assertTrue(output.getValue("upload.html").toString().contains("File Upload Instructions")); + } + + + + /******************************************************************************* + ** + *******************************************************************************/ + @Test + void testRun_bulkEdit_setsExpectedOutput() throws QException + { + input.setProcessName("person.bulkEditWithFile"); + + step.run(input, output); + + assertEquals(true, output.getValue("isBulkEdit")); + assertNotNull(output.getValue("upload.html")); + String html = output.getValue("upload.html").toString(); + assertTrue(html.contains("edit in the")); + assertTrue(html.contains("Person.csv")); + } + + + + /******************************************************************************* + ** + *******************************************************************************/ + @Test + void testRun_stepBackClearsFile() throws QException + { + output.getProcessState().setIsStepBack(true); + output.addValue("theFile", "something"); + + step.run(input, output); + + assertNull(output.getValue("theFile")); + } +}