2021-03-08 09:56:23 +01:00
package org.gcube.informationsystem.resourceregistry.instances.multicontext ;
2021-09-16 15:23:04 +02:00
import java.net.URI ;
2021-10-28 10:50:46 +02:00
import java.util.HashSet ;
import java.util.Map ;
2021-09-17 10:35:57 +02:00
import java.util.Set ;
2021-09-16 15:23:04 +02:00
import java.util.UUID ;
import org.gcube.informationsystem.model.impl.properties.PropagationConstraintImpl ;
import org.gcube.informationsystem.model.reference.properties.PropagationConstraint ;
import org.gcube.informationsystem.model.reference.properties.PropagationConstraint.AddConstraint ;
import org.gcube.informationsystem.model.reference.properties.PropagationConstraint.RemoveConstraint ;
import org.gcube.informationsystem.resourceregistry.api.exceptions.AvailableInAnotherContextException ;
import org.gcube.informationsystem.resourceregistry.api.exceptions.NotFoundException ;
2021-10-25 11:00:54 +02:00
import org.gcube.informationsystem.resourceregistry.api.exceptions.types.SchemaViolationException ;
2021-03-08 09:56:23 +01:00
import org.gcube.informationsystem.resourceregistry.contexts.ContextUtility ;
2021-09-16 17:23:13 +02:00
import org.gcube.informationsystem.resourceregistry.contexts.entities.ContextManagement ;
2021-03-08 09:56:23 +01:00
import org.gcube.informationsystem.resourceregistry.instances.model.entities.FacetManagement ;
import org.gcube.informationsystem.resourceregistry.instances.model.relations.ConsistsOfManagement ;
2021-09-16 15:23:04 +02:00
import org.gcube.informationsystem.resourceregistry.instances.model.relations.IsRelatedToManagement ;
2023-05-02 14:48:38 +02:00
import org.gcube.informationsystem.resourceregistry.requests.RequestUtility ;
2023-02-07 16:27:10 +01:00
import org.gcube.informationsystem.serialization.ElementMapper ;
2021-09-16 15:23:04 +02:00
import org.gcube.resourcemanagement.model.impl.entities.facets.ContactFacetImpl ;
import org.gcube.resourcemanagement.model.impl.entities.facets.CoverageFacetImpl ;
import org.gcube.resourcemanagement.model.impl.entities.facets.IdentifierFacetImpl ;
import org.gcube.resourcemanagement.model.impl.entities.resources.DatasetImpl ;
import org.gcube.resourcemanagement.model.impl.properties.ValueSchemaImpl ;
import org.gcube.resourcemanagement.model.impl.relations.consistsof.HasOwnerImpl ;
import org.gcube.resourcemanagement.model.impl.relations.consistsof.HasSpatialCoverageImpl ;
import org.gcube.resourcemanagement.model.impl.relations.consistsof.IsIdentifiedByImpl ;
import org.gcube.resourcemanagement.model.impl.relations.isrelatedto.IsCorrelatedToImpl ;
import org.gcube.resourcemanagement.model.reference.entities.facets.ContactFacet ;
import org.gcube.resourcemanagement.model.reference.entities.facets.CoverageFacet ;
import org.gcube.resourcemanagement.model.reference.entities.facets.IdentifierFacet ;
import org.gcube.resourcemanagement.model.reference.entities.facets.IdentifierFacet.IdentificationType ;
2021-03-08 09:56:23 +01:00
import org.gcube.resourcemanagement.model.reference.entities.facets.SoftwareFacet ;
2021-09-16 15:23:04 +02:00
import org.gcube.resourcemanagement.model.reference.entities.resources.Dataset ;
2021-03-08 09:56:23 +01:00
import org.gcube.resourcemanagement.model.reference.entities.resources.EService ;
2021-09-16 15:23:04 +02:00
import org.gcube.resourcemanagement.model.reference.properties.ValueSchema ;
import org.gcube.resourcemanagement.model.reference.relations.consistsof.HasCoverage ;
import org.gcube.resourcemanagement.model.reference.relations.consistsof.HasOwner ;
2021-03-08 09:56:23 +01:00
import org.gcube.resourcemanagement.model.reference.relations.consistsof.IsIdentifiedBy ;
2021-09-16 15:23:04 +02:00
import org.gcube.resourcemanagement.model.reference.relations.isrelatedto.IsCorrelatedTo ;
import org.junit.Assert ;
2021-03-08 09:56:23 +01:00
import org.junit.Test ;
2021-09-16 15:23:04 +02:00
import org.slf4j.Logger ;
import org.slf4j.LoggerFactory ;
2021-03-08 09:56:23 +01:00
2021-03-09 12:58:46 +01:00
public class AddToContextTest extends MultiContextTest {
2021-09-16 15:23:04 +02:00
private static Logger logger = LoggerFactory . getLogger ( AddToContextTest . class ) ;
2021-03-08 09:56:23 +01:00
@Test
public void testCreateEServiceAndRemoveFromContextRequiredFacet ( ) throws Exception {
2023-05-02 14:48:38 +02:00
EService eService = createEService ( ) ;
2021-03-08 09:56:23 +01:00
@SuppressWarnings ( " unchecked " )
2021-03-08 17:05:56 +01:00
IsIdentifiedBy < EService , SoftwareFacet > isIdentifiedBy = ( IsIdentifiedBy < EService , SoftwareFacet > ) eService . getConsistsOf ( IsIdentifiedBy . class ) . get ( 0 ) ;
2021-03-08 09:56:23 +01:00
ConsistsOfManagement consistsOfManagement = new ConsistsOfManagement ( ) ;
consistsOfManagement . setElementType ( IsIdentifiedBy . NAME ) ;
2023-04-28 11:43:48 +02:00
consistsOfManagement . setUUID ( isIdentifiedBy . getID ( ) ) ;
2021-03-08 09:56:23 +01:00
try {
consistsOfManagement . removeFromContext ( ContextUtility . getCurrentSecurityContext ( ) . getUUID ( ) ) ;
throw new Exception ( " You should not be able to delete a mandatory ConsistsOf " ) ;
} catch ( SchemaViolationException e ) {
// As expected
} catch ( Exception e ) {
2023-05-02 14:48:38 +02:00
deleteResource ( eService ) ;
2021-03-08 09:56:23 +01:00
throw e ;
}
SoftwareFacet softwareFacet = isIdentifiedBy . getTarget ( ) ;
FacetManagement facetManagement = new FacetManagement ( ) ;
facetManagement . setElementType ( SoftwareFacet . NAME ) ;
2023-04-28 11:43:48 +02:00
facetManagement . setUUID ( softwareFacet . getID ( ) ) ;
2021-03-08 09:56:23 +01:00
try {
facetManagement . removeFromContext ( ContextUtility . getCurrentSecurityContext ( ) . getUUID ( ) ) ;
throw new Exception ( " You should not be able to delete a mandatory Facet " ) ;
} catch ( SchemaViolationException e ) {
// As expected
} catch ( Exception e ) {
2023-05-02 14:48:38 +02:00
deleteResource ( eService ) ;
2021-03-08 09:56:23 +01:00
throw e ;
}
2023-05-02 14:48:38 +02:00
deleteResource ( eService ) ;
2021-03-08 09:56:23 +01:00
}
2021-09-16 15:23:04 +02:00
protected Dataset createDataset ( String id , String uuidString ) throws Exception {
Dataset dataset = new DatasetImpl ( ) ;
2023-04-28 11:43:48 +02:00
dataset . setID ( UUID . fromString ( uuidString ) ) ;
2021-09-16 15:23:04 +02:00
IdentifierFacet identifierFacet = new IdentifierFacetImpl ( ) ;
identifierFacet . setValue ( id ) ;
2023-04-26 21:44:03 +02:00
identifierFacet . setIdentificationType ( IdentificationType . STRING ) ;
2021-09-16 15:23:04 +02:00
identifierFacet . setPersistent ( false ) ;
IsIdentifiedBy < Dataset , IdentifierFacet > isIdentifiedBy = new IsIdentifiedByImpl < > ( dataset , identifierFacet ) ;
dataset . addFacet ( isIdentifiedBy ) ;
ContactFacet contactFacet = new ContactFacetImpl ( ) ;
contactFacet . setTitle ( " Dr. " ) ;
contactFacet . setName ( " Luca " ) ;
contactFacet . setSurname ( " Frosini " ) ;
contactFacet . setEMail ( " luca.frosini@d4science.org " ) ;
HasOwner < Dataset , ContactFacet > hasOwner = new HasOwnerImpl < > ( dataset , contactFacet ) ;
dataset . addFacet ( hasOwner ) ;
CoverageFacet coverageFacet = new CoverageFacetImpl ( ) ;
ValueSchema coverage = new ValueSchemaImpl ( ) ;
coverage . setValue ( " Test " ) ;
coverage . setSchema ( new URI ( " String " ) ) ;
coverageFacet . setCoverage ( coverage ) ;
HasCoverage < Dataset , CoverageFacet > hasCoverage = new HasSpatialCoverageImpl < Dataset , CoverageFacet > ( dataset , coverageFacet ) ;
dataset . addFacet ( hasCoverage ) ;
try {
deleteResource ( dataset ) ;
} catch ( NotFoundException e ) {
// OK
logger . trace ( " OK " ) ;
}
return createResource ( dataset ) ;
}
protected PropagationConstraint getPropagationConstraint ( ) {
PropagationConstraint propagationConstraint = new PropagationConstraintImpl ( ) ;
propagationConstraint . setAddConstraint ( AddConstraint . propagate ) ;
propagationConstraint . setRemoveConstraint ( RemoveConstraint . cascade ) ;
return propagationConstraint ;
}
@SuppressWarnings ( " unchecked " )
2021-09-16 17:23:13 +02:00
protected IsCorrelatedTo < Dataset , Dataset > createIsCorrelatedTo ( Dataset source , Dataset target , String uuid ) throws Exception {
2021-09-16 15:23:04 +02:00
PropagationConstraint propagationConstraint = getPropagationConstraint ( ) ;
IsCorrelatedTo < Dataset , Dataset > isCorrelatedTo = new IsCorrelatedToImpl < Dataset , Dataset > ( source , target , propagationConstraint ) ;
2023-04-28 11:43:48 +02:00
isCorrelatedTo . setID ( UUID . fromString ( uuid ) ) ;
2021-09-16 15:23:04 +02:00
IsRelatedToManagement isRelatedToManagement = getIsRelatedToManagement ( isCorrelatedTo ) ;
String json = isRelatedToManagement . create ( ) ;
isCorrelatedTo = ElementMapper . unmarshal ( isCorrelatedTo . getClass ( ) , json ) ;
return isCorrelatedTo ;
}
2021-10-28 10:50:46 +02:00
protected void checkNumerOfContext ( Dataset dataset , int expectedContextSize ) throws Exception {
2021-09-16 17:23:13 +02:00
setContextByName ( GCUBE ) ;
2023-05-02 14:48:38 +02:00
RequestUtility . getRequestInfo ( ) . get ( ) . setHierarchicalMode ( true ) ;
RequestUtility . getRequestInfo ( ) . get ( ) . setIncludeContexts ( true ) ;
2021-09-16 17:23:13 +02:00
2021-10-28 10:50:46 +02:00
String json = getResourceManagement ( dataset ) . read ( ) ;
2023-04-28 14:52:46 +02:00
logger . trace ( " Resource with contexts in HierarchicalMode from server is {} " , json ) ;
2021-10-28 10:50:46 +02:00
Dataset r1 = ElementMapper . unmarshal ( Dataset . class , json ) ;
2023-04-28 14:52:46 +02:00
logger . trace ( " Resource with contexts in HierarchicalMode with Jackson is {} " , r1 . toString ( ) ) ;
2021-09-16 17:23:13 +02:00
2023-04-28 14:52:46 +02:00
Map < UUID , String > contextsR1 = r1 . getContexts ( ) ;
2021-10-28 10:50:46 +02:00
Set < String > contextsR1Fullname = new HashSet < > ( contextsR1 . values ( ) ) ;
2023-04-28 11:43:48 +02:00
logger . info ( " Contexts of {} with UUID {} and Id {} are {} " , Dataset . NAME , r1 . getID ( ) , ( ( IdentifierFacet ) r1 . getIdentificationFacets ( ) . get ( 0 ) ) . getValue ( ) , contextsR1Fullname ) ;
2021-10-28 10:50:46 +02:00
Assert . assertTrue ( contextsR1 . size ( ) = = expectedContextSize ) ;
2021-09-16 17:23:13 +02:00
2023-05-02 14:48:38 +02:00
RequestUtility . getRequestInfo ( ) . get ( ) . setHierarchicalMode ( false ) ;
RequestUtility . getRequestInfo ( ) . get ( ) . setIncludeContexts ( false ) ;
2021-09-16 17:23:13 +02:00
}
2021-09-16 15:23:04 +02:00
@Test
public void testAddToContextFromDifferentSourceContext ( ) throws Exception {
setContextByName ( GCUBE ) ;
2021-09-17 10:35:57 +02:00
/* Needed to init context cache */
ContextManagement contextManagement = new ContextManagement ( ) ;
contextManagement . all ( false ) ;
2021-09-16 15:23:04 +02:00
Dataset datasetR1 = createDataset ( " R1 " , " da111111-dada-1111-1111-111111111111 " ) ;
Dataset datasetR2 = null ;
Dataset datasetR3 = null ;
try {
addToContextThenTestIfBehaveProperly ( datasetR1 , false , DEVNEXT ) ;
addToContextThenTestIfBehaveProperly ( datasetR1 , false , DEVSEC ) ;
setContextByName ( DEVNEXT ) ;
datasetR2 = createDataset ( " R2 " , " da222222-dada-2222-2222-222222222222 " ) ;
2021-09-16 17:23:13 +02:00
IsCorrelatedTo < Dataset , Dataset > isCorrelatedToR1R2 = createIsCorrelatedTo ( datasetR1 , datasetR2 , " da111111-1111-aaaa-2222-222222222222 " ) ;
2021-09-16 15:23:04 +02:00
getResourceManagement ( datasetR1 ) . exists ( ) ;
getIsRelatedToManagement ( isCorrelatedToR1R2 ) . exists ( ) ;
getResourceManagement ( datasetR2 ) . exists ( ) ;
setContextByName ( DEVSEC ) ;
datasetR3 = createDataset ( " R3 " , " da333333-dada-3333-3333-333333333333 " ) ;
2021-09-16 17:23:13 +02:00
IsCorrelatedTo < Dataset , Dataset > isCorrelatedToR1R3 = createIsCorrelatedTo ( datasetR1 , datasetR3 , " da111111-1111-aaaa-3333-333333333333 " ) ;
2021-09-16 15:23:04 +02:00
getResourceManagement ( datasetR1 ) . exists ( ) ;
getIsRelatedToManagement ( isCorrelatedToR1R3 ) . exists ( ) ;
getResourceManagement ( datasetR3 ) . exists ( ) ;
try {
getIsRelatedToManagement ( isCorrelatedToR1R2 ) . exists ( ) ;
} catch ( AvailableInAnotherContextException e ) {
// OK
logger . trace ( " As expected " ) ;
}
try {
getResourceManagement ( datasetR2 ) . exists ( ) ;
} catch ( AvailableInAnotherContextException e ) {
// OK
logger . trace ( " As expected " ) ;
}
setContextByName ( DEVNEXT ) ;
try {
getIsRelatedToManagement ( isCorrelatedToR1R3 ) . exists ( ) ;
} catch ( AvailableInAnotherContextException e ) {
// OK
logger . trace ( " As expected " ) ;
}
try {
getResourceManagement ( datasetR3 ) . exists ( ) ;
} catch ( AvailableInAnotherContextException e ) {
// OK
logger . trace ( " As expected " ) ;
}
setContextByName ( GCUBE ) ;
getResourceManagement ( datasetR1 ) . exists ( ) ;
try {
getIsRelatedToManagement ( isCorrelatedToR1R2 ) . exists ( ) ;
} catch ( AvailableInAnotherContextException e ) {
// OK
logger . trace ( " As expected " ) ;
}
try {
getResourceManagement ( datasetR2 ) . exists ( ) ;
} catch ( AvailableInAnotherContextException e ) {
// OK
logger . trace ( " As expected " ) ;
}
try {
getIsRelatedToManagement ( isCorrelatedToR1R3 ) . exists ( ) ;
} catch ( AvailableInAnotherContextException e ) {
// OK
logger . trace ( " As expected " ) ;
}
try {
getResourceManagement ( datasetR3 ) . exists ( ) ;
} catch ( AvailableInAnotherContextException e ) {
// OK
logger . trace ( " As expected " ) ;
}
2021-10-28 10:50:46 +02:00
checkNumerOfContext ( datasetR1 , 3 ) ;
checkNumerOfContext ( datasetR2 , 1 ) ;
checkNumerOfContext ( datasetR3 , 1 ) ;
2021-09-16 15:23:04 +02:00
2021-09-16 17:23:13 +02:00
setContextByName ( DEVNEXT ) ;
addToContextThenTestIfBehaveProperly ( datasetR1 , false , NEXTNEXT ) ;
2021-10-28 10:50:46 +02:00
checkNumerOfContext ( datasetR1 , 4 ) ;
checkNumerOfContext ( datasetR2 , 2 ) ;
checkNumerOfContext ( datasetR3 , 1 ) ;
2021-09-16 15:23:04 +02:00
2021-09-16 17:23:13 +02:00
setContextByName ( DEVSEC ) ;
addToContextThenTestIfBehaveProperly ( datasetR1 , false , DEVVRE ) ;
2021-10-28 10:50:46 +02:00
checkNumerOfContext ( datasetR1 , 5 ) ;
checkNumerOfContext ( datasetR2 , 2 ) ;
checkNumerOfContext ( datasetR3 , 2 ) ;
2021-09-16 15:23:04 +02:00
} catch ( Exception e ) {
logger . error ( " " , e ) ;
throw e ;
} finally {
2023-05-02 14:48:38 +02:00
RequestUtility . getRequestInfo ( ) . get ( ) . setHierarchicalMode ( false ) ;
RequestUtility . getRequestInfo ( ) . get ( ) . setIncludeContexts ( false ) ;
2021-09-16 15:23:04 +02:00
setContextByName ( GCUBE ) ;
deleteResource ( datasetR1 ) ;
if ( datasetR2 ! = null ) {
setContextByName ( DEVNEXT ) ;
deleteResource ( datasetR2 ) ;
}
if ( datasetR3 ! = null ) {
setContextByName ( DEVSEC ) ;
deleteResource ( datasetR3 ) ;
}
}
}
2022-06-07 17:15:50 +02:00
// @Test
// public void testAddToContext() throws Exception {
// ContextCache contextCache = ContextCache.getInstance();
// HostingNode hostingNode = new HostingNodeImpl();
2023-04-19 11:36:49 +02:00
// hostingNode.setUUID(UUID.fromString("a87bb07e-5320-4fd8-a48d-bf3cc55756c4"));
2023-05-02 14:48:38 +02:00
// ResourceManagement resourceManagement = getResourceManagement(hostingNode);
2022-06-07 17:15:50 +02:00
// resourceManagement.setDryRun(false);
// UUID contextUUID = ContextUtility.getInstance().getSecurityContextByFullName("/gcube/devsec").getUUID();
// resourceManagement.addToContext(contextUUID);
// }
2021-03-08 09:56:23 +01:00
}