Java Code Examples for org.apache.atlas.type.AtlasType#fromJson()

The following examples show how to use org.apache.atlas.type.AtlasType#fromJson() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestAtlasRelationshipDef.java    From incubator-atlas with Apache License 2.0 6 votes vote down vote up
@Test
public void testRelationshipDefSerDeEmpty() throws AtlasBaseException {

    AtlasRelationshipEndDef ep1 = new AtlasRelationshipEndDef("typeA", "attr1", Cardinality.SINGLE);
    AtlasRelationshipEndDef ep2 = new AtlasRelationshipEndDef("typeB", "attr2", Cardinality.SINGLE);
    AtlasRelationshipDef relationshipDef = new AtlasRelationshipDef("emptyRelationshipDef", "desc 1", "version1",
            RelationshipCategory.ASSOCIATION, AtlasRelationshipDef.PropagateTags.ONE_TO_TWO, ep1, ep2);

    String jsonString = AtlasType.toJson(relationshipDef);
    System.out.println(jsonString);
    assertNotNull(jsonString);

    AtlasRelationshipDef relationshipDef2 = AtlasType.fromJson(jsonString, AtlasRelationshipDef.class);
    String jsonString2 = AtlasType.toJson(relationshipDef2);

    assertEquals(jsonString, jsonString2);
    assertEquals(relationshipDef2, relationshipDef,
            "Incorrect serialization/deserialization of AtlasRelationshipDef");
}
 
Example 2
Source File: AtlasImportRequestTest.java    From atlas with Apache License 2.0 6 votes vote down vote up
@Test
public void serializeAtlasImportRequstFromJsonWithEmptyTransforms() {
    String jsonData = "{ \"options\": { \"transforms\": \"{ }\" } }";

    AtlasImportRequest request = AtlasType.fromJson(jsonData, AtlasImportRequest.class);

    assertNotNull(request);
    assertNotNull(request.getOptions());
    assertNotNull(request.getOptions().get(AtlasImportRequest.TRANSFORMS_KEY));

    ImportTransforms tr = ImportTransforms.fromJson(request.getOptions().get(AtlasImportRequest.TRANSFORMS_KEY));

    assertNotNull(tr);
    assertNotNull(tr.getTransforms());
    assertEquals(tr.getTransforms().size(), 0);
}
 
Example 3
Source File: AtlasImportRequestTest.java    From incubator-atlas with Apache License 2.0 6 votes vote down vote up
@Test
public void serializeAtlasImportRequstFromJsonWith1Transform() {
    String jsonData = "{ \"options\": { \"transforms\": \"{ \\\"hive_db\\\": { \\\"qualifiedName\\\": [ \\\"replace:@cl1:@cl2\\\" ] } }\" } }";

    AtlasImportRequest request = AtlasType.fromJson(jsonData, AtlasImportRequest.class);

    assertNotNull(request);
    assertNotNull(request.getOptions());
    assertNotNull(request.getOptions().get(AtlasImportRequest.TRANSFORMS_KEY));

    ImportTransforms tr = ImportTransforms.fromJson(request.getOptions().get(AtlasImportRequest.TRANSFORMS_KEY));

    assertNotNull(tr);
    assertNotNull(tr.getTransforms());
    assertEquals(tr.getTransforms().size(), 1);
    assertTrue(tr.getTransforms().containsKey("hive_db"));
    assertEquals(tr.getTransforms("hive_db").entrySet().size(), 1);
    assertTrue(tr.getTransforms("hive_db").containsKey("qualifiedName"));
    assertEquals(tr.getTransforms("hive_db").get("qualifiedName").size(), 1);
}
 
Example 4
Source File: TestEntitiesREST.java    From incubator-atlas with Apache License 2.0 5 votes vote down vote up
AtlasEntity serDeserEntity(AtlasEntity entity) throws IOException {
    //Convert from json to object and back to trigger the case where it gets translated to a map for attributes instead of AtlasEntity
    String      jsonString = AtlasType.toJson(entity);
    AtlasEntity newEntity  = AtlasType.fromJson(jsonString, AtlasEntity.class);

    return newEntity;
}
 
