From 148b5b8a78fec8b1ada5c4958f8a3baaf8db6621 Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Mon, 20 May 2019 15:20:12 +0000 Subject: [PATCH] Merged from branch git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-publishing/gcat-client@179477 82a268e6-3cf1-43bd-a215-b396298e98cf --- .classpath | 2 +- .settings/org.eclipse.jdt.core.prefs | 3 - distro/changelog.xml | 2 + pom.xml | 2 +- .../org/gcube/gcat/client/GCatClient.java | 8 +- src/main/java/org/gcube/gcat/client/Item.java | 13 +++ .../java/org/gcube/gcat/client/Profile.java | 32 ++++--- .../org/gcube/gcat/client/ContextTest.java | 93 ++++--------------- .../org/gcube/gcat/client/ProfileTest.java | 83 +++++++++++++++-- src/test/resources/config.properties | 1 - src/test/resources/token.properties | 19 ---- 11 files changed, 137 insertions(+), 121 deletions(-) delete mode 100644 src/test/resources/config.properties delete mode 100644 src/test/resources/token.properties diff --git a/.classpath b/.classpath index fae1a2b..8e795b1 100644 --- a/.classpath +++ b/.classpath @@ -22,7 +22,7 @@ - + diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs index 714351a..4ede96d 100644 --- a/.settings/org.eclipse.jdt.core.prefs +++ b/.settings/org.eclipse.jdt.core.prefs @@ -1,5 +1,2 @@ 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 diff --git a/distro/changelog.xml b/distro/changelog.xml index 07ed0af..b86bd20 100644 --- a/distro/changelog.xml +++ b/distro/changelog.xml @@ -3,6 +3,8 @@ Added the possibility to enforce gCat service URL + Added the possibility to disable social post #16322 + Added the possibility to get a porfile as JSON First Release diff --git a/pom.xml b/pom.xml index 6f95a3c..ab3320c 100644 --- a/pom.xml +++ b/pom.xml @@ -48,7 +48,7 @@ org.gcube.data-publishing gcat-api - 1.0.0-SNAPSHOT + [1.0.0-SNAPSHOT,2.0.0-SNAPSHOT) org.gcube.resources.discovery diff --git a/src/main/java/org/gcube/gcat/client/GCatClient.java b/src/main/java/org/gcube/gcat/client/GCatClient.java index 43c41d0..ffcd3b0 100644 --- a/src/main/java/org/gcube/gcat/client/GCatClient.java +++ b/src/main/java/org/gcube/gcat/client/GCatClient.java @@ -121,12 +121,15 @@ abstract class GCatClient { } } - protected String create(String body, String... paths) { + protected String create(String body, Map queryParams, String... paths) { try { initRequest(); for(String p : paths) { gxhttpStringRequest.path(p); } + if(queryParams!=null && queryParams.size()>0) { + gxhttpStringRequest.queryParams(queryParams); + } HttpURLConnection httpURLConnection = gxhttpStringRequest.post(body); return parseHttpURLConnection(httpURLConnection); }catch (WebApplicationException e) { @@ -136,6 +139,9 @@ abstract class GCatClient { } } + protected String create(String body, String... paths) { + return create(body, (Map) null, paths); + } protected String read(String... paths) throws WebApplicationException { try { diff --git a/src/main/java/org/gcube/gcat/client/Item.java b/src/main/java/org/gcube/gcat/client/Item.java index c85bcb1..14d21cc 100644 --- a/src/main/java/org/gcube/gcat/client/Item.java +++ b/src/main/java/org/gcube/gcat/client/Item.java @@ -31,6 +31,19 @@ public class Item extends GCatClient implements org.gcube.gcat.api.interfaces.It return super.list(queryParams); } + + public String create(String json, boolean socialPost) { + try { + Map queryParams = new HashMap<>(); + queryParams.put(GCatConstants.SOCIAL_POST_PARAMETER, String.valueOf(socialPost)); + return super.create(json, queryParams); + }catch (WebApplicationException e) { + throw e; + }catch (Exception e) { + throw new WebApplicationException(e); + } + } + @Override public String create(String json) { return super.create(json); diff --git a/src/main/java/org/gcube/gcat/client/Profile.java b/src/main/java/org/gcube/gcat/client/Profile.java index 37c42d7..53b0f19 100644 --- a/src/main/java/org/gcube/gcat/client/Profile.java +++ b/src/main/java/org/gcube/gcat/client/Profile.java @@ -5,6 +5,7 @@ import java.net.MalformedURLException; import java.net.URL; import javax.ws.rs.WebApplicationException; +import javax.ws.rs.core.HttpHeaders; import javax.ws.rs.core.MediaType; import org.gcube.gcat.api.GCatConstants; @@ -27,10 +28,12 @@ public class Profile extends GCatClient implements org.gcube.gcat.api.interfaces return super.list(null); } - @Override - public String create(String name, String xml) { + protected String createOrUpdate(String name, String xml) { try { initRequest(); + gxhttpStringRequest.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_XML); + gxhttpStringRequest.header(HttpHeaders.ACCEPT, MediaType.APPLICATION_XML); + gxhttpStringRequest.path(name); HttpURLConnection httpURLConnection = gxhttpStringRequest.put(xml); return parseHttpURLConnection(httpURLConnection); }catch (WebApplicationException e) { @@ -39,13 +42,26 @@ public class Profile extends GCatClient implements org.gcube.gcat.api.interfaces throw new WebApplicationException(e); } } + + @Override + public String create(String name, String xml) { + return createOrUpdate(name, xml); + } @Override public String read(String name) { + return read(name, false); + } + + public String read(String name, boolean asJSON) { try { initRequest(); gxhttpStringRequest.path(name); - gxhttpStringRequest.header("Accept", MediaType.APPLICATION_XML); + if(asJSON) { + gxhttpStringRequest.header(HttpHeaders.ACCEPT, GCatConstants.APPLICATION_JSON_CHARSET_UTF_8); + } else { + gxhttpStringRequest.header(HttpHeaders.ACCEPT, MediaType.APPLICATION_XML); + } HttpURLConnection httpURLConnection = gxhttpStringRequest.get(); return parseHttpURLConnection(httpURLConnection); }catch (WebApplicationException e) { @@ -55,17 +71,9 @@ public class Profile extends GCatClient implements org.gcube.gcat.api.interfaces } } - 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); + return createOrUpdate(name, xml); } @Override diff --git a/src/test/java/org/gcube/gcat/client/ContextTest.java b/src/test/java/org/gcube/gcat/client/ContextTest.java index 4b4c314..dc49df9 100644 --- a/src/test/java/org/gcube/gcat/client/ContextTest.java +++ b/src/test/java/org/gcube/gcat/client/ContextTest.java @@ -5,7 +5,6 @@ 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; @@ -16,7 +15,6 @@ 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; @@ -24,101 +22,44 @@ 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"; + protected static Properties properties; + 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; - - public static final String FORCED_URL_VARNAME = "FORCED_URL"; - public static final String FORCED_URL; + public static final String DEFAULT_TEST_SCOPE_NAME; 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); - + InputStream input = ContextTest.class.getClassLoader().getResourceAsStream(PROPERTIES_FILENAME); + try { // load the properties file properties.load(input); - } catch (IOException e) { + } 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); - } - + DEFAULT_TEST_SCOPE_NAME = "/pred4s/preprod/preVRE"; } - public static String getCurrentScope(String token) throws ObjectNotFound, Exception{ + 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 setContextByName(String fullContextName) throws ObjectNotFound, Exception { + String token = ContextTest.properties.getProperty(fullContextName); + setContext(token); + } - public static void setContext(String token) throws ObjectNotFound, Exception{ + public static void setContext(String token) throws ObjectNotFound, Exception { SecurityTokenProvider.instance.set(token); AuthorizationEntry authorizationEntry = Constants.authorizationService().get(token); ClientInfo clientInfo = authorizationEntry.getClientInfo(); @@ -130,12 +71,12 @@ public class ContextTest { } @BeforeClass - public static void beforeClass() throws Exception{ - setContext(DEFAULT_TEST_SCOPE); + public static void beforeClass() throws Exception { + setContextByName(DEFAULT_TEST_SCOPE_NAME); } @AfterClass - public static void afterClass() throws Exception{ + public static void afterClass() throws Exception { SecurityTokenProvider.instance.reset(); ScopeProvider.instance.reset(); } diff --git a/src/test/java/org/gcube/gcat/client/ProfileTest.java b/src/test/java/org/gcube/gcat/client/ProfileTest.java index d7fbe98..6c970c1 100644 --- a/src/test/java/org/gcube/gcat/client/ProfileTest.java +++ b/src/test/java/org/gcube/gcat/client/ProfileTest.java @@ -4,7 +4,9 @@ import java.io.IOException; import java.io.StringReader; import java.util.ArrayList; import java.util.List; +import java.util.concurrent.TimeUnit; +import javax.ws.rs.NotFoundException; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; @@ -27,14 +29,14 @@ public class ProfileTest extends ContextTest { private static Logger logger = LoggerFactory.getLogger(ProfileTest.class); @Test - public void safeTest() throws IOException, ParserConfigurationException, SAXException { + public void safeTest() throws Exception { 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); + logger.debug("Got Profiles {}\n", profilesString); List profiles = mapper.readValue(profilesString, arrayType); for(String name : profiles) { @@ -45,18 +47,85 @@ public class ProfileTest extends ContextTest { InputSource is = new InputSource(new StringReader(xml)); dBuilder.parse(is); - String retAsXML = profile.read(name, false); + String retAsXML = profile.read(name, false); logger.debug("Got XML (explicit) Profile {}", retAsXML); - is = new InputSource(new StringReader(retAsXML)); - dBuilder.parse(is); + InputSource is2 = new InputSource(new StringReader(retAsXML)); + dBuilder.parse(is2); - Assert.assertTrue(xml.compareTo(retAsXML) == 0); + //Assert.assertTrue(xml.compareTo(retAsXML) == 0); String json = profile.read(name, true); - logger.debug("Got JSON Profile {}", json); + logger.debug("Got JSON Profile {}\n", json); mapper.readTree(json); } } + public static final String PROFILE_NAME = "TestEmptyProfile"; + public static final String PROFILE_XML = "testfalseString1Test Field"; + + // @Test + public void delete() throws Exception { + Profile profile = new Profile(); + profile.delete(PROFILE_NAME); + } + + @Test + public void list() throws Exception { + Profile profile = new Profile(); + String list = profile.list(); + logger.debug("Got Profiles {}\n", list); + } + + @Test + public void createDeleteTest() throws Exception { + + ObjectMapper mapper = new ObjectMapper(); + JavaType arrayType = mapper.getTypeFactory().constructCollectionType(ArrayList.class, String.class); + + Profile profile = new Profile(); + profile.create(PROFILE_NAME, String.format(PROFILE_XML, PROFILE_NAME)); + + + String list = profile.list(); + logger.debug("Got Profiles {}\n", list); + + boolean found = false; + int count = 1; + + while(!found) { + List profiles = mapper.readValue(list, arrayType); + if(profiles.contains(PROFILE_NAME)) { + found = true; + } + if(count >= 5) { + throw new NotFoundException(String.format("%s not found after %d retries", PROFILE_NAME, count)); + } + if(!found) { + Thread.sleep(TimeUnit.SECONDS.toMillis(2*count)); + ++count; + } + } + + String xml = profile.read(PROFILE_NAME); + + profile.delete(PROFILE_NAME); + + while(found) { + List profiles = mapper.readValue(list, arrayType); + if(!profiles.contains(PROFILE_NAME)) { + found = false; + } + if(count >= 5) { + throw new Exception(String.format("%s still found after %d retries", PROFILE_NAME, count)); + } + if(found) { + Thread.sleep(TimeUnit.SECONDS.toMillis(2*count)); + ++count; + } + } + + } + + } diff --git a/src/test/resources/config.properties b/src/test/resources/config.properties deleted file mode 100644 index 9c4eab0..0000000 --- a/src/test/resources/config.properties +++ /dev/null @@ -1 +0,0 @@ -/gcube/preprod/preVRE=3c5b250d-6101-467a-b343-601a495ed4dd-98187548 \ No newline at end of file diff --git a/src/test/resources/token.properties b/src/test/resources/token.properties deleted file mode 100644 index 2d991b8..0000000 --- a/src/test/resources/token.properties +++ /dev/null @@ -1,19 +0,0 @@ -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 \ No newline at end of file