Skip to content

Commit

Permalink
Test: Upgrading GuestService test coverage to 100%
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcos Gonçalves committed Jan 3, 2024
1 parent 101f152 commit 2b3cd86
Showing 1 changed file with 138 additions and 3 deletions.
141 changes: 138 additions & 3 deletions src/test/java/br/com/darksun/service/GuestServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;

Expand Down Expand Up @@ -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 );
Expand Down Expand Up @@ -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 );
Expand Down Expand Up @@ -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( ) );
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 2b3cd86

Please sign in to comment.