This commit is contained in:
Luca Frosini 2021-01-28 22:53:10 +01:00
parent 27cec7ec31
commit 58d7aa93c3
9 changed files with 90 additions and 64 deletions

View File

@ -114,7 +114,7 @@ public class ContextUtility {
contexts.put(securityContext.getUUID(), securityContext); contexts.put(securityContext.getUUID(), securityContext);
} }
private synchronized SecurityContext getSecurityContextByFullName(String fullName) throws ContextException { public synchronized SecurityContext getSecurityContextByFullName(String fullName) throws ContextException {
try { try {
SecurityContext securityContext = null; SecurityContext securityContext = null;

View File

@ -780,7 +780,7 @@ public abstract class ElementManagement<El extends OElement> {
return affectedInstances; return affectedInstances;
} catch(ResourceRegistryException e) { } catch(ResourceRegistryException e) {
logger.error("Unable to add {} with UUID {} to Context with UUID {} - Cause is {}", elementType, uuid, contextUUID, e.getMessage()); logger.error("Unable to add {} with UUID {} to Context with UUID {} - Reason is {}", elementType, uuid, contextUUID, e.getMessage());
if(oDatabaseDocument != null) { if(oDatabaseDocument != null) {
oDatabaseDocument.rollback(); oDatabaseDocument.rollback();
} }

View File

@ -111,12 +111,12 @@ public class ElementManagementUtility {
} }
} }
private static OElement getAnyElementByUUID(ODatabaseDocument orientGraph, UUID uuid) public static OElement getAnyElementByUUID(ODatabaseDocument oDatabaseDocument, UUID uuid)
throws NotFoundException, ResourceRegistryException { throws NotFoundException, ResourceRegistryException {
try { try {
return Utility.getElementByUUID(orientGraph, null, uuid, OVertex.class); return Utility.getElementByUUID(oDatabaseDocument, null, uuid, OVertex.class);
} catch(NotFoundException e) { } catch(NotFoundException e) {
return Utility.getElementByUUID(orientGraph, null, uuid, OEdge.class); return Utility.getElementByUUID(oDatabaseDocument, null, uuid, OEdge.class);
} catch(ResourceRegistryException e) { } catch(ResourceRegistryException e) {
throw e; throw e;
} catch(Exception e) { } catch(Exception e) {

View File

@ -298,14 +298,14 @@ public abstract class EntityManagement<E extends EntityElement> extends EntityEl
} }
public String reallyQuery(String relationType, String referenceType, UUID referenceUUID, ODirection direction, public String reallyQuery(String relationType, String referenceType, UUID referenceUUID, ODirection direction,
boolean polymorphic, Map<String,String> constraint) throws ResourceRegistryException { boolean polymorphic, Map<String,String> constraint, boolean includeRelationInResult) throws ResourceRegistryException {
ObjectMapper objectMapper = new ObjectMapper(); ObjectMapper objectMapper = new ObjectMapper();
ArrayNode arrayNode = objectMapper.createArrayNode(); ArrayNode arrayNode = objectMapper.createArrayNode();
Iterable<?> references = null; Iterable<?> references = null;
if(referenceUUID != null) { if(referenceUUID != null) {
OElement element = ElementManagementUtility.getAnyElementByUUID(referenceUUID); OElement element = ElementManagementUtility.getAnyElementByUUID(oDatabaseDocument, referenceUUID);
if(element instanceof OVertex) { if(element instanceof OVertex) {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
EntityManagement<Entity> entityManagement = ElementManagementUtility.getEntityManagement(getWorkingContext(), EntityManagement<Entity> entityManagement = ElementManagementUtility.getEntityManagement(getWorkingContext(),
@ -415,14 +415,27 @@ public abstract class EntityManagement<E extends EntityElement> extends EntityEl
} }
} }
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
EntityManagement entityManagement = ElementManagementUtility.getEntityManagement(getWorkingContext(), EntityManagement entityManagement = ElementManagementUtility.getEntityManagement(getWorkingContext(),
oDatabaseDocument, vertex); oDatabaseDocument, vertex);
try { try {
if(referenceUUID!=null && entityManagement.getUUID().compareTo(referenceUUID) == 0) { if(referenceUUID!=null && entityManagement.getUUID().compareTo(referenceUUID) == 0) {
continue; continue;
} }
JsonNode jsonNode = entityManagement.serializeAsJson();
JsonNode jsonNode;
if(includeRelationInResult) {
@SuppressWarnings("rawtypes")
RelationManagement relationManagement = ElementManagementUtility.getRelationManagement(getWorkingContext(),
oDatabaseDocument, edge);
jsonNode = relationManagement.serializeAsJson();
}else {
jsonNode = entityManagement.serializeAsJson();
}
arrayNode.add(jsonNode); arrayNode.add(jsonNode);
} catch(ResourceRegistryException e) { } catch(ResourceRegistryException e) {
logger.error("Unable to correctly serialize {}. It will be excluded from results. {}", logger.error("Unable to correctly serialize {}. It will be excluded from results. {}",
@ -547,7 +560,7 @@ public abstract class EntityManagement<E extends EntityElement> extends EntityEl
} }
public String query(String relationType, String referenceType, UUID referenceUUID, ODirection direction, public String query(String relationType, String referenceType, UUID referenceUUID, ODirection direction,
boolean polymorphic, Map<String,String> constraint) throws ResourceRegistryException { boolean polymorphic, Map<String,String> constraint, boolean includeRelationInResult) throws ResourceRegistryException {
try { try {
oDatabaseDocument = getWorkingContext().getDatabaseDocument(PermissionMode.READER); oDatabaseDocument = getWorkingContext().getDatabaseDocument(PermissionMode.READER);
@ -601,7 +614,7 @@ public abstract class EntityManagement<E extends EntityElement> extends EntityEl
break; break;
} }
return reallyQuery(relationType, referenceType, referenceUUID, direction, polymorphic, constraint); return reallyQuery(relationType, referenceType, referenceUUID, direction, polymorphic, constraint, includeRelationInResult);
} catch(ResourceRegistryException e) { } catch(ResourceRegistryException e) {
throw e; throw e;

View File

@ -333,7 +333,7 @@ public class Access extends BaseRest {
* All the EService identified By a SoftwareFacet : * All the EService identified By a SoftwareFacet :
* GET /access/query/EService/isIdentifiedBy/SoftwareFacet?polymorphic=true&direction=out * GET /access/query/EService/isIdentifiedBy/SoftwareFacet?polymorphic=true&direction=out
* *
* The Eservice identified By the SoftwareFacet with UUID 7bc997c3-d005-40ff-b9ed-c4b6a35851f1 : * All the EService identified By the SoftwareFacet with UUID 7bc997c3-d005-40ff-b9ed-c4b6a35851f1 :
* GET /access/query/EService/isIdentifiedBy/SoftwareFacet?reference=7bc997c3-d005-40ff-b9ed-c4b6a35851f1&polymorphic=true&direction=out * GET /access/query/EService/isIdentifiedBy/SoftwareFacet?reference=7bc997c3-d005-40ff-b9ed-c4b6a35851f1&polymorphic=true&direction=out
* *
* All the Resources identified By a ContactFacet : * All the Resources identified By a ContactFacet :
@ -362,6 +362,7 @@ public class Access extends BaseRest {
@QueryParam(AccessPath.REFERENCE_PARAM) String reference, @QueryParam(AccessPath.REFERENCE_PARAM) String reference,
@QueryParam(AccessPath.POLYMORPHIC_PARAM) @DefaultValue("false") Boolean polymorphic, @QueryParam(AccessPath.POLYMORPHIC_PARAM) @DefaultValue("false") Boolean polymorphic,
@QueryParam(AccessPath.DIRECTION_PARAM) @DefaultValue("out") String direction, @QueryParam(AccessPath.DIRECTION_PARAM) @DefaultValue("out") String direction,
@QueryParam(AccessPath.INCLUDE_RELATION_PARAM) @DefaultValue("false") Boolean includeRelation,
@Context UriInfo uriInfo) throws ResourceRegistryException { @Context UriInfo uriInfo) throws ResourceRegistryException {
logger.info("Requested {} instances having a(n) {} ({}={}} with {} ({}={})", resourcetype, relationType, logger.info("Requested {} instances having a(n) {} ({}={}} with {} ({}={})", resourcetype, relationType,
@ -417,7 +418,7 @@ public class Access extends BaseRest {
} }
return ((ResourceManagement) erManagement).query(relationType, referenceType, refereceUUID, directionEnum, return ((ResourceManagement) erManagement).query(relationType, referenceType, refereceUUID, directionEnum,
polymorphic, constraint); polymorphic, constraint, includeRelation);
} }
String error = String.format("%s is not a %s type", resourcetype, Resource.NAME); String error = String.format("%s is not a %s type", resourcetype, Resource.NAME);

View File

@ -65,7 +65,11 @@ public class ContextTest {
} }
} }
public static String getCurrentScope(String token) throws ObjectNotFound, Exception { public static String getCurrentContextFullName() throws ObjectNotFound, Exception {
return getContextFullNameByToken(SecurityTokenProvider.instance.get());
}
public static String getContextFullNameByToken(String token) throws ObjectNotFound, Exception {
AuthorizationEntry authorizationEntry = Constants.authorizationService().get(token); AuthorizationEntry authorizationEntry = Constants.authorizationService().get(token);
String context = authorizationEntry.getContext(); String context = authorizationEntry.getContext();
logger.info("Context of token {} is {}", token, context); logger.info("Context of token {} is {}", token, context);
@ -85,7 +89,7 @@ public class ContextTest {
String qualifier = authorizationEntry.getQualifier(); String qualifier = authorizationEntry.getQualifier();
Caller caller = new Caller(clientInfo, qualifier); Caller caller = new Caller(clientInfo, qualifier);
AuthorizationProvider.instance.set(caller); AuthorizationProvider.instance.set(caller);
ScopeProvider.instance.set(getCurrentScope(token)); ScopeProvider.instance.set(getContextFullNameByToken(token));
} }
@BeforeClass @BeforeClass

View File

@ -711,32 +711,34 @@ public class ERManagementTest extends ContextTest {
ResourceManagement resourceManagement = new ResourceManagement(); ResourceManagement resourceManagement = new ResourceManagement();
resourceManagement.setElementType(Service.NAME); resourceManagement.setElementType(Service.NAME);
boolean includeRelation = false;
/* Getting Hosting Node */ /* Getting Hosting Node */
String json = resourceManagement.query(IsRelatedTo.NAME, EService.NAME, eServiceUUID, ODirection.BOTH, true, null); String json = resourceManagement.query(IsRelatedTo.NAME, EService.NAME, eServiceUUID, ODirection.BOTH, true, null, includeRelation);
List<Resource> resourceList = ElementMapper.unmarshalList(Resource.class, json); List<Resource> resourceList = ElementMapper.unmarshalList(Resource.class, json);
Assert.assertTrue(resourceList.size()==1); Assert.assertTrue(resourceList.size()==1);
Resource resource = resourceList.get(0); Resource resource = resourceList.get(0);
Assert.assertTrue(resource.getHeader().getUUID().compareTo(hostingNodeUUID)==0); Assert.assertTrue(resource.getHeader().getUUID().compareTo(hostingNodeUUID)==0);
json = resourceManagement.query(IsRelatedTo.NAME, EService.NAME, eServiceUUID, ODirection.OUT, true, null); json = resourceManagement.query(IsRelatedTo.NAME, EService.NAME, eServiceUUID, ODirection.OUT, true, null, includeRelation);
resourceList = ElementMapper.unmarshalList(Resource.class, json); resourceList = ElementMapper.unmarshalList(Resource.class, json);
Assert.assertTrue(resourceList.size()==1); Assert.assertTrue(resourceList.size()==1);
resource = resourceList.get(0); resource = resourceList.get(0);
Assert.assertTrue(resource.getHeader().getUUID().compareTo(hostingNodeUUID)==0); Assert.assertTrue(resource.getHeader().getUUID().compareTo(hostingNodeUUID)==0);
json = resourceManagement.query(IsRelatedTo.NAME, EService.NAME, eServiceUUID, ODirection.IN, true, null); json = resourceManagement.query(IsRelatedTo.NAME, EService.NAME, eServiceUUID, ODirection.IN, true, null, includeRelation);
resourceList = ElementMapper.unmarshalList(Resource.class, json); resourceList = ElementMapper.unmarshalList(Resource.class, json);
Assert.assertTrue(resourceList.size()==0); Assert.assertTrue(resourceList.size()==0);
json = resourceManagement.query(IsRelatedTo.NAME, EService.NAME, eServiceUUID, ODirection.BOTH, false, null); json = resourceManagement.query(IsRelatedTo.NAME, EService.NAME, eServiceUUID, ODirection.BOTH, false, null, includeRelation);
resourceList = ElementMapper.unmarshalList(Resource.class, json); resourceList = ElementMapper.unmarshalList(Resource.class, json);
Assert.assertTrue(resourceList.size()==0); Assert.assertTrue(resourceList.size()==0);
json = resourceManagement.query(IsRelatedTo.NAME, EService.NAME, eServiceUUID, ODirection.OUT, false, null); json = resourceManagement.query(IsRelatedTo.NAME, EService.NAME, eServiceUUID, ODirection.OUT, false, null, includeRelation);
resourceList = ElementMapper.unmarshalList(Resource.class, json); resourceList = ElementMapper.unmarshalList(Resource.class, json);
Assert.assertTrue(resourceList.size()==0); Assert.assertTrue(resourceList.size()==0);
json = resourceManagement.query(IsRelatedTo.NAME, EService.NAME, eServiceUUID, ODirection.IN, false, null); json = resourceManagement.query(IsRelatedTo.NAME, EService.NAME, eServiceUUID, ODirection.IN, false, null, includeRelation);
resourceList = ElementMapper.unmarshalList(Resource.class, json); resourceList = ElementMapper.unmarshalList(Resource.class, json);
Assert.assertTrue(resourceList.size()==0); Assert.assertTrue(resourceList.size()==0);
/* END Getting Hosting Node */ /* END Getting Hosting Node */
@ -744,29 +746,29 @@ public class ERManagementTest extends ContextTest {
/* Getting EService */ /* Getting EService */
json = resourceManagement.query(IsRelatedTo.NAME, HostingNode.NAME, hostingNodeUUID, ODirection.BOTH, true, null); json = resourceManagement.query(IsRelatedTo.NAME, HostingNode.NAME, hostingNodeUUID, ODirection.BOTH, true, null, includeRelation);
resourceList = ElementMapper.unmarshalList(Resource.class, json); resourceList = ElementMapper.unmarshalList(Resource.class, json);
Assert.assertTrue(resourceList.size()==1); Assert.assertTrue(resourceList.size()==1);
Assert.assertTrue(resourceList.get(0).getHeader().getUUID().compareTo(eServiceUUID)==0); Assert.assertTrue(resourceList.get(0).getHeader().getUUID().compareTo(eServiceUUID)==0);
json = resourceManagement.query(IsRelatedTo.NAME, HostingNode.NAME, hostingNodeUUID, ODirection.OUT, true, null); json = resourceManagement.query(IsRelatedTo.NAME, HostingNode.NAME, hostingNodeUUID, ODirection.OUT, true, null, includeRelation);
resourceList = ElementMapper.unmarshalList(Resource.class, json); resourceList = ElementMapper.unmarshalList(Resource.class, json);
Assert.assertTrue(resourceList.size()==0); Assert.assertTrue(resourceList.size()==0);
json = resourceManagement.query(IsRelatedTo.NAME, HostingNode.NAME, hostingNodeUUID, ODirection.IN, true, null); json = resourceManagement.query(IsRelatedTo.NAME, HostingNode.NAME, hostingNodeUUID, ODirection.IN, true, null, includeRelation);
resourceList = ElementMapper.unmarshalList(Resource.class, json); resourceList = ElementMapper.unmarshalList(Resource.class, json);
Assert.assertTrue(resourceList.size()==1); Assert.assertTrue(resourceList.size()==1);
Assert.assertTrue(resourceList.get(0).getHeader().getUUID().compareTo(eServiceUUID)==0); Assert.assertTrue(resourceList.get(0).getHeader().getUUID().compareTo(eServiceUUID)==0);
json = resourceManagement.query(IsRelatedTo.NAME, HostingNode.NAME, hostingNodeUUID, ODirection.BOTH, false, null); json = resourceManagement.query(IsRelatedTo.NAME, HostingNode.NAME, hostingNodeUUID, ODirection.BOTH, false, null, includeRelation);
resourceList = ElementMapper.unmarshalList(Resource.class, json); resourceList = ElementMapper.unmarshalList(Resource.class, json);
Assert.assertTrue(resourceList.size()==0); Assert.assertTrue(resourceList.size()==0);
json = resourceManagement.query(IsRelatedTo.NAME, HostingNode.NAME, hostingNodeUUID, ODirection.OUT, false, null); json = resourceManagement.query(IsRelatedTo.NAME, HostingNode.NAME, hostingNodeUUID, ODirection.OUT, false, null, includeRelation);
resourceList = ElementMapper.unmarshalList(Resource.class, json); resourceList = ElementMapper.unmarshalList(Resource.class, json);
Assert.assertTrue(resourceList.size()==0); Assert.assertTrue(resourceList.size()==0);
json = resourceManagement.query(IsRelatedTo.NAME, HostingNode.NAME, hostingNodeUUID, ODirection.IN, false, null); json = resourceManagement.query(IsRelatedTo.NAME, HostingNode.NAME, hostingNodeUUID, ODirection.IN, false, null, includeRelation);
resourceList = ElementMapper.unmarshalList(Resource.class, json); resourceList = ElementMapper.unmarshalList(Resource.class, json);
Assert.assertTrue(resourceList.size()==0); Assert.assertTrue(resourceList.size()==0);
/* END Getting HostingNode */ /* END Getting HostingNode */
@ -777,12 +779,12 @@ public class ERManagementTest extends ContextTest {
/* EService --ConsistsOf--> SoftwareFacet*/ /* EService --ConsistsOf--> SoftwareFacet*/
try { try {
json = resourceManagement.query(ConsistsOf.NAME, SoftwareFacet.NAME, identificationFacetUUID, ODirection.BOTH, true, null); json = resourceManagement.query(ConsistsOf.NAME, SoftwareFacet.NAME, identificationFacetUUID, ODirection.BOTH, true, null, includeRelation);
}catch(InvalidQueryException e) { }catch(InvalidQueryException e) {
// Ok expected // Ok expected
} }
json = resourceManagement.query(ConsistsOf.NAME, SoftwareFacet.NAME, identificationFacetUUID, ODirection.OUT, true, null); json = resourceManagement.query(ConsistsOf.NAME, SoftwareFacet.NAME, identificationFacetUUID, ODirection.OUT, true, null, includeRelation);
resourceList = ElementMapper.unmarshalList(Resource.class, json); resourceList = ElementMapper.unmarshalList(Resource.class, json);
Assert.assertTrue(resourceList.size()==1); Assert.assertTrue(resourceList.size()==1);
resource = resourceList.get(0); resource = resourceList.get(0);
@ -791,24 +793,24 @@ public class ERManagementTest extends ContextTest {
Assert.assertTrue(targetIdentificationFacet.getHeader().getUUID().compareTo(identificationFacetUUID)==0); Assert.assertTrue(targetIdentificationFacet.getHeader().getUUID().compareTo(identificationFacetUUID)==0);
try { try {
json = resourceManagement.query(ConsistsOf.NAME, SoftwareFacet.NAME, identificationFacetUUID, ODirection.IN, true, null); json = resourceManagement.query(ConsistsOf.NAME, SoftwareFacet.NAME, identificationFacetUUID, ODirection.IN, true, null, includeRelation);
}catch(InvalidQueryException e) { }catch(InvalidQueryException e) {
// Ok expected // Ok expected
} }
try { try {
json = resourceManagement.query(ConsistsOf.NAME, SoftwareFacet.NAME, identificationFacetUUID, ODirection.BOTH, false, null); json = resourceManagement.query(ConsistsOf.NAME, SoftwareFacet.NAME, identificationFacetUUID, ODirection.BOTH, false, null, includeRelation);
}catch(InvalidQueryException e) { }catch(InvalidQueryException e) {
// Ok expected // Ok expected
} }
json = resourceManagement.query(ConsistsOf.NAME, SoftwareFacet.NAME, identificationFacetUUID, ODirection.OUT, false, null); json = resourceManagement.query(ConsistsOf.NAME, SoftwareFacet.NAME, identificationFacetUUID, ODirection.OUT, false, null, includeRelation);
resourceList = ElementMapper.unmarshalList(Resource.class, json); resourceList = ElementMapper.unmarshalList(Resource.class, json);
Assert.assertTrue(resourceList.size()==0); Assert.assertTrue(resourceList.size()==0);
try { try {
json = resourceManagement.query(ConsistsOf.NAME, SoftwareFacet.NAME, identificationFacetUUID, ODirection.IN, false, null); json = resourceManagement.query(ConsistsOf.NAME, SoftwareFacet.NAME, identificationFacetUUID, ODirection.IN, false, null, includeRelation);
}catch(InvalidQueryException e) { }catch(InvalidQueryException e) {
// Ok expected // Ok expected
} }

View File

@ -117,10 +117,12 @@ public class ResourceManagementTest extends ContextTest {
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
ElementManagement erManagement = ElementManagementUtility.getERManagement(type); ElementManagement erManagement = ElementManagementUtility.getERManagement(type);
boolean includeRelation = false;
if (erManagement instanceof ResourceManagement) { if (erManagement instanceof ResourceManagement) {
boolean[] booleans = new boolean[] {true, false}; boolean[] booleans = new boolean[] {true, false};
for(boolean bool : booleans) { for(boolean bool : booleans) {
String ret = ((ResourceManagement) erManagement).query(relationType, facetType, null, ODirection.OUT, bool, constraint); String ret = ((ResourceManagement) erManagement).query(relationType, facetType, null, ODirection.OUT, bool, constraint, includeRelation);
logger.debug("Result of query for {}polymorphic {} --{}--> {} with constaint {} is {}", bool ? "" : "NOT ", logger.debug("Result of query for {}polymorphic {} --{}--> {} with constaint {} is {}", bool ? "" : "NOT ",
type, relationType, facetType, constraint, ret); type, relationType, facetType, constraint, ret);
} }
@ -199,7 +201,9 @@ public class ResourceManagementTest extends ContextTest {
Configuration expected = createdConfigurations.get(i); Configuration expected = createdConfigurations.get(i);
UUID expectedUUID = expected.getHeader().getUUID(); UUID expectedUUID = expected.getHeader().getUUID();
boolean includeRelation = false;
for(Boolean polymorphic : polymorphics) { for(Boolean polymorphic : polymorphics) {
@ -213,7 +217,7 @@ public class ResourceManagementTest extends ContextTest {
resourceType, relationType, referenceType, constraint); resourceType, relationType, referenceType, constraint);
String ret = resourceManagement.query(relationType, referenceType, refereceUUID, directionEnum, String ret = resourceManagement.query(relationType, referenceType, refereceUUID, directionEnum,
polymorphic, constraint); polymorphic, constraint, includeRelation);
List<Configuration> list = ElementMapper.unmarshalList(Configuration.class, ret); List<Configuration> list = ElementMapper.unmarshalList(Configuration.class, ret);
Assert.assertTrue(list.size()==expectedSize); Assert.assertTrue(list.size()==expectedSize);
@ -239,7 +243,7 @@ public class ResourceManagementTest extends ContextTest {
parentResourceType, relationType, referenceType, constraint); parentResourceType, relationType, referenceType, constraint);
String retPolimorphic = parentResourceManagement.query(relationType, referenceType, refereceUUID, directionEnum, String retPolimorphic = parentResourceManagement.query(relationType, referenceType, refereceUUID, directionEnum,
polymorphic, constraint); polymorphic, constraint, includeRelation);
List<ConfigurationTemplate> listPolimorphic = ElementMapper.unmarshalList(ConfigurationTemplate.class, retPolimorphic); List<ConfigurationTemplate> listPolimorphic = ElementMapper.unmarshalList(ConfigurationTemplate.class, retPolimorphic);
Assert.assertTrue(listPolimorphic.size()==expectedSize); Assert.assertTrue(listPolimorphic.size()==expectedSize);

View File

@ -220,11 +220,8 @@ public class BasicTest extends ContextTest {
/*------------------------------------------------------------------------*/ /*------------------------------------------------------------------------*/
logger.debug("Switching to another context");
ContextTest.setContextByName(ALTERNATIVE_TEST_SCOPE);
try { try {
addToContextThenTestIfBehaveProperly(eService, true); addToContextThenTestIfBehaveProperly(eService, true, ALTERNATIVE_TEST_SCOPE);
}finally { }finally {
UUID eServiceUUID = eService.getHeader().getUUID(); UUID eServiceUUID = eService.getHeader().getUUID();
resourceManagement = new ResourceManagement(); resourceManagement = new ResourceManagement();
@ -307,15 +304,19 @@ public class BasicTest extends ContextTest {
public boolean mustBeAdded(Relation<Resource, Entity> r); public boolean mustBeAdded(Relation<Resource, Entity> r);
public boolean mustBeRemoved(Relation<Resource, Entity> r);
} }
protected Resource getAndAddIsRelatedTo(Resource r, RelationConstraint relationConstraint) throws ResourceRegistryException, Exception { protected Resource getAndAddIsRelatedTo(Resource r, RelationConstraint relationConstraint) throws ResourceRegistryException, Exception {
ResourceManagement resourceManagement = new ResourceManagement(); ResourceManagement resourceManagement = new ResourceManagement();
resourceManagement.setElementType(Resource.NAME);
String resourceType = TypeMapper.getType(r); String resourceType = TypeMapper.getType(r);
// resourceManagement.setElementType(resourceType);
UUID resourceUUID = r.getHeader().getUUID(); UUID resourceUUID = r.getHeader().getUUID();
// resourceManagement.setUUID(resourceUUID); // resourceManagement.setUUID(resourceUUID);
String ret = resourceManagement.query(IsRelatedTo.NAME, resourceType, resourceUUID, ODirection.OUT, true, new HashMap<>()); String ret = resourceManagement.query(IsRelatedTo.NAME, resourceType, resourceUUID, ODirection.IN, true, new HashMap<>(), true);
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
List<IsRelatedTo> isRelatedToList = ElementMapper.unmarshalList(IsRelatedTo.class, ret); List<IsRelatedTo> isRelatedToList = ElementMapper.unmarshalList(IsRelatedTo.class, ret);
for(@SuppressWarnings("rawtypes") IsRelatedTo isRelatedTo : isRelatedToList) { for(@SuppressWarnings("rawtypes") IsRelatedTo isRelatedTo : isRelatedToList) {
@ -342,6 +343,11 @@ public class BasicTest extends ContextTest {
public boolean mustBeAdded(Relation<Resource, Entity> r) { public boolean mustBeAdded(Relation<Resource, Entity> r) {
return r.getPropagationConstraint().getAddConstraint() == AddConstraint.propagate; return r.getPropagationConstraint().getAddConstraint() == AddConstraint.propagate;
} }
@Override
public boolean mustBeRemoved(Relation<Resource, Entity> r) {
throw new UnsupportedOperationException();
}
}; };
@ -363,14 +369,15 @@ public class BasicTest extends ContextTest {
return expected; return expected;
} }
protected void addToContextThenTestIfBehaveProperly(Resource r, boolean dryRun) throws ResourceRegistryException, Exception { protected void addToContextThenTestIfBehaveProperly(Resource r, boolean dryRun, String targetContextFullName) throws ResourceRegistryException, Exception {
Map<UUID, Element> expectedInstances = getAddedExpectedInstances(r); Map<UUID, Element> expectedInstances = getAddedExpectedInstances(r);
ResourceManagement resourceManagement = new ResourceManagement(); ResourceManagement resourceManagement = new ResourceManagement();
resourceManagement.setElementType(TypeMapper.getType(r)); resourceManagement.setElementType(TypeMapper.getType(r));
resourceManagement.setUUID(r.getHeader().getUUID()); resourceManagement.setUUID(r.getHeader().getUUID());
resourceManagement.setDryRunContextSharing(dryRun); resourceManagement.setDryRunContextSharing(dryRun);
UUID contextUUID = ContextUtility.getCurrentSecurityContext().getUUID(); UUID contextUUID = ContextUtility.getInstance().getSecurityContextByFullName(targetContextFullName).getUUID();
Map<UUID, JsonNode> affectedInstances = resourceManagement.addToContext(contextUUID); Map<UUID, JsonNode> affectedInstances = resourceManagement.addToContext(contextUUID);
SortedSet<UUID> expectedInstancesUUID = new TreeSet<>(expectedInstances.keySet()); SortedSet<UUID> expectedInstancesUUID = new TreeSet<>(expectedInstances.keySet());
@ -490,8 +497,11 @@ public class BasicTest extends ContextTest {
hostingNode, disk, null); hostingNode, disk, null);
hostingNode.addFacet(hasPersistentMemory); hostingNode.addFacet(hasPersistentMemory);
PropagationConstraint propagationConstraint = new PropagationConstraintImpl();
propagationConstraint.setAddConstraint(AddConstraint.unpropagate);
propagationConstraint.setRemoveConstraint(RemoveConstraint.cascade);
Activates<HostingNode, EService> activates = new ActivatesImpl<HostingNode, EService>( Activates<HostingNode, EService> activates = new ActivatesImpl<HostingNode, EService>(
hostingNode, eService, null); hostingNode, eService, propagationConstraint);
UUID activatedUUID = UUID.randomUUID(); UUID activatedUUID = UUID.randomUUID();
activates.setHeader(new HeaderImpl(activatedUUID)); activates.setHeader(new HeaderImpl(activatedUUID));
hostingNode.attachResource(activates); hostingNode.attachResource(activates);
@ -515,27 +525,16 @@ public class BasicTest extends ContextTest {
hostingNodeInstances.put(consistsOf.getTarget().getHeader().getUUID(), consistsOf.getTarget()); hostingNodeInstances.put(consistsOf.getTarget().getHeader().getUUID(), consistsOf.getTarget());
} }
/*
IsRelatedToManagement isRelatedToManagement = new IsRelatedToManagement();
isRelatedToManagement.setElementType(Activates.NAME);
isRelatedToManagement.setUUID(activatedUUID);
String activatesString = isRelatedToManagement.read();
@SuppressWarnings("unchecked")
Activates<HostingNode, EService> unmarshalledActivates = ElementMapper.unmarshal(Activates.class, activatesString);
// An intermediate variable has been used to suppress warning here and not to the whole test function
activates = unmarshalledActivates;
hostingNode.attachResource(activates);
*/
/* ------------------------------------------------------------------ */ /* ------------------------------------------------------------------ */
logger.debug("Switching to another context"); String currentContextFulName = ContextTest.getCurrentContextFullName();
ContextTest.setContextByName(ALTERNATIVE_TEST_SCOPE); String targetContextFullName = ALTERNATIVE_TEST_SCOPE;
addToContextThenTestIfBehaveProperly(hostingNode, true, targetContextFullName);
logger.debug("Switching to the target context");
ContextTest.setContextByName(targetContextFullName);
addToContextThenTestIfBehaveProperly(hostingNode, true);
UUID contextUUID = ContextUtility.getCurrentSecurityContext().getUUID(); UUID contextUUID = ContextUtility.getCurrentSecurityContext().getUUID();
try { try {
@ -548,7 +547,10 @@ public class BasicTest extends ContextTest {
// OK // OK
} }
addToContextThenTestIfBehaveProperly(hostingNode, false); logger.debug("Switching back to the target context");
ContextTest.setContextByName(currentContextFulName);
addToContextThenTestIfBehaveProperly(hostingNode, false, targetContextFullName);
resourceManagement = new ResourceManagement(); resourceManagement = new ResourceManagement();
resourceManagement.setUUID(hostingNodeUUID); resourceManagement.setUUID(hostingNodeUUID);