From eb40fd0eeb93914750ccb172fc084ab2d458a6a1 Mon Sep 17 00:00:00 2001 From: lucio Date: Wed, 6 Nov 2019 15:31:21 +0100 Subject: [PATCH] token ora scope made mandatory --- pom.xml | 2 +- .../stubs/registry/CollectorStubs.java | 17 ++++---- .../GCoreEndpointPublisherTests.java | 41 ++++++++++--------- src/test/resources/gCoreEndpoint.xml | 2 +- 4 files changed, 32 insertions(+), 30 deletions(-) diff --git a/pom.xml b/pom.xml index 703684b..bf5e491 100644 --- a/pom.xml +++ b/pom.xml @@ -21,7 +21,7 @@ org.gcube.resources registry-publisher - 1.3.0-SNAPSHOT + 1.3.1-SNAPSHOT diff --git a/src/main/java/org/gcube/informationsystem/publisher/stubs/registry/CollectorStubs.java b/src/main/java/org/gcube/informationsystem/publisher/stubs/registry/CollectorStubs.java index 71406e2..1181337 100644 --- a/src/main/java/org/gcube/informationsystem/publisher/stubs/registry/CollectorStubs.java +++ b/src/main/java/org/gcube/informationsystem/publisher/stubs/registry/CollectorStubs.java @@ -40,7 +40,7 @@ public class CollectorStubs implements RegistryStub { StringBuilder callUrl = new StringBuilder(endopoint).append("/").append(type); try { URL url = new URL(callUrl.toString()); - HttpURLConnection connection = makeRequest(url, "POST", false); + HttpURLConnection connection = makeRequest(url, "POST"); connection.setDoOutput(true); connection.setRequestProperty("Content-type", "text/xml"); @@ -63,7 +63,7 @@ public class CollectorStubs implements RegistryStub { StringBuilder callUrl = new StringBuilder(endopoint).append("/").append(type).append("/").append(id); try { URL url = new URL(callUrl.toString()); - HttpURLConnection connection = makeRequest(url, "PUT", false); + HttpURLConnection connection = makeRequest(url, "PUT"); connection.setDoOutput(true); connection.setRequestProperty("Content-type", "text/xml"); @@ -85,7 +85,7 @@ public class CollectorStubs implements RegistryStub { StringBuilder callUrl = new StringBuilder(endopoint).append("/").append(type).append("/").append(id); try { URL url = new URL(callUrl.toString()); - HttpURLConnection connection = makeRequest(url, "DELETE", false); + HttpURLConnection connection = makeRequest(url, "DELETE"); connection.setDoInput(true); if (connection.getResponseCode()!=200){ log.info("response code is not 200"); @@ -99,19 +99,18 @@ public class CollectorStubs implements RegistryStub { - private HttpURLConnection makeRequest(URL url, String method, boolean includeTokenInHeader) throws Exception{ + private HttpURLConnection makeRequest(URL url, String method) throws Exception{ HttpURLConnection connection; if (url.toString().startsWith("https://")) connection = (HttpsURLConnection)url.openConnection(); else connection = (HttpURLConnection)url.openConnection(); - if (includeTokenInHeader){ - if (SecurityTokenProvider.instance.get()==null) throw new RuntimeException("null token passed"); + if (SecurityTokenProvider.instance.get()!=null) connection.setRequestProperty(TOKEN_HEADER_ENTRY,SecurityTokenProvider.instance.get()); - } else { - if (ScopeProvider.instance.get()==null) throw new RuntimeException("null scope passed"); + else if (ScopeProvider.instance.get()!=null) connection.setRequestProperty(SCOPE_HEADER_ENTRY,ScopeProvider.instance.get()); - } + else throw new RuntimeException("Collector requires authorization (via token or scope)"); + connection.setRequestMethod(method); return connection; } diff --git a/src/test/java/org/gcube/informationsystem/publisher/GCoreEndpointPublisherTests.java b/src/test/java/org/gcube/informationsystem/publisher/GCoreEndpointPublisherTests.java index b75f54e..63ce882 100644 --- a/src/test/java/org/gcube/informationsystem/publisher/GCoreEndpointPublisherTests.java +++ b/src/test/java/org/gcube/informationsystem/publisher/GCoreEndpointPublisherTests.java @@ -2,6 +2,8 @@ package org.gcube.informationsystem.publisher; import static org.gcube.common.resources.gcore.Resources.print; import static org.junit.Assert.*; + +import org.gcube.common.authorization.library.provider.SecurityTokenProvider; import org.gcube.common.resources.gcore.GCoreEndpoint; import org.gcube.common.resources.gcore.Resource; import org.gcube.common.resources.gcore.Resource.Type; @@ -14,38 +16,39 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class GCoreEndpointPublisherTests { - + private static final Logger log = LoggerFactory.getLogger(RegistryPublisherTests.class); static GCoreEndpoint running; static RegistryPublisher rp; static Resource r; - + @BeforeClass public static void init(){ -// ScopeProvider.instance.set("/d4science.research-infrastructures.eu/EUBrazilOpenBio"); + // ScopeProvider.instance.set("/d4science.research-infrastructures.eu/EUBrazilOpenBio"); ScopeProvider.instance.set("/gcube/devNext"); - running = Resources.unmarshal(GCoreEndpoint.class, PublisherTest.class.getClassLoader().getResourceAsStream("gCoreEndpoint.xml")); + SecurityTokenProvider.instance.set("52b59669-ccde-46d2-a4da-108b9e941f7c-98187548"); rp=RegistryPublisherFactory.create(); } - + @Test public void printTest(){ - print(running); - //resource-specific tests + //print(running); + //resource-specific tests assertEquals(Type.GCOREENDPOINT,running.type()); } - + @Test public void registerCreate(){ - r=rp.create(running); - System.out.println("new resource created: "); - if(r!=null) - print(r); - assertEquals(running,r); + + for (int i = 0; i<10; i++) { + running = Resources.unmarshal(GCoreEndpoint.class, PublisherTest.class.getClassLoader().getResourceAsStream("gCoreEndpoint.xml")); + r=rp.create(running); + System.out.println("new resource created: "+r.id()); + } } - - - @AfterClass + + + /*@AfterClass public static void forceDeleteResource(){ try { Thread.sleep(3000); @@ -59,8 +62,8 @@ public class GCoreEndpointPublisherTests { advancedPublisher.forceRemove(r); ScopeProvider.instance.set(currentScope); - - } - + + }*/ + } diff --git a/src/test/resources/gCoreEndpoint.xml b/src/test/resources/gCoreEndpoint.xml index ca88ff4..ba23aea 100644 --- a/src/test/resources/gCoreEndpoint.xml +++ b/src/test/resources/gCoreEndpoint.xml @@ -1,6 +1,6 @@ -5d244ab0-9d79-11e3-af18-dd5904b11dd6 + RunningInstance