Search in sources :

Example 11 with IStruct

use of org.apache.atlas.typesystem.IStruct in project incubator-atlas by apache.

the class NotificationEntityChangeListenerTest method testGetAllTraitsSuperTraits.

@Test
public void testGetAllTraitsSuperTraits() throws Exception {
    TypeSystem typeSystem = mock(TypeSystem.class);
    String traitName = "MyTrait";
    IStruct myTrait = new Struct(traitName);
    String superTraitName = "MySuperTrait";
    TraitType traitDef = mock(TraitType.class);
    Set<String> superTypeNames = Collections.singleton(superTraitName);
    TraitType superTraitDef = mock(TraitType.class);
    Set<String> superSuperTypeNames = Collections.emptySet();
    Referenceable entity = getEntity("id", myTrait);
    when(typeSystem.getDataType(TraitType.class, traitName)).thenReturn(traitDef);
    when(typeSystem.getDataType(TraitType.class, superTraitName)).thenReturn(superTraitDef);
    when(traitDef.getAllSuperTypeNames()).thenReturn(superTypeNames);
    when(superTraitDef.getAllSuperTypeNames()).thenReturn(superSuperTypeNames);
    List<IStruct> allTraits = NotificationEntityChangeListener.getAllTraits(entity, typeSystem);
    assertEquals(2, allTraits.size());
    for (IStruct trait : allTraits) {
        String typeName = trait.getTypeName();
        assertTrue(typeName.equals(traitName) || typeName.equals(superTraitName));
    }
}
Also used : TypeSystem(org.apache.atlas.typesystem.types.TypeSystem) Referenceable(org.apache.atlas.typesystem.Referenceable) TraitType(org.apache.atlas.typesystem.types.TraitType) IStruct(org.apache.atlas.typesystem.IStruct) IStruct(org.apache.atlas.typesystem.IStruct) Struct(org.apache.atlas.typesystem.Struct) Test(org.testng.annotations.Test)

Example 12 with IStruct

use of org.apache.atlas.typesystem.IStruct in project incubator-atlas by apache.

the class NotificationEntityChangeListenerTest method getEntity.

private Referenceable getEntity(String id, IStruct... traits) {
    String typeName = "typeName";
    Map<String, Object> values = new HashMap<>();
    List<String> traitNames = new LinkedList<>();
    Map<String, IStruct> traitMap = new HashMap<>();
    for (IStruct trait : traits) {
        String traitName = trait.getTypeName();
        traitNames.add(traitName);
        traitMap.put(traitName, trait);
    }
    return new Referenceable(id, typeName, values, traitNames, traitMap);
}
Also used : Referenceable(org.apache.atlas.typesystem.Referenceable) HashMap(java.util.HashMap) LinkedList(java.util.LinkedList) IStruct(org.apache.atlas.typesystem.IStruct)

Example 13 with IStruct

use of org.apache.atlas.typesystem.IStruct in project incubator-atlas by apache.

the class EntityAuditListener method onTraitsAdded.

@Override
public void onTraitsAdded(ITypedReferenceableInstance entity, Collection<? extends IStruct> traits) throws AtlasException {
    if (traits != null) {
        for (IStruct trait : traits) {
            EntityAuditEvent event = createEvent(entity, EntityAuditAction.TAG_ADD, "Added trait: " + InstanceSerialization.toJson(trait, true));
            auditRepository.putEvents(event);
        }
    }
}
Also used : EntityAuditEvent(org.apache.atlas.EntityAuditEvent) IStruct(org.apache.atlas.typesystem.IStruct)

Example 14 with IStruct

use of org.apache.atlas.typesystem.IStruct in project incubator-atlas by apache.

the class NotificationEntityChangeListener method getAllTraits.

// ----- helper methods -------------------------------------------------
// ----- helper methods ----------------------------------------------------
@VisibleForTesting
public static List<IStruct> getAllTraits(IReferenceableInstance entityDefinition, TypeSystem typeSystem) throws AtlasException {
    List<IStruct> traitInfo = new LinkedList<>();
    for (String traitName : entityDefinition.getTraits()) {
        IStruct trait = entityDefinition.getTrait(traitName);
        String typeName = trait.getTypeName();
        Map<String, Object> valuesMap = trait.getValuesMap();
        traitInfo.add(new Struct(typeName, valuesMap));
        traitInfo.addAll(getSuperTraits(typeName, valuesMap, typeSystem));
    }
    return traitInfo;
}
Also used : LinkedList(java.util.LinkedList) IStruct(org.apache.atlas.typesystem.IStruct) IStruct(org.apache.atlas.typesystem.IStruct) Struct(org.apache.atlas.typesystem.Struct) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 15 with IStruct

