Queries to check if a context exists and to fetch a context given its UUID
git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/resource-management/resource-manager@162159 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
parent
4e6d0a1192
commit
ef245330c7
|
@ -0,0 +1,62 @@
|
|||
package org.gcube.resourcemanagement.manager.webapp.context;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.gcube.informationsystem.model.entity.Context;
|
||||
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
|
||||
import org.gcube.informationsystem.resourceregistry.client.ResourceRegistryClient;
|
||||
import org.gcube.informationsystem.resourceregistry.client.ResourceRegistryClientFactory;
|
||||
|
||||
/**
|
||||
* Queries submitted within the context resource.
|
||||
*
|
||||
* @author Manuele Simi (ISTI CNR)
|
||||
*
|
||||
*/
|
||||
final class Queries {
|
||||
|
||||
private final ResourceRegistryClient localRegistryClient = ResourceRegistryClientFactory.create();
|
||||
|
||||
/**
|
||||
* Tests if the given context exists
|
||||
*
|
||||
* @param uuid
|
||||
* @param registryClient
|
||||
* @return
|
||||
*/
|
||||
protected boolean contextExists(UUID uuid, ResourceRegistryClient ... registryClient) {
|
||||
try {
|
||||
return client(registryClient).exists(Context.NAME, uuid);
|
||||
} catch (ResourceRegistryException e) {
|
||||
return false;
|
||||
}catch (RuntimeException e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches the context.
|
||||
*
|
||||
* @param uuid
|
||||
* @param registryClient
|
||||
* @return
|
||||
*/
|
||||
protected Context fetchContext(UUID uuid, ResourceRegistryClient ... registryClient) {
|
||||
try {
|
||||
return client(registryClient).getInstance(Context.class, uuid);
|
||||
} catch (ResourceRegistryException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private ResourceRegistryClient client(ResourceRegistryClient ... registryClient) {
|
||||
if (registryClient.length > 0 && Objects.nonNull(registryClient[0]))
|
||||
return registryClient[0];
|
||||
else
|
||||
return this.localRegistryClient;
|
||||
}
|
||||
}
|
|
@ -11,10 +11,6 @@ import javax.ws.rs.core.Response.Status;
|
|||
import org.gcube.common.authorization.library.provider.CalledMethodProvider;
|
||||
import org.gcube.informationsystem.model.entity.Context;
|
||||
import static org.gcube.resourcemanagement.manager.io.rs.RMContextPath.*;
|
||||
|
||||
import org.gcube.resourcemanagement.manager.io.codeexceptions.LocalCodeException;
|
||||
import org.gcube.resourcemanagement.manager.io.codeexceptions.WebCodeException;
|
||||
import org.gcube.resourcemanagement.manager.io.rs.RMCode;
|
||||
import org.gcube.resourcemanagement.manager.webapp.ResourceInitializer;
|
||||
import org.gcube.resourcemanagement.manager.webapp.context.CreateRequest;
|
||||
import org.gcube.resourcemanagement.manager.webapp.context.ResponseFromResourceRegistry;
|
||||
|
|
|
@ -0,0 +1,77 @@
|
|||
/**
|
||||
*
|
||||
*/
|
||||
package org.gcube.resourcemanagement.manager.webapp.context;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
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.SecurityTokenProvider;
|
||||
import org.gcube.common.scope.api.ScopeProvider;
|
||||
import org.gcube.informationsystem.resourceregistry.client.ResourceRegistryClient;
|
||||
import org.gcube.informationsystem.resourceregistry.client.ResourceRegistryClientImpl;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.FixMethodOrder;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.BlockJUnit4ClassRunner;
|
||||
import org.junit.runners.MethodSorters;
|
||||
|
||||
/**
|
||||
* Test cases for {@link Queries}
|
||||
*
|
||||
* @author Manuele Simi (ISTI CNR)
|
||||
*
|
||||
*/
|
||||
@RunWith(BlockJUnit4ClassRunner.class)
|
||||
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
|
||||
public class QueriesTest {
|
||||
|
||||
private final static String RR = "http://manuele-registry.dev.d4science.org/resource-registry";
|
||||
|
||||
public static final String DEFAULT_TEST_SCOPE ="71b9ef86-76ba-4346-b16e-9a1af203f6db-98187548";
|
||||
private static boolean skipTest = false;
|
||||
|
||||
@BeforeClass
|
||||
public static void beforeClass() throws Exception {
|
||||
setContext(DEFAULT_TEST_SCOPE);
|
||||
}
|
||||
|
||||
public static void setContext(String token) throws ObjectNotFound, Exception {
|
||||
if (DEFAULT_TEST_SCOPE.isEmpty()) {
|
||||
skipTest = true;
|
||||
return;
|
||||
}
|
||||
SecurityTokenProvider.instance.set(token);
|
||||
ScopeProvider.instance.set(getCurrentScope(token));
|
||||
}
|
||||
|
||||
public static String getCurrentScope(String token) throws ObjectNotFound, Exception {
|
||||
AuthorizationEntry authorizationEntry = Constants.authorizationService().get(token);
|
||||
String context = authorizationEntry.getContext();
|
||||
return context;
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void afterClass() throws Exception {
|
||||
SecurityTokenProvider.instance.reset();
|
||||
ScopeProvider.instance.reset();
|
||||
}
|
||||
/**
|
||||
* Test method for {@link org.gcube.resourcemanagement.manager.webapp.context.Queries#contextExists(java.util.UUID)}.
|
||||
*/
|
||||
@Test
|
||||
public void testContextExists() {
|
||||
if (skipTest)
|
||||
return;
|
||||
ResourceRegistryClient client = new ResourceRegistryClientImpl(RR);
|
||||
Queries queries = new Queries();
|
||||
assertFalse("Context does exist, but it should not", queries.contextExists(UUID.randomUUID(),client));
|
||||
}
|
||||
|
||||
}
|
|
@ -11,6 +11,7 @@
|
|||
|
||||
<logger name="org.gcube" level="INFO" />
|
||||
<logger name="org.gcube.resourcemanagement" level="TRACE" />
|
||||
<logger name="org.gcube.informationsystem" level="TRACE" />
|
||||
|
||||
|
||||
<root level="WARN">
|
||||
|
|
Loading…
Reference in New Issue