4040import static org .junit .jupiter .api .Assertions .assertEquals ;
4141import static org .junit .jupiter .api .Assertions .assertFalse ;
4242import static org .junit .jupiter .api .Assertions .assertNotNull ;
43+ import static org .junit .jupiter .api .Assertions .assertNull ;
4344import static org .junit .jupiter .api .Assertions .assertTrue ;
4445import static org .mockito .ArgumentMatchers .any ;
46+ import static org .mockito .Mockito .never ;
47+ import static org .mockito .Mockito .times ;
4548import static org .mockito .Mockito .verify ;
4649import static org .mockito .Mockito .when ;
4750
@@ -197,4 +200,47 @@ public void getClientApplicationApiKeyTest() {
197200 assertTrue (result .isPresent ());
198201 assertEquals (apiKey , result .get ().getApiKey ());
199202 }
200- }
203+
204+ @ Test
205+ public void testDeleteApiKey_success () {
206+ // Setup
207+ UUID appId = UUID .randomUUID ();
208+ ClientApplication application = ClientApplication .builder ()
209+ .id (appId )
210+ .name ("Test App" )
211+ .apiKey ("someapikey" )
212+ .relyingPartyHostname ("localhost" )
213+ .createdAt (Instant .now ())
214+ .updatedAt (Instant .now ())
215+ .build ();
216+
217+ when (repository .findById (appId )).thenReturn (Optional .of (application ));
218+ // Mock repository.save to return the application object after modifying it in the service
219+ when (repository .save (any (ClientApplication .class ))).thenReturn (application );
220+
221+ // Execute
222+ boolean result = service .deleteApiKey (appId );
223+
224+ // Validate
225+ assertTrue (result );
226+ assertNull (application .getApiKey ()); // Verify the API key was set to null
227+ verify (repository , times (1 )).findById (appId );
228+ verify (repository , times (1 )).save (application ); // Verify save was called with the modified object
229+ }
230+
231+ @ Test
232+ public void testDeleteApiKey_notFound () {
233+ // Setup
234+ UUID appId = UUID .randomUUID ();
235+
236+ when (repository .findById (appId )).thenReturn (Optional .empty ());
237+
238+ // Execute
239+ boolean result = service .deleteApiKey (appId );
240+
241+ // Validate
242+ assertFalse (result );
243+ verify (repository , times (1 )).findById (appId );
244+ verify (repository , never ()).save (any (ClientApplication .class )); // Verify save was not called
245+ }
246+ }
0 commit comments