Example 5
Source File: StartEntityFetchByExportRequestTest.java    From atlas with Apache License 2.0 5 votes vote down vote up
@Test
public void fetchTypeUniqueAttributes() {
    String exportRequestJson = "{ \"itemsToExport\": [ { \"typeName\": \"hive_db\", \"uniqueAttributes\": {\"qualifiedName\": \"stocks@cl1\"} } ]}";
    AtlasExportRequest exportRequest = AtlasType.fromJson(exportRequestJson, AtlasExportRequest.class);

    startEntityFetchByExportRequestSpy.get(exportRequest);
    assertEquals(startEntityFetchByExportRequestSpy.getGeneratedQuery(), startEntityFetchByExportRequestSpy.getQueryTemplateForMatchType(exportRequest.getMatchTypeOptionValue()));
    assertEquals(startEntityFetchByExportRequestSpy.getSuppliedBindingsMap().get(BINDING_PARAMETER_TYPENAME), "hive_db");
    assertEquals(startEntityFetchByExportRequestSpy.getSuppliedBindingsMap().get(BINDING_PARAMETER_ATTR_NAME), "Referenceable.qualifiedName");
    assertEquals(startEntityFetchByExportRequestSpy.getSuppliedBindingsMap().get(BINDING_PARAMTER_ATTR_VALUE), "stocks@cl1");
}
 
Example 6
Source File: TestAtlasClassificationDef.java    From incubator-atlas with Apache License 2.0 5 votes vote down vote up
@Test
public void testClassificationDefSerDe() {
    AtlasClassificationDef classificationDef = ModelTestUtil.getClassificationDef();

    String jsonString = AtlasType.toJson(classificationDef);

    AtlasClassificationDef classificationDef2 = AtlasType.fromJson(jsonString, AtlasClassificationDef.class);

    assertEquals(classificationDef2, classificationDef,
                 "Incorrect serialization/deserialization of AtlasClassificationDef");
}
 
Example 7
Source File: TestAtlasClassificationDef.java    From atlas with Apache License 2.0 5 votes vote down vote up
@Test
public void testClassificationDefSerDeWithSuperType() {
    AtlasClassificationDef classificationDef = ModelTestUtil.getClassificationDefWithSuperType();

    String jsonString = AtlasType.toJson(classificationDef);

    AtlasClassificationDef classificationDef2 = AtlasType.fromJson(jsonString, AtlasClassificationDef.class);

    assertEquals(classificationDef2, classificationDef,
                 "Incorrect serialization/deserialization of AtlasClassificationDef with superType");
}
 
Example 8
Source File: GraphHelper.java    From atlas with Apache License 2.0 5 votes vote down vote up
public static Map getCustomAttributes(AtlasElement element) {
    Map    ret               = null;
    String customAttrsString = element.getProperty(CUSTOM_ATTRIBUTES_PROPERTY_KEY, String.class);

    if (customAttrsString != null) {
        ret = AtlasType.fromJson(customAttrsString, Map.class);
    }

    return ret;
}
 
Example 9
Source File: CreateUpdateEntitiesResult.java    From incubator-atlas with Apache License 2.0 5 votes vote down vote up
/**
 * Deserializes the given json into an instance of
 * CreateUpdateEntitiesResult.
 *
 * @param json
 *            the (unmodified) json that comes back from Atlas.
 * @return
 * @throws AtlasServiceException
 */
public static CreateUpdateEntitiesResult fromJson(String json) throws AtlasServiceException {

    GuidMapping guidMapping = AtlasType.fromJson(json, GuidMapping.class);
    EntityResult entityResult = EntityResult.fromString(json);
    CreateUpdateEntitiesResult result = new CreateUpdateEntitiesResult();
    result.setEntityResult(entityResult);
    result.setGuidMapping(guidMapping);
    return result;
}
 
Example 10
Source File: TestAtlasBusinessMetadataDef.java    From atlas with Apache License 2.0 5 votes vote down vote up
@Test
public void businessMetadataDefSerDes() {
    AtlasBusinessMetadataDef businessMetadataDef = new AtlasBusinessMetadataDef("test_businessMetadata", "test_description", null);
    String jsonString = AtlasType.toJson(businessMetadataDef);

    AtlasBusinessMetadataDef businessMetadataDef1 = AtlasType.fromJson(jsonString, AtlasBusinessMetadataDef.class);
    assertEquals(businessMetadataDef, businessMetadataDef1,
            "Incorrect serialization/deserialization of AtlasBusinessMetadataDef");
}
 
Example 11
Source File: ZipSourceDirect.java    From atlas with Apache License 2.0 5 votes vote down vote up
private <T> T convertFromJson(Class<T> clazz, String jsonData) throws AtlasBaseException {
    try {
        return AtlasType.fromJson(jsonData, clazz);

    } catch (Exception e) {
        throw new AtlasBaseException("Error converting file to JSON.", e);
    }
}
 
