Added utility
git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/private/luca.frosini/infrastructure-tests@178390 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
parent
7f41965489
commit
5966c5c151
7
pom.xml
7
pom.xml
|
@ -50,7 +50,12 @@
|
|||
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.gcube.common</groupId>
|
||||
<artifactId>context-enumeration</artifactId>
|
||||
<version>[1.0.0-SNAPSHOT, 2.0.0-SNAPSHOT)</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.gcube.common</groupId>
|
||||
<artifactId>authorization-client</artifactId>
|
||||
|
|
|
@ -12,6 +12,7 @@ import org.gcube.common.authorization.library.AuthorizationEntry;
|
|||
import org.gcube.common.authorization.library.provider.ContainerInfo;
|
||||
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
|
||||
import org.gcube.common.authorization.library.provider.UserInfo;
|
||||
import org.gcube.testutility.ContextTest;
|
||||
import org.junit.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
@ -30,9 +31,9 @@ public class ContextAuthorizationTest extends ContextTest {
|
|||
|
||||
@Test
|
||||
public void generateApplicationToken() throws Exception {
|
||||
ContextTest.setContext(ROOT);
|
||||
ContextTest.setContextByName("/d4science.research-infrastructures.eu");
|
||||
|
||||
String context = "/d4science.research-infrastructures.eu/D4Research/AGINFRAplusDev";
|
||||
String context = "/d4science.research-infrastructures.eu";
|
||||
UserInfo userInfo = new UserInfo("luca.frosini",new ArrayList<>());
|
||||
String userToken = authorizationService().generateUserToken(userInfo, context);
|
||||
SecurityTokenProvider.instance.set(userToken);
|
||||
|
|
|
@ -1,122 +0,0 @@
|
|||
/**
|
||||
*
|
||||
*/
|
||||
package org.gcube.authorization;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.gcube.common.authorization.client.Constants;
|
||||
import org.gcube.common.authorization.client.exceptions.ObjectNotFound;
|
||||
import org.gcube.common.authorization.library.AuthorizationEntry;
|
||||
import org.gcube.common.authorization.library.provider.AuthorizationProvider;
|
||||
import org.gcube.common.authorization.library.provider.ClientInfo;
|
||||
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
|
||||
import org.gcube.common.authorization.library.utils.Caller;
|
||||
import org.gcube.common.scope.api.ScopeProvider;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.BeforeClass;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* @author Luca Frosini (ISTI - CNR)
|
||||
*
|
||||
*/
|
||||
public class ContextTest {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(ContextTest.class);
|
||||
|
||||
protected static final String PROPERTIES_FILENAME = "token.properties";
|
||||
|
||||
private static final String GCUBE_VARNAME = "GCUBE";
|
||||
public static final String GCUBE;
|
||||
|
||||
private static final String GCUBE_DEVNEXT_VARNAME = "GCUBE_DEVNEXT";
|
||||
public static final String GCUBE_DEVNEXT;
|
||||
|
||||
private static final String GCUBE_DEVNEXT_NEXTNEXT_VARNAME = "GCUBE_DEVNEXT_NEXTNEXT";
|
||||
public static final String GCUBE_DEVNEXT_NEXTNEXT;
|
||||
|
||||
public static final String GCUBE_DEVSEC_VARNAME = "GCUBE_DEVSEC";
|
||||
public static final String GCUBE_DEVSEC;
|
||||
|
||||
public static final String GCUBE_DEVSEC_DEVVRE_VARNAME = "GCUBE_DEVSEC_DEVVRE";
|
||||
public static final String GCUBE_DEVSEC_DEVVRE;
|
||||
|
||||
|
||||
private static final String GCUBE_DEVNEXT_ANOTHER_USER_VARNAME = "GCUBE_DEVNEXT_ANOTHER_USER";
|
||||
public static final String GCUBE_DEVNEXT_ANOTHER_USER;
|
||||
|
||||
public static final String DEFAULT_TEST_SCOPE;
|
||||
|
||||
public static final String GCUBE_PRE_PROD_PREVRE_VARNAME = "GCUBE_PRE_PROD_PREVRE";
|
||||
public static final String GCUBE_PRE_PROD_PREVRE;
|
||||
|
||||
public static final String GCUBE_PRE_PROD_PARTHENOS_REGISTRY_VARNAME = "GCUBE_PRE_PROD_PARTHENOS_REGISTRY";
|
||||
public static final String GCUBE_PRE_PROD_PARTHENOS_REGISTRY;
|
||||
|
||||
public static final String ROOT_VARNAME = "ROOT";
|
||||
public static final String ROOT;
|
||||
|
||||
static {
|
||||
Properties properties = new Properties();
|
||||
InputStream input = ContextTest.class.getClassLoader().getResourceAsStream(PROPERTIES_FILENAME);
|
||||
|
||||
try {
|
||||
// load the properties file
|
||||
properties.load(input);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
GCUBE = properties.getProperty(GCUBE_VARNAME);
|
||||
|
||||
GCUBE_DEVNEXT = properties.getProperty(GCUBE_DEVNEXT_VARNAME);
|
||||
GCUBE_DEVNEXT_NEXTNEXT = properties.getProperty(GCUBE_DEVNEXT_NEXTNEXT_VARNAME);
|
||||
|
||||
GCUBE_DEVSEC = properties.getProperty(GCUBE_DEVSEC_VARNAME);
|
||||
GCUBE_DEVSEC_DEVVRE = properties.getProperty(GCUBE_DEVSEC_DEVVRE_VARNAME);
|
||||
|
||||
GCUBE_DEVNEXT_ANOTHER_USER = properties.getProperty(GCUBE_DEVNEXT_ANOTHER_USER_VARNAME);
|
||||
|
||||
GCUBE_PRE_PROD_PARTHENOS_REGISTRY = properties.getProperty(GCUBE_PRE_PROD_PARTHENOS_REGISTRY_VARNAME);
|
||||
GCUBE_PRE_PROD_PREVRE = properties.getProperty(GCUBE_PRE_PROD_PREVRE_VARNAME);
|
||||
ROOT = properties.getProperty(ROOT_VARNAME);
|
||||
|
||||
DEFAULT_TEST_SCOPE = GCUBE_PRE_PROD_PREVRE;
|
||||
|
||||
}
|
||||
|
||||
public static String getCurrentScope(String token) throws ObjectNotFound, Exception{
|
||||
AuthorizationEntry authorizationEntry = Constants.authorizationService().get(token);
|
||||
String context = authorizationEntry.getContext();
|
||||
logger.info("Context of token {} is {}", token, context);
|
||||
return context;
|
||||
}
|
||||
|
||||
|
||||
public static void setContext(String token) throws ObjectNotFound, Exception{
|
||||
SecurityTokenProvider.instance.set(token);
|
||||
AuthorizationEntry authorizationEntry = Constants.authorizationService().get(token);
|
||||
ClientInfo clientInfo = authorizationEntry.getClientInfo();
|
||||
logger.debug("User : {} - Type : {}", clientInfo.getId(), clientInfo.getType().name());
|
||||
String qualifier = authorizationEntry.getQualifier();
|
||||
Caller caller = new Caller(clientInfo, qualifier);
|
||||
AuthorizationProvider.instance.set(caller);
|
||||
ScopeProvider.instance.set(getCurrentScope(token));
|
||||
}
|
||||
|
||||
@BeforeClass
|
||||
public static void beforeClass() throws Exception{
|
||||
setContext(DEFAULT_TEST_SCOPE);
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void afterClass() throws Exception{
|
||||
SecurityTokenProvider.instance.reset();
|
||||
ScopeProvider.instance.reset();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,134 @@
|
|||
package org.gcube.phd;
|
||||
|
||||
import static org.gcube.common.authorization.client.Constants.authorizationService;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import java.io.StringWriter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import org.gcube.common.authorization.client.Constants;
|
||||
import org.gcube.common.authorization.library.AuthorizationEntry;
|
||||
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
|
||||
import org.gcube.common.authorization.library.provider.UserInfo;
|
||||
import org.gcube.common.resources.gcore.GCoreEndpoint;
|
||||
import org.gcube.common.resources.gcore.GenericResource;
|
||||
import org.gcube.common.resources.gcore.HostingNode;
|
||||
import org.gcube.common.resources.gcore.Resource;
|
||||
import org.gcube.common.resources.gcore.Resources;
|
||||
import org.gcube.common.resources.gcore.ServiceEndpoint;
|
||||
import org.gcube.common.resources.gcore.Software;
|
||||
import org.gcube.common.scope.impl.ScopeBean;
|
||||
import org.gcube.context.ContextElaborator;
|
||||
import org.gcube.resources.discovery.client.api.DiscoveryClient;
|
||||
import org.gcube.resources.discovery.client.queries.api.SimpleQuery;
|
||||
import org.gcube.resources.discovery.icclient.ICFactory;
|
||||
import org.gcube.testutility.ContextTest;
|
||||
import org.junit.Test;
|
||||
|
||||
public class GlobalStatistics extends ContextElaborator {
|
||||
|
||||
private File directory;
|
||||
|
||||
private Set<String> visited;
|
||||
|
||||
public GlobalStatistics() {
|
||||
visited = new HashSet<String>();
|
||||
directory = new File("/home/lucafrosini/Desktop/Statistiche/Global");
|
||||
if(!directory.exists()) {
|
||||
directory.mkdirs();
|
||||
}
|
||||
}
|
||||
|
||||
private void printLineToFile(String line, File file) throws IOException {
|
||||
synchronized (file) {
|
||||
try (FileWriter fw = new FileWriter(file, true);
|
||||
BufferedWriter bw = new BufferedWriter(fw);
|
||||
PrintWriter out = new PrintWriter(bw)) {
|
||||
out.println(line);
|
||||
out.flush();
|
||||
} catch (IOException e) {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected String getUnmarshalledResource(Resource gr) {
|
||||
StringWriter stringWriter = new StringWriter();
|
||||
Resources.marshal(gr, stringWriter);
|
||||
return stringWriter.toString();
|
||||
}
|
||||
|
||||
private void addToCSV(Resource r) throws IOException {
|
||||
StringBuffer stringBuffer = new StringBuffer();
|
||||
File statisticsFile = new File(directory, r.getClass().getSimpleName() + ".csv");
|
||||
if(!statisticsFile.exists()) {
|
||||
statisticsFile.createNewFile();
|
||||
stringBuffer.append(r.getClass().getSimpleName());
|
||||
stringBuffer.append("(byte size)");
|
||||
printLineToFile(stringBuffer.toString(), statisticsFile);
|
||||
stringBuffer = new StringBuffer();
|
||||
}
|
||||
String unmarshalledR = getUnmarshalledResource(r);
|
||||
final byte[] grUTF8Bytes = unmarshalledR.getBytes("UTF-8");
|
||||
stringBuffer.append(grUTF8Bytes.length);
|
||||
printLineToFile(stringBuffer.toString(), statisticsFile);
|
||||
}
|
||||
|
||||
public <R extends Resource> void analizeInstances(Class<R> clz) throws IOException {
|
||||
DiscoveryClient<R> client = ICFactory.clientFor(clz);
|
||||
SimpleQuery query = ICFactory.queryFor(clz);
|
||||
List<R> instances = client.submit(query);
|
||||
for(R r : instances){
|
||||
String id = r.id();
|
||||
if(!visited.contains(id)) {
|
||||
visited.add(id);
|
||||
addToCSV(r);
|
||||
}else {
|
||||
logger.debug("{} already managed", id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void generateStatistics() throws Exception {
|
||||
List<Class<? extends Resource>> classes = new ArrayList<>();
|
||||
classes.add(GCoreEndpoint.class);
|
||||
classes.add(HostingNode.class);
|
||||
classes.add(ServiceEndpoint.class);
|
||||
classes.add(GenericResource.class);
|
||||
classes.add(Software.class);
|
||||
|
||||
for(Class<? extends Resource> clz : classes) {
|
||||
analizeInstances(clz);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public String generateUserToken(String context) throws Exception {
|
||||
AuthorizationEntry authorizationEntry = Constants.authorizationService().get(SecurityTokenProvider.instance.get());
|
||||
UserInfo userInfo = (UserInfo) authorizationEntry.getClientInfo();
|
||||
String userToken = authorizationService().generateUserToken(userInfo, context);
|
||||
logger.trace("Token for Context {} for {} is {}", context, userInfo.getId(), userToken);
|
||||
return userToken;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void elaborateContext(ScopeBean scopeBean) throws Exception {
|
||||
String token = generateUserToken(scopeBean.toString());
|
||||
ContextTest.setContext(token);
|
||||
generateStatistics();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test() throws Exception {
|
||||
ContextTest.setContextByName("/d4science.research-infrastructures.eu");
|
||||
all();
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue