Skip to content

Commit

Permalink
[AB2D-6187] increase api test coverage (#1388)
Browse files Browse the repository at this point in the history
## 🎫 Ticket

https://jira.cms.gov/browse/AB2D-6187

## 🛠 Changes

Adds test coverage for `CustomUserDetailsService`

## ℹ️ Context

This is a part of my journey to achieve 90% test coverage everywhere

---------

Co-authored-by: Anna Smirnova <[email protected]>
Co-authored-by: smirnovaae <[email protected]>
  • Loading branch information
3 people authored Oct 23, 2024
1 parent dd9e9b5 commit a9a4bd3
Showing 1 changed file with 39 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package gov.cms.ab2d.api.security;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThrows;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import org.junit.jupiter.api.Test;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import gov.cms.ab2d.common.model.PdpClient;
import gov.cms.ab2d.common.repository.PdpClientRepository;

class CustomUserDetailsServiceTest {

@Test
void testLoadUserByUsername1() {
PdpClientRepository pdpClientRepository = mock(PdpClientRepository.class);
CustomUserDetailsService customUserDetailsService = new CustomUserDetailsService(pdpClientRepository);
PdpClient pdpClient = new PdpClient();
when(pdpClientRepository.findByClientId("clientId")).thenReturn(pdpClient);
assertEquals(
customUserDetailsService.loadUserByUsername("clientId"),
pdpClient
);
}

@Test
void testLoadUserByUsername2() {
PdpClientRepository pdpClientRepository = mock(PdpClientRepository.class);
CustomUserDetailsService customUserDetailsService = new CustomUserDetailsService(pdpClientRepository);
when(pdpClientRepository.findByClientId(null)).thenReturn(null);
assertThrows(
UsernameNotFoundException.class,
() -> {customUserDetailsService.loadUserByUsername(null);}
);
}

}

0 comments on commit a9a4bd3

Please sign in to comment.