You need to set LdapHostnameVerificationSSLSocketFactory class in the current thread and Confluence Sysadmin user in the AuthenticatedUserThreadLocal (for permission to update user).
Try this code
Thread currentThread = Thread.currentThread();
ClassLoader classLoader= currentThread.getContextClassLoader();
currentThread.setContextClassLoader(LdapHostnameVerificationSSLSocketFactory.class.getClassLoader());
AuthenticatedUserThreadLocal.set(“Confluence Sys admin username”);
userAccessor.addMembership(“group_name”, “username”);