UserNamespaceAuthorizationServiceImpl implements UserNamespaceAuthorizationService { @PublishNotificationMessages @NamespacePermission(fields = "#key?.namespace", permissions = NamespacePermissionEnum.GRANT) @Override public UserNamespaceAuthorization updateUserNamespaceAuthorization(UserNamespaceAuthorizationKey key, UserNamespaceAuthorizationUpdateRequest request) { validateUserNamespaceAuthorizationKey(key); validateUserNamespaceAuthorizationUpdateRequest(request); UserNamespaceAuthorizationEntity userNamespaceAuthorizationEntity = getUserNamespaceAuthorizationEntity(key); UserAuthorizations userAuthorizations = currentUserService.getCurrentUser(); if (userAuthorizations.getUserId().equalsIgnoreCase(key.getUserId()) && userNamespaceAuthorizationEntity.getGrantPermission()) { Assert.isTrue(request.getNamespacePermissions().contains(NamespacePermissionEnum.GRANT), "Users are not allowed to remove their own GRANT namespace permission." + " Please include the GRANT namespace permission in this request, or have another user remove the GRANT permission."); } updateNamespacePermissions(userNamespaceAuthorizationEntity, request.getNamespacePermissions()); userNamespaceAuthorizationDao.saveAndRefresh(userNamespaceAuthorizationEntity); messageNotificationEventService.processUserNamespaceAuthorizationChangeNotificationEvent(key); return createUserNamespaceAuthorizationFromEntity(userNamespaceAuthorizationEntity); }  @PublishNotificationMessages @NamespacePermission(fields = "#request?.userNamespaceAuthorizationKey?.namespace", permissions = NamespacePermissionEnum.GRANT) @Override UserNamespaceAuthorization createUserNamespaceAuthorization(UserNamespaceAuthorizationCreateRequest request); @PublishNotificationMessages @NamespacePermission(fields = "#key?.namespace", permissions = NamespacePermissionEnum.GRANT) @Override UserNamespaceAuthorization updateUserNamespaceAuthorization(UserNamespaceAuthorizationKey key, UserNamespaceAuthorizationUpdateRequest request); @NamespacePermission(fields = "#key?.namespace", permissions = NamespacePermissionEnum.READ) @Override UserNamespaceAuthorization getUserNamespaceAuthorization(UserNamespaceAuthorizationKey key); @PublishNotificationMessages @NamespacePermission(fields = "#key?.namespace", permissions = NamespacePermissionEnum.GRANT) @Override UserNamespaceAuthorization deleteUserNamespaceAuthorization(UserNamespaceAuthorizationKey key); @Override UserNamespaceAuthorizations getUserNamespaceAuthorizationsByUserId(String userId); @NamespacePermission(fields = "#namespace", permissions = NamespacePermissionEnum.READ) @Override UserNamespaceAuthorizations getUserNamespaceAuthorizationsByNamespace(String namespace); void validateUserNamespaceAuthorizationKey(UserNamespaceAuthorizationKey key); void validateNamespacePermissions(List<NamespacePermissionEnum> namespacePermissions);  }
@Test public void testUpdateUserNamespaceAuthorizationHappyPath() { when(alternateKeyHelper.validateStringParameter("namespace", NAMESPACE_VALUE)).thenReturn(NAMESPACE_VALUE); when(alternateKeyHelper.validateStringParameter("user id", USER_ID_VALUE)).thenReturn(USER_ID_VALUE); when(currentUserService.getCurrentUser()).thenReturn(USER_AUTHORIZATIONS); when(userNamespaceAuthorizationDao.getUserNamespaceAuthorizationByKey(USER_NAMESPACE_AUTHORIZATION_KEY)) .thenReturn(USER_NAMESPACE_AUTHORIZATION_ENTITY); when(userNamespaceAuthorizationHelper.getNamespacePermissions(USER_NAMESPACE_AUTHORIZATION_ENTITY)).thenReturn(NAMESPACE_PERMISSIONS); UserNamespaceAuthorization userNamespaceAuthorization = userNamespaceAuthorizationService.updateUserNamespaceAuthorization(USER_NAMESPACE_AUTHORIZATION_KEY, USER_NAMESPACE_AUTHORIZATION_UPDATE_REQUEST); assertEquals(USER_NAMESPACE_AUTHORIZATION_ID, userNamespaceAuthorization.getId()); assertEquals(USER_NAMESPACE_AUTHORIZATION_KEY, userNamespaceAuthorization.getUserNamespaceAuthorizationKey()); assertEquals(NAMESPACE_PERMISSIONS, userNamespaceAuthorization.getNamespacePermissions()); verify(alternateKeyHelper).validateStringParameter("namespace", NAMESPACE_VALUE); verify(alternateKeyHelper).validateStringParameter("user id", USER_ID_VALUE); verify(currentUserService).getCurrentUser(); verify(userNamespaceAuthorizationDao).getUserNamespaceAuthorizationByKey(USER_NAMESPACE_AUTHORIZATION_KEY); verify(userNamespaceAuthorizationDao).saveAndRefresh(any(UserNamespaceAuthorizationEntity.class)); verify(messageNotificationEventService).processUserNamespaceAuthorizationChangeNotificationEvent(USER_NAMESPACE_AUTHORIZATION_KEY); verify(userNamespaceAuthorizationHelper).getNamespacePermissions(USER_NAMESPACE_AUTHORIZATION_ENTITY); verifyNoMoreInteractionsHelper(); }