Skip to content

Commit

Permalink
fixed test
Browse files Browse the repository at this point in the history
  • Loading branch information
salvatore-coppola committed Oct 9, 2023
1 parent a2ccacf commit fc9e80c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -290,6 +289,8 @@ public <E extends Exception> void foreachPermission(final PermissionConsumer<E>

public static class AuthenticationException extends Exception {

private static final long serialVersionUID = -8534499595655286448L;

public enum Reason {
USER_NOT_FOUND,
INCORRECT_PASSWORD,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -77,7 +77,7 @@ public class IdentityEndpointsTest extends AbstractRequestHandlerTest {

private static Set<UserDTO> userConfigs;

@Parameterized.Parameters
@Parameterized.Parameters(name = "transport={index}")
public static Collection<Transport> transports() {
return Arrays.asList(new RestTransport(REST_APP_ID), new MqttTransport(MQTT_APP_ID));
}
Expand All @@ -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");
Expand All @@ -134,7 +133,7 @@ public void shouldReturnDefinedPermissions() {
}

@Test
public void shouldReturnUserConfig() {
public void shouldReturnUserConfig() throws KuraException {
givenUserConfigs(new UserDTO("testuser2", //
new HashSet<String>(Arrays.asList("perm1", "perm2")), //
false, //
Expand All @@ -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) {
Expand All @@ -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<String>(Arrays.asList("perm1", "perm2")));

when(identityServiceMock.getUserConfig()).thenReturn(userConfigs);
when(identityServiceMock.getUser(user.getUserName())).thenReturn(user);
}

@BeforeClass
Expand All @@ -190,7 +190,7 @@ public static void setUp() throws Exception {
registerIdentityServiceMock();
}

private static void createIdentityServiceMock() {
private static void createIdentityServiceMock() throws KuraException {
givenIdentityService();

final Dictionary<String, Object> configurationServiceProperties = new Hashtable<>();
Expand Down

0 comments on commit fc9e80c

Please sign in to comment.