From fa2cce07a9194edbfcaf8a7cec76c221c425641d Mon Sep 17 00:00:00 2001 From: Martin Stockhammer Date: Tue, 10 Nov 2020 13:44:44 +0100 Subject: [PATCH] Fixing return codes for user service v2 --- .../redback/rest/services/v2/DefaultUserService.java | 11 ++++++++++- .../redback/rest/services/v2/UserServiceTest.java | 9 ++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/redback-integrations/redback-rest/redback-rest-services/src/main/java/org/apache/archiva/redback/rest/services/v2/DefaultUserService.java b/redback-integrations/redback-rest/redback-rest-services/src/main/java/org/apache/archiva/redback/rest/services/v2/DefaultUserService.java index 5b011098..8e7518f8 100644 --- a/redback-integrations/redback-rest/redback-rest-services/src/main/java/org/apache/archiva/redback/rest/services/v2/DefaultUserService.java +++ b/redback-integrations/redback-rest/redback-rest-services/src/main/java/org/apache/archiva/redback/rest/services/v2/DefaultUserService.java @@ -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 ) { diff --git a/redback-integrations/redback-rest/redback-rest-services/src/test/java/org/apache/archiva/redback/rest/services/v2/UserServiceTest.java b/redback-integrations/redback-rest/redback-rest-services/src/test/java/org/apache/archiva/redback/rest/services/v2/UserServiceTest.java index eab9e2b0..c673582b 100644 --- a/redback-integrations/redback-rest/redback-rest-services/src/test/java/org/apache/archiva/redback/rest/services/v2/UserServiceTest.java +++ b/redback-integrations/redback-rest/redback-rest-services/src/test/java/org/apache/archiva/redback/rest/services/v2/UserServiceTest.java @@ -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( ) ); + } } }