From 917965a33ddaf63a6ec7911563db6f6c8e531a55 Mon Sep 17 00:00:00 2001 From: Teriuihi Date: Sun, 14 Jan 2024 11:09:35 +0100 Subject: [PATCH] Add TestForm class and set up related unit tests The changes add a new TestForm class to implement unit tests for form handling in the application. The included tests verify the storing, retrieving, and verification of forms from a database, as well as form object creation and confirmation of form data. This is a step towards improving the code's reliability and making it easier to catch potential bugs or issues. --- src/test/java/TestForm.java | 40 +++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 src/test/java/TestForm.java diff --git a/src/test/java/TestForm.java b/src/test/java/TestForm.java new file mode 100644 index 0000000..06b352f --- /dev/null +++ b/src/test/java/TestForm.java @@ -0,0 +1,40 @@ +import com.alttd.forms.contact.ContactFormData; +import com.alttd.forms.contact.StoreFormQuery; +import com.alttd.forms.database.DatabaseConnection; +import com.alttd.forms.form.Form; +import com.alttd.forms.verify_mail.FormQuery; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import java.util.Optional; + +public class TestForm { + + private static int code; + + @BeforeAll + public static void testInsert() { + Assertions.assertDoesNotThrow(DatabaseConnection::initialize); + StoreFormQuery storeFormQuery = new StoreFormQuery(); + ContactFormData contactFormData = new ContactFormData("akastijn", "akastijn@alttd.com", "This is a test question."); + storeFormQuery.storeFormForVerificationCode("akastijn@alttd.com", contactFormData). + thenAccept(code -> { + TestForm.code = code; + Assertions.assertTrue(code > 999 && code < 10000); + }) + .exceptionally(throwable -> Assertions.fail()); + } + + @Test + public void testRetrieveForm() { + Assertions.assertDoesNotThrow(DatabaseConnection::initialize); + new FormQuery().getFormForCode(String.valueOf(code), "akastijn@alttd.com").thenAccept(result -> { + Assertions.assertTrue(result.failReason().isEmpty()); + Optional
optionalForm = result.form(); + Assertions.assertTrue(optionalForm.isPresent()); + Form form = optionalForm.get(); + Assertions.assertInstanceOf(ContactFormData.class, form); + }); + } +} \ No newline at end of file