implemented user/me client and tests

This commit is contained in:
Alfredo Oliviero 2024-05-20 12:44:50 +02:00
parent b047444ae0
commit 11bb16c98d
8 changed files with 246 additions and 83 deletions

View File

@ -1,71 +0,0 @@
package org.gcube.idm.client;
import java.net.URI;
import java.net.URISyntaxException;
import org.gcube.idm.client.beans.ResponseBean;
import org.gcube.idm.client.clients.IdmRestClient;
import org.gcube.idm.client.model.OwnerInfo;
import org.gcube.idm.client.model.UserProfile;
import org.gcube.idm.common.models.IdmVerifyObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class DefaultIdmMeClient extends AbstractIdmClient implements IdmMeClient {
protected String auth_token;
public DefaultIdmMeClient(IdmRestClient client, String auth_token) throws URISyntaxException {
super(client);
this.auth_token = auth_token;
}
public DefaultIdmMeClient(String base_url, String auth_token) throws URISyntaxException {
super(base_url);
this.auth_token = auth_token;
}
public DefaultIdmMeClient(URI base_url, String auth_token) throws URISyntaxException {
super(base_url);
this.auth_token = auth_token;
}
protected static Logger logger = LoggerFactory.getLogger(DefaultIdmMeClient.class);
@Override
public String getMeId() {
ResponseBean<String> resp = this.client.performGetRequest("users/me/id", auth_token, String.class);
return resp.getResult();
}
@Override
public String getMeEmail() {
ResponseBean<String> resp = this.client.performGetRequest("users/me/email", auth_token, String.class);
return resp.getResult();
}
@Override
public String getMeUsername() {
ResponseBean<String> resp = this.client.performGetRequest("users/me/username", auth_token, String.class);
return resp.getResult();
}
@Override
public OwnerInfo getMeOwner() {
ResponseBean<OwnerInfo> resp = this.client.performGetRequest("users/me/owner", auth_token, OwnerInfo.class);
return resp.getResult();
}
@Override
public UserProfile getMeProfile() {
ResponseBean<UserProfile> resp = this.client.performGetRequest("users/me/profile", auth_token, UserProfile.class);
return resp.getResult();
}
@Override
public IdmVerifyObject verifyToken() {
ResponseBean<IdmVerifyObject> resp = this.client.performGetRequest("users/me/verify", auth_token, IdmVerifyObject.class);
return resp.getResult();
}
}

View File

