1+ package com .statusneo .vms .cache ;
2+
3+ import com .statusneo .vms .model .Employee ;
4+ import com .statusneo .vms .repository .EmployeeRepository ;
5+ import org .junit .jupiter .api .BeforeEach ;
6+ import org .junit .jupiter .api .Test ;
7+ import org .junit .jupiter .api .extension .ExtendWith ;
8+ import org .mockito .Mock ;
9+ import org .mockito .junit .jupiter .MockitoExtension ;
10+
11+ import java .util .*;
12+ import java .util .stream .IntStream ;
13+
14+ import static org .junit .jupiter .api .Assertions .*;
15+ import static org .mockito .Mockito .when ;
16+
17+ @ ExtendWith (MockitoExtension .class )
18+ class EmployeeNameCacheTest {
19+
20+ @ Mock
21+ private EmployeeRepository employeeRepository ;
22+
23+ private EmployeeNameCache cache ;
24+
25+ @ BeforeEach
26+ void setup () {
27+ cache = new EmployeeNameCache (employeeRepository );
28+ }
29+
30+ private Employee emp (String name , String email ) {
31+ Employee e = new Employee ();
32+ e .setName (name );
33+ e .setEmail (email );
34+ return e ;
35+ }
36+
37+ @ Test
38+ void initializeCache_populates_from_repository_and_searches_by_prefix () {
39+ List <Employee > list = Arrays .asList (
40+ emp ("Alice Johnson" , "alice@example.com" ),
41+ emp ("Bob Smith" , "bob@example.com" ),
42+ emp ("Alicia Keys" , "alicia@example.com" )
43+ );
44+ when (employeeRepository .findAll ()).thenReturn (list );
45+
46+ cache .initializeCache ();
47+
48+ List <String > aNames = cache .getEmployeeNamesByPrefix ("Ali" );
49+ assertTrue (aNames .stream ().anyMatch (s -> s .equals ("Alice Johnson" )));
50+ assertTrue (aNames .stream ().anyMatch (s -> s .equals ("Alicia Keys" )));
51+
52+ List <Employee > employees = cache .getEmployeesByPrefix ("Bob" );
53+ assertEquals (1 , employees .size ());
54+ assertEquals ("Bob Smith" , employees .get (0 ).getName ());
55+
56+ Employee byName = cache .getEmployeeByName ("Alice Johnson" );
57+ assertNotNull (byName );
58+ assertEquals ("alice@example.com" , byName .getEmail ());
59+
60+ Map <String , Object > stats = cache .getCacheStats ();
61+ assertEquals (3 , ((Integer ) stats .get ("totalEmployees" )).intValue ());
62+ assertEquals (true , stats .get ("cacheInitialized" ));
63+ }
64+
65+ @ Test
66+ void getEmployeeNamesByPrefix_limits_to_max_suggestions () {
67+ // create more than MAX_SUGGESTIONS employees with same prefix "Emp"
68+ List <Employee > many = new ArrayList <>();
69+ IntStream .range (0 , 20 ).forEach (i -> many .add (emp ("EmpUser" + i , "u" +i +"@example.com" )));
70+ when (employeeRepository .findAll ()).thenReturn (many );
71+
72+ cache .initializeCache ();
73+
74+ List <String > results = cache .getEmployeeNamesByPrefix ("Emp" );
75+ // MAX_SUGGESTIONS is 10 in implementation
76+ assertTrue (results .size () <= 10 );
77+ assertEquals (10 , results .size ());
78+ }
79+
80+ @ Test
81+ void clear_removes_all_entries () {
82+ when (employeeRepository .findAll ()).thenReturn (Arrays .asList (emp ("One" ,"one@example.com" )));
83+ cache .initializeCache ();
84+
85+ Map <String , Object > statsBefore = cache .getCacheStats ();
86+ assertEquals (1 , ((Integer ) statsBefore .get ("totalEmployees" )).intValue ());
87+
88+ cache .clear ();
89+ Map <String , Object > statsAfter = cache .getCacheStats ();
90+ assertEquals (0 , ((Integer ) statsAfter .get ("totalEmployees" )).intValue ());
91+ assertEquals (false , statsAfter .get ("cacheInitialized" ));
92+ }
93+
94+ @ Test
95+ void insert_and_update_employee_behaviour () {
96+ Employee john = emp ("John Doe" , "john@old.example" );
97+ cache .insertEmployee (john );
98+
99+ Employee got = cache .getEmployeeByName ("John Doe" );
100+ assertNotNull (got );
101+ assertEquals ("john@old.example" , got .getEmail ());
102+
103+ // update with new email, same name
104+ Employee johnUpdated = emp ("John Doe" , "john@new.example" );
105+ cache .updateEmployee (johnUpdated );
106+
107+ Employee gotAfter = cache .getEmployeeByName ("John Doe" );
108+ assertNotNull (gotAfter );
109+ assertEquals ("john@new.example" , gotAfter .getEmail ());
110+ }
111+
112+ @ Test
113+ void bulkInsertEmployees_adds_many_entries () {
114+ List <Employee > employees = Arrays .asList (
115+ emp ("A" , "a@example.com" ),
116+ emp ("B" , "b@example.com" ),
117+ emp ("C" , "c@example.com" )
118+ );
119+ cache .clear ();
120+ cache .bulkInsertEmployees (employees );
121+
122+ Map <String , Object > stats = cache .getCacheStats ();
123+ assertEquals (3 , ((Integer ) stats .get ("totalEmployees" )).intValue ());
124+
125+ List <Employee > found = cache .getEmployeesByPrefix ("" );
126+ // should return up to available employees
127+ assertTrue (found .size () >= 3 );
128+ }
129+ }
0 commit comments