Creating a gcat-client refs #13216

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-publishing/gcat-client@176865 82a268e6-3cf1-43bd-a215-b396298e98cf
master
Luca Frosini 5 years ago
commit b27ea3de74

@ -0,0 +1,36 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>

@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>gcat-client</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
</natures>
</projectDescription>

@ -0,0 +1,6 @@
eclipse.preferences.version=1
encoding//src/main/java=UTF-8
encoding//src/main/resources=UTF-8
encoding//src/test/java=UTF-8
encoding//src/test/resources=UTF-8
encoding/<project>=UTF-8

@ -0,0 +1,5 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.source=1.8

@ -0,0 +1,4 @@
activeProfiles=
eclipse.preferences.version=1
resolveWorkspaceProjects=true
version=1

@ -0,0 +1,104 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.gcube.tools</groupId>
<artifactId>maven-parent</artifactId>
<version>1.0.0</version>
</parent>
<groupId>org.gcube.data-publishing</groupId>
<artifactId>gcat-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>gCat Client</name>
<description>gCat Client</description>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.gcube.distribution</groupId>
<artifactId>gcube-bom</artifactId>
<version>LATEST</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.gcube.distribution</groupId>
<artifactId>gcube-smartgears-bom</artifactId>
<version>LATEST</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<distroDirectory>${project.basedir}/distro</distroDirectory>
<serviceClass>DataPublishing</serviceClass>
</properties>
<scm>
<connection>scm:https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-publishing/${project.artifactId}</connection>
<developerConnection>scm:https://svn.d4science.research-infrastructures.eu/gcube//trunk/data-publishing/${project.artifactId}</developerConnection>
<url>https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-publishing/${project.artifactId}</url>
</scm>
<dependencies>
<dependency>
<groupId>org.gcube.data-publishing</groupId>
<artifactId>gcat-api</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.gcube.resources.discovery</groupId>
<artifactId>ic-client</artifactId>
</dependency>
<dependency>
<groupId>org.gcube.common</groupId>
<artifactId>gxHTTP</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>2.27</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<scope>provided</scope>
</dependency>
<!-- Test Dependencies -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.0.13</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<id>make-servicearchive</id>
<phase>package</phase>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

@ -0,0 +1,195 @@
package org.gcube.gcat.client;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.ws.rs.ForbiddenException;
import javax.ws.rs.NotFoundException;
import javax.ws.rs.WebApplicationException;
import org.gcube.common.gxhttp.reference.GXConnection.HTTPMETHOD;
import org.gcube.common.gxhttp.request.GXHTTPStringRequest;
import org.gcube.gcat.api.GCatConstants;
/**
* @author Luca Frosini (ISTI - CNR)
*/
abstract class GCatClient {
protected URL serviceURL;
protected List<String> basePaths;
protected GXHTTPStringRequest gxhttpStringRequest;
public GCatClient(String basePath, String... basePaths) throws MalformedURLException {
this.serviceURL = GCatClientDiscovery.getServiceURL();
this.basePaths = new ArrayList<>();
this.basePaths.add(basePath);
this.basePaths.addAll(Arrays.asList(basePaths));
}
protected static StringBuilder getStringBuilder(InputStream inputStream) throws IOException {
StringBuilder result = new StringBuilder();
try(BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
String line;
while((line = reader.readLine()) != null) {
result.append(line);
}
}
return result;
}
protected String parseHttpURLConnection(HttpURLConnection connection) throws WebApplicationException {
try {
int responseCode = connection.getResponseCode();
// String responseMessage = connection.getResponseMessage();
if(connection.getRequestMethod().compareTo(HTTPMETHOD.HEAD.toString()) == 0) {
if(responseCode == HttpURLConnection.HTTP_NO_CONTENT) {
return null;
}
if(responseCode == HttpURLConnection.HTTP_NOT_FOUND) {
throw new NotFoundException();
}
if(responseCode == HttpURLConnection.HTTP_FORBIDDEN) {
throw new ForbiddenException();
}
}
if(responseCode >= HttpURLConnection.HTTP_BAD_REQUEST) {
InputStream inputStream = connection.getErrorStream();
StringBuilder result = getStringBuilder(inputStream);
String res = result.toString();
throw new WebApplicationException(res, responseCode);
}
StringBuilder result = getStringBuilder(connection.getInputStream());
return result.toString();
} catch (WebApplicationException e) {
throw e;
} catch (Exception e) {
throw new WebApplicationException(e);
} finally {
connection.disconnect();
}
}
protected void initRequest() throws UnsupportedEncodingException {
gxhttpStringRequest = GXHTTPStringRequest.newRequest(serviceURL.toString());
gxhttpStringRequest.from(GCatClient.class.getSimpleName());
for(String p : basePaths) {
gxhttpStringRequest.path(p);
}
}
protected String list(Map<String, String> queryParams, String... paths) throws WebApplicationException {
try {
initRequest();
for(String p : paths) {
gxhttpStringRequest.path(p);
}
gxhttpStringRequest.queryParams(queryParams);
HttpURLConnection httpURLConnection = gxhttpStringRequest.get();
return parseHttpURLConnection(httpURLConnection);
}catch (WebApplicationException e) {
throw e;
}catch (Exception e) {
throw new WebApplicationException(e);
}
}
protected String create(String body, String... paths) {
try {
initRequest();
for(String p : paths) {
gxhttpStringRequest.path(p);
}
HttpURLConnection httpURLConnection = gxhttpStringRequest.post(body);
return parseHttpURLConnection(httpURLConnection);
}catch (WebApplicationException e) {
throw e;
}catch (Exception e) {
throw new WebApplicationException(e);
}
}
protected String read(String... paths) throws WebApplicationException {
try {
initRequest();
for(String p : paths) {
gxhttpStringRequest.path(p);
}
HttpURLConnection httpURLConnection = gxhttpStringRequest.get();
return parseHttpURLConnection(httpURLConnection);
}catch (WebApplicationException e) {
throw e;
}catch (Exception e) {
throw new WebApplicationException(e);
}
}
protected String update(String body, String... paths) throws WebApplicationException {
try {
initRequest();
for(String p : paths) {
gxhttpStringRequest.path(p);
}
HttpURLConnection httpURLConnection = gxhttpStringRequest.put(body);
return parseHttpURLConnection(httpURLConnection);
}catch (WebApplicationException e) {
throw e;
}catch (Exception e) {
throw new WebApplicationException(e);
}
}
protected String patch(String body, String... paths) throws WebApplicationException {
try {
initRequest();
for(String p : paths) {
gxhttpStringRequest.path(p);
}
gxhttpStringRequest.withBody(body);
HttpURLConnection httpURLConnection = gxhttpStringRequest.patch();
return parseHttpURLConnection(httpURLConnection);
}catch (WebApplicationException e) {
throw e;
}catch (Exception e) {
throw new WebApplicationException(e);
}
}
protected String delete(Boolean purge, String... paths) throws WebApplicationException {
try {
initRequest();
for(String p : paths) {
gxhttpStringRequest.path(p);
}
if(purge!=null) {
Map<String, String> queryParams = new HashMap<>();
queryParams.put(GCatConstants.PURGE_QUERY_PARAMETER, String.valueOf(purge));
gxhttpStringRequest.queryParams(queryParams);
}
HttpURLConnection httpURLConnection = gxhttpStringRequest.delete();
return parseHttpURLConnection(httpURLConnection);
}catch (WebApplicationException e) {
throw e;
}catch (Exception e) {
throw new WebApplicationException(e);
}
}
}

