Changed the way to set context for tests

This commit is contained in:
Luca Frosini 2019-10-28 15:06:31 +01:00
parent b599c27351
commit 10e5feb9d9
11 changed files with 62 additions and 81 deletions

View File

@ -24,78 +24,59 @@ import org.slf4j.LoggerFactory;
* @author Luca Frosini (ISTI - CNR)
*
*/
public class ScopedTest {
public class ContextTest {
private static final Logger logger = LoggerFactory.getLogger(ScopedTest.class);
private static final Logger logger = LoggerFactory.getLogger(ContextTest.class);
protected static final String PROPERTIES_FILENAME = "token.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 PARENT_DEFAULT_TEST_SCOPE;
public static final String DEFAULT_TEST_SCOPE_ANOTHER_USER;
public static final String DEFAULT_TEST_SCOPE;
public static final String ALTERNATIVE_TEST_SCOPE;
public static final String DEFAULT_TEST_SCOPE_ANOTHER_USER;
static {
Properties properties = new Properties();
InputStream input = ScopedTest.class.getClassLoader().getResourceAsStream(PROPERTIES_FILENAME);
properties = new Properties();
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);
}
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);
// PARENT_DEFAULT_TEST_SCOPE = "/pred4s"
// DEFAULT_TEST_SCOPE_NAME = PARENT_DEFAULT_TEST_SCOPE + "preprod";
// ALTERNATIVE_TEST_SCOPE = DEFAULT_TEST_SCOPE_NAME + "/preVRE";
DEFAULT_TEST_SCOPE = GCUBE_DEVNEXT;
PARENT_DEFAULT_TEST_SCOPE = GCUBE;
DEFAULT_TEST_SCOPE_ANOTHER_USER = GCUBE_DEVNEXT_ANOTHER_USER;
ALTERNATIVE_TEST_SCOPE = GCUBE_DEVNEXT_NEXTNEXT;
PARENT_DEFAULT_TEST_SCOPE = "/gcube";
DEFAULT_TEST_SCOPE = PARENT_DEFAULT_TEST_SCOPE + "devNext";
ALTERNATIVE_TEST_SCOPE = DEFAULT_TEST_SCOPE + "/NextNext";
DEFAULT_TEST_SCOPE_ANOTHER_USER = "lucio.lelii_" + DEFAULT_TEST_SCOPE;
}
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{
private 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);
@ -103,12 +84,12 @@ public class ScopedTest {
}
@BeforeClass
public static void beforeClass() throws Exception{
setContext(DEFAULT_TEST_SCOPE);
public static void beforeClass() throws Exception {
setContextByName(DEFAULT_TEST_SCOPE);
}
@AfterClass
public static void afterClass() throws Exception{
public static void afterClass() throws Exception {
SecurityTokenProvider.instance.reset();
ScopeProvider.instance.reset();
}

View File

@ -3,7 +3,7 @@
*/
package org.gcube.informationsystem.resourceregistry.access;
import org.gcube.informationsystem.resourceregistry.ScopedTest;
import org.gcube.informationsystem.resourceregistry.ContextTest;
import org.gcube.informationsystem.resourceregistry.api.exceptions.query.InvalidQueryException;
import org.gcube.informationsystem.resourceregistry.query.QueryImpl;
import org.junit.Test;
@ -14,7 +14,7 @@ import org.slf4j.LoggerFactory;
* @author Luca Frosini (ISTI - CNR)
*
*/
public class QueryImplTest extends ScopedTest {
public class QueryImplTest extends ContextTest {
private static Logger logger = LoggerFactory.getLogger(QueryImplTest.class);

View File

@ -8,7 +8,7 @@ import org.gcube.informationsystem.base.impl.properties.HeaderImpl;
import org.gcube.informationsystem.context.impl.entities.ContextImpl;
import org.gcube.informationsystem.context.reference.entities.Context;
import org.gcube.informationsystem.context.reference.relations.IsParentOf;
import org.gcube.informationsystem.resourceregistry.ScopedTest;
import org.gcube.informationsystem.resourceregistry.ContextTest;
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.context.ContextAlreadyPresentException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.context.ContextException;
@ -30,7 +30,7 @@ import com.orientechnologies.orient.core.metadata.security.ORole;
import com.orientechnologies.orient.core.metadata.security.OSecurity;
import com.orientechnologies.orient.core.metadata.security.OUser;
public class ContextManagementTest extends ScopedTest {
public class ContextManagementTest extends ContextTest {
private static Logger logger = LoggerFactory.getLogger(ContextManagementTest.class);

View File

@ -30,7 +30,7 @@ import org.gcube.informationsystem.model.reference.relations.ConsistsOf;
import org.gcube.informationsystem.model.reference.relations.IsIdentifiedBy;
import org.gcube.informationsystem.model.reference.relations.IsRelatedTo;
import org.gcube.informationsystem.model.reference.relations.Relation;
import org.gcube.informationsystem.resourceregistry.ScopedTest;
import org.gcube.informationsystem.resourceregistry.ContextTest;
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.facet.FacetNotFoundException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.query.InvalidQueryException;
@ -82,7 +82,7 @@ import com.tinkerpop.blueprints.Direction;
* @author Luca Frosini (ISTI - CNR)
*
*/
public class ERManagementTest extends ScopedTest {
public class ERManagementTest extends ContextTest {
private static Logger logger = LoggerFactory
.getLogger(ERManagementTest.class);
@ -321,7 +321,7 @@ public class ERManagementTest extends ScopedTest {
readCpuFacet.getVendor()) == 0);
Assert.assertTrue(uuid.compareTo(readCpuFacet.getHeader().getUUID()) == 0);
ScopedTest.setContext(DEFAULT_TEST_SCOPE_ANOTHER_USER);
ContextTest.setContextByName(DEFAULT_TEST_SCOPE_ANOTHER_USER);
String newVendor = "Intel";
String newClockSpeed = "2 GHz";

View File

@ -4,12 +4,12 @@ import java.security.Key;
import org.gcube.informationsystem.model.impl.properties.EncryptedImpl;
import org.gcube.informationsystem.model.reference.properties.Encrypted;
import org.gcube.informationsystem.resourceregistry.ScopedTest;
import org.gcube.informationsystem.resourceregistry.ContextTest;
import org.gcube.informationsystem.resourceregistry.dbinitialization.DatabaseEnvironment;
import org.junit.Assert;
import org.junit.Test;
public class EncryptionTest extends ScopedTest {
public class EncryptionTest extends ContextTest {
public static final String PLAIN_VALUE = "my-test";

View File

@ -33,7 +33,7 @@ import org.gcube.informationsystem.model.reference.properties.PropagationConstra
import org.gcube.informationsystem.model.reference.properties.PropagationConstraint.RemoveConstraint;
import org.gcube.informationsystem.model.reference.relations.ConsistsOf;
import org.gcube.informationsystem.model.reference.relations.IsIdentifiedBy;
import org.gcube.informationsystem.resourceregistry.ScopedTest;
import org.gcube.informationsystem.resourceregistry.ContextTest;
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
import org.gcube.informationsystem.resourceregistry.er.entity.ResourceManagement;
import org.gcube.informationsystem.utils.ISMapper;
@ -69,7 +69,7 @@ import com.fasterxml.jackson.databind.JsonMappingException;
* @author Luca Frosini (ISTI - CNR)
*
*/
public class SmartgearResourcesTest extends ScopedTest {
public class SmartgearResourcesTest extends ContextTest {
private static Logger logger = LoggerFactory
.getLogger(SmartgearResourcesTest.class);

View File

@ -4,7 +4,7 @@ import java.util.UUID;
import org.gcube.informationsystem.base.reference.ER;
import org.gcube.informationsystem.model.reference.entities.Facet;
import org.gcube.informationsystem.resourceregistry.ScopedTest;
import org.gcube.informationsystem.resourceregistry.ContextTest;
import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.facet.FacetAlreadyPresentException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.facet.FacetAvailableInAnotherContextException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.facet.FacetNotFoundException;
@ -23,7 +23,7 @@ import org.slf4j.LoggerFactory;
/**
* @author Luca Frosini (ISTI - CNR)
*/
public class FacetManagementTest extends ScopedTest {
public class FacetManagementTest extends ContextTest {
private static Logger logger = LoggerFactory.getLogger(ERManagementTest.class);
@ -221,7 +221,7 @@ public class FacetManagementTest extends ScopedTest {
@Test
public void testHierarchy() throws Exception {
/* Setting scope /gcube/devNext/NextNext */
ScopedTest.setContext(GCUBE_DEVNEXT_NEXTNEXT);
ContextTest.setContextByName(ALTERNATIVE_TEST_SCOPE);
SoftwareFacet softwareFacet = getSoftwareFacet();
@ -237,7 +237,7 @@ public class FacetManagementTest extends ScopedTest {
checkSoftwareFacetAssertion(softwareFacet, VERSION);
/* Setting (parent) scope /gcube/devNext */
ScopedTest.setContext(GCUBE_DEVNEXT);
ContextTest.setContextByName(DEFAULT_TEST_SCOPE);
assertThrow(softwareFacet, FacetAvailableInAnotherContextException.class, (SoftwareFacet s) -> {
read(s);
@ -250,7 +250,7 @@ public class FacetManagementTest extends ScopedTest {
checkSoftwareFacetAssertion(softwareFacet, VERSION);
/* Setting (parent of parent) scope /gcube */
ScopedTest.setContext(GCUBE);
ContextTest.setContextByName(PARENT_DEFAULT_TEST_SCOPE);
softwareFacet = read(softwareFacet);
checkSoftwareFacetAssertion(softwareFacet, VERSION);
@ -272,7 +272,7 @@ public class FacetManagementTest extends ScopedTest {
checkSoftwareFacetAssertion(softwareFacet, NEW_VERSION);
/* Restoring scope /gcube/devNext/NextNext */
ScopedTest.setContext(GCUBE_DEVNEXT_NEXTNEXT);
ContextTest.setContextByName(ALTERNATIVE_TEST_SCOPE);
read(softwareFacet);
checkSoftwareFacetAssertion(softwareFacet, NEW_VERSION);
@ -284,7 +284,7 @@ public class FacetManagementTest extends ScopedTest {
});
/* Setting (parent) scope /gcube/devNext */
ScopedTest.setContext(GCUBE_DEVNEXT);
ContextTest.setContextByName(DEFAULT_TEST_SCOPE);
assertThrow(softwareFacet, FacetAvailableInAnotherContextException.class, (SoftwareFacet s) -> {
read(s);
});
@ -297,7 +297,7 @@ public class FacetManagementTest extends ScopedTest {
});
/* Setting (parent of parent) scope /gcube */
ScopedTest.setContext(GCUBE);
ContextTest.setContextByName(PARENT_DEFAULT_TEST_SCOPE);
// The facet must be readable in hierarchic mode in /gcube because the context
// has been explicitly added
read(softwareFacet);

View File

@ -8,7 +8,7 @@ import org.gcube.informationsystem.model.impl.relations.IsIdentifiedByImpl;
import org.gcube.informationsystem.model.reference.entities.Facet;
import org.gcube.informationsystem.model.reference.relations.ConsistsOf;
import org.gcube.informationsystem.model.reference.relations.IsIdentifiedBy;
import org.gcube.informationsystem.resourceregistry.ScopedTest;
import org.gcube.informationsystem.resourceregistry.ContextTest;
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
import org.gcube.informationsystem.resourceregistry.er.ERManagement;
import org.gcube.informationsystem.resourceregistry.er.ERManagementUtility;
@ -24,7 +24,7 @@ import org.slf4j.LoggerFactory;
import com.tinkerpop.blueprints.Direction;
public class ResourceManagementTest extends ScopedTest {
public class ResourceManagementTest extends ContextTest {
private static Logger logger = LoggerFactory.getLogger(ResourceManagementTest.class);

View File

@ -15,7 +15,7 @@ import org.gcube.informationsystem.model.reference.properties.PropagationConstra
import org.gcube.informationsystem.model.reference.properties.PropagationConstraint.AddConstraint;
import org.gcube.informationsystem.model.reference.properties.PropagationConstraint.RemoveConstraint;
import org.gcube.informationsystem.model.reference.relations.IsIdentifiedBy;
import org.gcube.informationsystem.resourceregistry.ScopedTest;
import org.gcube.informationsystem.resourceregistry.ContextTest;
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.context.ContextNotFoundException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.facet.FacetAvailableInAnotherContextException;
@ -64,7 +64,7 @@ import org.slf4j.LoggerFactory;
* @author Luca Frosini (ISTI - CNR)
*
*/
public class BasicTest extends ScopedTest {
public class BasicTest extends ContextTest {
private static Logger logger = LoggerFactory
.getLogger(BasicTest.class);
@ -95,7 +95,7 @@ public class BasicTest extends ScopedTest {
/* ------------------------------------------------------------------ */
logger.debug("Switching to another scope");
ScopedTest.setContext(ScopedTest.PARENT_DEFAULT_TEST_SCOPE);
ContextTest.setContextByName(PARENT_DEFAULT_TEST_SCOPE);
try {
facetManagement = new FacetManagement();
facetManagement.setUUID(uuid);
@ -139,7 +139,7 @@ public class BasicTest extends ScopedTest {
/* ------------------------------------------------------------------ */
logger.debug("Setting back default scope");
ScopedTest.setContext(ScopedTest.DEFAULT_TEST_SCOPE);
ContextTest.setContextByName(DEFAULT_TEST_SCOPE);
facetManagement = new FacetManagement();
facetManagement.setUUID(uuid);
@ -257,7 +257,7 @@ public class BasicTest extends ScopedTest {
/* ------------------------------------------------------------------ */
logger.debug("Switching to another scope");
ScopedTest.setContext(ScopedTest.ALTERNATIVE_TEST_SCOPE);
ContextTest.setContextByName(ALTERNATIVE_TEST_SCOPE);
resourceManagement = new ResourceManagement();
resourceManagement.setUUID(uuid);
@ -305,7 +305,7 @@ public class BasicTest extends ScopedTest {
/* ------------------------------------------------------------------ */
logger.debug("Setting back default scope");
ScopedTest.setContext(ScopedTest.DEFAULT_TEST_SCOPE);
ContextTest.setContextByName(DEFAULT_TEST_SCOPE);
resourceManagement = new ResourceManagement();
resourceManagement.setUUID(eServiceUUID);
@ -381,7 +381,7 @@ public class BasicTest extends ScopedTest {
/* ------------------------------------------------------------------ */
logger.debug("Switching to alternative scope");
ScopedTest.setContext(ScopedTest.ALTERNATIVE_TEST_SCOPE);
ContextTest.setContextByName(ALTERNATIVE_TEST_SCOPE);
/*
* resourceManagement = new ResourceManagement();
@ -416,7 +416,7 @@ public class BasicTest extends ScopedTest {
/* ------------------------------------------------------------------ */
logger.debug("Setting back default scope");
ScopedTest.setContext(ScopedTest.DEFAULT_TEST_SCOPE);
ContextTest.setContextByName(DEFAULT_TEST_SCOPE);
resourceManagement = new ResourceManagement();
resourceManagement.setUUID(hnUUID);
@ -452,7 +452,7 @@ public class BasicTest extends ScopedTest {
/* ------------------------------------------------------------------ */
logger.debug("Switching to alternative scope again");
ScopedTest.setContext(ScopedTest.ALTERNATIVE_TEST_SCOPE);
ContextTest.setContextByName(ALTERNATIVE_TEST_SCOPE);
// TODO checks here
resourceManagement = new ResourceManagement();

View File

@ -5,7 +5,7 @@ import java.util.UUID;
import org.gcube.informationsystem.model.impl.relations.IsIdentifiedByImpl;
import org.gcube.informationsystem.model.reference.entities.Facet;
import org.gcube.informationsystem.model.reference.relations.IsIdentifiedBy;
import org.gcube.informationsystem.resourceregistry.ScopedTest;
import org.gcube.informationsystem.resourceregistry.ContextTest;
import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.facet.FacetAlreadyPresentException;
import org.gcube.informationsystem.resourceregistry.api.exceptions.entity.resource.ResourceAlreadyPresentException;
import org.gcube.informationsystem.resourceregistry.er.entity.FacetManagement;
@ -20,7 +20,7 @@ import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class RuleTest extends ScopedTest {
public class RuleTest extends ContextTest {
private static Logger logger = LoggerFactory
.getLogger(RuleTest.class);

View File

@ -1,11 +1,11 @@
package org.gcube.informationsystem.resourceregistry.query;
import org.gcube.informationsystem.resourceregistry.ScopedTest;
import org.gcube.informationsystem.resourceregistry.ContextTest;
import org.gcube.informationsystem.resourceregistry.api.exceptions.query.InvalidQueryException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class QueryTest extends ScopedTest {
public class QueryTest extends ContextTest {
private static Logger logger = LoggerFactory.getLogger(QueryTest.class);