Example 12
Source File: RestUtilsTest.java    From atlas with Apache License 2.0 5 votes vote down vote up
private AtlasAttributeDef convertToJsonAndBack(AtlasTypeRegistry registry, AtlasStructDef structDef, AtlasAttributeDef attributeDef, boolean compositeExpected) throws AtlasBaseException {
    AtlasTypeDefGraphStoreV2 typeDefStore = makeTypeStore(registry);
    AtlasStructType          structType   = (AtlasStructType) registry.getType(structDef.getName());
    AtlasAttribute           attribute    = structType.getAttribute(attributeDef.getName());
    String                   attribJson   = AtlasStructDefStoreV2.toJsonFromAttribute(attribute);
    Map                      attrInfo     = AtlasType.fromJson(attribJson, Map.class);

    Assert.assertEquals(attrInfo.get("isComposite"), compositeExpected);

    return AtlasStructDefStoreV2.toAttributeDefFromJson(structDef, attrInfo, typeDefStore);
}
 
Example 13
Source File: TestEntitiesREST.java    From atlas with Apache License 2.0 5 votes vote down vote up
AtlasEntity serDeserEntity(AtlasEntity entity) throws IOException {
    //Convert from json to object and back to trigger the case where it gets translated to a map for attributes instead of AtlasEntity
    String      jsonString = AtlasType.toJson(entity);
    AtlasEntity newEntity  = AtlasType.fromJson(jsonString, AtlasEntity.class);

    return newEntity;
}
 
Example 14
Source File: CreateUpdateEntitiesResult.java    From atlas with Apache License 2.0 5 votes vote down vote up
/**
 * Deserializes the given json into an instance of
 * CreateUpdateEntitiesResult.
 *
 * @param json
 *            the (unmodified) json that comes back from Atlas.
 * @return
 * @throws AtlasServiceException
 */
public static CreateUpdateEntitiesResult fromJson(String json) throws AtlasServiceException {

    GuidMapping guidMapping = AtlasType.fromJson(json, GuidMapping.class);
    EntityResult entityResult = EntityResult.fromString(json);
    CreateUpdateEntitiesResult result = new CreateUpdateEntitiesResult();
    result.setEntityResult(entityResult);
    result.setGuidMapping(guidMapping);
    return result;
}
 
Example 15
Source File: TestAtlasEntity.java    From atlas with Apache License 2.0 4 votes vote down vote up
@Test
public void testEntitySerDeWithSuperTypes() throws AtlasBaseException {
    AtlasEntityDef    entityDef    = ModelTestUtil.getEntityDefWithSuperTypes();
    AtlasTypeRegistry typeRegistry = ModelTestUtil.getTypesRegistry();
    AtlasEntityType   entityType   = typeRegistry.getEntityTypeByName(entityDef.getName());

    assertNotNull(entityType);

    AtlasEntity ent1 =  entityType.createDefaultValue();

    String jsonString = AtlasType.toJson(ent1);

    AtlasEntity ent2 = AtlasType.fromJson(jsonString, AtlasEntity.class);

    entityType.normalizeAttributeValues(ent2);

    assertEquals(ent2, ent1, "Incorrect serialization/deserialization of AtlasEntity with superTypes");
}
 
Example 16
Source File: AtlasStructDefStoreV2.java    From atlas with Apache License 2.0 4 votes vote down vote up
@VisibleForTesting
public static AtlasAttributeDef toAttributeDefFromJson(AtlasStructDef           structDef,
                                                       Map                      attribInfo,
                                                       AtlasTypeDefGraphStoreV2 typeDefStore)
    throws AtlasBaseException {
    AtlasAttributeDef ret = new AtlasAttributeDef();

    ret.setName((String) attribInfo.get("name"));
    ret.setTypeName((String) attribInfo.get("dataType"));
    ret.setIsUnique((Boolean) attribInfo.get("isUnique"));
    ret.setIsIndexable((Boolean) attribInfo.get("isIndexable"));
    ret.setIncludeInNotification((Boolean) attribInfo.get("includeInNotification"));
    ret.setDefaultValue((String) attribInfo.get("defaultValue"));
    ret.setDescription((String) attribInfo.get("description"));

    if(attribInfo.get("options") != null) {
        ret.setOptions(AtlasType.fromJson((String) attribInfo.get("options"), Map.class));
    }

    ret.setDisplayName((String) attribInfo.get("displayName"));

    if ((Boolean)attribInfo.get("isComposite")) {
        ret.addConstraint(new AtlasConstraintDef(AtlasConstraintDef.CONSTRAINT_TYPE_OWNED_REF));
    }

    final String reverseAttributeName = (String) attribInfo.get("reverseAttributeName");
    if (StringUtils.isNotBlank(reverseAttributeName)) {
        ret.addConstraint(new AtlasConstraintDef(AtlasConstraintDef.CONSTRAINT_TYPE_INVERSE_REF,
                new HashMap<String, Object>() {{
                    put(AtlasConstraintDef.CONSTRAINT_PARAM_ATTRIBUTE, reverseAttributeName);
                }}));
    }

    Map     multiplicity = AtlasType.fromJson((String) attribInfo.get("multiplicity"), Map.class);
    Number  minCount     = (Number) multiplicity.get("lower");
    Number  maxCount     = (Number) multiplicity.get("upper");
    Boolean isUnique     = (Boolean) multiplicity.get("isUnique");

    if (minCount == null || minCount.intValue() == 0) {
        ret.setIsOptional(true);
        ret.setValuesMinCount(0);
    } else {
        ret.setIsOptional(false);
        ret.setValuesMinCount(minCount.intValue());
    }

    if (maxCount == null || maxCount.intValue() < 2) {
        ret.setCardinality(AtlasAttributeDef.Cardinality.SINGLE);
        ret.setValuesMaxCount(1);
    } else {
        if (isUnique == null || isUnique == Boolean.FALSE) {
            ret.setCardinality(AtlasAttributeDef.Cardinality.LIST);
        } else {
            ret.setCardinality(AtlasAttributeDef.Cardinality.SET);
        }

        ret.setValuesMaxCount(maxCount.intValue());
    }

    Number searchWeight = (Number) attribInfo.get("searchWeight");
    if( searchWeight != null ) {
        ret.setSearchWeight(searchWeight.intValue());
    } else {
        ret.setSearchWeight(-1);
    }

    String indexType = (String) attribInfo.get("indexType");
    if(!StringUtils.isEmpty(indexType)) {
        ret.setIndexType(AtlasAttributeDef.IndexType.valueOf(indexType));
    }
    return ret;
}
 
