Fixing return codes for user service v2

This commit is contained in:
Martin Stockhammer 2020-11-10 13:44:44 +01:00
parent bbf073c7da
commit fa2cce07a9
2 changed files with 18 additions and 2 deletions

View File

@ -220,6 +220,9 @@ public class DefaultUserService
public UserInfo createUser( User user )
throws RedbackServiceException
{
if (user==null) {
throw new RedbackServiceException( ErrorMessage.of( MessageKeys.ERR_USER_ID_EMPTY ), 422 );
}
UserInfo result;
if ( Arrays.binarySearch( INVALID_CREATE_USER_NAMES, user.getUserId( ) ) >= 0 )
{
@ -307,6 +310,9 @@ public class DefaultUserService
public void deleteUser( String userId )
throws RedbackServiceException
{
if (StringUtils.isEmpty( userId )) {
throw new RedbackServiceException( MessageKeys.ERR_USER_ID_EMPTY, 404 );
}
try
{
@ -348,6 +354,9 @@ public class DefaultUserService
public UserInfo getUser( String userId )
throws RedbackServiceException
{
if (StringUtils.isEmpty( userId)) {
throw new RedbackServiceException( ErrorMessage.of( MessageKeys.ERR_USER_ID_EMPTY ), 404 );
}
try
{
if ( "guest".equals( userId ) )
@ -359,7 +368,7 @@ public class DefaultUserService
}
catch ( UserNotFoundException e )
{
return null;
throw new RedbackServiceException( ErrorMessage.of( MessageKeys.ERR_USER_NOT_FOUND ), 404 );
}
catch ( UserManagerException e )
{

View File

@ -48,6 +48,7 @@ import org.springframework.test.context.junit.jupiter.SpringExtension;
import javax.inject.Inject;
import javax.ws.rs.ForbiddenException;
import javax.ws.rs.NotFoundException;
import javax.ws.rs.core.MediaType;
import java.util.Collection;
import java.util.Collections;
@ -564,7 +565,13 @@ public class UserServiceTest
{
getUserService( getAdminAuthzHeader( ) ).deleteUser( "toto" );
getUserService( getAdminAuthzHeader( ) ).removeFromCache( "toto" );
assertNull( getUserService( getAdminAuthzHeader( ) ).getUser( "toto" ) );
try
{
getUserService( getAdminAuthzHeader( ) ).getUser( "toto" );
assertTrue( false, "404 should be thrown for non existing resource" );
} catch ( NotFoundException e ) {
assertEquals( 404, e.getResponse( ).getStatus( ) );
}
}
}