From 2b3cd86d4c90f6a4c8bc83a9b44578465611139f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcos=20Gon=C3=A7alves?= Date: Wed, 3 Jan 2024 15:54:06 -0300 Subject: [PATCH] Test: Upgrading GuestService test coverage to 100% --- .../com/darksun/service/GuestServiceTest.java | 141 +++++++++++++++++- 1 file changed, 138 insertions(+), 3 deletions(-) diff --git a/src/test/java/br/com/darksun/service/GuestServiceTest.java b/src/test/java/br/com/darksun/service/GuestServiceTest.java index 99bcc37..adb894d 100644 --- a/src/test/java/br/com/darksun/service/GuestServiceTest.java +++ b/src/test/java/br/com/darksun/service/GuestServiceTest.java @@ -15,12 +15,14 @@ import org.junit.jupiter.api.Test; import org.mockito.Mockito; +import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Optional; import java.util.stream.Collectors; import static br.com.darksun.TestUtils.mockList; +import static br.com.darksun.service.GuestService.GUEST_ROLE; import static br.com.darksun.service.GuestService.HOST_ROLE; import static org.mockito.Mockito.*; @@ -52,13 +54,121 @@ public void create_Success_Host( ) { Guest response = service.create( newGuest ); response.setId( 1L ); - Assertions.assertTrue( BcryptUtil.matches( SIMPLE_PASSWORD, newGuest.getPassword( ) ) ); + Assertions.assertEquals( HOST_ROLE, response.getRole( ) ); + Assertions.assertTrue( BcryptUtil.matches( SIMPLE_PASSWORD, response.getPassword( ) ) ); response.setPassword( guestList.getFirst( ).getPassword( ) ); Assertions.assertEquals( guestList.getFirst( ), response ); verify( repository, times( 1 ) ).findByNameOptional( any( ) ); verify( repository, times( 1 ) ).persist( any( Guest.class ) ); } + @Test + public void create_Success_Guest( ) { + Guest newGuest = new Guest( null, "Coppola", SIMPLE_PASSWORD, GUEST_ROLE ); + when( repository.findByNameOptional( any( ) ) ).thenReturn( Optional.empty( ) ); + doNothing( ).when( repository ).persist( any( Guest.class ) ); + + Guest response = service.create( newGuest ); + response.setId( 1L ); + + Assertions.assertEquals( GUEST_ROLE, response.getRole( ) ); + Assertions.assertTrue( BcryptUtil.matches( SIMPLE_PASSWORD, response.getPassword( ) ) ); + response.setPassword( guestList.getFirst( ).getPassword( ) ); + Assertions.assertEquals( newGuest, response ); + verify( repository, times( 1 ) ).findByNameOptional( any( ) ); + verify( repository, times( 1 ) ).persist( any( Guest.class ) ); + } + + @Test + public void create_Fail_Repeated( ) { + Guest newGuest = new Guest( null, "Coppola", SIMPLE_PASSWORD, HOST_ROLE ); + when( repository.findByNameOptional( any( ) ) ).thenReturn( + Optional.of( guestList.getFirst( ) ) ); + doNothing( ).when( repository ).persist( any( Guest.class ) ); + + boolean wasThrown = false; + try { + service.create( newGuest ); + } catch ( IllegalArgumentException ex ) { + wasThrown = true; + } + + Assertions.assertTrue( wasThrown ); + verify( repository, times( 1 ) ).findByNameOptional( any( ) ); + verify( repository, never( ) ).persist( any( Guest.class ) ); + } + + @Test + public void firstHost_Success( ) { + PanacheQuery query = mockList( new ArrayList( ) ); + when( repository.findAll( ) ).thenReturn( query ); + doNothing( ).when( repository ).persist( any( Guest.class ) ); + + Guest response = service.firstHost( ); + response.setId( 1L ); + + Assertions.assertEquals( HOST_ROLE, response.getRole( ) ); + Assertions.assertTrue( BcryptUtil.matches( "", response.getPassword( ) ) ); + response.setPassword( guestList.getFirst( ).getPassword( ) ); + Assertions.assertEquals( guestList.getFirst( ), response ); + verify( repository, times( 1 ) ).findAll( ); + verify( repository, times( 1 ) ).findByNameOptional( any( ) ); + verify( repository, times( 1 ) ).persist( any( Guest.class ) ); + } + + @Test + public void firstHost_Fail_GuestListNotEmpty( ) { + PanacheQuery query = mockList( guestList ); + when( repository.findAll( ) ).thenReturn( query ); + doNothing( ).when( repository ).persist( any( Guest.class ) ); + + boolean wasThrown = false; + try { + service.firstHost( ); + } catch ( IllegalArgumentException e ) { + wasThrown = true; + } + + Assertions.assertTrue( wasThrown ); + verify( repository, times( 1 ) ).findAll( ); + verify( repository, never( ) ).findByNameOptional( any( ) ); + verify( repository, never( ) ).persist( any( Guest.class ) ); + } + + @Test + public void invite_Success( ) { + when( repository.findByNameOptional( any( ) ) ).thenReturn( Optional.empty( ) ); + doNothing( ).when( repository ).persist( any( Guest.class ) ); + + Guest response = service.invite( guestList.getFirst( ).getName( ) ); + response.setId( 1L ); + + Assertions.assertEquals( GUEST_ROLE, response.getRole( ) ); + Assertions.assertTrue( BcryptUtil.matches( "", response.getPassword( ) ) ); + response.setPassword( guestList.getFirst( ).getPassword( ) ); + Assertions.assertEquals( guestList.getFirst( ).getName( ), response.getName( ) ); + verify( repository, times( 1 ) ).findByNameOptional( any( ) ); + verify( repository, times( 1 ) ).persist( any( Guest.class ) ); + } + + @Test + public void invite_Fail_Repeated( ) { + when( repository.findByNameOptional( any( ) ) ).thenReturn( + Optional.of( guestList.getFirst( ) ) ); + doNothing( ).when( repository ).persist( any( Guest.class ) ); + + boolean wasThrown = false; + try { + service.invite( guestList.getFirst( ).getName( ) ); + } catch ( IllegalArgumentException ex ) { + wasThrown = true; + } + + Assertions.assertTrue( wasThrown ); + verify( repository, times( 1 ) ).findByNameOptional( any( ) ); + verify( repository, never( ) ).persist( any( Guest.class ) ); + } + @Test public void readAll_Success( ) { PanacheQuery query = mockList( guestList ); @@ -104,6 +214,31 @@ public void readById_Fail_NotFound( ) { verify( repository, times( 1 ) ).findByIdOptional( any( ) ); } + @Test + public void readBName_Success( ) { + when( repository.findByNameOptional( any( ) ) ).thenReturn( + Optional.of( guestList.getFirst( ) ) ); + + Guest response = service.readByName( guestList.getFirst( ).getName( ) ); + Assertions.assertEquals( guestList.getFirst( ), response ); + verify( repository, times( 1 ) ).findByNameOptional( any( ) ); + } + + @Test + public void readByName_Fail_NotFound( ) { + when( repository.findByNameOptional( any( ) ) ).thenReturn( Optional.empty( ) ); + + boolean wasThrown = false; + try { + service.readByName( guestList.getFirst( ).getName( ) ); + } catch ( EntityNotFoundException ex ) { + wasThrown = true; + } + + Assertions.assertTrue( wasThrown ); + verify( repository, times( 1 ) ).findByNameOptional( any( ) ); + } + @Test public void getAllInvitations_Success( ) { PanacheQuery query = mockList( guestList ); @@ -132,7 +267,7 @@ public void update_Success_Host( ) { Guest response = service.update( newGuest ); - Assertions.assertTrue( BcryptUtil.matches( SIMPLE_PASSWORD, newGuest.getPassword( ) ) ); + Assertions.assertTrue( BcryptUtil.matches( SIMPLE_PASSWORD, response.getPassword( ) ) ); response.setPassword( guestList.getFirst( ).getPassword( ) ); Assertions.assertEquals( guestList.getFirst( ), response ); verify( repository, times( 1 ) ).findByIdOptional( any( ) ); @@ -181,7 +316,7 @@ public void delete_Sucess( ) { } @Test - public void delete_Fail( ) { + public void delete_Fail_NotFound( ) { when( repository.deleteById( any( ) ) ).thenReturn( false ); boolean wasThrown = false;