Skip to content

Commit

Permalink
fix(rest.identity): adding specific error messages when user is not f…
Browse files Browse the repository at this point in the history
…ound (#5047)

* fix(rest.identity): adding specific error messages when user not found

Signed-off-by: SimoneFiorani <[email protected]>

* Updated exception throwing strategy

Signed-off-by: SimoneFiorani <[email protected]>

* Updated tests

Signed-off-by: SimoneFiorani <[email protected]>

* Added tests for coverage

Signed-off-by: SimoneFiorani <[email protected]>

* Updated tests

Signed-off-by: SimoneFiorani <[email protected]>

---------

Signed-off-by: SimoneFiorani <[email protected]>
  • Loading branch information
sfiorani authored Dec 13, 2023
1 parent ab9a34d commit 4db9733
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;

import org.eclipse.kura.KuraErrorCode;
import org.eclipse.kura.KuraException;
import org.eclipse.kura.cloudconnection.request.RequestHandler;
import org.eclipse.kura.cloudconnection.request.RequestHandlerRegistry;
import org.eclipse.kura.configuration.ConfigurationService;
Expand Down Expand Up @@ -138,6 +141,12 @@ public UserDTO getUser(final UserDTO userName) {
try {
logger.debug(DEBUG_MESSAGE, "getUser");
return this.identityService.getUser(userName.getUserName());
} catch (KuraException e) {
if (e.getCode().equals(KuraErrorCode.NOT_FOUND)) {
throw DefaultExceptionHandler.buildWebApplicationException(Status.NOT_FOUND, "Identity does not exist");
} else {
throw DefaultExceptionHandler.toWebApplicationException(e);
}
} catch (Exception e) {
throw DefaultExceptionHandler.toWebApplicationException(e);
}
Expand All @@ -152,6 +161,12 @@ public Response deleteUser(final UserDTO userName) {
try {
logger.debug(DEBUG_MESSAGE, "deleteUser");
this.identityService.deleteUser(userName.getUserName());
} catch (KuraException e) {
if (e.getCode().equals(KuraErrorCode.NOT_FOUND)) {
throw DefaultExceptionHandler.buildWebApplicationException(Status.NOT_FOUND, "Identity does not exist");
} else {
throw DefaultExceptionHandler.toWebApplicationException(e);
}
} catch (Exception e) {
throw DefaultExceptionHandler.toWebApplicationException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,14 @@ public void createUser(UserDTO user) throws KuraException {
}
}

public void deleteUser(String userName) {
this.userAdminHelper.deleteUser(userName);
public void deleteUser(String userName) throws KuraException {

if (this.userAdminHelper.getUser(userName).isPresent()) {
this.userAdminHelper.deleteUser(userName);
} else {
throw new KuraException(KuraErrorCode.NOT_FOUND, "Identity does not exist");
}

}

public UserDTO getUser(String userName) throws KuraException {
Expand All @@ -88,7 +94,7 @@ public UserDTO getUser(String userName) throws KuraException {
fillPermissions(Collections.singletonMap(user.get().getName(), userFound));
return userFound;
} else {
throw new KuraException(KuraErrorCode.NOT_FOUND, IDENTITY + userName + " not found");
throw new KuraException(KuraErrorCode.NOT_FOUND, "Identity does not exist");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*******************************************************************************/
package org.eclipse.kura.internal.rest.identity.provider.test;

import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.when;
Expand All @@ -27,6 +28,7 @@
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.eclipse.kura.KuraErrorCode;
import org.eclipse.kura.KuraException;
import org.eclipse.kura.core.testutil.requesthandler.AbstractRequestHandlerTest;
import org.eclipse.kura.core.testutil.requesthandler.MqttTransport;
Expand Down Expand Up @@ -81,6 +83,10 @@ public class IdentityEndpointsTest extends AbstractRequestHandlerTest {
IdentityEndpointsTest.class.getResourceAsStream("/getPasswordRequirementsResponse.json"), "UTF-8")
.useDelimiter("\\A").next().replace(" ", "");

private static final String EXPECTED_NON_EXISTING_USER_RESPONSE = new Scanner(
IdentityEndpointsTest.class.getResourceAsStream("/getNonExistingUserResponse.json"), "UTF-8")
.useDelimiter("\\A").next();

private static Set<UserDTO> userConfigs;

private static ServiceRegistration<IdentityService> identityServiceRegistration;
Expand Down Expand Up @@ -178,6 +184,28 @@ public void shouldReturnPasswordRequirements() throws KuraException {
thenResponseBodyEqualsJson(EXPECTED_GET_PASSWORD_REQUIREMENTS_RESPONSE);
}

@Test
public void shouldReturnNonExistingUserDeleteResponse() throws KuraException {
givenIdentityService();

whenRequestIsPerformed(new MethodSpec(METHOD_SPEC_DELETE, MQTT_METHOD_SPEC_DEL), "/identities",
gson.toJson(new UserDTO("nonExistingUser", null, false, false)));

thenResponseCodeIs(404);
thenResponseBodyEqualsJson(EXPECTED_NON_EXISTING_USER_RESPONSE);
}

@Test
public void shouldReturnNonExistingUserPostResponse() throws KuraException {
givenIdentityService();

whenRequestIsPerformed(new MethodSpec(METHOD_SPEC_POST), "/identities/byName",
gson.toJson(new UserDTO("nonExistingUser", null, false, false)));

thenResponseCodeIs(404);
thenResponseBodyEqualsJson(EXPECTED_NON_EXISTING_USER_RESPONSE);
}

private void givenUser(UserDTO userParam) {
user = userParam;
}
Expand All @@ -200,8 +228,15 @@ private static void givenIdentityService() throws KuraException {

when(identityServiceMock.getUser("testuser"))
.thenReturn(new UserDTO("testuser", Collections.emptySet(), true, false));

}

when(identityServiceMock.getUser("nonExistingUser"))
.thenThrow(new KuraException(KuraErrorCode.NOT_FOUND, "Identity does not exist"));

doThrow(new KuraException(KuraErrorCode.NOT_FOUND, "Identity does not exist")).when(identityServiceMock)
.deleteUser("nonExistingUser");

when(identityServiceMock.getValidatorOptions()).thenReturn(new ValidatorOptions(8, false, false, false));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"message" : "Identity does not exist"}

0 comments on commit 4db9733

Please sign in to comment.