From a9a4bd349f6775d87a8f3af5b113391f0bc9d09d Mon Sep 17 00:00:00 2001 From: "kai [they]" Date: Wed, 23 Oct 2024 13:03:05 -0700 Subject: [PATCH] [AB2D-6187] increase `api` test coverage (#1388) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## 🎫 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 <132938234+smirnovaae@users.noreply.github.com> Co-authored-by: smirnovaae --- .../CustomUserDetailsServiceTest.java | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 api/src/test/java/gov/cms/ab2d/api/security/CustomUserDetailsServiceTest.java diff --git a/api/src/test/java/gov/cms/ab2d/api/security/CustomUserDetailsServiceTest.java b/api/src/test/java/gov/cms/ab2d/api/security/CustomUserDetailsServiceTest.java new file mode 100644 index 000000000..3d9a677ea --- /dev/null +++ b/api/src/test/java/gov/cms/ab2d/api/security/CustomUserDetailsServiceTest.java @@ -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);} + ); + } + +}