feature: #1299 Add Identity Map Pattern (#2094)

* #1299 IMPLEMENT IDENTITY MAP PATTERN.

* #1299 Add docstrings, README, testCases and class diagram.

* #1299 Update a comment string in DB implementation.

* #1299 Fix code smells.

* #1299 Fix code smells and add comments to App.java

* #1299 Update constant name.

* #1299 Remove java version dependency and update README.md.

* #1299 Add lombok to PersonFinder.java.

* #1299 Add dependency to maven-assembly-plugin.

* #1299 Use java streams in PersonDbSimulatorImplementation.java.

* #1299 Add print statements while returning the person object.

* #1299 Update README.md.

* #1299 Add puml file.

* Update README.md
This commit is contained in:
u7275858
2022-11-10 06:55:44 +11:00
committed by GitHub
parent 3831a82b69
commit d2599a2904
17 changed files with 1176 additions and 0 deletions
@@ -0,0 +1,35 @@
/*
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
*
* The MIT License
* Copyright © 2014-2022 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.identitymap;
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,75 @@
/*
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
*
* The MIT License
* Copyright © 2014-2022 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.identitymap;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
class IdentityMapTest {
@Test
void addToMap(){
// new instance of an identity map(not connected to any DB here)
IdentityMap idMap = new IdentityMap();
// Dummy person instances
Person person1 = new Person(11, "Michael", 27304159);
Person person2 = new Person(22, "John", 42273631);
Person person3 = new Person(33, "Arthur", 27489171);
Person person4 = new Person(44, "Finn", 20499078);
// id already in map
Person person5 = new Person(11, "Michael", 40599078);
// All records go into identity map
idMap.addPerson(person1);
idMap.addPerson(person2);
idMap.addPerson(person3);
idMap.addPerson(person4);
idMap.addPerson(person5);
// Test no duplicate in our Map.
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");
}
@Test
void testGetFromMap() {
// new instance of an identity map(not connected to any DB here)
IdentityMap idMap = new IdentityMap();
// Dummy person instances
Person person1 = new Person(11, "Michael", 27304159);
Person person2 = new Person(22, "John", 42273631);
Person person3 = new Person(33, "Arthur", 27489171);
Person person4 = new Person(44, "Finn", 20499078);
Person person5 = new Person(55, "Michael", 40599078);
// All records go into identity map
idMap.addPerson(person1);
idMap.addPerson(person2);
idMap.addPerson(person3);
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");
// Test for person with given id not in map
Assertions.assertNull(idMap.getPerson(1), "Incorrect person record returned");
}
}
@@ -0,0 +1,122 @@
/*
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
*
* The MIT License
* Copyright © 2014-2022 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.identitymap;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
class PersonDbSimulatorImplementationTest {
@Test
void testInsert(){
// DataBase initialization.
PersonDbSimulatorImplementation db = new PersonDbSimulatorImplementation();
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);
Person person3 = new Person(3, "Arthur", 27489171);
db.insert(person1);
db.insert(person2);
db.insert(person3);
// Test size after insertion.
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);
db.insert(person5duplicate);
// Test size after attempt to insert record with duplicate key.
Assertions.assertEquals(5,db.size(),"Incorrect size for data base");
}
@Test
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));
}
@Test
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.");
}
@Test
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);
// Test if IdNotFoundException is thrown when person with ID 3 is not in DB.
Assertions.assertThrows(IdNotFoundException.class,()->db.update(person3));
}
@Test
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);
db.update(person);
Assertions.assertEquals(person,db.find(2),"Incorrect update.");
}
@Test
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));
}
@Test
void deleteInDb(){
PersonDbSimulatorImplementation db = new PersonDbSimulatorImplementation();
Person person1 = new Person(1, "Thomas", 27304159);
Person person2 = new Person(2, "John", 42273631);
db.insert(person1);
db.insert(person2);
// delete the record.
db.delete(1);
// test size of database after deletion.
Assertions.assertEquals(1,db.size(),"Size after deletion is incorrect.");
// try to find deleted record in db.
Assertions.assertThrows(IdNotFoundException.class,()->db.find(1));
}
}
@@ -0,0 +1,112 @@
/*
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
*
* The MIT License
* Copyright © 2014-2022 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.identitymap;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
class PersonFinderTest {
@Test
void personFoundInDB(){
// personFinderInstance
PersonFinder personFinder = new PersonFinder();
// init database for our personFinder
PersonDbSimulatorImplementation db = new PersonDbSimulatorImplementation();
// Dummy persons
Person person1 = new Person(1, "John", 27304159);
Person person2 = new Person(2, "Thomas", 42273631);
Person person3 = new Person(3, "Arthur", 27489171);
Person person4 = new Person(4, "Finn", 20499078);
Person person5 = new Person(5, "Michael", 40599078);
// Add data to the database.
db.insert(person1);
db.insert(person2);
db.insert(person3);
db.insert(person4);
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.");
}
@Test
void personFoundInIdMap(){
// personFinderInstance
PersonFinder personFinder = new PersonFinder();
// init database for our personFinder
PersonDbSimulatorImplementation db = new PersonDbSimulatorImplementation();
// Dummy persons
Person person1 = new Person(1, "John", 27304159);
Person person2 = new Person(2, "Thomas", 42273631);
Person person3 = new Person(3, "Arthur", 27489171);
Person person4 = new Person(4, "Finn", 20499078);
Person person5 = new Person(5, "Michael", 40599078);
// Add data to the database.
db.insert(person1);
db.insert(person2);
db.insert(person3);
db.insert(person4);
db.insert(person5);
personFinder.setDb(db);
// 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.");
// 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.");
}
@Test
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));
// Dummy persons
Person person1 = new Person(1, "John", 27304159);
Person person2 = new Person(2, "Thomas", 42273631);
Person person3 = new Person(3, "Arthur", 27489171);
Person person4 = new Person(4, "Finn", 20499078);
Person person5 = new Person(5, "Michael", 40599078);
db.insert(person1);
db.insert(person2);
db.insert(person3);
db.insert(person4);
db.insert(person5);
personFinder.setDb(db);
// Assure that the database has been updated.
Assertions.assertEquals(person4,personFinder.getPerson(4),"Find returns incorrect record");
// Assure key is in DB now.
Assertions.assertDoesNotThrow(()->personFinder.getPerson(1));
// Assure key not in DB.
Assertions.assertThrows(IdNotFoundException.class,()->personFinder.getPerson(6));
}
}
@@ -0,0 +1,43 @@
/*
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
*
* The MIT License
* Copyright © 2014-2022 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.identitymap;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
class PersonTest {
@Test
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 with duplicate nationalID.
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");
}
}