@ -0,0 +1,131 @@
package org.gcube.gcat.client;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.gcube.common.resources.gcore.GCoreEndpoint;
import org.gcube.common.resources.gcore.ServiceEndpoint;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.gcat.api.GCatConstants;
import org.gcube.resources.discovery.client.queries.api.SimpleQuery;
import org.gcube.resources.discovery.icclient.ICFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author Luca Frosini (ISTI - CNR)
*/
public class GCatClientDiscovery {
private static final Logger logger = LoggerFactory.getLogger(GCatClientDiscovery.class);
protected static Map<String, URL> clients;
static {
clients = new HashMap<>();
}
protected static void forceToURL(String adddress) throws MalformedURLException {
if(adddress!=null && adddress.compareTo("")!=0) {
String context = getContext();
URL url = new URL(adddress);
clients.put(context, url);
}
}
private static String classFormat = "$resource/Profile/ServiceClass/text() eq '%1s'";
private static String nameFormat = "$resource/Profile/ServiceName/text() eq '%1s'";
private static String statusFormat = "$resource/Profile/DeploymentData/Status/text() eq 'ready'";
private static String containsFormat = "$entry/@EntryName eq '%1s'";
private static String serviceEndpointCategoryFormat = "$resource/Profile/Category/text() eq '%1s'";
private static String serviceEndpointNameFormat = "$resource/Profile/Name/text() eq '%1s'";
private static String serviceEndpointstatusFormat = "$resource/Profile/RunTime/Status/text() eq 'READY'";
private static SimpleQuery queryForService(){
return ICFactory.queryFor(GCoreEndpoint.class)
.addCondition(String.format(classFormat, GCatConstants.SERVICE_CLASS))
.addCondition(String.format(nameFormat, GCatConstants.SERVICE_NAME))
.addCondition(String.format(statusFormat))
.addVariable("$entry","$resource/Profile/AccessPoint/RunningInstanceInterfaces/Endpoint")
.addCondition(String.format(containsFormat, GCatConstants.SERVICE_ENTRY_NAME))
.setResult("$entry/text()");
}
private static SimpleQuery queryForProxy(){
return ICFactory.queryFor(ServiceEndpoint.class)
.addCondition(String.format(serviceEndpointCategoryFormat, GCatConstants.SERVICE_CLASS))
.addCondition(String.format(serviceEndpointNameFormat, GCatConstants.SERVICE_NAME))
.addCondition(String.format(serviceEndpointstatusFormat))
.addVariable("$entry","$resource/Profile/AccessPoint/Interface/Endpoint")
.addCondition(String.format(containsFormat, GCatConstants.SERVICE_ENTRY_NAME))
.setResult("$entry/text()");
}
protected static List<String> getAddresses(){
List<String> addresses = new ArrayList<>();
try {
SimpleQuery proxyQuery = queryForProxy();
addresses = ICFactory.client().submit(proxyQuery);
if(addresses==null || addresses.isEmpty()){
throw new Exception("No ResourceRegistry Proxy Found");
}
} catch (Exception e) {
logger.debug("{}. Looking for RunningInstance.", e.getMessage());
SimpleQuery serviceQuery = queryForService();
addresses = ICFactory.client().submit(serviceQuery);
}
return addresses;
}
protected static String getContext() {
String context = null;
if (SecurityTokenProvider.instance.get() == null) {
if (ScopeProvider.instance.get() == null) {
throw new RuntimeException(
"Null Token and Scope. Please set your token first.");
}
context = ScopeProvider.instance.get();
} else {
context = SecurityTokenProvider.instance.get();
}
return context;
}
public static URL getServiceURL() throws MalformedURLException {
String context = getContext();
URL url = clients.get(context);
if(url==null){
List<String> addresses = getAddresses();
if(addresses==null || addresses.isEmpty()){
String error = String.format("No %s:%s found in the current context", GCatConstants.SERVICE_CLASS, GCatConstants.SERVICE_NAME);
throw new RuntimeException(error);
}
Random random = new Random();
int index = random.nextInt(addresses.size());
url = new URL(addresses.get(index));
clients.put(context, url);
}
return url;
}
}