@ -0,0 +1,129 @@
package org.gcube.idm.client;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.gcube.com.fasterxml.jackson.databind.JavaType;
import org.gcube.idm.client.beans.ResponseBean;
import org.gcube.idm.client.clients.IdmRestClient;
import org.gcube.idm.client.model.OwnerInfo;
import org.gcube.idm.client.model.UserInfo;
import org.gcube.idm.client.model.UserInspect;
import org.gcube.idm.client.model.UserProfile;
import org.gcube.idm.client.model.util.JsonUtilsCustom;
import org.gcube.idm.common.models.IdmUser;
import org.gcube.idm.common.models.IdmVerifyObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import jakarta.ws.rs.BadRequestException;
import jakarta.ws.rs.NotAuthorizedException;
public class DefaultMeClient extends AbstractIdmClient implements IdmMeClient {
protected String auth_token;
public DefaultMeClient(IdmRestClient client, String auth_token) throws URISyntaxException {
super(client);
this.auth_token = auth_token;
}
public DefaultMeClient(String base_url, String auth_token) throws URISyntaxException {
super(base_url);
this.auth_token = auth_token;
}
public DefaultMeClient(URI base_url, String auth_token) throws URISyntaxException {
super(base_url);
this.auth_token = auth_token;
}
protected static Logger logger = LoggerFactory.getLogger(DefaultMeClient.class);
@Override
public String getMeId() {
ResponseBean<String> resp = this.client.performGetRequest("users/me/id", auth_token, String.class);
return resp.getResult();
}
@Override
public String getMeEmail() {
ResponseBean<String> resp = this.client.performGetRequest("users/me/email", auth_token, String.class);
return resp.getResult();
}
@Override
public String getMeUsername() {
ResponseBean<String> resp = this.client.performGetRequest("users/me/username", auth_token, String.class);
return resp.getResult();
}
@Override
public OwnerInfo getMeOwner() {
ResponseBean<OwnerInfo> resp = this.client.performGetRequest("users/me/owner", auth_token, OwnerInfo.class);
return resp.getResult();
}
@Override
public UserProfile getMeProfile() {
ResponseBean<UserProfile> resp = this.client.performGetRequest("users/me/profile", auth_token,
UserProfile.class);
return resp.getResult();
}
@Override
public IdmVerifyObject verifyToken() {
ResponseBean<IdmVerifyObject> resp = this.client.performGetRequest("users/me/verify", auth_token,
IdmVerifyObject.class);
return resp.getResult();
}
@Override
public String getMeName() throws NotAuthorizedException, BadRequestException {
ResponseBean<String> resp = this.client.performGetRequest("users/me/name", auth_token, String.class);
return resp.getResult();
}
@Override
public Map<String, List<String>> getMeAttributes() throws NotAuthorizedException, BadRequestException {
JavaType stringListType = JsonUtilsCustom.geListOfObjectsType(String.class);
JavaType stringType = JsonUtilsCustom.getObjectJT(String.class);
JavaType mapOfStringList = JsonUtilsCustom.geMapOfObjectsType(stringType, stringListType);
ResponseBean<Map<String, List<String>>> resp = this.client.performGetRequest("users/me/attributes", auth_token,
mapOfStringList);
return resp.getResult();
}
// @Override
// public String getMeAttribute(String attribute) throws NotAuthorizedException,
// BadRequestException {
// // TODO Auto-generated method stub
// throw new UnsupportedOperationException("Unimplemented method
// 'getMeAttribute'");
// }
@Override
public IdmUser getMeUser() throws NotAuthorizedException, BadRequestException {
ResponseBean<IdmUser> resp = this.client.performGetRequest("users/me/user", auth_token, IdmUser.class);
return resp.getResult();
}
@Override
public UserInfo getMe() throws NotAuthorizedException, BadRequestException {
ResponseBean<UserInfo> resp = this.client.performGetRequest("users/me", auth_token, UserInfo.class);
return resp.getResult();
}
@Override
public UserInspect getMeInspect() throws NotAuthorizedException, BadRequestException {
HashMap<String, String> params = new HashMap<>();
params.put(("inspect"), "true");
HashMap<String, String> headers = IdmRestClient.getHeadersWithAuth(auth_token, null);
ResponseBean<UserInspect> resp = this.client.performGetRequest("users/me", headers, params, UserInspect.class);
return resp.getResult();
}
}

View File