Example 17
Source File: TestAtlasClassification.java    From incubator-atlas with Apache License 2.0 4 votes vote down vote up
@Test
public void testClassificationSerDe() throws AtlasBaseException {
    AtlasClassificationDef  classificationDef  = ModelTestUtil.getClassificationDef();
    AtlasTypeRegistry       typeRegistry       = ModelTestUtil.getTypesRegistry();
    AtlasClassificationType classificationType = typeRegistry.getClassificationTypeByName(classificationDef.getName());

    assertNotNull(classificationType);

    AtlasClassification ent1 = ModelTestUtil.newClassification(classificationDef, typeRegistry);

    String jsonString = AtlasType.toJson(ent1);

    AtlasClassification ent2 = AtlasType.fromJson(jsonString, AtlasClassification.class);

    classificationType.normalizeAttributeValues(ent2);

    assertEquals(ent2, ent1, "Incorrect serialization/deserialization of AtlasClassification");
}
 
Example 18
Source File: TestAtlasClassification.java    From incubator-atlas with Apache License 2.0 4 votes vote down vote up
@Test
public void testClassificationSerDeWithSuperType() throws AtlasBaseException {
    AtlasClassificationDef  classificationDef  = ModelTestUtil.getClassificationDefWithSuperType();
    AtlasTypeRegistry       typeRegistry       = ModelTestUtil.getTypesRegistry();
    AtlasClassificationType classificationType = typeRegistry.getClassificationTypeByName(classificationDef.getName());

    assertNotNull(classificationType);

    AtlasClassification ent1 =  classificationType.createDefaultValue();

    String jsonString = AtlasType.toJson(ent1);

    AtlasClassification ent2 = AtlasType.fromJson(jsonString, AtlasClassification.class);

    classificationType.normalizeAttributeValues(ent2);

    assertEquals(ent2, ent1, "Incorrect serialization/deserialization of AtlasClassification with superType");
}
 
Example 19
Source File: TestAtlasStructDef.java    From atlas with Apache License 2.0 3 votes vote down vote up
@Test
public void testStructDefSerDeEmpty() {
    AtlasStructDef structDef = new AtlasStructDef("emptyStructDef");

    String jsonString = AtlasType.toJson(structDef);

    AtlasStructDef structDef2 = AtlasType.fromJson(jsonString, AtlasStructDef.class);

    assertEquals(structDef2, structDef, "Incorrect serialization/deserialization of AtlasStructDef");
}
 
Example 20
Source File: TestAtlasEnumDef.java    From incubator-atlas with Apache License 2.0 3 votes vote down vote up
@Test
public void testEnumDefSerDe() {
    AtlasEnumDef enumDef = ModelTestUtil.getEnumDef();

    String jsonString = AtlasType.toJson(enumDef);

    AtlasEnumDef enumDef2 = AtlasType.fromJson(jsonString, AtlasEnumDef.class);

    assertEquals(enumDef, enumDef2, "Incorrect serialization/deserialization of AtlasEnumDef");
}