@ -0,0 +1,67 @@
package org.gcube.gcat.client;
import java.net.MalformedURLException;
import java.util.HashMap;
import java.util.Map;
import javax.ws.rs.WebApplicationException;
import javax.xml.ws.WebServiceException;
import org.gcube.gcat.api.GCatConstants;
/**
* @author Luca Frosini (ISTI - CNR)
*/
public class Group extends GCatClient implements org.gcube.gcat.api.interfaces.Group<String,Void> {
public Group() throws MalformedURLException {
super(GROUPS);
}
@Override
public String list(int limit, int offset) throws WebApplicationException {
Map<String, String> queryParams = new HashMap<>();
queryParams.put(GCatConstants.LIMIT_PARAMETER, String.valueOf(limit));
queryParams.put(GCatConstants.OFFSET_PARAMETER, String.valueOf(offset));
return super.list(queryParams);
}
@Override
public String create(String json) {
return super.create(json);
}
@Override
public String read(String name) {
return super.read(name);
}
@Override
public String update(String name, String json) {
return super.update(json, name);
}
@Override
public String patch(String name, String json) {
return super.patch(json, name);
}
@Override
public Void delete(String name) {
super.delete(false, name);
return null;
}
@Override
public Void delete(String name, boolean purge) throws WebServiceException {
super.delete(purge, name);
return null;
}
@Override
public Void purge(String name) throws WebServiceException {
super.delete(true, name);
return null;
}
}

@ -0,0 +1,61 @@
package org.gcube.gcat.client;
import java.net.MalformedURLException;
import java.util.HashMap;
import java.util.Map;
import javax.ws.rs.WebApplicationException;
import javax.xml.ws.WebServiceException;
import org.gcube.gcat.api.GCatConstants;
/**
* @author Luca Frosini (ISTI - CNR)
*/
public class Item extends GCatClient implements org.gcube.gcat.api.interfaces.Item<String,Void> {
public Item() throws MalformedURLException {
super(ITEMS);
}
@Override
public String list(int limit, int offset) throws WebApplicationException {
Map<String, String> queryParams = new HashMap<>();
queryParams.put(GCatConstants.LIMIT_PARAMETER, String.valueOf(limit));
queryParams.put(GCatConstants.OFFSET_PARAMETER, String.valueOf(offset));
return super.list(queryParams);
}
@Override
public String create(String json) {
return super.create(json);
}
@Override
public String read(String name) {
return super.read(name);
}
@Override
public String update(String name, String json) {
return super.update(json, name);
}
@Override
public Void delete(String name) {
super.delete(false, name);
return null;
}
@Override
public Void delete(String name, boolean purge) throws WebServiceException {
super.delete(purge, name);
return null;
}
@Override
public Void purge(String name) throws WebServiceException {
super.delete(true, name);
return null;
}
}

@ -0,0 +1,19 @@
package org.gcube.gcat.client;
import java.net.MalformedURLException;
/**
* @author Luca Frosini (ISTI - CNR)
*/
public class License extends GCatClient implements org.gcube.gcat.api.interfaces.License {
public License() throws MalformedURLException {
super(LICENSES);
}
@Override
public String list() {
return super.list(null);
}
}

@ -0,0 +1,19 @@
package org.gcube.gcat.client;
import java.net.MalformedURLException;
/**
* @author Luca Frosini (ISTI - CNR)
*/
public class Namespace extends GCatClient implements org.gcube.gcat.api.interfaces.Namespace {
public Namespace() throws MalformedURLException {
super(NAMESPACES);
}
@Override
public String list() {
return super.list(null);
}
}

@ -0,0 +1,54 @@
package org.gcube.gcat.client;
import java.net.MalformedURLException;
import java.util.HashMap;
import java.util.Map;
import javax.ws.rs.WebApplicationException;
import org.gcube.gcat.api.GCatConstants;
/**
* @author Luca Frosini (ISTI - CNR)
*/
public class Organization extends GCatClient implements org.gcube.gcat.api.interfaces.Organization<String,Void> {
public Organization() throws MalformedURLException {
super(ORGANIZATIONS);
}
@Override
public String list(int limit, int offset) throws WebApplicationException {
Map<String, String> queryParams = new HashMap<>();
queryParams.put(GCatConstants.LIMIT_PARAMETER, String.valueOf(limit));
queryParams.put(GCatConstants.OFFSET_PARAMETER, String.valueOf(offset));
return super.list(queryParams);
}
@Override
public String create(String json) {
return super.create(json);
}
@Override
public String read(String name) {
return super.read(name);
}
@Override
public String update(String name, String json) {
return super.update(json, name);
}
@Override
public String patch(String name, String json) {
return super.patch(json, name);
}
@Override
public Void delete(String name) {
super.delete(null, name);
return null;
}
}

