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.
This commit is contained in:
Teriuihi 2024-01-14 11:09:35 +01:00
parent 0464281a38
commit 917965a33d

View File

@ -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<Form> optionalForm = result.form();
Assertions.assertTrue(optionalForm.isPresent());
Form form = optionalForm.get();
Assertions.assertInstanceOf(ContactFormData.class, form);
});
}
}