use of org.apache.atlas.typesystem.IStruct in project incubator-atlas by apache.

the class EntityResource method getTraitDefinitionForEntity.

/**
     * Fetches the trait definition for an entity given its guid and trait name
     *
     * @param guid globally unique identifier for the entity
     * @param traitName name of the trait
     */
@GET
@Path("{guid}/traitDefinitions/{traitName}")
@Produces(Servlets.JSON_MEDIA_TYPE)
public Response getTraitDefinitionForEntity(@PathParam("guid") String guid, @PathParam("traitName") String traitName) {
    if (LOG.isDebugEnabled()) {
        LOG.debug("==> EntityResource.getTraitDefinitionForEntity({}, {})", guid, traitName);
    }
    AtlasPerfTracer perf = null;
    try {
        if (AtlasPerfTracer.isPerfTraceEnabled(PERF_LOG)) {
            perf = AtlasPerfTracer.getPerfTracer(PERF_LOG, "EntityResource.getTraitDefinitionForEntity(" + guid + ", " + traitName + ")");
        }
        if (LOG.isDebugEnabled()) {
            LOG.debug("Fetching trait definition for entity {} and trait name {}", guid, traitName);
        }
        final AtlasClassification classification = entitiesStore.getClassification(guid, traitName);
        IStruct traitDefinition = restAdapters.getTrait(classification);
        JSONObject response = new JSONObject();
        response.put(AtlasClient.REQUEST_ID, Servlets.getRequestId());
        response.put(AtlasClient.RESULTS, new JSONObject(InstanceSerialization.toJson(traitDefinition, true)));
        return Response.ok(response).build();
    } catch (AtlasBaseException e) {
        LOG.error("Unable to get trait definition for entity {} and trait {}", guid, traitName, e);
        throw toWebApplicationException(e);
    } catch (IllegalArgumentException e) {
        LOG.error("Unable to get trait definition for entity {} and trait {}", guid, traitName, e);
        throw new WebApplicationException(Servlets.getErrorResponse(e, Response.Status.BAD_REQUEST));
    } catch (WebApplicationException e) {
        LOG.error("Unable to get trait definition for entity {} and trait {}", guid, traitName, e);
        throw e;
    } catch (Throwable e) {
        LOG.error("Unable to get trait definition for entity {} and trait {}", guid, traitName, e);
        throw new WebApplicationException(Servlets.getErrorResponse(e, Response.Status.INTERNAL_SERVER_ERROR));
    } finally {
        AtlasPerfTracer.log(perf);
        if (LOG.isDebugEnabled()) {
            LOG.debug("<== EntityResource.getTraitDefinitionForEntity({}, {})", guid, traitName);
        }
    }
}
Also used : AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) JSONObject(org.codehaus.jettison.json.JSONObject) AtlasPerfTracer(org.apache.atlas.utils.AtlasPerfTracer) AtlasClassification(org.apache.atlas.model.instance.AtlasClassification) IStruct(org.apache.atlas.typesystem.IStruct)

Aggregations

IStruct (org.apache.atlas.typesystem.IStruct)34 Struct (org.apache.atlas.typesystem.Struct)16 Test (org.testng.annotations.Test)12 LinkedList (java.util.LinkedList)11 Referenceable (org.apache.atlas.typesystem.Referenceable)11 ITypedStruct (org.apache.atlas.typesystem.ITypedStruct)10 ITypedReferenceableInstance (org.apache.atlas.typesystem.ITypedReferenceableInstance)8 HashMap (java.util.HashMap)7 TraitType (org.apache.atlas.typesystem.types.TraitType)6 AtlasBaseException (org.apache.atlas.exception.AtlasBaseException)5 Map (java.util.Map)4 AtlasClassification (org.apache.atlas.model.instance.AtlasClassification)4 ImmutableList (com.google.common.collect.ImmutableList)3 ImmutableMap (com.google.common.collect.ImmutableMap)3 ArrayList (java.util.ArrayList)3 AtlasException (org.apache.atlas.AtlasException)3 IReferenceableInstance (org.apache.atlas.typesystem.IReferenceableInstance)3 Id (org.apache.atlas.typesystem.persistence.Id)3 List (java.util.List)2 EntityAuditEvent (org.apache.atlas.EntityAuditEvent)2