feat: added notification pattern (#2629)

Co-authored-by: Ilkka Seppälä <iluwatar@users.noreply.github.com>
This commit is contained in:
sugavanesh
2024-03-09 18:16:46 +05:30
committed by GitHub
parent b2ca49a4e5
commit 249efd1e71
17 changed files with 849 additions and 0 deletions
@@ -0,0 +1,14 @@
package com.iluwatar;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
class AppTest {
@Test
void shouldExecuteApplicationWithoutException() {
assertDoesNotThrow(() -> App.main(new String[]{}));
}
}
@@ -0,0 +1,53 @@
package com.iluwatar;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.read.ListAppender;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.time.LocalDate;
import java.util.logging.Logger;
import static org.junit.jupiter.api.Assertions.*;
class RegisterWorkerFormTest {
private RegisterWorkerForm registerWorkerForm;
@Test
void submitSuccessfully() {
// Ensure the worker is null initially
registerWorkerForm = new RegisterWorkerForm("John Doe", "Engineer", LocalDate.of(1990, 1, 1));
assertNull(registerWorkerForm.worker);
// Submit the form
registerWorkerForm.submit();
// Verify that the worker is not null after submission
assertNotNull(registerWorkerForm.worker);
// Verify that the worker's properties are set correctly
assertEquals("John Doe", registerWorkerForm.worker.getName());
assertEquals("Engineer", registerWorkerForm.worker.getOccupation());
assertEquals(LocalDate.of(1990, 1, 1), registerWorkerForm.worker.getDateOfBirth());
}
@Test
void submitWithErrors() {
// Set up the worker with a notification containing errors
registerWorkerForm = new RegisterWorkerForm(null, null, null);
// Submit the form
registerWorkerForm.submit();
// Verify that the worker's properties remain unchanged
assertNull(registerWorkerForm.worker.getName());
assertNull(registerWorkerForm.worker.getOccupation());
assertNull(registerWorkerForm.worker.getDateOfBirth());
// Verify the presence of errors
assertEquals(registerWorkerForm.worker.getNotification().getErrors().size(), 4);
}
}
@@ -0,0 +1,90 @@
package com.iluwatar;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import java.time.LocalDate;
import static org.junit.jupiter.api.Assertions.*;
@Slf4j
class RegisterWorkerTest {
@Test
void runSuccessfully() {
RegisterWorkerDto validWorkerDto = createValidWorkerDto();
validWorkerDto.setupWorkerDto("name", "occupation", LocalDate.of(2000, 12, 1));
RegisterWorker registerWorker = new RegisterWorker(validWorkerDto);
// Run the registration process
registerWorker.run();
// Verify that there are no errors in the notification
assertFalse(registerWorker.getNotification().hasErrors());
}
@Test
void runWithMissingName() {
RegisterWorkerDto workerDto = createValidWorkerDto();
workerDto.setupWorkerDto(null, "occupation", LocalDate.of(2000, 12, 1));
RegisterWorker registerWorker = new RegisterWorker(workerDto);
// Run the registration process
registerWorker.run();
// Verify that the notification contains the missing name error
assertTrue(registerWorker.getNotification().hasErrors());
assertTrue(registerWorker.getNotification().getErrors().contains(RegisterWorkerDto.MISSING_NAME));
assertEquals(registerWorker.getNotification().getErrors().size(), 1);
}
@Test
void runWithMissingOccupation() {
RegisterWorkerDto workerDto = createValidWorkerDto();
workerDto.setupWorkerDto("name", null, LocalDate.of(2000, 12, 1));
RegisterWorker registerWorker = new RegisterWorker(workerDto);
// Run the registration process
registerWorker.run();
// Verify that the notification contains the missing occupation error
assertTrue(registerWorker.getNotification().hasErrors());
assertTrue(registerWorker.getNotification().getErrors().contains(RegisterWorkerDto.MISSING_OCCUPATION));
assertEquals(registerWorker.getNotification().getErrors().size(), 1);
}
@Test
void runWithMissingDOB() {
RegisterWorkerDto workerDto = createValidWorkerDto();
workerDto.setupWorkerDto("name", "occupation", null);
RegisterWorker registerWorker = new RegisterWorker(workerDto);
// Run the registration process
registerWorker.run();
// Verify that the notification contains the missing DOB error
assertTrue(registerWorker.getNotification().hasErrors());
assertTrue(registerWorker.getNotification().getErrors().contains(RegisterWorkerDto.MISSING_DOB));
assertEquals(registerWorker.getNotification().getErrors().size(), 2);
}
@Test
void runWithUnderageDOB() {
RegisterWorkerDto workerDto = createValidWorkerDto();
workerDto.setDateOfBirth(LocalDate.now().minusYears(17)); // Under 18
workerDto.setupWorkerDto("name", "occupation", LocalDate.now().minusYears(17));
RegisterWorker registerWorker = new RegisterWorker(workerDto);
// Run the registration process
registerWorker.run();
// Verify that the notification contains the underage DOB error
assertTrue(registerWorker.getNotification().hasErrors());
assertTrue(registerWorker.getNotification().getErrors().contains(RegisterWorkerDto.DOB_TOO_SOON));
assertEquals(registerWorker.getNotification().getErrors().size(), 1);
}
private RegisterWorkerDto createValidWorkerDto() {
return new RegisterWorkerDto();
}
}