@ -0,0 +1,72 @@
package org.gcube.gcat.client;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import org.gcube.gcat.api.GCatConstants;
/**
* @author Luca Frosini (ISTI - CNR)
*/
public class Profile extends GCatClient implements org.gcube.gcat.api.interfaces.Profile<String,Void> {
public Profile() throws MalformedURLException {
super(PROFILES);
}
@Override
public String list() throws WebApplicationException {
return super.list(null);
}
@Override
public String create(String name, String xml) {
try {
initRequest();
HttpURLConnection httpURLConnection = gxhttpStringRequest.put(xml);
return parseHttpURLConnection(httpURLConnection);
}catch (WebApplicationException e) {
throw e;
}catch (Exception e) {
throw new WebApplicationException(e);
}
}
@Override
public String read(String name) {
try {
initRequest();
gxhttpStringRequest.path(name);
gxhttpStringRequest.header("Accept", MediaType.APPLICATION_XML);
HttpURLConnection httpURLConnection = gxhttpStringRequest.get();
return parseHttpURLConnection(httpURLConnection);
}catch (WebApplicationException e) {
throw e;
}catch (Exception e) {
throw new WebApplicationException(e);
}
}
public String read(String name, boolean asJSON) {
if(!asJSON) {
return read(name);
}
gxhttpStringRequest.header("Accept", GCatConstants.APPLICATION_JSON_CHARSET_UTF_8);
return super.read(name);
}
@Override
public String update(String name, String xml) {
return super.update(xml, name);
}
@Override
public Void delete(String name) {
super.delete(null, name);
return null;
}
}

@ -0,0 +1,41 @@
package org.gcube.gcat.client;
import java.net.MalformedURLException;
/**
* @author Luca Frosini (ISTI - CNR)
*/
public class Resource extends GCatClient implements org.gcube.gcat.api.interfaces.Resource<String,Void> {
public Resource() throws MalformedURLException {
super(Item.ITEMS);
}
@Override
public String list(String itemID) {
return super.list(null, itemID, RESOURCES);
}
@Override
public String create(String itemID, String json) {
return super.create(json, itemID, RESOURCES);
}
@Override
public String read(String itemID, String resourceID) {
return super.read(itemID, RESOURCES, resourceID);
}
@Override
public String update(String itemID, String resourceID, String json) {
return super.read(json, itemID, RESOURCES, resourceID);
}
@Override
public Void delete(String itemID, String resourceID) {
super.delete(null, itemID, RESOURCES, resourceID);
return null;
}
}

@ -0,0 +1,44 @@
package org.gcube.gcat.client;
import java.net.MalformedURLException;
import javax.xml.ws.WebServiceException;
/**
* @author Luca Frosini (ISTI - CNR)
*/
public class User extends GCatClient implements org.gcube.gcat.api.interfaces.User<String,Void> {
public User() throws MalformedURLException {
super(USERS);
}
@Override
public String list() {
return super.list(null);
}
@Override
public String create(String json) throws WebServiceException {
return super.create(json);
}
@Override
public String read(String username) {
return super.read(username);
}
@Override
public String update(String username, String json) {
return super.update(json, username);
}
@Override
public Void delete(String username) {
super.delete(null, username);
return null;
}
}

