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 14403 additions and 17632 deletions
@@ -27,12 +27,12 @@ package com.iluwatar.identitymap;
import lombok.extern.slf4j.Slf4j;
/**
* The basic idea behind the Identity Map is to have a series of maps containing objects that have been pulled from the database.
* The below example demonstrates the identity map pattern by creating a sample DB.
* Since only 1 DB has been created we only have 1 map corresponding to it for the purpose of this demo.
* When you load an object from the database, you first check the map.
* If theres an object in it that corresponds to the one youre loading, you return it. If not, you go to the database,
* putting the objects on the map for future reference as you load them.
* The basic idea behind the Identity Map is to have a series of maps containing objects that have
* been pulled from the database. The below example demonstrates the identity map pattern by
* creating a sample DB. Since only 1 DB has been created we only have 1 map corresponding to it for
* the purpose of this demo. When you load an object from the database, you first check the map. If
* theres an object in it that corresponds to the one youre loading, you return it. If not, you go
* to the database, putting the objects on the map for future reference as you load them.
*/
@Slf4j
public class App {
@@ -24,9 +24,7 @@
*/
package com.iluwatar.identitymap;
/**
* Using Runtime Exception to control the flow in case Person Id doesn't exist.
*/
/** Using Runtime Exception to control the flow in case Person Id doesn't exist. */
public class IdNotFoundException extends RuntimeException {
public IdNotFoundException(final String message) {
super(message);
@@ -30,20 +30,20 @@ import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
/**
* This class stores the map into which we will be caching records after loading them from a DataBase.
* Stores the records as a Hash Map with the personNationalIDs as keys.
* This class stores the map into which we will be caching records after loading them from a
* DataBase. Stores the records as a Hash Map with the personNationalIDs as keys.
*/
@Slf4j
@Getter
public class IdentityMap {
private Map<Integer, Person> personMap = new HashMap<>();
/**
* Add person to the map.
*/
/** Add person to the map. */
public void addPerson(Person person) {
if (!personMap.containsKey(person.getPersonNationalId())) {
personMap.put(person.getPersonNationalId(), person);
} else { // Ensure that addPerson does not update a record. This situation will never arise in our implementation. Added only for testing purposes.
} else { // Ensure that addPerson does not update a record. This situation will never arise in
// our implementation. Added only for testing purposes.
LOGGER.info("Key already in Map");
}
}
@@ -63,14 +63,11 @@ public class IdentityMap {
return person;
}
/**
* Get the size of the map.
*/
/** Get the size of the map. */
public int size() {
if (personMap == null) {
return 0;
}
return personMap.size();
}
}
@@ -31,28 +31,27 @@ import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
/**
* Person definition.
*/
/** Person definition. */
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
@Getter
@Setter
@AllArgsConstructor
public final class Person implements Serializable {
@Serial
private static final long serialVersionUID = 1L;
@Serial private static final long serialVersionUID = 1L;
@EqualsAndHashCode.Include
private int personNationalId;
@EqualsAndHashCode.Include private int personNationalId;
private String name;
private long phoneNum;
@Override
public String toString() {
return "Person ID is : " + personNationalId + " ; Person Name is : " + name + " ; Phone Number is :" + phoneNum;
return "Person ID is : "
+ personNationalId
+ " ; Person Name is : "
+ name
+ " ; Phone Number is :"
+ phoneNum;
}
}
@@ -24,9 +24,7 @@
*/
package com.iluwatar.identitymap;
/**
* Simulator interface for Person DB.
*/
/** Simulator interface for Person DB. */
public interface PersonDbSimulator {
Person find(int personNationalId);
@@ -35,5 +33,4 @@ public interface PersonDbSimulator {
void update(Person person);
void delete(int personNationalId);
}
@@ -30,26 +30,26 @@ import java.util.Optional;
import lombok.extern.slf4j.Slf4j;
/**
* This is a sample database implementation. The database is in the form of an arraylist which stores records of
* different persons. The personNationalId acts as the primary key for a record.
* Operations :
* -> find (look for object with a particular ID)
* -> insert (insert record for a new person into the database)
* -> update (update the record of a person). To do this, create a new person instance with the same ID as the record you
* want to update. Then call this method with that person as an argument.
* -> delete (delete the record for a particular ID)
* This is a sample database implementation. The database is in the form of an arraylist which
* stores records of different persons. The personNationalId acts as the primary key for a record.
* Operations : -> find (look for object with a particular ID) -> insert (insert record for a new
* person into the database) -> update (update the record of a person). To do this, create a new
* person instance with the same ID as the record you want to update. Then call this method with
* that person as an argument. -> delete (delete the record for a particular ID)
*/
@Slf4j
public class PersonDbSimulatorImplementation implements PersonDbSimulator {
//This simulates a table in the database. To extend logic to multiple tables just add more lists to the implementation.
// This simulates a table in the database. To extend logic to multiple tables just add more lists
// to the implementation.
private List<Person> personList = new ArrayList<>();
static final String NOT_IN_DATA_BASE = " not in DataBase";
static final String ID_STR = "ID : ";
@Override
public Person find(int personNationalId) throws IdNotFoundException {
Optional<Person> elem = personList.stream().filter(p -> p.getPersonNationalId() == personNationalId).findFirst();
Optional<Person> elem =
personList.stream().filter(p -> p.getPersonNationalId() == personNationalId).findFirst();
if (elem.isEmpty()) {
throw new IdNotFoundException(ID_STR + personNationalId + NOT_IN_DATA_BASE);
}
@@ -59,7 +59,10 @@ public class PersonDbSimulatorImplementation implements PersonDbSimulator {
@Override
public void insert(Person person) {
Optional<Person> elem = personList.stream().filter(p -> p.getPersonNationalId() == person.getPersonNationalId()).findFirst();
Optional<Person> elem =
personList.stream()
.filter(p -> p.getPersonNationalId() == person.getPersonNationalId())
.findFirst();
if (elem.isPresent()) {
LOGGER.info("Record already exists.");
return;
@@ -69,7 +72,10 @@ public class PersonDbSimulatorImplementation implements PersonDbSimulator {
@Override
public void update(Person person) throws IdNotFoundException {
Optional<Person> elem = personList.stream().filter(p -> p.getPersonNationalId() == person.getPersonNationalId()).findFirst();
Optional<Person> elem =
personList.stream()
.filter(p -> p.getPersonNationalId() == person.getPersonNationalId())
.findFirst();
if (elem.isPresent()) {
elem.get().setName(person.getName());
elem.get().setPhoneNum(person.getPhoneNum());
@@ -85,7 +91,8 @@ public class PersonDbSimulatorImplementation implements PersonDbSimulator {
* @param id : personNationalId for person whose record is to be deleted.
*/
public void delete(int id) throws IdNotFoundException {
Optional<Person> elem = personList.stream().filter(p -> p.getPersonNationalId() == id).findFirst();
Optional<Person> elem =
personList.stream().filter(p -> p.getPersonNationalId() == id).findFirst();
if (elem.isPresent()) {
personList.remove(elem.get());
LOGGER.info("Record deleted successfully.");
@@ -94,14 +101,11 @@ public class PersonDbSimulatorImplementation implements PersonDbSimulator {
throw new IdNotFoundException(ID_STR + id + NOT_IN_DATA_BASE);
}
/**
* Return the size of the database.
*/
/** Return the size of the database. */
public int size() {
if (personList == null) {
return 0;
}
return personList.size();
}
}
@@ -29,11 +29,11 @@ import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
/**
* Any object of this class stores a DataBase and an Identity Map. When we try to look for a key we first check if
* it has been cached in the Identity Map and return it if it is indeed in the map.
* If that is not the case then go to the DataBase, get the record, store it in the
* Identity Map and then return the record. Now if we look for the record again we will find it in the table itself which
* will make lookup faster.
* Any object of this class stores a DataBase and an Identity Map. When we try to look for a key we
* first check if it has been cached in the Identity Map and return it if it is indeed in the map.
* If that is not the case then go to the DataBase, get the record, store it in the Identity Map and
* then return the record. Now if we look for the record again we will find it in the table itself
* which will make lookup faster.
*/
@Slf4j
@Getter
@@ -43,6 +43,7 @@ public class PersonFinder {
// Access to the Identity Map
private IdentityMap identityMap = new IdentityMap();
private PersonDbSimulatorImplementation db = new PersonDbSimulatorImplementation();
/**
* get person corresponding to input ID.
*
@@ -23,13 +23,14 @@
* THE SOFTWARE.
*/
package com.iluwatar.identitymap;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
class AppTest {
import org.junit.jupiter.api.Test;
class AppTest {
@Test
void shouldExecuteApplicationWithoutException() {
assertDoesNotThrow(() -> App.main(new String[]{}));
assertDoesNotThrow(() -> App.main(new String[] {}));
}
}
@@ -29,7 +29,7 @@ import org.junit.jupiter.api.Test;
class IdentityMapTest {
@Test
void addToMap(){
void addToMap() {
// new instance of an identity map(not connected to any DB here)
IdentityMap idMap = new IdentityMap();
// Dummy person instances
@@ -46,10 +46,12 @@ class IdentityMapTest {
idMap.addPerson(person4);
idMap.addPerson(person5);
// Test no duplicate in our Map.
Assertions.assertEquals(4,idMap.size(),"Size of the map is incorrect");
Assertions.assertEquals(4, idMap.size(), "Size of the map is incorrect");
// Test record not updated by add method.
Assertions.assertEquals(27304159,idMap.getPerson(11).getPhoneNum(),"Incorrect return value for phone number");
Assertions.assertEquals(
27304159, idMap.getPerson(11).getPhoneNum(), "Incorrect return value for phone number");
}
@Test
void testGetFromMap() {
// new instance of an identity map(not connected to any DB here)
@@ -67,8 +69,8 @@ class IdentityMapTest {
idMap.addPerson(person4);
idMap.addPerson(person5);
// Test for dummy persons in the map
Assertions.assertEquals(person1,idMap.getPerson(11),"Incorrect person record returned");
Assertions.assertEquals(person4,idMap.getPerson(44),"Incorrect person record returned");
Assertions.assertEquals(person1, idMap.getPerson(11), "Incorrect person record returned");
Assertions.assertEquals(person4, idMap.getPerson(44), "Incorrect person record returned");
// Test for person with given id not in map
Assertions.assertNull(idMap.getPerson(1), "Incorrect person record returned");
}
@@ -29,10 +29,10 @@ import org.junit.jupiter.api.Test;
class PersonDbSimulatorImplementationTest {
@Test
void testInsert(){
void testInsert() {
// DataBase initialization.
PersonDbSimulatorImplementation db = new PersonDbSimulatorImplementation();
Assertions.assertEquals(0,db.size(),"Size of null database should be 0");
Assertions.assertEquals(0, db.size(), "Size of null database should be 0");
// Dummy persons.
Person person1 = new Person(1, "Thomas", 27304159);
Person person2 = new Person(2, "John", 42273631);
@@ -41,71 +41,77 @@ class PersonDbSimulatorImplementationTest {
db.insert(person2);
db.insert(person3);
// Test size after insertion.
Assertions.assertEquals(3,db.size(),"Incorrect size for database.");
Assertions.assertEquals(3, db.size(), "Incorrect size for database.");
Person person4 = new Person(4, "Finn", 20499078);
Person person5 = new Person(5, "Michael", 40599078);
db.insert(person4);
db.insert(person5);
// Test size after more insertions.
Assertions.assertEquals(5,db.size(),"Incorrect size for database.");
Person person5duplicate = new Person(5,"Kevin",89589122);
Assertions.assertEquals(5, db.size(), "Incorrect size for database.");
Person person5duplicate = new Person(5, "Kevin", 89589122);
db.insert(person5duplicate);
// Test size after attempt to insert record with duplicate key.
Assertions.assertEquals(5,db.size(),"Incorrect size for data base");
Assertions.assertEquals(5, db.size(), "Incorrect size for data base");
}
@Test
void findNotInDb(){
void findNotInDb() {
PersonDbSimulatorImplementation db = new PersonDbSimulatorImplementation();
Person person1 = new Person(1, "Thomas", 27304159);
Person person2 = new Person(2, "John", 42273631);
db.insert(person1);
db.insert(person2);
// Test if IdNotFoundException is thrown where expected.
Assertions.assertThrows(IdNotFoundException.class,()->db.find(3));
Assertions.assertThrows(IdNotFoundException.class, () -> db.find(3));
}
@Test
void findInDb(){
void findInDb() {
PersonDbSimulatorImplementation db = new PersonDbSimulatorImplementation();
Person person1 = new Person(1, "Thomas", 27304159);
Person person2 = new Person(2, "John", 42273631);
db.insert(person1);
db.insert(person2);
Assertions.assertEquals(person2,db.find(2),"Record that was found was incorrect.");
Assertions.assertEquals(person2, db.find(2), "Record that was found was incorrect.");
}
@Test
void updateNotInDb(){
void updateNotInDb() {
PersonDbSimulatorImplementation db = new PersonDbSimulatorImplementation();
Person person1 = new Person(1, "Thomas", 27304159);
Person person2 = new Person(2, "John", 42273631);
db.insert(person1);
db.insert(person2);
Person person3 = new Person(3,"Micheal",25671234);
Person person3 = new Person(3, "Micheal", 25671234);
// Test if IdNotFoundException is thrown when person with ID 3 is not in DB.
Assertions.assertThrows(IdNotFoundException.class,()->db.update(person3));
Assertions.assertThrows(IdNotFoundException.class, () -> db.update(person3));
}
@Test
void updateInDb(){
void updateInDb() {
PersonDbSimulatorImplementation db = new PersonDbSimulatorImplementation();
Person person1 = new Person(1, "Thomas", 27304159);
Person person2 = new Person(2, "John", 42273631);
db.insert(person1);
db.insert(person2);
Person person = new Person(2,"Thomas",42273690);
Person person = new Person(2, "Thomas", 42273690);
db.update(person);
Assertions.assertEquals(person,db.find(2),"Incorrect update.");
Assertions.assertEquals(person, db.find(2), "Incorrect update.");
}
@Test
void deleteNotInDb(){
void deleteNotInDb() {
PersonDbSimulatorImplementation db = new PersonDbSimulatorImplementation();
Person person1 = new Person(1, "Thomas", 27304159);
Person person2 = new Person(2, "John", 42273631);
db.insert(person1);
db.insert(person2);
// Test if IdNotFoundException is thrown when person with this ID not in DB.
Assertions.assertThrows(IdNotFoundException.class,()->db.delete(3));
Assertions.assertThrows(IdNotFoundException.class, () -> db.delete(3));
}
@Test
void deleteInDb(){
void deleteInDb() {
PersonDbSimulatorImplementation db = new PersonDbSimulatorImplementation();
Person person1 = new Person(1, "Thomas", 27304159);
Person person2 = new Person(2, "John", 42273631);
@@ -114,9 +120,8 @@ class PersonDbSimulatorImplementationTest {
// delete the record.
db.delete(1);
// test size of database after deletion.
Assertions.assertEquals(1,db.size(),"Size after deletion is incorrect.");
Assertions.assertEquals(1, db.size(), "Size after deletion is incorrect.");
// try to find deleted record in db.
Assertions.assertThrows(IdNotFoundException.class,()->db.find(1));
Assertions.assertThrows(IdNotFoundException.class, () -> db.find(1));
}
}
@@ -29,7 +29,7 @@ import org.junit.jupiter.api.Test;
class PersonFinderTest {
@Test
void personFoundInDB(){
void personFoundInDB() {
// personFinderInstance
PersonFinder personFinder = new PersonFinder();
// init database for our personFinder
@@ -48,14 +48,20 @@ class PersonFinderTest {
db.insert(person5);
personFinder.setDb(db);
Assertions.assertEquals(person1,personFinder.getPerson(1),"Find person returns incorrect record.");
Assertions.assertEquals(person3,personFinder.getPerson(3),"Find person returns incorrect record.");
Assertions.assertEquals(person2,personFinder.getPerson(2),"Find person returns incorrect record.");
Assertions.assertEquals(person5,personFinder.getPerson(5),"Find person returns incorrect record.");
Assertions.assertEquals(person4,personFinder.getPerson(4),"Find person returns incorrect record.");
Assertions.assertEquals(
person1, personFinder.getPerson(1), "Find person returns incorrect record.");
Assertions.assertEquals(
person3, personFinder.getPerson(3), "Find person returns incorrect record.");
Assertions.assertEquals(
person2, personFinder.getPerson(2), "Find person returns incorrect record.");
Assertions.assertEquals(
person5, personFinder.getPerson(5), "Find person returns incorrect record.");
Assertions.assertEquals(
person4, personFinder.getPerson(4), "Find person returns incorrect record.");
}
@Test
void personFoundInIdMap(){
void personFoundInIdMap() {
// personFinderInstance
PersonFinder personFinder = new PersonFinder();
// init database for our personFinder
@@ -76,19 +82,20 @@ class PersonFinderTest {
// Assure key is not in the ID map.
Assertions.assertFalse(personFinder.getIdentityMap().getPersonMap().containsKey(3));
// Assure key is in the database.
Assertions.assertEquals(person3,personFinder.getPerson(3),"Finder returns incorrect record.");
Assertions.assertEquals(person3, personFinder.getPerson(3), "Finder returns incorrect record.");
// Assure that the record for this key is cached in the Map now.
Assertions.assertTrue(personFinder.getIdentityMap().getPersonMap().containsKey(3));
// Find the record again. This time it will be found in the map.
Assertions.assertEquals(person3,personFinder.getPerson(3),"Finder returns incorrect record.");
Assertions.assertEquals(person3, personFinder.getPerson(3), "Finder returns incorrect record.");
}
@Test
void personNotFoundInDB(){
void personNotFoundInDB() {
PersonFinder personFinder = new PersonFinder();
// init database for our personFinder
PersonDbSimulatorImplementation db = new PersonDbSimulatorImplementation();
personFinder.setDb(db);
Assertions.assertThrows(IdNotFoundException.class,()->personFinder.getPerson(1));
Assertions.assertThrows(IdNotFoundException.class, () -> personFinder.getPerson(1));
// Dummy persons
Person person1 = new Person(1, "John", 27304159);
Person person2 = new Person(2, "Thomas", 42273631);
@@ -102,11 +109,10 @@ class PersonFinderTest {
db.insert(person5);
personFinder.setDb(db);
// Assure that the database has been updated.
Assertions.assertEquals(person4,personFinder.getPerson(4),"Find returns incorrect record");
Assertions.assertEquals(person4, personFinder.getPerson(4), "Find returns incorrect record");
// Assure key is in DB now.
Assertions.assertDoesNotThrow(()->personFinder.getPerson(1));
Assertions.assertDoesNotThrow(() -> personFinder.getPerson(1));
// Assure key not in DB.
Assertions.assertThrows(IdNotFoundException.class,()->personFinder.getPerson(6));
Assertions.assertThrows(IdNotFoundException.class, () -> personFinder.getPerson(6));
}
}
@@ -29,15 +29,15 @@ import org.junit.jupiter.api.Test;
class PersonTest {
@Test
void testEquality(){
void testEquality() {
// dummy persons.
Person person1 = new Person(1,"Harry",989950022);
Person person2 = new Person(2,"Kane",989920011);
Assertions.assertNotEquals(person1,person2,"Incorrect equality condition");
Person person1 = new Person(1, "Harry", 989950022);
Person person2 = new Person(2, "Kane", 989920011);
Assertions.assertNotEquals(person1, person2, "Incorrect equality condition");
// person with duplicate nationalID.
Person person3 = new Person(2,"John",789012211);
Person person3 = new Person(2, "John", 789012211);
// If nationalID is equal then persons are equal(even if name or phoneNum are different).
// This situation will never arise in this implementation. Only for testing.
Assertions.assertEquals(person2,person3,"Incorrect inequality condition");
Assertions.assertEquals(person2, person3, "Incorrect inequality condition");
}
}