deps: Refactor dependencies (#3224)

* remove spring dep
move junit, logging, mockito under dep mgmt

* upgrade anti-corruption-layer deps

* async method invocation

* balking, bloc

* bridge to bytecode

* caching

* callback - cqrs

* component - health check

* hexagonal - metadata mapping

* rest of the patterns

* remove checkstyle, take spotless into use
This commit is contained in:
Ilkka Seppälä
2025-03-29 19:34:27 +02:00
committed by GitHub
parent 371439aeaa
commit 0ca162a55c
1863 changed files with 14408 additions and 17637 deletions
@@ -27,14 +27,14 @@ package com.iluwatar;
import java.time.LocalDate;
/**
* The notification pattern captures information passed between layers, validates the information, and returns
* any errors to the presentation layer if needed.
* The notification pattern captures information passed between layers, validates the information,
* and returns any errors to the presentation layer if needed.
*
* <p>In this code, this pattern is implemented through the example of a form being submitted to register
* a worker. The worker inputs their name, occupation, and date of birth to the RegisterWorkerForm (which acts
* as our presentation layer), and passes it to the RegisterWorker class (our domain layer) which validates it.
* Any errors caught by the domain layer are then passed back to the presentation layer through the
* RegisterWorkerDto.</p>
* <p>In this code, this pattern is implemented through the example of a form being submitted to
* register a worker. The worker inputs their name, occupation, and date of birth to the
* RegisterWorkerForm (which acts as our presentation layer), and passes it to the RegisterWorker
* class (our domain layer) which validates it. Any errors caught by the domain layer are then
* passed back to the presentation layer through the RegisterWorkerDto.
*/
public class App {
@@ -46,5 +46,4 @@ public class App {
var form = new RegisterWorkerForm(NAME, OCCUPATION, DATE_OF_BIRTH);
form.submit();
}
}
@@ -28,13 +28,12 @@ import lombok.Getter;
import lombok.NoArgsConstructor;
/**
* Layer super type for all Data Transfer Objects.
* Also contains code for accessing our notification.
* Layer super type for all Data Transfer Objects. Also contains code for accessing our
* notification.
*/
@Getter
@NoArgsConstructor
public class DataTransferObject {
private final Notification notification = new Notification();
}
@@ -30,9 +30,8 @@ import lombok.Getter;
import lombok.NoArgsConstructor;
/**
* The notification. Used for storing errors and any other methods
* that may be necessary for when we send information back to the
* presentation layer.
* The notification. Used for storing errors and any other methods that may be necessary for when we
* send information back to the presentation layer.
*/
@Getter
@NoArgsConstructor
@@ -28,8 +28,8 @@ import lombok.AllArgsConstructor;
import lombok.Getter;
/**
* Error class for storing information on the error.
* Error ID is not necessary, but may be useful for serialisation.
* Error class for storing information on the error. Error ID is not necessary, but may be useful
* for serialisation.
*/
@Getter
@AllArgsConstructor
@@ -29,19 +29,18 @@ import java.time.Period;
import lombok.extern.slf4j.Slf4j;
/**
* Class which handles actual internal logic and validation for worker registration.
* Part of the domain layer which collects information and sends it back to the presentation.
* Class which handles actual internal logic and validation for worker registration. Part of the
* domain layer which collects information and sends it back to the presentation.
*/
@Slf4j
public class RegisterWorker extends ServerCommand {
static final int LEGAL_AGE = 18;
protected RegisterWorker(RegisterWorkerDto worker) {
super(worker);
}
/**
* Validates the data provided and adds it to the database in the backend.
*/
/** Validates the data provided and adds it to the database in the backend. */
public void run() {
validate();
@@ -50,12 +49,10 @@ public class RegisterWorker extends ServerCommand {
}
}
/**
* Validates our data. Checks for any errors and if found, adds to notification.
*/
/** Validates our data. Checks for any errors and if found, adds to notification. */
private void validate() {
var ourData = ((RegisterWorkerDto) this.data);
//check if any of submitted data is not given
// check if any of submitted data is not given
// passing for empty value validation
fail(isNullOrBlank(ourData.getName()), RegisterWorkerDto.MISSING_NAME);
fail(isNullOrBlank(ourData.getOccupation()), RegisterWorkerDto.MISSING_OCCUPATION);
@@ -93,7 +90,7 @@ public class RegisterWorker extends ServerCommand {
* If a condition is met, adds the error to our notification.
*
* @param condition condition to check for.
* @param error error to add if condition met.
* @param error error to add if condition met.
*/
protected void fail(boolean condition, NotificationError error) {
if (condition) {
@@ -29,8 +29,8 @@ import lombok.Getter;
import lombok.Setter;
/**
* Data transfer object which stores information about the worker. This is carried between
* objects and layers to reduce the number of method calls made.
* Data transfer object which stores information about the worker. This is carried between objects
* and layers to reduce the number of method calls made.
*/
@Getter
@Setter
@@ -39,30 +39,20 @@ public class RegisterWorkerDto extends DataTransferObject {
private String occupation;
private LocalDate dateOfBirth;
/**
* Error for when name field is blank or missing.
*/
public static final NotificationError MISSING_NAME =
new NotificationError(1, "Name is missing");
/** Error for when name field is blank or missing. */
public static final NotificationError MISSING_NAME = new NotificationError(1, "Name is missing");
/**
* Error for when occupation field is blank or missing.
*/
/** Error for when occupation field is blank or missing. */
public static final NotificationError MISSING_OCCUPATION =
new NotificationError(2, "Occupation is missing");
new NotificationError(2, "Occupation is missing");
/**
* Error for when date of birth field is blank or missing.
*/
/** Error for when date of birth field is blank or missing. */
public static final NotificationError MISSING_DOB =
new NotificationError(3, "Date of birth is missing");
new NotificationError(3, "Date of birth is missing");
/**
* Error for when date of birth is less than 18 years ago.
*/
/** Error for when date of birth is less than 18 years ago. */
public static final NotificationError DOB_TOO_SOON =
new NotificationError(4, "Worker registered must be over 18");
new NotificationError(4, "Worker registered must be over 18");
protected RegisterWorkerDto() {
super();
@@ -71,8 +61,8 @@ public class RegisterWorkerDto extends DataTransferObject {
/**
* Simple set up function for capturing our worker information.
*
* @param name Name of the worker
* @param occupation occupation of the worker
* @param name Name of the worker
* @param occupation occupation of the worker
* @param dateOfBirth Date of Birth of the worker
*/
public void setupWorkerDto(String name, String occupation, LocalDate dateOfBirth) {
@@ -28,9 +28,8 @@ import java.time.LocalDate;
import lombok.extern.slf4j.Slf4j;
/**
* The form submitted by the user, part of the presentation layer,
* linked to the domain layer through a data transfer object and
* linked to the service layer directly.
* The form submitted by the user, part of the presentation layer, linked to the domain layer
* through a data transfer object and linked to the service layer directly.
*/
@Slf4j
public class RegisterWorkerForm {
@@ -41,10 +40,10 @@ public class RegisterWorkerForm {
RegisterWorkerService service = new RegisterWorkerService();
/**
* Constructor.
* Constructor.
*
* @param name Name of the worker
* @param occupation occupation of the worker
* @param name Name of the worker
* @param occupation occupation of the worker
* @param dateOfBirth Date of Birth of the worker
*/
public RegisterWorkerForm(String name, String occupation, LocalDate dateOfBirth) {
@@ -53,16 +52,14 @@ public class RegisterWorkerForm {
this.dateOfBirth = dateOfBirth;
}
/**
* Attempts to submit the form for registering a worker.
*/
/** Attempts to submit the form for registering a worker. */
public void submit() {
//Transmit information to our transfer object to communicate between layers
// Transmit information to our transfer object to communicate between layers
saveToWorker();
//call the service layer to register our worker
// call the service layer to register our worker
service.registerWorker(worker);
//check for any errors
// check for any errors
if (worker.getNotification().hasErrors()) {
indicateErrors();
LOGGER.info("Not registered, see errors");
@@ -71,9 +68,7 @@ public class RegisterWorkerForm {
}
}
/**
* Saves worker information to the data transfer object.
*/
/** Saves worker information to the data transfer object. */
private void saveToWorker() {
worker = new RegisterWorkerDto();
worker.setName(name);
@@ -81,9 +76,7 @@ public class RegisterWorkerForm {
worker.setDateOfBirth(dateOfBirth);
}
/**
* Check for any errors with form submission and show them to the user.
*/
/** Check for any errors with form submission and show them to the user. */
public void indicateErrors() {
worker.getNotification().getErrors().forEach(error -> LOGGER.error(error.toString()));
}
@@ -25,13 +25,13 @@
package com.iluwatar;
/**
* Service used to register a worker.
* This represents the basic framework of a service layer which can be built upon.
* Service used to register a worker. This represents the basic framework of a service layer which
* can be built upon.
*/
public class RegisterWorkerService {
/**
* Creates and runs a command object to do the work needed,
* in this case, register a worker in the system.
* Creates and runs a command object to do the work needed, in this case, register a worker in the
* system.
*
* @param registration worker to be registered if possible
*/
@@ -27,8 +27,8 @@ package com.iluwatar;
import lombok.AllArgsConstructor;
/**
* Stores the dto and access the notification within it.
* Acting as a layer supertype in this instance for the domain layer.
* Stores the dto and access the notification within it. Acting as a layer supertype in this
* instance for the domain layer.
*/
@AllArgsConstructor
public class ServerCommand {
@@ -24,15 +24,14 @@
*/
package com.iluwatar;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import org.junit.jupiter.api.Test;
class AppTest {
@Test
void shouldExecuteApplicationWithoutException() {
assertDoesNotThrow(() -> App.main(new String[]{}));
assertDoesNotThrow(() -> App.main(new String[] {}));
}
}
@@ -33,42 +33,41 @@ import org.junit.jupiter.api.Test;
class RegisterWorkerFormTest {
private RegisterWorkerForm registerWorkerForm;
private RegisterWorkerForm registerWorkerForm;
@Test
void submitSuccessfully() {
// Ensure the worker is null initially
registerWorkerForm = new RegisterWorkerForm("John Doe", "Engineer", LocalDate.of(1990, 1, 1));
@Test
void submitSuccessfully() {
// Ensure the worker is null initially
registerWorkerForm = new RegisterWorkerForm("John Doe", "Engineer", LocalDate.of(1990, 1, 1));
assertNull(registerWorkerForm.worker);
assertNull(registerWorkerForm.worker);
// Submit the form
registerWorkerForm.submit();
// Submit the form
registerWorkerForm.submit();
// Verify that the worker is not null after submission
assertNotNull(registerWorkerForm.worker);
// 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());
}
// 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);
@Test
void submitWithErrors() {
// Set up the worker with a notification containing errors
registerWorkerForm = new RegisterWorkerForm(null, null, null);
// Submit the form
registerWorkerForm.submit();
// 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);
}
// 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);
}
}
@@ -24,91 +24,97 @@
*/
package com.iluwatar;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
import java.time.LocalDate;
import static org.junit.jupiter.api.Assertions.*;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
@Slf4j
class RegisterWorkerTest {
@Test
void runSuccessfully() {
RegisterWorkerDto validWorkerDto = createValidWorkerDto();
validWorkerDto.setupWorkerDto("name", "occupation", LocalDate.of(2000, 12, 1));
RegisterWorker registerWorker = new RegisterWorker(validWorkerDto);
@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();
// Run the registration process
registerWorker.run();
// Verify that there are no errors in the notification
assertFalse(registerWorker.getNotification().hasErrors());
}
// 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);
@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();
// 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);
}
// 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);
@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();
// 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);
}
// 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);
@Test
void runWithMissingDOB() {
RegisterWorkerDto workerDto = createValidWorkerDto();
workerDto.setupWorkerDto("name", "occupation", null);
RegisterWorker registerWorker = new RegisterWorker(workerDto);
// Run the registration process
registerWorker.run();
// 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);
}
// 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);
@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();
// 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);
}
// 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();
}
private RegisterWorkerDto createValidWorkerDto() {
return new RegisterWorkerDto();
}
}