From fc9e80c58785588e0e127ed552d31298e41239fd Mon Sep 17 00:00:00 2001 From: Salvatore Coppola Date: Mon, 9 Oct 2023 15:32:04 +0200 Subject: [PATCH] fixed test --- .../kura/util/useradmin/UserAdminHelper.java | 3 +- .../provider/test/IdentityEndpointsTest.java | 42 +++++++++---------- 2 files changed, 23 insertions(+), 22 deletions(-) diff --git a/kura/org.eclipse.kura.util/src/main/java/org/eclipse/kura/util/useradmin/UserAdminHelper.java b/kura/org.eclipse.kura.util/src/main/java/org/eclipse/kura/util/useradmin/UserAdminHelper.java index 449cc4b3fe0..d4463e2864d 100644 --- a/kura/org.eclipse.kura.util/src/main/java/org/eclipse/kura/util/useradmin/UserAdminHelper.java +++ b/kura/org.eclipse.kura.util/src/main/java/org/eclipse/kura/util/useradmin/UserAdminHelper.java @@ -89,7 +89,6 @@ public void requirePermissions(final String username, final String... permission } } - @SuppressWarnings("unchecked") public void changeUserPassword(final String username, final String userPassword) throws AuthenticationException { final User user = getUser(username) .orElseThrow(() -> new AuthenticationException(AuthenticationException.Reason.USER_NOT_FOUND)); @@ -290,6 +289,8 @@ public void foreachPermission(final PermissionConsumer public static class AuthenticationException extends Exception { + private static final long serialVersionUID = -8534499595655286448L; + public enum Reason { USER_NOT_FOUND, INCORRECT_PASSWORD, diff --git a/kura/test/org.eclipse.kura.rest.identity.provider.test/src/main/java/org/eclipse/kura/internal/rest/identity/provider/test/IdentityEndpointsTest.java b/kura/test/org.eclipse.kura.rest.identity.provider.test/src/main/java/org/eclipse/kura/internal/rest/identity/provider/test/IdentityEndpointsTest.java index ac299a486c5..536fb347949 100644 --- a/kura/test/org.eclipse.kura.rest.identity.provider.test/src/main/java/org/eclipse/kura/internal/rest/identity/provider/test/IdentityEndpointsTest.java +++ b/kura/test/org.eclipse.kura.rest.identity.provider.test/src/main/java/org/eclipse/kura/internal/rest/identity/provider/test/IdentityEndpointsTest.java @@ -61,7 +61,7 @@ public class IdentityEndpointsTest extends AbstractRequestHandlerTest { private static IdentityService identityServiceMock = mock(IdentityService.class); - private UserDTO user; + private static UserDTO user; private Gson gson = new Gson(); @@ -77,7 +77,7 @@ public class IdentityEndpointsTest extends AbstractRequestHandlerTest { private static Set userConfigs; - @Parameterized.Parameters + @Parameterized.Parameters(name = "transport={index}") public static Collection transports() { return Arrays.asList(new RestTransport(REST_APP_ID), new MqttTransport(MQTT_APP_ID)); } @@ -87,44 +87,43 @@ public IdentityEndpointsTest(Transport transport) { } @Test - public void shouldInvokeCreateUserSuccessfully() { - givenIdentityService(); - + public void shouldInvokeCreateUserSuccessfully() throws KuraException { givenUser(new UserDTO("testuser", Collections.emptySet(), true, false, "testpassw")); - whenRequestIsPerformed(new MethodSpec(METHOD_SPEC_POST), "/users", gson.toJson(this.user)); + givenIdentityService(); + + whenRequestIsPerformed(new MethodSpec(METHOD_SPEC_POST), "/users", gson.toJson(user)); thenRequestSucceeds(); thenResponseBodyIsEmpty(); } @Test - public void shouldInvokeDeleteUserSuccessfully() { - givenIdentityService(); - + public void shouldInvokeDeleteUserSuccessfully() throws KuraException { givenUser(new UserDTO("testuser", Collections.emptySet(), true, false, "testpassw")); - whenRequestIsPerformed(new MethodSpec(METHOD_SPEC_DELETE, MQTT_METHOD_SPEC_DEL), "/users", - gson.toJson(this.user)); + givenIdentityService(); + + whenRequestIsPerformed(new MethodSpec(METHOD_SPEC_DELETE, MQTT_METHOD_SPEC_DEL), "/users", gson.toJson(user)); thenRequestSucceeds(); thenResponseBodyIsEmpty(); } @Test - public void shouldReturnUserSuccessfully() { - givenIdentityService(); - + public void shouldReturnUserSuccessfully() throws KuraException { givenUser(new UserDTO("testuser", Collections.emptySet(), true, false, "testpassw")); - whenRequestIsPerformed(new MethodSpec(METHOD_SPEC_GET, METHOD_SPEC_GET), "/users", gson.toJson(this.user)); + givenIdentityService(); + + whenRequestIsPerformed(new MethodSpec(METHOD_SPEC_GET, METHOD_SPEC_GET), "/users", gson.toJson(user)); thenRequestSucceeds(); thenResponseBodyEqualsJson(EXPECTED_GET_USER_RESPONSE); } @Test - public void shouldReturnDefinedPermissions() { + public void shouldReturnDefinedPermissions() throws KuraException { givenIdentityService(); whenRequestIsPerformed(new MethodSpec(METHOD_SPEC_GET), "/defined-permissions"); @@ -134,7 +133,7 @@ public void shouldReturnDefinedPermissions() { } @Test - public void shouldReturnUserConfig() { + public void shouldReturnUserConfig() throws KuraException { givenUserConfigs(new UserDTO("testuser2", // new HashSet(Arrays.asList("perm1", "perm2")), // false, // @@ -161,8 +160,8 @@ public void shouldInvokeSetUserConfigSuccessfully() throws KuraException { thenRequestSucceeds(); } - private void givenUser(UserDTO user) { - this.user = user; + private void givenUser(UserDTO userParam) { + user = userParam; } private void givenUserConfigs(UserDTO... userConfigurations) { @@ -175,13 +174,14 @@ private void givenUserConfigRequest(UserDTO... userDTO) { } - private static void givenIdentityService() { + private static void givenIdentityService() throws KuraException { reset(identityServiceMock); when(identityServiceMock.getDefinedPermissions()) .thenReturn(new HashSet(Arrays.asList("perm1", "perm2"))); when(identityServiceMock.getUserConfig()).thenReturn(userConfigs); + when(identityServiceMock.getUser(user.getUserName())).thenReturn(user); } @BeforeClass @@ -190,7 +190,7 @@ public static void setUp() throws Exception { registerIdentityServiceMock(); } - private static void createIdentityServiceMock() { + private static void createIdentityServiceMock() throws KuraException { givenIdentityService(); final Dictionary configurationServiceProperties = new Hashtable<>();