@ -90,7 +90,7 @@ public class IdmClientFactory {
String url = null;
try {
url = this.config.getServerUrl();
return new DefaultIdmMeClient(url, auth_token);
return new DefaultMeClient(url, auth_token);
} catch (Exception e) {
logger.error("cannot create idm client with url ", url);

View File

@ -1,7 +1,13 @@
package org.gcube.idm.client;
import java.util.List;
import java.util.Map;
import org.gcube.idm.client.model.OwnerInfo;
import org.gcube.idm.client.model.UserInfo;
import org.gcube.idm.client.model.UserInspect;
import org.gcube.idm.client.model.UserProfile;
import org.gcube.idm.common.models.IdmUser;
import org.gcube.idm.common.models.IdmVerifyObject;
import jakarta.ws.rs.BadRequestException;
@ -9,11 +15,28 @@ import jakarta.ws.rs.NotAuthorizedException;
public interface IdmMeClient {
public UserInfo getMe() throws NotAuthorizedException, BadRequestException;
public String getMeId() throws NotAuthorizedException, BadRequestException;
public String getMeEmail() throws NotAuthorizedException, BadRequestException;
public String getMeUsername() throws NotAuthorizedException, BadRequestException;
public String getMeName() throws NotAuthorizedException, BadRequestException;
public Map<String, List<String>> getMeAttributes() throws NotAuthorizedException, BadRequestException;
// public String getMeAttribute(String attribute) throws NotAuthorizedException, BadRequestException;
public IdmUser getMeUser() throws NotAuthorizedException, BadRequestException;
public OwnerInfo getMeOwner() throws NotAuthorizedException, BadRequestException;
public UserProfile getMeProfile() throws NotAuthorizedException, BadRequestException;
public IdmVerifyObject verifyToken() throws NotAuthorizedException, BadRequestException;
public UserInspect getMeInspect() throws NotAuthorizedException, BadRequestException;
}

View File

@ -0,0 +1,9 @@
package org.gcube.idm.client.model;
import org.gcube.idm.common.models.IdmUser;
public class UserInfo {
public OwnerInfo owner;
public UserProfile profile;
public IdmUser user;
}

View File

@ -0,0 +1,13 @@
package org.gcube.idm.client.model;
public class UserInspect extends UserInfo {
// public OwnerInfo owner;
// public UserProfile profile;
// public IdmUser user;
public Object roles;
public Object groups;
public Object groupRolesRealm;
public Object groupRolesClients;
public Object verify;
}

View File

@ -1,6 +1,7 @@
package org.gcube.idm.client.model.util;
import java.util.List;
import java.util.Map;
import org.gcube.com.fasterxml.jackson.databind.DeserializationFeature;
import org.gcube.com.fasterxml.jackson.databind.JavaType;
@ -32,7 +33,6 @@ public class JsonUtilsCustom {
return _tf;
}
public static JavaType getObjectJT(Class<?> classType) {
TypeFactory tf = getTypeFactory();
// map key type
@ -65,6 +65,19 @@ public class JsonUtilsCustom {
return listOfObject;
}
public static JavaType geMapOfObjectsType(Class<?> keyClass, Class<?> valueClass) {
TypeFactory tf = getTypeFactory();
JavaType keyType = tf.constructType(keyClass);
JavaType valueType = tf.constructType(valueClass);
return geMapOfObjectsType(keyType, valueType);
}
public static JavaType geMapOfObjectsType(JavaType keyType, JavaType valueType) {
TypeFactory tf = getTypeFactory();
JavaType mapOfObject = tf.constructParametrizedType(Map.class, Map.class, keyType, valueType);
return mapOfObject;
}
public static JavaType getResponseBeanOfListOfObjectsType(Class<?> classType) {
JavaType listOfObjectType = geListOfObjectsType(classType);
return getResponseBeanOfListOfObjectsType(listOfObjectType);

View File

@ -5,12 +5,15 @@ import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;
import java.io.IOException;
import java.rmi.ServerException;
import java.util.List;
import java.util.Map;
import org.gcube.idm.client.IdmMeClient;
import org.gcube.idm.client.IdmUsersClient;
import org.gcube.idm.client.model.UserInfo;
import org.gcube.idm.client.model.UserInspect;
import org.gcube.idm.client.model.OwnerInfo;
import org.gcube.idm.client.model.UserProfile;
import org.gcube.idm.common.models.IdmUser;
import org.gcube.idm.common.models.IdmVerifyObject;
import org.junit.Test;
@ -18,12 +21,26 @@ public class IdmUsersMeClientTest extends ClientContextTest {
@Test
public void getMe() throws IOException {
fail("Unimplemented");
IdmMeClient client = getMeClient();
UserInfo resp = client.getMe();
assertNotNull(resp);
String expected = "andrea.rossi";
assertEquals(expected, resp.user.getUsername());
assertEquals(expected, resp.owner.getId());
assertEquals(expected, resp.profile.getUsername());
}
@Test
public void getMeInspect() throws IOException {
fail("Unimplemented");
IdmMeClient client = getMeClient();
UserInspect resp = client.getMeInspect();
assertNotNull(resp);
assertNotNull(resp);
String email = resp.user.getEmail();
String expected = "m.assante@gmail.com";
assertEquals(expected, email);
}
@Test
@ -78,26 +95,56 @@ public class IdmUsersMeClientTest extends ClientContextTest {
@Test
public void getMeId() throws IOException {
fail("Unimplemented");
IdmMeClient client = getMeClient();
String id = client.getMeId();
String expected = "771f6151-00ae-45c2-a754-f0546d98f482";
assertEquals(expected, id);
}
@Test
public void getMeUsername() throws IOException {
fail("Unimplemented");
IdmMeClient client = getMeClient();
String id = client.getMeUsername();
String expected = "andrea.rossi";
assertEquals(expected, id);
}
@Test
public void getMeName() throws IOException {
fail("Unimplemented");
IdmMeClient client = getMeClient();
String name = client.getMeName();
String expected = "Andrea Rossi";
assertEquals(expected, name);
}
@Test
public void getMeAttributes() throws IOException {
fail("Unimplemented");
IdmMeClient client = getMeClient();
Map<String, List<String>> attributes = client.getMeAttributes();
assertEquals("attributes #", 12, attributes.size());
String expected = "Argentina";
List<String> country_list = attributes.get("country");
assertEquals("country #", 1, country_list.size());
assertEquals(expected, country_list.get(0));
}
// @Test
// public void getMeAttribute() throws IOException {
// String expected = "Argentina";
// List<String> country_list = attributes.get("country");
// assertEquals("country #", 1, country_list.size());
// assertEquals(expected, country_list.get(0));
// }
@Test
public void getMeUser() throws IOException {
fail("Unimplemented");
IdmMeClient client = getMeClient();
IdmUser resp = client.getMeUser();
assertNotNull(resp);
String expected = "andrea.rossi";
assertEquals(expected, resp.getUsername());
}
}