UserNamespaceAuthorizationServiceImpl implements UserNamespaceAuthorizationService { @PublishNotificationMessages @NamespacePermission(fields = "#request?.userNamespaceAuthorizationKey?.namespace", permissions = NamespacePermissionEnum.GRANT) @Override public UserNamespaceAuthorization createUserNamespaceAuthorization(UserNamespaceAuthorizationCreateRequest request) { validateUserNamespaceAuthorizationCreateRequest(request); UserNamespaceAuthorizationKey key = request.getUserNamespaceAuthorizationKey(); UserNamespaceAuthorizationEntity userNamespaceAuthorizationEntity = userNamespaceAuthorizationDao.getUserNamespaceAuthorizationByKey(key); if (userNamespaceAuthorizationEntity != null) { throw new AlreadyExistsException(String .format("Unable to create user namespace authorization with user id \"%s\" and namespace \"%s\" because it already exists.", key.getUserId(), key.getNamespace())); } NamespaceEntity namespaceEntity = namespaceDaoHelper.getNamespaceEntity(key.getNamespace()); userNamespaceAuthorizationEntity = createUserNamespaceAuthorizationEntity(key.getUserId(), namespaceEntity, request.getNamespacePermissions()); 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 testCreateUserNamespaceAuthorizationHappyPath() { when(alternateKeyHelper.validateStringParameter("namespace", NAMESPACE_VALUE)).thenReturn(NAMESPACE_VALUE); when(alternateKeyHelper.validateStringParameter("user id", USER_ID_VALUE)).thenReturn(USER_ID_VALUE); when(userNamespaceAuthorizationDao.getUserNamespaceAuthorizationByKey(USER_NAMESPACE_AUTHORIZATION_KEY)).thenReturn(null); when(namespaceDaoHelper.getNamespaceEntity(NAMESPACE_VALUE)).thenReturn(NAMESPACE_ENTITY); when(userNamespaceAuthorizationDao.saveAndRefresh(any())).thenReturn(USER_NAMESPACE_AUTHORIZATION_ENTITY); when(userNamespaceAuthorizationHelper.getNamespacePermissions(USER_NAMESPACE_AUTHORIZATION_ENTITY)).thenReturn(NAMESPACE_PERMISSIONS); UserNamespaceAuthorization userNamespaceAuthorization = userNamespaceAuthorizationService.createUserNamespaceAuthorization(USER_NAMESPACE_AUTHORIZATION_CREATE_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(namespaceDaoHelper).getNamespaceEntity(NAMESPACE_VALUE); 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(); }