@ -0,0 +1,143 @@
/**
*
*/
package org.gcube.gcat.client;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
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.gcube.gcat.client.GCatClientDiscovery;
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 TOKEN_PROPERTIES_FILENAME = "token.properties";
protected static final String CONFIG_PROPERTIES_FILENAME = "config.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;
public static final String FORCED_URL_VARNAME = "FORCED_URL";
public static final String FORCED_URL;
static {
Properties properties = new Properties();
InputStream input = ContextTest.class.getClassLoader().getResourceAsStream(TOKEN_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;
properties = new Properties();
input = ContextTest.class.getClassLoader().getResourceAsStream(CONFIG_PROPERTIES_FILENAME);
try {
// load the properties file
properties.load(input);
} catch (IOException e) {
throw new RuntimeException(e);
}
FORCED_URL = properties.getProperty(FORCED_URL_VARNAME);
try {
GCatClientDiscovery.forceToURL(FORCED_URL);
} catch(MalformedURLException e) {
throw new RuntimeException(e);
}
}
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,89 @@
package org.gcube.gcat.client;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.Assert;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
/**
* @author Luca Frosini (ISTI - CNR)
*/
public class GroupTest extends ContextTest {
private static Logger logger = LoggerFactory.getLogger(GroupTest.class);
private static final String NAME_KEY = "name";
private static final String NAME_VALUE = "0000";
private static final String TITLE_KEY = "title";
private static final String TITLE_VALUE = "0000 Title";
private static final String DISPLAY_NAME_KEY = "display_name";
private static final String DISPLAY_NAME_VALUE = "0000 Display Name";
// @Test
public void completeTest() throws IOException {
ObjectMapper mapper = new ObjectMapper();
Map<String,Object> map = new HashMap<>();
map.put(NAME_KEY, NAME_VALUE);
map.put(TITLE_KEY, TITLE_VALUE);
map.put(DISPLAY_NAME_KEY, DISPLAY_NAME_VALUE);
JavaType type = mapper.getTypeFactory().constructCollectionType(ArrayList.class, String.class);
Group group = new Group();
String json = mapper.writeValueAsString(map);
logger.debug("Going to create {}", json);
try {
group.create(json);
String list = group.list(10, 0);
logger.debug("Got list", list);
List<String> groups = mapper.readValue(list, type);
String name = groups.get(0);
Assert.assertTrue(name.compareTo(NAME_VALUE) == 0);
} catch(AssertionError e) {
group.delete(NAME_VALUE, true);
}
group.delete(NAME_VALUE, true);
String list = group.list(10, 0);
logger.debug("Got list", list);
List<String> groups = mapper.readValue(list, type);
String name = groups.get(0);
Assert.assertTrue(name.compareTo(NAME_VALUE) != 0);
}
@Test
public void safeTest() throws IOException {
ObjectMapper mapper = new ObjectMapper();
JavaType type = mapper.getTypeFactory().constructCollectionType(ArrayList.class, String.class);
Group group = new Group();
String list = group.list(10, 0);
logger.debug("Got list {}", list);
List<String> groups = mapper.readValue(list, type);
String name = groups.get(0);
String ret = group.read(name);
logger.debug("Got group {}", ret);
JsonNode jsonNode = mapper.readTree(ret);
String gotName = jsonNode.get(NAME_KEY).asText();
Assert.assertTrue(name.compareTo(gotName) == 0);
}
}

@ -0,0 +1,150 @@
package org.gcube.gcat.client;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.Assert;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
/**
* @author Luca Frosini (ISTI - CNR)
*/
public class ItemTest extends ContextTest {
private static Logger logger = LoggerFactory.getLogger(ItemTest.class);
private static final String NAME_KEY = "name";
private static final String NAME_VALUE = "00";
private static final String TITLE_KEY = "title";
private static final String TITLE_VALUE = "00 Title";
private static final String PRIVATE_KEY = "private";
private static final boolean PRIVATE_VALUE = false;
private static final String LICENSE_ID_KEY = "license_id";
private static final String TAGS_KEY = "tags";
private static final String EXTRAS_KEY = "extras";
private static final String KEY_KEY = "key";
private static final String VALUE_KEY = "value";
private static final String TYPE_KEY_VALUE = "system:type";
private static final String TYPE_VALUE_VALUE = "EmptyProfile";
private static final String TAG_VALUE = "MyTag";
private static final String ID_KEY = "id";
@Test
public void completeTest() throws IOException {
ObjectMapper mapper = new ObjectMapper();
JavaType listType = mapper.getTypeFactory().constructCollectionType(ArrayList.class, String.class);
JavaType licenseArrayType = mapper.getTypeFactory().constructCollectionType(ArrayList.class, JsonNode.class);
License license = new License();
String licencesStrings = license.list();
logger.debug("Got licenses {}", licencesStrings);
List<JsonNode> licences = mapper.readValue(licencesStrings, licenseArrayType);
JsonNode licenseJsonNode = licences.get(0);
String licenseID = licenseJsonNode.get(ID_KEY).asText();
Map<String,Object> map = new HashMap<>();
map.put(NAME_KEY, NAME_VALUE);
map.put(TITLE_KEY, TITLE_VALUE);
map.put(PRIVATE_KEY, PRIVATE_VALUE);
map.put(LICENSE_ID_KEY, licenseID);
List<Map<String,Object>> tags = new ArrayList<>();
Map<String,Object> tag = new HashMap<>();
tag.put(NAME_KEY, TAG_VALUE);
tags.add(tag);
map.put(TAGS_KEY, tags);
List<Map<String,Object>> extras = new ArrayList<>();
Map<String,Object> type = new HashMap<>();
type.put(KEY_KEY, TYPE_KEY_VALUE);
type.put(VALUE_KEY, TYPE_VALUE_VALUE);
extras.add(type);
map.put(EXTRAS_KEY, extras);
Item item = new Item();
String json = mapper.writeValueAsString(map);
logger.debug("Going to create {}", json);
item.create(json);
try {
String itemsString = item.list(10, 0);
logger.debug("Got items {}", itemsString);
List<String> items = mapper.readValue(itemsString, listType);
String name = items.get(0);
Assert.assertTrue(name.compareTo(NAME_VALUE) == 0);
} catch(AssertionError e) {
item.delete(NAME_VALUE, true);
throw e;
}
try {
String itemString = item.read(NAME_VALUE);
logger.debug("Got item {}", itemString);
JsonNode jsonNode = mapper.readTree(itemString);
String gotName = jsonNode.get(NAME_KEY).asText();
Assert.assertTrue(gotName.compareTo(NAME_VALUE) == 0);
String gotTitle = jsonNode.get(TITLE_KEY).asText();
Assert.assertTrue(gotTitle.compareTo(TITLE_VALUE) == 0);
boolean privateValue = jsonNode.get(PRIVATE_KEY).asBoolean();
Assert.assertTrue(privateValue==PRIVATE_VALUE);
String gotLicenseID = jsonNode.get(LICENSE_ID_KEY).asText();
Assert.assertTrue(gotLicenseID.compareTo(licenseID) == 0);
}catch (Throwable e) {
item.delete(NAME_VALUE, true);
throw e;
}
item.delete(NAME_VALUE, true);
String itemsString = item.list(10, 0);
logger.debug("Got list", itemsString);
List<String> items = mapper.readValue(itemsString, listType);
String name = items.get(0);
Assert.assertTrue(name.compareTo(NAME_VALUE) != 0);
}
@Test
public void safeTest() throws IOException {
ObjectMapper mapper = new ObjectMapper();
JavaType listType = mapper.getTypeFactory().constructCollectionType(ArrayList.class, String.class);
Item item = new Item();
String itemsString = item.list(10, 0);
logger.debug("Got list {}", itemsString);
List<String> items = mapper.readValue(itemsString, listType);
String name = items.get(0);
String ret = item.read(name);
logger.debug("Got item {}", ret);
JsonNode jsonNode = mapper.readTree(ret);
String gotName = jsonNode.get(NAME_KEY).asText();
Assert.assertTrue(name.compareTo(gotName) == 0);
}
}

@ -0,0 +1,40 @@
package org.gcube.gcat.client;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.junit.Assert;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
/**
* @author Luca Frosini (ISTI - CNR)
*/
public class LicenseTest extends ContextTest {
private static Logger logger = LoggerFactory.getLogger(LicenseTest.class);
private static final String ID_KEY = "id";
@Test
public void safeTest() throws IOException {
ObjectMapper mapper = new ObjectMapper();
JavaType licenseArrayType = mapper.getTypeFactory().constructCollectionType(ArrayList.class, JsonNode.class);
License license = new License();
String licencesStrings = license.list();
logger.debug("Got licenses {}", licencesStrings);
List<JsonNode> licences = mapper.readValue(licencesStrings, licenseArrayType);
Assert.assertTrue(licences.size()>0);
for(JsonNode licenseJsonNode : licences) {
String licenseID = licenseJsonNode.get(ID_KEY).asText();
logger.debug("License : {}", licenseID);
}
}
}

@ -0,0 +1,40 @@
package org.gcube.gcat.client;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.junit.Assert;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
/**
* @author Luca Frosini (ISTI - CNR)
*/
public class NamespaceTest extends ContextTest {
private static Logger logger = LoggerFactory.getLogger(NamespaceTest.class);
private static final String ID_KEY = "id";
@Test
public void safeTest() throws IOException {
ObjectMapper mapper = new ObjectMapper();
JavaType arrayType = mapper.getTypeFactory().constructCollectionType(ArrayList.class, JsonNode.class);
Namespace namespace = new Namespace();
String namespacesStrings = namespace.list();
logger.debug("Got namespace {}", namespacesStrings);
List<JsonNode> namespaces = mapper.readValue(namespacesStrings, arrayType);
Assert.assertTrue(namespaces.size()>0);
for(JsonNode namespaceJsonNode : namespaces) {
String namespaceID = namespaceJsonNode.get(ID_KEY).asText();
logger.debug("Namespace : {}", namespaceID);
}
}
}

@ -0,0 +1,89 @@
package org.gcube.gcat.client;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.Assert;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
/**
* @author Luca Frosini (ISTI - CNR)
*/
public class OrganizationTest extends ContextTest {
private static Logger logger = LoggerFactory.getLogger(OrganizationTest.class);
private static final String NAME_KEY = "name";
private static final String NAME_VALUE = "00";
private static final String TITLE_KEY = "title";
private static final String TITLE_VALUE = "00 Title";
private static final String DISPLAY_NAME_KEY = "display_name";
private static final String DISPLAY_NAME_VALUE = "0000 Display Name";
// @Test
public void completeTest() throws IOException {
ObjectMapper mapper = new ObjectMapper();
Map<String,Object> map = new HashMap<>();
map.put(NAME_KEY, NAME_VALUE);
map.put(TITLE_KEY, TITLE_VALUE);
map.put(DISPLAY_NAME_KEY, DISPLAY_NAME_VALUE);
JavaType arrayType = mapper.getTypeFactory().constructCollectionType(ArrayList.class, String.class);
Organization organization = new Organization();
String json = mapper.writeValueAsString(map);
logger.debug("Going to create {}", json);
try {
organization.create(json);
String organizationsString = organization.list(10, 0);
logger.debug("Got Organizations {}", organizationsString);
List<String> organizations = mapper.readValue(organizationsString, arrayType);
String name = organizations.get(0);
Assert.assertTrue(name.compareTo(NAME_VALUE) == 0);
} catch(AssertionError e) {
organization.delete(NAME_VALUE);
}
organization.delete(NAME_VALUE);
String organizationsString = organization.list(10, 0);
logger.debug("Got Organizations {}", organizationsString);
List<String> organizations = mapper.readValue(organizationsString, arrayType);
String name = organizations.get(0);
Assert.assertTrue(name.compareTo(NAME_VALUE) != 0);
}
@Test
public void safeTest() throws IOException {
ObjectMapper mapper = new ObjectMapper();
JavaType arrayType = mapper.getTypeFactory().constructCollectionType(ArrayList.class, String.class);
Organization organization = new Organization();
String organizationsString = organization.list(10, 0);
logger.debug("Got Organizations {}", organizationsString);
List<String> listArray = mapper.readValue(organizationsString, arrayType);
String name = listArray.get(0);
String ret = organization.read(name);
logger.debug("Got Organization {}", ret);
JsonNode jsonNode = mapper.readTree(ret);
String gotName = jsonNode.get(NAME_KEY).asText();
Assert.assertTrue(name.compareTo(gotName) == 0);
}
}

@ -0,0 +1,62 @@
package org.gcube.gcat.client;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.junit.Assert;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
/**
* @author Luca Frosini (ISTI - CNR)
*/
public class ProfileTest extends ContextTest {
private static Logger logger = LoggerFactory.getLogger(ProfileTest.class);
@Test
public void safeTest() throws IOException, ParserConfigurationException, SAXException {
ObjectMapper mapper = new ObjectMapper();
JavaType arrayType = mapper.getTypeFactory().constructCollectionType(ArrayList.class, String.class);
Profile profile = new Profile();
String profilesString = profile.list();
logger.debug("Got Profiles {}", profilesString);
List<String> profiles = mapper.readValue(profilesString, arrayType);
for(String name : profiles) {
String xml = profile.read(name);
logger.debug("Got XML Profile {}", xml);
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
InputSource is = new InputSource(new StringReader(xml));
dBuilder.parse(is);
String retAsXML = profile.read(name, false);
logger.debug("Got XML (explicit) Profile {}", retAsXML);
is = new InputSource(new StringReader(retAsXML));
dBuilder.parse(is);
Assert.assertTrue(xml.compareTo(retAsXML) == 0);
String json = profile.read(name, true);
logger.debug("Got JSON Profile {}", json);
mapper.readTree(json);
}
}
}

@ -0,0 +1,197 @@
package org.gcube.gcat.client;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.Assert;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
/**
* @author Luca Frosini (ISTI - CNR)
*/
public class ResourceTest extends ContextTest {
private static Logger logger = LoggerFactory.getLogger(ResourceTest.class);
private static final String NAME_KEY = "name";
private static final String NAME_VALUE = "00";
private static final String TITLE_KEY = "title";
private static final String TITLE_VALUE = "00 Title";
private static final String PRIVATE_KEY = "private";
private static final boolean PRIVATE_VALUE = false;
private static final String LICENSE_ID_KEY = "license_id";
private static final String TAGS_KEY = "tags";
private static final String EXTRAS_KEY = "extras";
private static final String KEY_KEY = "key";
private static final String VALUE_KEY = "value";
private static final String TYPE_KEY_VALUE = "system:type";
private static final String TYPE_VALUE_VALUE = "EmptyProfile";
private static final String TAG_VALUE = "MyTag";
private static final String ID_KEY = "id";
private static final String RESOURCE_NAME_VALUE = "Resource Name";
private static final String URL_KEY = "url";
private static final String URL_VALUE = "https://goo.gl/bFME6Q";
@Test
public void completeTest() throws IOException {
ObjectMapper mapper = new ObjectMapper();
JavaType listType = mapper.getTypeFactory().constructCollectionType(ArrayList.class, String.class);
JavaType jsonArrayType = mapper.getTypeFactory().constructCollectionType(ArrayList.class, JsonNode.class);
License license = new License();
String licencesStrings = license.list();
logger.debug("Got licenses {}", licencesStrings);
List<JsonNode> licences = mapper.readValue(licencesStrings, jsonArrayType);
JsonNode licenseJsonNode = licences.get(0);
String licenseID = licenseJsonNode.get(ID_KEY).asText();
Map<String,Object> map = new HashMap<>();
map.put(NAME_KEY, NAME_VALUE);
map.put(TITLE_KEY, TITLE_VALUE);
map.put(PRIVATE_KEY, PRIVATE_VALUE);
map.put(LICENSE_ID_KEY, licenseID);
List<Map<String,Object>> tags = new ArrayList<>();
Map<String,Object> tag = new HashMap<>();
tag.put(NAME_KEY, TAG_VALUE);
tags.add(tag);
map.put(TAGS_KEY, tags);
List<Map<String,Object>> extras = new ArrayList<>();
Map<String,Object> type = new HashMap<>();
type.put(KEY_KEY, TYPE_KEY_VALUE);
type.put(VALUE_KEY, TYPE_VALUE_VALUE);
extras.add(type);
map.put(EXTRAS_KEY, extras);
Item item = new Item();
String json = mapper.writeValueAsString(map);
logger.debug("Going to create {}", json);
String createdItemString = item.create(json);
try {
String itemsString = item.list(10, 0);
logger.debug("Got items {}", itemsString);
List<String> items = mapper.readValue(itemsString, listType);
String name = items.get(0);
Assert.assertTrue(name.compareTo(NAME_VALUE) == 0);
} catch(AssertionError e) {
item.delete(NAME_VALUE, true);
throw e;
}
try {
JsonNode createdItem = mapper.readTree(createdItemString);
String itemID = createdItem.get(ID_KEY).asText();
Resource resource = new Resource();
String resourcesString = resource.list(itemID);
logger.debug("Got resources {}", resourcesString);
List<JsonNode> resources = mapper.readValue(resourcesString, jsonArrayType);
Assert.assertTrue(resources.size()==0);
Map<String,Object> resourceMap = new HashMap<>();
resourceMap.put(NAME_KEY, RESOURCE_NAME_VALUE);
resourceMap.put(URL_KEY, URL_VALUE);
String resourceJson = mapper.writeValueAsString(resourceMap);
String createdResourceString = resource.create(itemID, resourceJson);
logger.debug("Created Resource {}", createdResourceString);
JsonNode createdResource = mapper.readTree(createdResourceString);
String resourceID = createdResource.get(ID_KEY).asText();
resource = new Resource();
resourcesString = resource.list(itemID);
logger.debug("Got resources {}", resourcesString);
resources = mapper.readValue(resourcesString, jsonArrayType);
Assert.assertTrue(resources.size()==1);
JsonNode gotResource = resources.get(0);
String gotResourceID = gotResource.get(ID_KEY).asText();
Assert.assertTrue(gotResourceID.compareTo(resourceID)==0);
String readResourceString = resource.read(itemID, resourceID);
JsonNode readResource = mapper.readTree(readResourceString);
String readResourceID = readResource.get(ID_KEY).asText();
Assert.assertTrue(readResourceID.compareTo(resourceID)==0);
resource.delete(itemID, resourceID);
resourcesString = resource.list(itemID);
logger.debug("Got resources {}", resourcesString);
resources = mapper.readValue(resourcesString, jsonArrayType);
Assert.assertTrue(resources.size()==0);
}catch (Throwable e) {
item.delete(NAME_VALUE, true);
throw e;
}
item.delete(NAME_VALUE, true);
String itemsString = item.list(10, 0);
logger.debug("Got list", itemsString);
List<String> items = mapper.readValue(itemsString, listType);
String name = items.get(0);
Assert.assertTrue(name.compareTo(NAME_VALUE) != 0);
}
@Test
public void safeTest() throws IOException {
ObjectMapper mapper = new ObjectMapper();
JavaType listType = mapper.getTypeFactory().constructCollectionType(ArrayList.class, String.class);
Item item = new Item();
String itemsString = item.list(10, 0);
logger.debug("Got items {}", itemsString);
List<String> items = mapper.readValue(itemsString, listType);
for(String name : items) {
String ret = item.read(name);
logger.debug("Got item {}", ret);
JsonNode jsonNode = mapper.readTree(ret);
String itemName = jsonNode.get(NAME_KEY).asText();
Assert.assertTrue(name.compareTo(itemName) == 0);
String itemID = jsonNode.get(ID_KEY).asText();
Resource resource = new Resource();
String resourcesString = resource.list(itemID);
logger.debug("Got resources {}", resourcesString);
List<String> resources = mapper.readValue(resourcesString, listType);
for(String resourceID : resources) {
String resourceString = resource.read(resourceID);
logger.debug("Got resource {}", resourceString);
mapper.readTree(resourceString);
}
}
}
}

@ -0,0 +1,41 @@
package org.gcube.gcat.client;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.junit.Assert;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
/**
* @author Luca Frosini (ISTI - CNR)
*/
public class UserTest extends ContextTest {
private static Logger logger = LoggerFactory.getLogger(UserTest.class);
private static final String ID_KEY = "id";
@Test
public void safeTest() throws IOException {
ObjectMapper mapper = new ObjectMapper();
JavaType arrayType = mapper.getTypeFactory().constructCollectionType(ArrayList.class, JsonNode.class);
User user = new User();
String usersString = user.list();
logger.debug("Got Users {}", usersString);
List<JsonNode> users = mapper.readValue(usersString, arrayType);
Assert.assertTrue(users.size() > 0);
for(JsonNode userJsonNode : users) {
String userID = userJsonNode.get(ID_KEY).asText();
logger.debug("Users : {}", userID);
}
}
}

@ -0,0 +1 @@
/gcube/preprod/preVRE=3c5b250d-6101-467a-b343-601a495ed4dd-98187548

@ -0,0 +1,19 @@
GCUBE=0b47600a-1c53-4a47-b07b-03851cc28c8a-98187548
GCUBE_DEVNEXT=577013f6-2d07-4071-af7b-c3c6a064fbda-98187548
GCUBE_DEVNEXT_NEXTNEXT=7c66c94c-7f6e-49cd-9a34-909cd3832f3e-98187548
GCUBE_DEVSEC=a2c82e3a-82ca-4fd9-b37d-f1eb49a22bd5-98187548
GCUBE_DEVSEC_DEVVRE=4646ff97-40d1-443c-8cf9-5892957d3d64-98187548
GCUBE_DEVNEXT_ANOTHER_USER=52b59669-ccde-46d2-a4da-108b9e941f7c-98187548
GCUBE_DEVVRE_ANOTHER_USER=f851ba11-bd3e-417a-b2c2-753b02bac506-98187548
# accountingaggregator
# ROOT=18fed2d9-030b-4c77-93af-af2015d945f7-843339462
# luca.frosini
ROOT=cb220668-c7dc-4ed5-9e51-933125d0aa57-843339462
GCUBE_PRE_PROD_PREVRE=1ba19f5d-5ca1-44d6-a8b0-c081e722327e-98187548
GCUBE_PRE_PROD_PARTHENOS_REGISTRY=4ac1fb39-e24e-416e-a98b-ab30e997e745-98187548
Loading…
Cancel
Save