http://git-wip-us.apache.org/repos/asf/atlas/blob/75415862/repository/src/main/java/org/apache/atlas/repository/ogm/glossary/AtlasGlossaryCategoryDTO.java ---------------------------------------------------------------------- diff --git a/repository/src/main/java/org/apache/atlas/repository/ogm/glossary/AtlasGlossaryCategoryDTO.java b/repository/src/main/java/org/apache/atlas/repository/ogm/glossary/AtlasGlossaryCategoryDTO.java new file mode 100644 index 0000000..b26ee76 --- /dev/null +++ b/repository/src/main/java/org/apache/atlas/repository/ogm/glossary/AtlasGlossaryCategoryDTO.java @@ -0,0 +1,159 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.atlas.repository.ogm.glossary; + +import org.apache.atlas.exception.AtlasBaseException; +import org.apache.atlas.model.glossary.AtlasGlossaryCategory; +import org.apache.atlas.model.instance.AtlasEntity; +import org.apache.atlas.model.instance.AtlasRelatedObjectId; +import org.apache.atlas.model.instance.AtlasRelationship; +import org.apache.atlas.type.AtlasTypeRegistry; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Component; + +import javax.inject.Inject; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; +import java.util.Objects; + +@Component +public class AtlasGlossaryCategoryDTO extends AbstractGlossaryDTO<AtlasGlossaryCategory> { + private static final Logger LOG = LoggerFactory.getLogger(AtlasGlossaryCategoryDTO.class); + + @Inject + protected AtlasGlossaryCategoryDTO(final AtlasTypeRegistry typeRegistry) { + super(typeRegistry, AtlasGlossaryCategory.class, true); + } + + @Override + public AtlasGlossaryCategory from(final AtlasEntity entity) { + if (LOG.isDebugEnabled()) { + LOG.debug("==> AtlasGlossaryCategoryDTO.from()", entity); + } + Objects.requireNonNull(entity, "entity"); + + AtlasGlossaryCategory ret = new AtlasGlossaryCategory(); + + ret.setGuid(entity.getGuid()); + ret.setQualifiedName((String) entity.getAttribute("qualifiedName")); + ret.setDisplayName((String) entity.getAttribute("displayName")); + ret.setShortDescription((String) entity.getAttribute("shortDescription")); + ret.setLongDescription((String) entity.getAttribute("longDescription")); + + Object anchor = entity.getRelationshipAttribute("anchor"); + if (anchor instanceof AtlasRelatedObjectId) { + LOG.debug("Processing anchor"); + if (((AtlasRelatedObjectId) anchor).getRelationshipStatus() == AtlasRelationship.Status.ACTIVE) { + ret.setAnchor(constructGlossaryId((AtlasRelatedObjectId) anchor)); + } + } + + Object parentCategory = entity.getRelationshipAttribute("parentCategory"); + if (parentCategory instanceof AtlasRelatedObjectId) { + LOG.debug("Processing parentCategory"); + if (((AtlasRelatedObjectId) parentCategory).getRelationshipStatus() == AtlasRelationship.Status.ACTIVE) { + ret.setParentCategory(constructRelatedCategoryId((AtlasRelatedObjectId) parentCategory)); + } + } + + Object childrenCategories = entity.getRelationshipAttribute("childrenCategories"); + if (childrenCategories instanceof Collection) { + LOG.debug("Processing childrenCategories"); + for (Object child : (Collection) childrenCategories) { + if (child instanceof AtlasRelatedObjectId) { + if (((AtlasRelatedObjectId) child).getRelationshipStatus() == AtlasRelationship.Status.ACTIVE) { + ret.addChild(constructRelatedCategoryId((AtlasRelatedObjectId) child)); + } + } + } + } + + Object terms = entity.getRelationshipAttribute("terms"); + if (terms instanceof Collection) { + LOG.debug("Processing terms"); + for (Object term : (Collection) terms) { + if (term instanceof AtlasRelatedObjectId) { + if (((AtlasRelatedObjectId) term).getRelationshipStatus() == AtlasRelationship.Status.ACTIVE) { + ret.addTerm(constructRelatedTermId((AtlasRelatedObjectId) term)); + } + } + } + } + + return ret; + } + + @Override + public AtlasGlossaryCategory from(final AtlasEntity.AtlasEntityWithExtInfo entityWithExtInfo) { + if (LOG.isDebugEnabled()) { + LOG.debug("==> AtlasGlossaryCategoryDTO.from()", entityWithExtInfo); + } + Objects.requireNonNull(entityWithExtInfo, "entity"); + AtlasGlossaryCategory ret = from(entityWithExtInfo.getEntity()); + + if (LOG.isDebugEnabled()) { + LOG.debug("<== AtlasGlossaryCategoryDTO.from() : {}", ret); + } + return ret; + } + + @Override + public AtlasEntity toEntity(final AtlasGlossaryCategory obj) throws AtlasBaseException { + if (LOG.isDebugEnabled()) { + LOG.debug("==> AtlasGlossaryCategoryDTO.toEntity()", obj); + } + Objects.requireNonNull(obj, "atlasGlossaryCategory"); + Objects.requireNonNull(obj.getQualifiedName(), "atlasGlossaryCategory qualifiedName must be specified"); + Objects.requireNonNull(obj.getAnchor(), "atlasGlossaryCategory anchor must be specified"); + + AtlasEntity ret = getDefaultAtlasEntity(obj); + + ret.setAttribute("qualifiedName", obj.getQualifiedName()); + ret.setAttribute("displayName", obj.getDisplayName()); + ret.setAttribute("shortDescription", obj.getShortDescription()); + ret.setAttribute("longDescription", obj.getLongDescription()); + + if (LOG.isDebugEnabled()) { + LOG.debug("<== AtlasGlossaryCategoryDTO.toEntity() : {}", ret); + } + return ret; + } + + @Override + public AtlasEntity.AtlasEntityWithExtInfo toEntityWithExtInfo(final AtlasGlossaryCategory obj) throws AtlasBaseException { + if (LOG.isDebugEnabled()) { + LOG.debug("==> AtlasGlossaryCategoryDTO.toEntityWithExtInfo()", obj); + } + Objects.requireNonNull(obj, "atlasGlossaryCategory"); + AtlasEntity.AtlasEntityWithExtInfo ret = new AtlasEntity.AtlasEntityWithExtInfo(toEntity(obj)); + + if (LOG.isDebugEnabled()) { + LOG.debug("<== AtlasGlossaryCategoryDTO.toEntityWithExtInfo() : {}", ret); + } + return ret; + } + + @Override + public Map<String, Object> getUniqueAttributes(final AtlasGlossaryCategory obj) { + Map<String, Object> ret = new HashMap<>(); + ret.put("qualifiedName", obj.getQualifiedName()); + return ret; + } +}
http://git-wip-us.apache.org/repos/asf/atlas/blob/75415862/repository/src/main/java/org/apache/atlas/repository/ogm/glossary/AtlasGlossaryDTO.java ---------------------------------------------------------------------- diff --git a/repository/src/main/java/org/apache/atlas/repository/ogm/glossary/AtlasGlossaryDTO.java b/repository/src/main/java/org/apache/atlas/repository/ogm/glossary/AtlasGlossaryDTO.java new file mode 100644 index 0000000..e378660 --- /dev/null +++ b/repository/src/main/java/org/apache/atlas/repository/ogm/glossary/AtlasGlossaryDTO.java @@ -0,0 +1,155 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.atlas.repository.ogm.glossary; + +import org.apache.atlas.exception.AtlasBaseException; +import org.apache.atlas.model.glossary.AtlasGlossary; +import org.apache.atlas.model.instance.AtlasEntity; +import org.apache.atlas.model.instance.AtlasRelatedObjectId; +import org.apache.atlas.model.instance.AtlasRelationship; +import org.apache.atlas.type.AtlasTypeRegistry; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Component; + +import javax.inject.Inject; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; +import java.util.Objects; + +@Component +public class AtlasGlossaryDTO extends AbstractGlossaryDTO<AtlasGlossary> { + private static final Logger LOG = LoggerFactory.getLogger(AtlasGlossaryDTO.class); + + @Inject + public AtlasGlossaryDTO(AtlasTypeRegistry typeRegistry) { + super(typeRegistry, AtlasGlossary.class, true); + } + + @Override + public AtlasGlossary from(final AtlasEntity entity) { + if (LOG.isDebugEnabled()) { + LOG.debug("==> AtlasGlossaryDTO.from()", entity); + } + + Objects.requireNonNull(entity, "entity"); + + AtlasGlossary ret = new AtlasGlossary(); + + ret.setGuid(entity.getGuid()); + ret.setQualifiedName((String) entity.getAttribute("qualifiedName")); + ret.setDisplayName((String) entity.getAttribute("displayName")); + ret.setShortDescription((String) entity.getAttribute("shortDescription")); + ret.setLongDescription((String) entity.getAttribute("longDescription")); + ret.setLanguage((String) entity.getAttribute("language")); + ret.setUsage((String) entity.getAttribute("usage")); + + Object categoriesAttr = entity.getRelationshipAttribute("categories"); + Object termsAttr = entity.getRelationshipAttribute("terms"); + + // Populate categories + if (Objects.nonNull(categoriesAttr)) { + LOG.debug("Processing categories"); + if (categoriesAttr instanceof Collection) { + for (Object o : (Collection) categoriesAttr) { + if (o instanceof AtlasRelatedObjectId) { + if (((AtlasRelatedObjectId) o).getRelationshipStatus() == AtlasRelationship.Status.ACTIVE) { + ret.addCategory(constructRelatedCategoryId((AtlasRelatedObjectId) o)); + } + } + } + } + } + + // Populate terms + if (Objects.nonNull(termsAttr)) { + LOG.debug("Processing terms"); + if (termsAttr instanceof Collection) { + for (Object o : (Collection) termsAttr) { + if (o instanceof AtlasRelatedObjectId) { + if (((AtlasRelatedObjectId) o).getRelationshipStatus() == AtlasRelationship.Status.ACTIVE) { + ret.addTerm(constructRelatedTermId((AtlasRelatedObjectId) o)); + } + } + } + } + } + + return ret; + } + + @Override + public AtlasGlossary from(final AtlasEntity.AtlasEntityWithExtInfo entityWithExtInfo) { + if (LOG.isDebugEnabled()) { + LOG.debug("==> AtlasGlossaryDTO.from()",entityWithExtInfo); + } + Objects.requireNonNull(entityWithExtInfo, "entity"); + AtlasGlossary ret = from(entityWithExtInfo.getEntity()); + + if (LOG.isDebugEnabled()) { + LOG.debug("<== AtlasGlossaryDTO.from() : {}", ret); + } + return ret; + } + + @Override + public AtlasEntity toEntity(final AtlasGlossary obj) throws AtlasBaseException { + if (LOG.isDebugEnabled()) { + LOG.debug("==> AtlasGlossaryDTO.toEntity()", obj); + } + Objects.requireNonNull(obj, "atlasGlossary"); + Objects.requireNonNull(obj.getQualifiedName(), "atlasGlossary qualifiedName must be specified"); + + AtlasEntity ret = getDefaultAtlasEntity(obj); + + ret.setAttribute("qualifiedName", obj.getQualifiedName()); + ret.setAttribute("displayName", obj.getDisplayName()); + ret.setAttribute("shortDescription", obj.getShortDescription()); + ret.setAttribute("longDescription", obj.getLongDescription()); + ret.setAttribute("language", obj.getLanguage()); + ret.setAttribute("usage", obj.getUsage()); + + if (LOG.isDebugEnabled()) { + LOG.debug("<== AtlasGlossaryDTO.toEntity() : {}", ret); + } + return ret; + } + + @Override + public AtlasEntity.AtlasEntityWithExtInfo toEntityWithExtInfo(final AtlasGlossary obj) throws AtlasBaseException { + if (LOG.isDebugEnabled()) { + LOG.debug("==> AtlasGlossaryDTO.toEntityWithExtInfo()", obj); + } + Objects.requireNonNull(obj, "atlasGlossary"); + AtlasEntity entity = toEntity(obj); + AtlasEntity.AtlasEntityWithExtInfo ret = new AtlasEntity.AtlasEntityWithExtInfo(entity); + + if (LOG.isDebugEnabled()) { + LOG.debug("<== AtlasGlossaryDTO.toEntityWithExtInfo() : {}", ret); + } + return ret; + } + + @Override + public Map<String, Object> getUniqueAttributes(final AtlasGlossary obj) { + Map<String, Object> ret = new HashMap<>(); + ret.put("qualifiedName", obj.getQualifiedName()); + return ret; + } +} http://git-wip-us.apache.org/repos/asf/atlas/blob/75415862/repository/src/main/java/org/apache/atlas/repository/ogm/glossary/AtlasGlossaryTermDTO.java ---------------------------------------------------------------------- diff --git a/repository/src/main/java/org/apache/atlas/repository/ogm/glossary/AtlasGlossaryTermDTO.java b/repository/src/main/java/org/apache/atlas/repository/ogm/glossary/AtlasGlossaryTermDTO.java new file mode 100644 index 0000000..3f8bfe2 --- /dev/null +++ b/repository/src/main/java/org/apache/atlas/repository/ogm/glossary/AtlasGlossaryTermDTO.java @@ -0,0 +1,250 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.atlas.repository.ogm.glossary; + +import org.apache.atlas.exception.AtlasBaseException; +import org.apache.atlas.model.glossary.AtlasGlossaryTerm; +import org.apache.atlas.model.instance.AtlasEntity; +import org.apache.atlas.model.instance.AtlasEntityHeader; +import org.apache.atlas.model.instance.AtlasRelatedObjectId; +import org.apache.atlas.model.instance.AtlasRelationship; +import org.apache.atlas.type.AtlasTypeRegistry; +import org.apache.commons.collections.CollectionUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Component; + +import javax.inject.Inject; +import java.util.Collection; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; + +@Component +public class AtlasGlossaryTermDTO extends AbstractGlossaryDTO<AtlasGlossaryTerm> { + private static final Logger LOG = LoggerFactory.getLogger(AtlasGlossaryTermDTO.class); + + @Inject + protected AtlasGlossaryTermDTO(final AtlasTypeRegistry typeRegistry) { + super(typeRegistry, AtlasGlossaryTerm.class, true); + } + + @Override + public AtlasGlossaryTerm from(final AtlasEntity entity) { + if (LOG.isDebugEnabled()) { + LOG.debug("==> AtlasGlossaryTermDTO.from()", entity); + } + + Objects.requireNonNull(entity, "entity"); + + AtlasGlossaryTerm ret = new AtlasGlossaryTerm(); + + ret.setGuid(entity.getGuid()); + ret.setQualifiedName((String) entity.getAttribute("qualifiedName")); + ret.setDisplayName((String) entity.getAttribute("displayName")); + ret.setShortDescription((String) entity.getAttribute("shortDescription")); + ret.setLongDescription((String) entity.getAttribute("longDescription")); + ret.setExamples((List<String>) entity.getAttribute("examples")); + ret.setAbbreviation((String) entity.getAttribute("abbreviation")); + + Object anchor = entity.getRelationshipAttribute("anchor"); + if (anchor instanceof AtlasRelatedObjectId) { + LOG.debug("Processing anchor"); + if (((AtlasRelatedObjectId) anchor).getRelationshipStatus() == AtlasRelationship.Status.ACTIVE) { + ret.setAnchor(constructGlossaryId((AtlasRelatedObjectId) anchor)); + } + } + + Object categories = entity.getRelationshipAttribute("categories"); + if (categories instanceof Collection) { + LOG.debug("Processing categories"); + for (Object category : (Collection) categories) { + if (category instanceof AtlasRelatedObjectId) { + if (((AtlasRelatedObjectId) category).getRelationshipStatus() == AtlasRelationship.Status.ACTIVE) { + ret.addCategory(constructTermCategorizationId((AtlasRelatedObjectId) category)); + } + } + } + } +// ret.setContextRelevantTerms(toRelatedObjectIdsSet(entity.getRelationshipAttribute("contextRelevantTerms"))); +// ret.setUsedInContexts(toRelatedObjectIdsSet(entity.getRelationshipAttribute("usedInContexts"))); + + Object assignedEntities = entity.getRelationshipAttribute("assignedEntities"); + if (assignedEntities instanceof Collection) { + LOG.debug("Processing assigned entities"); + for (Object assignedEntity : (Collection) assignedEntities) { + if (assignedEntity instanceof AtlasRelatedObjectId) { + AtlasRelatedObjectId id = (AtlasRelatedObjectId) assignedEntity; + if (id.getRelationshipStatus() == AtlasRelationship.Status.ACTIVE) { + AtlasEntityHeader entityHeader = new AtlasEntityHeader(id.getTypeName(), id.getGuid(), id.getUniqueAttributes()); + if (entityHeader.getAttributes() == null) { + entityHeader.setAttributes(new HashMap<>()); + } + entityHeader.getAttributes().put("relationGuid", id.getRelationshipGuid()); + ret.addAssignedEntity(entityHeader); + } + } + } + } + + Object seeAlso = entity.getRelationshipAttribute("seeAlso"); + if (seeAlso instanceof Collection && CollectionUtils.isNotEmpty((Collection) seeAlso)) { + LOG.debug("Processing RelatedTerm(seeAlso)"); + ret.setSeeAlso(toRelatedTermIdsSet(seeAlso)); + } + + Object synonyms = entity.getRelationshipAttribute("synonyms"); + if (synonyms instanceof Collection && CollectionUtils.isNotEmpty((Collection) synonyms)) { + LOG.debug("Processing Synonym(synonyms)"); + ret.setSynonyms(toRelatedTermIdsSet(synonyms)); + } + + Object antonyms = entity.getRelationshipAttribute("antonyms"); + if (antonyms instanceof Collection && CollectionUtils.isNotEmpty((Collection) antonyms)) { + LOG.debug("Processing Antonym(antonyms)"); + ret.setAntonyms(toRelatedTermIdsSet(antonyms)); + } + + Object preferredTerms = entity.getRelationshipAttribute("preferredTerms"); + if (preferredTerms instanceof Collection && CollectionUtils.isNotEmpty((Collection) preferredTerms)) { + LOG.debug("Processing preferredTerm(preferredTerms)"); + ret.setPreferredTerms(toRelatedTermIdsSet(preferredTerms)); + } + + Object preferredToTerms = entity.getRelationshipAttribute("preferredToTerms"); + if (preferredToTerms instanceof Collection && CollectionUtils.isNotEmpty((Collection) preferredToTerms)) { + LOG.debug("Processing preferredTerm(preferredToTerms)"); + ret.setPreferredToTerms(toRelatedTermIdsSet(preferredToTerms)); + } + + Object replacementTerms = entity.getRelationshipAttribute("replacementTerms"); + if (replacementTerms instanceof Collection && CollectionUtils.isNotEmpty((Collection) replacementTerms)) { + LOG.debug("Processing ReplacementTerm(replacementTerms)"); + ret.setReplacementTerms(toRelatedTermIdsSet(replacementTerms)); + } + + Object replacedBy = entity.getRelationshipAttribute("replacedBy"); + if (replacedBy instanceof Collection && CollectionUtils.isNotEmpty((Collection) replacedBy)) { + LOG.debug("Processing ReplacementTerm(replacedBy)"); + ret.setReplacedBy(toRelatedTermIdsSet(replacedBy)); + } + + Object translationTerms = entity.getRelationshipAttribute("translationTerms"); + if (translationTerms instanceof Collection && CollectionUtils.isNotEmpty((Collection) translationTerms)) { + LOG.debug("Processing Translation(translationTerms)"); + ret.setTranslationTerms(toRelatedTermIdsSet(translationTerms)); + } + + Object translatedTerms = entity.getRelationshipAttribute("translatedTerms"); + if (translatedTerms instanceof Collection && CollectionUtils.isNotEmpty((Collection) translatedTerms)) { + LOG.debug("Processing Translation(translatedTerms)"); + ret.setTranslatedTerms(toRelatedTermIdsSet(translatedTerms)); + } + + Object isA = entity.getRelationshipAttribute("isA"); + if (isA instanceof Collection && CollectionUtils.isNotEmpty((Collection) isA)) { + LOG.debug("Processing Classifies(isA)"); + ret.setIsA(toRelatedTermIdsSet(isA)); + } + + Object classifies = entity.getRelationshipAttribute("classifies"); + if (classifies instanceof Collection && CollectionUtils.isNotEmpty((Collection) classifies)) { + LOG.debug("Processing Classifies(classifies)"); + ret.setClassifies(toRelatedTermIdsSet(classifies)); + } + + Object validValues = entity.getRelationshipAttribute("validValues"); + if (validValues instanceof Collection && CollectionUtils.isNotEmpty((Collection) validValues)) { + LOG.debug("Processing validValue(validValues)"); + ret.setValidValues(toRelatedTermIdsSet(validValues)); + } + + Object validValuesFor = entity.getRelationshipAttribute("validValuesFor"); + if (validValuesFor instanceof Collection && CollectionUtils.isNotEmpty((Collection) validValuesFor)) { + LOG.debug("Processing validValue(validValuesFor)"); + ret.setValidValuesFor(toRelatedTermIdsSet(validValuesFor)); + } + + if (LOG.isDebugEnabled()) { + LOG.debug("<== AtlasGlossaryTermDTO.from() : {}", ret); + } + return ret; + } + + @Override + public AtlasGlossaryTerm from(final AtlasEntity.AtlasEntityWithExtInfo entityWithExtInfo) { + if (LOG.isDebugEnabled()) { + LOG.debug("==> AtlasGlossaryTermDTO.from()", entityWithExtInfo); + } + Objects.requireNonNull(entityWithExtInfo, "entityWithExtInfo"); + AtlasGlossaryTerm ret = from(entityWithExtInfo.getEntity()); + + if (LOG.isDebugEnabled()) { + LOG.debug("<== AtlasGlossaryTermDTO.from() : {}", ret); + } + return ret; + } + + @Override + public AtlasEntity toEntity(final AtlasGlossaryTerm obj) throws AtlasBaseException { + if (LOG.isDebugEnabled()) { + LOG.debug("==> AtlasGlossaryTermDTO.toEntity()", obj); + } + Objects.requireNonNull(obj, "atlasGlossaryTerm"); + Objects.requireNonNull(obj.getQualifiedName(), "atlasGlossaryTerm qualifiedName must be specified"); + Objects.requireNonNull(obj.getAnchor(), "atlasGlossaryTerm anchor must be specified"); + + AtlasEntity ret = getDefaultAtlasEntity(obj); + + ret.setAttribute("qualifiedName", obj.getQualifiedName()); + ret.setAttribute("displayName", obj.getDisplayName()); + ret.setAttribute("shortDescription", obj.getShortDescription()); + ret.setAttribute("longDescription", obj.getLongDescription()); + ret.setAttribute("examples", obj.getExamples()); + ret.setAttribute("abbreviation", obj.getAbbreviation()); + + if (LOG.isDebugEnabled()) { + LOG.debug("<== AtlasGlossaryTermDTO.toEntity() : {}", ret); + } + return ret; + } + + @Override + public AtlasEntity.AtlasEntityWithExtInfo toEntityWithExtInfo(final AtlasGlossaryTerm obj) throws AtlasBaseException { + if (LOG.isDebugEnabled()) { + LOG.debug("==> AtlasGlossaryTermDTO.toEntityWithExtInfo()", obj); + } + + Objects.requireNonNull(obj, "atlasGlossaryTerm"); + AtlasEntity entity = toEntity(obj); + AtlasEntity.AtlasEntityWithExtInfo ret = new AtlasEntity.AtlasEntityWithExtInfo(entity); + + if (LOG.isDebugEnabled()) { + LOG.debug("<== AtlasGlossaryTermDTO.toEntityWithExtInfo() : {}", ret); + } + return ret; + } + + @Override + public Map<String, Object> getUniqueAttributes(final AtlasGlossaryTerm obj) { + Map<String, Object> ret = new HashMap<>(); + ret.put("qualifiedName", obj.getQualifiedName()); + return ret; + } +} http://git-wip-us.apache.org/repos/asf/atlas/blob/75415862/repository/src/main/java/org/apache/atlas/repository/ogm/profiles/AtlasSavedSearchDTO.java ---------------------------------------------------------------------- diff --git a/repository/src/main/java/org/apache/atlas/repository/ogm/profiles/AtlasSavedSearchDTO.java b/repository/src/main/java/org/apache/atlas/repository/ogm/profiles/AtlasSavedSearchDTO.java new file mode 100644 index 0000000..f38912c --- /dev/null +++ b/repository/src/main/java/org/apache/atlas/repository/ogm/profiles/AtlasSavedSearchDTO.java @@ -0,0 +1,110 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.atlas.repository.ogm.profiles; + +import org.apache.atlas.exception.AtlasBaseException; +import org.apache.atlas.model.discovery.SearchParameters; +import org.apache.atlas.model.instance.AtlasEntity; +import org.apache.atlas.model.instance.AtlasEntity.AtlasEntityWithExtInfo; +import org.apache.atlas.model.profile.AtlasUserSavedSearch; +import org.apache.atlas.repository.ogm.AbstractDataTransferObject; +import org.apache.atlas.type.AtlasType; +import org.apache.atlas.type.AtlasTypeRegistry; +import org.apache.commons.lang3.StringUtils; +import org.springframework.stereotype.Component; + +import javax.inject.Inject; +import java.util.HashMap; +import java.util.Map; + + +@Component +public class AtlasSavedSearchDTO extends AbstractDataTransferObject<AtlasUserSavedSearch> { + private static final String PROPERTY_NAME = "name"; + private static final String PROPERTY_OWNER_NAME = "ownerName"; + private static final String PROPERTY_SEARCH_PARAMETERS = "searchParameters"; + private static final String PROPERTY_UNIQUE_NAME = "uniqueName"; + private static final String PROPERTY_SEARCH_TYPE = "searchType"; + private static final String PROPERTY_UI_PARAMETERS = "uiParameters"; + + @Inject + public AtlasSavedSearchDTO(AtlasTypeRegistry typeRegistry) { + super(typeRegistry, AtlasUserSavedSearch.class, true); + } + + @Override + public AtlasUserSavedSearch from(AtlasEntity entity) { + AtlasUserSavedSearch savedSearch = new AtlasUserSavedSearch(); + + savedSearch.setGuid(entity.getGuid()); + savedSearch.setName((String) entity.getAttribute(PROPERTY_NAME)); + savedSearch.setOwnerName((String) entity.getAttribute(PROPERTY_OWNER_NAME)); + savedSearch.setSearchType(AtlasUserSavedSearch.SavedSearchType.to((String) entity.getAttribute(PROPERTY_SEARCH_TYPE))); + + String jsonSearchParams = (String) entity.getAttribute(PROPERTY_SEARCH_PARAMETERS); + + if (StringUtils.isNotEmpty(jsonSearchParams)) { + savedSearch.setSearchParameters(AtlasType.fromJson(jsonSearchParams, SearchParameters.class)); + } + + savedSearch.setUiParameters((String) entity.getAttribute(PROPERTY_UI_PARAMETERS)); + + return savedSearch; + } + + @Override + public AtlasUserSavedSearch from(AtlasEntityWithExtInfo entityWithExtInfo) { + return from(entityWithExtInfo.getEntity()); + } + + @Override + public AtlasEntity toEntity(AtlasUserSavedSearch obj) throws AtlasBaseException { + AtlasEntity entity = getDefaultAtlasEntity(obj); + + entity.setAttribute(PROPERTY_NAME, obj.getName()); + entity.setAttribute(PROPERTY_OWNER_NAME, obj.getOwnerName()); + entity.setAttribute(PROPERTY_SEARCH_TYPE, obj.getSearchType()); + entity.setAttribute(PROPERTY_UNIQUE_NAME, getUniqueValue(obj)); + + if (obj.getSearchParameters() != null) { + entity.setAttribute(PROPERTY_SEARCH_PARAMETERS, AtlasType.toJson(obj.getSearchParameters())); + } + + entity.setAttribute(PROPERTY_UI_PARAMETERS, obj.getUiParameters()); + + return entity; + } + + @Override + public AtlasEntityWithExtInfo toEntityWithExtInfo(AtlasUserSavedSearch obj) throws AtlasBaseException { + return new AtlasEntityWithExtInfo(toEntity(obj)); + } + + @Override + public Map<String, Object> getUniqueAttributes(AtlasUserSavedSearch obj) { + Map<String, Object> ret = new HashMap<>(); + + ret.put(PROPERTY_UNIQUE_NAME, getUniqueValue(obj)); + + return ret; + } + + private String getUniqueValue(AtlasUserSavedSearch obj) { + return String.format("%s:%s", obj.getOwnerName(), obj.getName()) ; + } +} http://git-wip-us.apache.org/repos/asf/atlas/blob/75415862/repository/src/main/java/org/apache/atlas/repository/ogm/profiles/AtlasUserProfileDTO.java ---------------------------------------------------------------------- diff --git a/repository/src/main/java/org/apache/atlas/repository/ogm/profiles/AtlasUserProfileDTO.java b/repository/src/main/java/org/apache/atlas/repository/ogm/profiles/AtlasUserProfileDTO.java new file mode 100644 index 0000000..aa51cbf --- /dev/null +++ b/repository/src/main/java/org/apache/atlas/repository/ogm/profiles/AtlasUserProfileDTO.java @@ -0,0 +1,120 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.atlas.repository.ogm.profiles; + +import org.apache.atlas.exception.AtlasBaseException; +import org.apache.atlas.model.instance.AtlasEntity; +import org.apache.atlas.model.instance.AtlasEntity.AtlasEntityWithExtInfo; +import org.apache.atlas.model.instance.AtlasObjectId; +import org.apache.atlas.model.profile.AtlasUserProfile; +import org.apache.atlas.model.profile.AtlasUserSavedSearch; +import org.apache.atlas.repository.ogm.AbstractDataTransferObject; +import org.apache.atlas.type.AtlasTypeRegistry; +import org.springframework.stereotype.Component; + +import javax.inject.Inject; +import java.util.*; + +@Component +public class AtlasUserProfileDTO extends AbstractDataTransferObject<AtlasUserProfile> { + private final String PROPERTY_USER_NAME = "name"; + private final String PROPERTY_FULL_NAME = "fullName"; + private final String PROPERTY_SAVED_SEARCHES = "savedSearches"; + + private final AtlasSavedSearchDTO savedSearchDTO; + + @Inject + public AtlasUserProfileDTO(AtlasTypeRegistry typeRegistry, AtlasSavedSearchDTO savedSearchDTO) { + super(typeRegistry, AtlasUserProfile.class, true); + + this.savedSearchDTO = savedSearchDTO; + } + + public AtlasUserProfile from(AtlasEntity entity) { + AtlasUserProfile profile = new AtlasUserProfile(); + + profile.setGuid(entity.getGuid()); + profile.setName((String) entity.getAttribute(PROPERTY_USER_NAME)); + profile.setFullName((String) entity.getAttribute(PROPERTY_FULL_NAME)); + + return profile; + } + + public AtlasUserProfile from(AtlasEntityWithExtInfo entityWithExtInfo) { + AtlasUserProfile userProfile = from(entityWithExtInfo.getEntity()); + + Object savedSearches = entityWithExtInfo.getEntity().getAttribute(PROPERTY_SAVED_SEARCHES); + + if (savedSearches instanceof Collection) { + for (Object o : (Collection) savedSearches) { + if (o instanceof AtlasObjectId) { + AtlasObjectId ssObjId = (AtlasObjectId) o; + AtlasEntity ssEntity = entityWithExtInfo.getReferredEntity(ssObjId.getGuid()); + + if (ssEntity != null && ssEntity.getStatus() == AtlasEntity.Status.ACTIVE) { + AtlasUserSavedSearch savedSearch = savedSearchDTO.from(ssEntity); + userProfile.getSavedSearches().add(savedSearch); + } + } + } + } + + return userProfile; + } + + @Override + public AtlasEntity toEntity(AtlasUserProfile obj) throws AtlasBaseException { + AtlasEntity entity = getDefaultAtlasEntity(obj); + + entity.setAttribute(PROPERTY_USER_NAME, obj.getName()); + entity.setAttribute(PROPERTY_FULL_NAME, obj.getFullName()); + + return entity; + } + + @Override + public AtlasEntityWithExtInfo toEntityWithExtInfo(AtlasUserProfile obj) throws AtlasBaseException { + AtlasEntity entity = toEntity(obj); + AtlasEntityWithExtInfo entityWithExtInfo = new AtlasEntityWithExtInfo(entity); + + List<AtlasObjectId> objectIds = new ArrayList<>(); + + for (AtlasUserSavedSearch ss : obj.getSavedSearches()) { + AtlasEntity ssEntity = savedSearchDTO.toEntity(ss); + + entityWithExtInfo.addReferredEntity(ssEntity); + + objectIds.add(new AtlasObjectId(ssEntity.getGuid(), savedSearchDTO.getEntityType().getTypeName(), savedSearchDTO.getUniqueAttributes(ss))); + } + + if (objectIds.size() > 0) { + entity.setAttribute(PROPERTY_SAVED_SEARCHES, objectIds); + } + + return entityWithExtInfo; + } + + @Override + public Map<String, Object> getUniqueAttributes(AtlasUserProfile obj) { + Map<String, Object> ret = new HashMap<>(); + + ret.put(PROPERTY_USER_NAME, obj.getName()); + + return ret; + } +} http://git-wip-us.apache.org/repos/asf/atlas/blob/75415862/repository/src/main/java/org/apache/atlas/repository/store/graph/v1/AtlasEntityChangeNotifier.java ---------------------------------------------------------------------- diff --git a/repository/src/main/java/org/apache/atlas/repository/store/graph/v1/AtlasEntityChangeNotifier.java b/repository/src/main/java/org/apache/atlas/repository/store/graph/v1/AtlasEntityChangeNotifier.java index b9945d4..e3f6f88 100644 --- a/repository/src/main/java/org/apache/atlas/repository/store/graph/v1/AtlasEntityChangeNotifier.java +++ b/repository/src/main/java/org/apache/atlas/repository/store/graph/v1/AtlasEntityChangeNotifier.java @@ -60,16 +60,16 @@ public class AtlasEntityChangeNotifier { private final Set<EntityChangeListener> entityChangeListeners; private final Set<EntityChangeListenerV2> entityChangeListenersV2; private final AtlasInstanceConverter instanceConverter; + private final FullTextMapperV2 fullTextMapperV2; - @Inject - private FullTextMapperV2 fullTextMapperV2; @Inject public AtlasEntityChangeNotifier(Set<EntityChangeListener> entityChangeListeners, Set<EntityChangeListenerV2> entityChangeListenersV2, - AtlasInstanceConverter instanceConverter) { + AtlasInstanceConverter instanceConverter, final FullTextMapperV2 fullTextMapperV2) { this.entityChangeListeners = entityChangeListeners; this.entityChangeListenersV2 = entityChangeListenersV2; this.instanceConverter = instanceConverter; + this.fullTextMapperV2 = fullTextMapperV2; } public void onEntitiesMutated(EntityMutationResponse entityMutationResponse, boolean isImport) throws AtlasBaseException { http://git-wip-us.apache.org/repos/asf/atlas/blob/75415862/repository/src/main/java/org/apache/atlas/repository/store/graph/v1/AtlasGraphUtilsV1.java ---------------------------------------------------------------------- diff --git a/repository/src/main/java/org/apache/atlas/repository/store/graph/v1/AtlasGraphUtilsV1.java b/repository/src/main/java/org/apache/atlas/repository/store/graph/v1/AtlasGraphUtilsV1.java index 13ee2a6..bd0dc0b 100644 --- a/repository/src/main/java/org/apache/atlas/repository/store/graph/v1/AtlasGraphUtilsV1.java +++ b/repository/src/main/java/org/apache/atlas/repository/store/graph/v1/AtlasGraphUtilsV1.java @@ -20,6 +20,7 @@ package org.apache.atlas.repository.store.graph.v1; import org.apache.atlas.ApplicationProperties; import org.apache.atlas.AtlasErrorCode; +import org.apache.atlas.SortOrder; import org.apache.atlas.discovery.SearchProcessor; import org.apache.atlas.exception.AtlasBaseException; import org.apache.atlas.model.TypeCategory; @@ -54,6 +55,7 @@ import java.util.Set; import static org.apache.atlas.repository.Constants.INDEX_SEARCH_VERTEX_PREFIX_DEFAULT; import static org.apache.atlas.repository.Constants.INDEX_SEARCH_VERTEX_PREFIX_PROPERTY; +import static org.apache.atlas.repository.graphdb.AtlasGraphQuery.SortOrder.*; /** * Utility methods for Graph. @@ -338,20 +340,30 @@ public class AtlasGraphUtilsV1 { return vertex; } - public static List<String> findEntityGUIDsByType(String typename) { + public static List<String> findEntityGUIDsByType(String typename, SortOrder sortOrder) { AtlasGraphQuery query = AtlasGraphProvider.getGraphInstance().query() .has(Constants.ENTITY_TYPE_PROPERTY_KEY, typename); + if (sortOrder != null) { + AtlasGraphQuery.SortOrder qrySortOrder = sortOrder == SortOrder.ASCENDING ? ASC : DESC; + query.orderBy(Constants.QUALIFIED_NAME, qrySortOrder); + } + Iterator<AtlasVertex> results = query.vertices().iterator(); + ArrayList<String> ret = new ArrayList<>(); + if (!results.hasNext()) { return Collections.emptyList(); } - ArrayList<String> entityList = new ArrayList<>(); while (results.hasNext()) { - entityList.add(getIdFromVertex(results.next())); + ret.add(getIdFromVertex(results.next())); } - return entityList; + return ret; + } + + public static List<String> findEntityGUIDsByType(String typename) { + return findEntityGUIDsByType(typename, null); } public static boolean relationshipTypeHasInstanceEdges(String typeName) throws AtlasBaseException { http://git-wip-us.apache.org/repos/asf/atlas/blob/75415862/repository/src/main/java/org/apache/atlas/repository/store/graph/v1/EntityGraphRetriever.java ---------------------------------------------------------------------- diff --git a/repository/src/main/java/org/apache/atlas/repository/store/graph/v1/EntityGraphRetriever.java b/repository/src/main/java/org/apache/atlas/repository/store/graph/v1/EntityGraphRetriever.java index 31f6d82..1d945f7 100644 --- a/repository/src/main/java/org/apache/atlas/repository/store/graph/v1/EntityGraphRetriever.java +++ b/repository/src/main/java/org/apache/atlas/repository/store/graph/v1/EntityGraphRetriever.java @@ -21,6 +21,7 @@ import com.fasterxml.jackson.core.type.TypeReference; import org.apache.atlas.AtlasErrorCode; import org.apache.atlas.exception.AtlasBaseException; import org.apache.atlas.model.TimeBoundary; +import org.apache.atlas.model.glossary.relations.AtlasTermAssignmentHeader; import org.apache.atlas.model.instance.AtlasClassification; import org.apache.atlas.model.instance.AtlasClassification.PropagationState; import org.apache.atlas.model.instance.AtlasEntity; @@ -68,6 +69,7 @@ import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; +import java.util.stream.Collectors; import static org.apache.atlas.model.instance.AtlasClassification.PropagationState.ACTIVE; import static org.apache.atlas.model.instance.AtlasClassification.PropagationState.DELETED; @@ -220,6 +222,11 @@ public final class EntityGraphRetriever { ret.setClassifications(classifications); ret.setClassificationNames(classificationNames); } + + if (CollectionUtils.isNotEmpty(entity.getMeanings())) { + ret.setMeanings(entity.getMeanings()); + ret.setMeaningNames(entity.getMeanings().stream().map(AtlasTermAssignmentHeader::getDisplayText).collect(Collectors.toList())); + } } return ret; @@ -359,6 +366,8 @@ public final class EntityGraphRetriever { ret.setStatus(GraphHelper.getStatus(entityVertex)); ret.setClassificationNames(getAllTraitNames(entityVertex)); + // TODO: Add the term mapping here + AtlasEntityType entityType = typeRegistry.getEntityTypeByName(typeName); if (entityType != null) { http://git-wip-us.apache.org/repos/asf/atlas/blob/75415862/repository/src/main/java/org/apache/atlas/repository/userprofile/UserProfileService.java ---------------------------------------------------------------------- diff --git a/repository/src/main/java/org/apache/atlas/repository/userprofile/UserProfileService.java b/repository/src/main/java/org/apache/atlas/repository/userprofile/UserProfileService.java index a428b92..aa21df7 100644 --- a/repository/src/main/java/org/apache/atlas/repository/userprofile/UserProfileService.java +++ b/repository/src/main/java/org/apache/atlas/repository/userprofile/UserProfileService.java @@ -144,11 +144,11 @@ public class UserProfileService { public void deleteUserProfile(String userName) throws AtlasBaseException { AtlasUserProfile profile = getUserProfile(userName); - dataAccess.deleteUsingGuid(profile.getGuid()); + dataAccess.delete(profile.getGuid()); } public void deleteSavedSearch(String guid) throws AtlasBaseException { - dataAccess.deleteUsingGuid(guid); + dataAccess.delete(guid); } public void deleteSearchBySearchName(String userName, String searchName) throws AtlasBaseException { http://git-wip-us.apache.org/repos/asf/atlas/blob/75415862/repository/src/test/java/org/apache/atlas/TestModules.java ---------------------------------------------------------------------- diff --git a/repository/src/test/java/org/apache/atlas/TestModules.java b/repository/src/test/java/org/apache/atlas/TestModules.java index c901e89..605be3d 100644 --- a/repository/src/test/java/org/apache/atlas/TestModules.java +++ b/repository/src/test/java/org/apache/atlas/TestModules.java @@ -28,6 +28,7 @@ import org.apache.atlas.discovery.AtlasDiscoveryService; import org.apache.atlas.discovery.AtlasLineageService; import org.apache.atlas.discovery.EntityDiscoveryService; import org.apache.atlas.discovery.EntityLineageService; +import org.apache.atlas.glossary.GlossaryService; import org.apache.atlas.graph.GraphSandboxUtil; import org.apache.atlas.listener.EntityChangeListener; import org.apache.atlas.listener.EntityChangeListenerV2; @@ -38,13 +39,21 @@ import org.apache.atlas.repository.audit.EntityAuditRepository; import org.apache.atlas.repository.graph.GraphBackedSearchIndexer; import org.apache.atlas.repository.graphdb.AtlasGraph; import org.apache.atlas.repository.impexp.ExportService; +import org.apache.atlas.repository.ogm.profiles.AtlasSavedSearchDTO; +import org.apache.atlas.repository.ogm.profiles.AtlasUserProfileDTO; +import org.apache.atlas.repository.ogm.DTORegistry; +import org.apache.atlas.repository.ogm.DataAccess; +import org.apache.atlas.repository.ogm.DataTransferObject; +import org.apache.atlas.repository.ogm.glossary.AtlasGlossaryCategoryDTO; +import org.apache.atlas.repository.ogm.glossary.AtlasGlossaryDTO; +import org.apache.atlas.repository.ogm.glossary.AtlasGlossaryTermDTO; import org.apache.atlas.repository.store.graph.AtlasEntityStore; import org.apache.atlas.repository.store.graph.AtlasRelationshipStore; import org.apache.atlas.repository.store.graph.BulkImporter; import org.apache.atlas.repository.store.graph.v1.AtlasEntityChangeNotifier; import org.apache.atlas.repository.store.graph.v1.AtlasEntityStoreV1; -import org.apache.atlas.repository.store.graph.v1.AtlasTypeDefGraphStoreV1; import org.apache.atlas.repository.store.graph.v1.AtlasRelationshipStoreV1; +import org.apache.atlas.repository.store.graph.v1.AtlasTypeDefGraphStoreV1; import org.apache.atlas.repository.store.graph.v1.BulkImporterImpl; import org.apache.atlas.repository.store.graph.v1.DeleteHandlerV1; import org.apache.atlas.repository.store.graph.v1.EntityGraphMapper; @@ -151,6 +160,20 @@ public class TestModules { Multibinder.newSetBinder(binder(), EntityChangeListenerV2.class); entityChangeListenerV2Binder.addBinding().to(EntityAuditListenerV2.class); + // OGM related mappings + Multibinder<DataTransferObject> availableDTOs = Multibinder.newSetBinder(binder(), DataTransferObject.class); + availableDTOs.addBinding().to(AtlasUserProfileDTO.class); + availableDTOs.addBinding().to(AtlasSavedSearchDTO.class); + availableDTOs.addBinding().to(AtlasGlossaryDTO.class); + availableDTOs.addBinding().to(AtlasGlossaryTermDTO.class); + availableDTOs.addBinding().to(AtlasGlossaryCategoryDTO.class); + + bind(DTORegistry.class).asEagerSingleton(); + bind(DataAccess.class).asEagerSingleton(); + + // Glossary related bindings + bind(GlossaryService.class).asEagerSingleton(); + final GraphTransactionInterceptor graphTransactionInterceptor = new GraphTransactionInterceptor(new AtlasGraphProvider().get()); requestInjection(graphTransactionInterceptor); bindInterceptor(Matchers.any(), Matchers.annotatedWith(GraphTransaction.class), graphTransactionInterceptor); http://git-wip-us.apache.org/repos/asf/atlas/blob/75415862/repository/src/test/java/org/apache/atlas/glossary/GlossaryServiceTest.java ---------------------------------------------------------------------- diff --git a/repository/src/test/java/org/apache/atlas/glossary/GlossaryServiceTest.java b/repository/src/test/java/org/apache/atlas/glossary/GlossaryServiceTest.java new file mode 100644 index 0000000..62a0cb4 --- /dev/null +++ b/repository/src/test/java/org/apache/atlas/glossary/GlossaryServiceTest.java @@ -0,0 +1,641 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.atlas.glossary; + +import org.apache.atlas.AtlasErrorCode; +import org.apache.atlas.SortOrder; +import org.apache.atlas.TestModules; +import org.apache.atlas.exception.AtlasBaseException; +import org.apache.atlas.model.glossary.AtlasGlossary; +import org.apache.atlas.model.glossary.AtlasGlossaryCategory; +import org.apache.atlas.model.glossary.AtlasGlossaryTerm; +import org.apache.atlas.model.glossary.enums.AtlasTermRelationshipStatus; +import org.apache.atlas.model.glossary.relations.AtlasGlossaryHeader; +import org.apache.atlas.model.glossary.relations.AtlasRelatedCategoryHeader; +import org.apache.atlas.model.glossary.relations.AtlasRelatedTermHeader; +import org.apache.atlas.model.instance.AtlasEntity; +import org.apache.atlas.model.instance.AtlasEntityHeader; +import org.apache.atlas.model.instance.EntityMutationResponse; +import org.apache.atlas.repository.impexp.ZipFileResourceTestUtils; +import org.apache.atlas.repository.store.graph.AtlasEntityStore; +import org.apache.atlas.repository.store.graph.v1.AtlasEntityStream; +import org.apache.atlas.store.AtlasTypeDefStore; +import org.apache.atlas.type.AtlasTypeRegistry; +import org.apache.atlas.utils.AtlasJson; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.testng.SkipException; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.DataProvider; +import org.testng.annotations.Guice; +import org.testng.annotations.Test; + +import javax.inject.Inject; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashSet; +import java.util.List; +import java.util.Map; + +import static org.testng.Assert.*; + +@Guice(modules = TestModules.TestOnlyModule.class) +public class GlossaryServiceTest { + private static final Logger LOG = LoggerFactory.getLogger(GlossaryServiceTest.class); + + @Inject + private GlossaryService glossaryService; + @Inject + private AtlasTypeDefStore typeDefStore; + @Inject + private AtlasTypeRegistry typeRegistry; + @Inject + private AtlasEntityStore entityStore; + + private AtlasGlossary bankGlossary; + private AtlasGlossaryTerm checkingAccount, savingsAccount, fixedRateMortgage, adjustableRateMortgage; + private AtlasGlossaryCategory customerCategory, accountCategory, mortgageCategory; + + private AtlasEntityHeader testEntityHeader; + + @BeforeClass + public void setupSampleGlossary() { + try { + ZipFileResourceTestUtils.loadAllModels("0000-Area0", typeDefStore, typeRegistry); + } catch (AtlasBaseException | IOException e) { + throw new SkipException("SubjectArea model loading failed"); + } + } + + @Test(groups = "Glossary.CREATE") + public void testCreateGlossary() { + bankGlossary = new AtlasGlossary(); + bankGlossary.setQualifiedName("testBankingGlossary"); + bankGlossary.setDisplayName("Banking glossary"); + bankGlossary.setShortDescription("Short description"); + bankGlossary.setLongDescription("Long description"); + bankGlossary.setUsage("N/A"); + bankGlossary.setLanguage("en-US"); + + try { + AtlasGlossary created = glossaryService.createGlossary(bankGlossary); + bankGlossary.setGuid(created.getGuid()); + } catch (AtlasBaseException e) { + fail("Glossary creation should've succeeded", e); + } + } + + @Test(groups = "Glossary.CREATE", dependsOnMethods = {"testCreateGlossary"}) + public void testCreateGlossaryTerms() { + AtlasGlossaryHeader glossaryId = new AtlasGlossaryHeader(); + glossaryId.setGlossaryGuid(bankGlossary.getGuid()); + + checkingAccount = new AtlasGlossaryTerm(); + checkingAccount.setQualifiedName("chk_acc@testBankingGlossary"); + checkingAccount.setDisplayName("A checking account"); + checkingAccount.setShortDescription("Short description"); + checkingAccount.setLongDescription("Long description"); + checkingAccount.setAbbreviation("CHK"); + checkingAccount.setExamples(Arrays.asList("Personal", "Joint")); + checkingAccount.setUsage("N/A"); + checkingAccount.setAnchor(glossaryId); + + savingsAccount = new AtlasGlossaryTerm(); + savingsAccount.setQualifiedName("sav_acc@testBankingGlossary"); + savingsAccount.setDisplayName("A savings account"); + savingsAccount.setShortDescription("Short description"); + savingsAccount.setLongDescription("Long description"); + savingsAccount.setAbbreviation("SAV"); + savingsAccount.setExamples(Arrays.asList("Personal", "Joint")); + savingsAccount.setUsage("N/A"); + savingsAccount.setAnchor(glossaryId); + + fixedRateMortgage = new AtlasGlossaryTerm(); + fixedRateMortgage.setQualifiedName("fixed_mtg@testBankingGlossary"); + fixedRateMortgage.setDisplayName("15/30 yr mortgage"); + fixedRateMortgage.setShortDescription("Short description"); + fixedRateMortgage.setLongDescription("Long description"); + fixedRateMortgage.setAbbreviation("FMTG"); + fixedRateMortgage.setExamples(Arrays.asList("15-yr", "30-yr")); + fixedRateMortgage.setUsage("N/A"); + fixedRateMortgage.setAnchor(glossaryId); + + adjustableRateMortgage = new AtlasGlossaryTerm(); + adjustableRateMortgage.setQualifiedName("arm_mtg@testBankingGlossary"); + adjustableRateMortgage.setDisplayName("ARM loans"); + adjustableRateMortgage.setShortDescription("Short description"); + adjustableRateMortgage.setLongDescription("Long description"); + adjustableRateMortgage.setAbbreviation("ARMTG"); + adjustableRateMortgage.setExamples(Arrays.asList("5/1", "7/1", "10/1")); + adjustableRateMortgage.setUsage("N/A"); + adjustableRateMortgage.setAnchor(glossaryId); + + try { + List<AtlasGlossaryTerm> terms = glossaryService.createTerms(Arrays.asList(checkingAccount, savingsAccount, fixedRateMortgage, adjustableRateMortgage)); + checkingAccount.setGuid(terms.get(0).getGuid()); + savingsAccount.setGuid(terms.get(1).getGuid()); + fixedRateMortgage.setGuid(terms.get(2).getGuid()); + adjustableRateMortgage.setGuid(terms.get(3).getGuid()); + } catch (AtlasBaseException e) { + fail("Term creation should've succeeded", e); + } + } + + @Test(groups = "Glossary.CREATE", dependsOnMethods = {"testCreateGlossaryTerms"}) + public void testCreateGlossaryCategory() { + AtlasGlossaryHeader glossaryId = new AtlasGlossaryHeader(); + glossaryId.setGlossaryGuid(bankGlossary.getGuid()); + + customerCategory = new AtlasGlossaryCategory(); + customerCategory.setQualifiedName("customer@testBankingGlossary"); + customerCategory.setDisplayName("Customer category"); + customerCategory.setShortDescription("Short description"); + customerCategory.setLongDescription("Long description"); + customerCategory.setAnchor(glossaryId); + + accountCategory = new AtlasGlossaryCategory(); + accountCategory.setQualifiedName("acc@testBankingGlossary"); + accountCategory.setDisplayName("Account categorization"); + accountCategory.setShortDescription("Short description"); + accountCategory.setLongDescription("Long description"); + accountCategory.setAnchor(glossaryId); + + mortgageCategory = new AtlasGlossaryCategory(); + mortgageCategory.setQualifiedName("mtg@testBankingGlossary"); + mortgageCategory.setDisplayName("Mortgage categorization"); + mortgageCategory.setShortDescription("Short description"); + mortgageCategory.setLongDescription("Long description"); + mortgageCategory.setAnchor(glossaryId); + + try { + List<AtlasGlossaryCategory> categories = glossaryService.createCategories(Arrays.asList(customerCategory, accountCategory, mortgageCategory)); + customerCategory.setGuid(categories.get(0).getGuid()); + accountCategory.setGuid(categories.get(1).getGuid()); + mortgageCategory.setGuid(categories.get(2).getGuid()); + } catch (AtlasBaseException e) { + fail("Category creation should've succeeded", e); + } + } + + @DataProvider + public Object[][] getAllGlossaryDataProvider() { + return new Object[][]{ + // limit, offset, sortOrder, expected + {1, 0, SortOrder.ASCENDING, 1}, + {5, 0, SortOrder.ASCENDING, 1}, + {10, 0, SortOrder.ASCENDING, 1}, + {1, 1, SortOrder.ASCENDING, 0}, + {5, 1, SortOrder.ASCENDING, 0}, + {10, 1, SortOrder.ASCENDING, 0}, + {1, 2, SortOrder.ASCENDING, 0}, + {5, 2, SortOrder.ASCENDING, 0}, + {10, 2, SortOrder.ASCENDING, 0}, + }; + } + + @Test(dataProvider = "getAllGlossaryDataProvider") + public void testGetAllGlossaries(int limit, int offset, SortOrder sortOrder, int expected) { + try { + List<AtlasGlossary> glossaries = glossaryService.getGlossaries(limit, offset, sortOrder); + assertEquals(glossaries.size(), expected); + } catch (AtlasBaseException e) { + fail("Get glossaries should've succeeded", e); + } + } + + @Test(groups = "Glossary.GET", dependsOnGroups = "Glossary.CREATE") + public void testGetGlossary() { + try { + AtlasGlossary glossary = glossaryService.getGlossary(bankGlossary.getGuid()); + assertNotNull(glossary); + assertEquals(glossary.getGuid(), bankGlossary.getGuid()); + } catch (AtlasBaseException e) { + fail("Glossary fetch should've succeeded", e); + } + } + + @Test(groups = "Glossary.UPDATE", dependsOnGroups = "Glossary.CREATE") + public void testUpdateGlossary() { + try { + bankGlossary = glossaryService.getGlossary(bankGlossary.getGuid()); + bankGlossary.setShortDescription("Updated short description"); + bankGlossary.setLongDescription("Updated long description"); + + AtlasGlossary updatedGlossary = glossaryService.updateGlossary(bankGlossary); + assertNotNull(updatedGlossary); + assertEquals(updatedGlossary.getGuid(), bankGlossary.getGuid()); + assertEquals(updatedGlossary, bankGlossary); + bankGlossary = updatedGlossary; + } catch (AtlasBaseException e) { + fail("Glossary fetch/update should've succeeded", e); + } + } + + @Test(dependsOnGroups = {"Glossary.GET", "Glossary.UPDATE", "Glossary.GET.postUpdate"}) // Should be the last test + public void testDeleteGlossary() { + try { + glossaryService.deleteGlossary(bankGlossary.getGuid()); + try { + glossaryService.getGlossary(bankGlossary.getGuid()); + } catch (AtlasBaseException e) { + assertEquals(e.getAtlasErrorCode(), AtlasErrorCode.INSTANCE_GUID_DELETED); + } + } catch (AtlasBaseException e) { + fail("Glossary delete should've succeeded", e); + } + } + + @Test(groups = "Glossary.GET", dependsOnGroups = "Glossary.CREATE") + public void testGetGlossaryTerm() { + for (AtlasGlossaryTerm t : Arrays.asList(checkingAccount, savingsAccount, fixedRateMortgage, adjustableRateMortgage)) { + try { + AtlasGlossaryTerm glossaryTerm = glossaryService.getTerm(t.getGuid()); + assertNotNull(glossaryTerm); + assertEquals(glossaryTerm.getGuid(), t.getGuid()); + } catch (AtlasBaseException e) { + fail("Glossary term fetching should've succeeded", e); + } + } + } + + @Test(groups = "Glossary.UPDATE", dependsOnGroups = "Glossary.CREATE") + public void testUpdateGlossaryTerm() { + List<AtlasGlossaryTerm> glossaryTerms = new ArrayList<>(); + for (AtlasGlossaryTerm term : Arrays.asList(checkingAccount, savingsAccount, fixedRateMortgage, adjustableRateMortgage)) { + try { + glossaryTerms.add(glossaryService.getTerm(term.getGuid())); + } catch (AtlasBaseException e) { + fail("Fetch of GlossaryTerm should've succeeded", e); + } + } + for (AtlasGlossaryTerm t : glossaryTerms) { + try { + t.setShortDescription("Updated short description"); + t.setLongDescription("Updated long description"); + + AtlasGlossaryTerm updatedTerm = glossaryService.updateTerm(t); + assertNotNull(updatedTerm); + assertEquals(updatedTerm.getGuid(), t.getGuid()); + } catch (AtlasBaseException e) { + fail("Glossary term fetch/update should've succeeded", e); + } + } + } + + @Test(groups = "Glossary.GET", dependsOnGroups = "Glossary.CREATE") + public void testGetGlossaryCategory() { + for (AtlasGlossaryCategory c : Arrays.asList(customerCategory, accountCategory, mortgageCategory)) { + try { + AtlasGlossaryCategory glossaryCategory = glossaryService.getCategory(c.getGuid()); + assertNotNull(glossaryCategory); + assertEquals(glossaryCategory.getGuid(), c.getGuid()); + } catch (AtlasBaseException e) { + fail("Glossary category fetching should've succeeded", e); + } + } + } + + @Test(groups = "Glossary.UPDATE", dependsOnGroups = "Glossary.CREATE") + public void testUpdateGlossaryCategory() { + List<AtlasGlossaryCategory> glossaryCategories = new ArrayList<>(); + for (AtlasGlossaryCategory glossaryCategory : Arrays.asList(customerCategory, accountCategory, mortgageCategory)) { + try { + glossaryCategories.add(glossaryService.getCategory(glossaryCategory.getGuid())); + } catch (AtlasBaseException e) { + fail("Category fetch should've succeeded", e); + } + } + + for (AtlasGlossaryCategory c : glossaryCategories) { + try { + AtlasGlossaryCategory glossaryCategory = glossaryService.getCategory(c.getGuid()); + glossaryCategory.setShortDescription("Updated short description"); + glossaryCategory.setLongDescription("Updated long description"); + + AtlasGlossaryCategory updatedCategory = glossaryService.updateCategory(glossaryCategory); + assertNotNull(updatedCategory); + assertEquals(updatedCategory.getGuid(), c.getGuid()); + } catch (AtlasBaseException e) { + fail("Glossary category fetching should've succeeded", e); + } + } + } + + @Test(groups = "Glossary.UPDATE", dependsOnGroups = "Glossary.CREATE") + public void testAddTermsToCategory() { + assertNotNull(accountCategory); + try { + accountCategory = glossaryService.getCategory(accountCategory.getGuid()); + } catch (AtlasBaseException e) { + fail("Fetch of accountCategory should've succeeded", e); + } + List<AtlasGlossaryTerm> terms = new ArrayList<>(); + for (AtlasGlossaryTerm term : Arrays.asList(checkingAccount, savingsAccount)) { + try { + terms.add(glossaryService.getTerm(term.getGuid())); + } catch (AtlasBaseException e) { + fail("Term fetching should've succeeded", e); + } + } + for (AtlasGlossaryTerm termEntry : terms) { + AtlasRelatedTermHeader relatedTermId = new AtlasRelatedTermHeader(); + relatedTermId.setTermGuid(termEntry.getGuid()); + relatedTermId.setStatus(AtlasTermRelationshipStatus.ACTIVE); + relatedTermId.setSteward("UT"); + relatedTermId.setSource("UT"); + relatedTermId.setExpression("N/A"); + relatedTermId.setDescription("Categorization under account category"); + accountCategory.addTerm(relatedTermId); + } + try { + AtlasGlossaryCategory updated = glossaryService.updateCategory(accountCategory); + assertNotNull(updated.getTerms()); + assertEquals(updated.getTerms().size(), 2); + accountCategory = updated; + } catch (AtlasBaseException e) { + fail("Glossary category update should've succeeded", e); + } + + assertNotNull(accountCategory); + try { + accountCategory = glossaryService.getCategory(accountCategory.getGuid()); + } catch (AtlasBaseException e) { + fail("Fetch of accountCategory should've succeeded", e); + } + + terms.clear(); + for (AtlasGlossaryTerm term : Arrays.asList(fixedRateMortgage, adjustableRateMortgage)) { + try { + terms.add(glossaryService.getTerm(term.getGuid())); + } catch (AtlasBaseException e) { + fail("Term fetching should've succeeded", e); + } + } + + for (AtlasGlossaryTerm termEntry : terms) { + AtlasRelatedTermHeader relatedTermId = new AtlasRelatedTermHeader(); + relatedTermId.setTermGuid(termEntry.getGuid()); + relatedTermId.setStatus(AtlasTermRelationshipStatus.ACTIVE); + relatedTermId.setSteward("UT"); + relatedTermId.setSource("UT"); + relatedTermId.setExpression("N/A"); + relatedTermId.setDescription("Categorization under mortgage category"); + + mortgageCategory.addTerm(relatedTermId); + } + try { + AtlasGlossaryCategory updated = glossaryService.updateCategory(mortgageCategory); + assertNotNull(updated.getTerms()); + assertEquals(updated.getTerms().size(), 2); + mortgageCategory = updated; + } catch (AtlasBaseException e) { + fail("Glossary category update should've succeeded", e); + } + + } + + @Test(groups = "Glossary.UPDATE", dependsOnGroups = "Glossary.CREATE") + public void testAddGlossaryCategoryChildren() { + assertNotNull(customerCategory); + try { + customerCategory = glossaryService.getCategory(customerCategory.getGuid()); + } catch (AtlasBaseException e) { + fail("Fetch of accountCategory should've succeeded", e); + } + List<AtlasGlossaryCategory> categories = new ArrayList<>(); + for (AtlasGlossaryCategory category : Arrays.asList(accountCategory, mortgageCategory)) { + try { + categories.add(glossaryService.getCategory(category.getGuid())); + } catch (AtlasBaseException e) { + fail("Category fetch should've succeeded"); + } + } + + for (AtlasGlossaryCategory category : categories) { + AtlasRelatedCategoryHeader id = new AtlasRelatedCategoryHeader(); + id.setCategoryGuid(category.getGuid()); + id.setDescription("Sub-category of customer"); + customerCategory.addChild(id); + } + + try { + AtlasGlossaryCategory updateGlossaryCategory = glossaryService.updateCategory(customerCategory); + assertNull(updateGlossaryCategory.getParentCategory()); + assertNotNull(updateGlossaryCategory.getChildrenCategories()); + assertEquals(updateGlossaryCategory.getChildrenCategories().size(), 2); + customerCategory = updateGlossaryCategory; + + LOG.debug(AtlasJson.toJson(updateGlossaryCategory)); + } catch (AtlasBaseException e) { + fail("Sub category addition should've succeeded", e); + } + } + + @Test(groups = "Glossary.GET.postUpdate", dependsOnGroups = "Glossary.UPDATE") + public void testCategoryRelation() { + AtlasGlossaryCategory parent = customerCategory; + + Map<String, List<AtlasRelatedCategoryHeader>> relatedCategories; + try { + relatedCategories = glossaryService.getRelatedCategories(parent.getGuid(), 0, -1, SortOrder.ASCENDING); + assertNotNull(relatedCategories); + assertNotNull(relatedCategories.get("children")); + assertEquals(relatedCategories.get("children").size(), 2); + } catch (AtlasBaseException e) { + fail("Category fetch should've succeeded", e); + } + + for (AtlasGlossaryCategory childCategory : Arrays.asList(accountCategory, mortgageCategory)) { + try { + relatedCategories = glossaryService.getRelatedCategories(childCategory.getGuid(), 0, -1, SortOrder.ASCENDING); + assertNotNull(relatedCategories.get("parent")); + assertEquals(relatedCategories.get("parent").size(), 1); + } catch (AtlasBaseException e) { + fail("Category fetch should've succeeded", e); + } + } + } + + @Test(groups = "Glossary.UPDATE", dependsOnGroups = "Glossary.CREATE") + public void testTermAssignment() { + AtlasEntity assetEntity = new AtlasEntity("Asset"); + assetEntity.setAttribute("qualifiedName", "testAsset"); + assetEntity.setAttribute("name", "testAsset"); + + try { + EntityMutationResponse response = entityStore.createOrUpdate(new AtlasEntityStream(assetEntity), false); + testEntityHeader = response.getFirstEntityCreated(); + assertNotNull(testEntityHeader); + } catch (AtlasBaseException e) { + fail("Entity creation should've succeeded", e); + } + + try { + glossaryService.assignTermToEntities(fixedRateMortgage.getGuid(), Arrays.asList(testEntityHeader)); + } catch (AtlasBaseException e) { + fail("Term assignment to asset should've succeeded", e); + } + + try { + List<AtlasEntityHeader> assignedEntities = glossaryService.getAssignedEntities(fixedRateMortgage.getGuid(), 0, 1, SortOrder.ASCENDING); + assertNotNull(assignedEntities); + assertEquals(assignedEntities.size(), 1); + Object relationGuid = assignedEntities.get(0).getAttribute("relationGuid"); + assertNotNull(relationGuid); + testEntityHeader.setAttribute("relationGuid", relationGuid); + } catch (AtlasBaseException e) { + fail("Term fetch should've succeeded",e); + } + + } + + // FIXME: The term dissociation is not working as intended. + @Test(groups = "Glossary.UPDATE", dependsOnGroups = "Glossary.CREATE", dependsOnMethods = "testTermAssignment") + public void testTermDissociation() { + try { + glossaryService.removeTermFromEntities(fixedRateMortgage.getGuid(), Arrays.asList(testEntityHeader)); + AtlasGlossaryTerm term = glossaryService.getTerm(fixedRateMortgage.getGuid()); + assertNotNull(term); + assertNull(term.getAssignedEntities()); + } catch (AtlasBaseException e) { + fail("Term update should've succeeded", e); + } + } + + @Test(groups = "Glossary.UPDATE", dependsOnGroups = "Glossary.CREATE") + public void testTermRelation() { + AtlasRelatedTermHeader relatedTerm = new AtlasRelatedTermHeader(); + relatedTerm.setTermGuid(savingsAccount.getGuid()); + relatedTerm.setStatus(AtlasTermRelationshipStatus.DRAFT); + relatedTerm.setSteward("UT"); + relatedTerm.setSource("UT"); + relatedTerm.setExpression("N/A"); + relatedTerm.setDescription("Related term"); + + assertNotNull(checkingAccount); + try { + checkingAccount = glossaryService.getTerm(checkingAccount.getGuid()); + } catch (AtlasBaseException e) { + fail("Glossary term fetch should've been a success", e); + } + + checkingAccount.setSeeAlso(new HashSet<>(Arrays.asList(relatedTerm))); + + try { + AtlasGlossaryTerm updatedTerm = glossaryService.updateTerm(checkingAccount); + assertNotNull(updatedTerm.getSeeAlso()); + assertEquals(updatedTerm.getSeeAlso().size(), 1); + } catch (AtlasBaseException e) { + fail("RelatedTerm association should've succeeded", e); + } + + relatedTerm.setTermGuid(fixedRateMortgage.getGuid()); + + assertNotNull(adjustableRateMortgage); + try { + adjustableRateMortgage = glossaryService.getTerm(adjustableRateMortgage.getGuid()); + } catch (AtlasBaseException e) { + fail("Glossary term fetch should've been a success", e); + } + + adjustableRateMortgage.setSeeAlso(new HashSet<>(Arrays.asList(relatedTerm))); + + try { + AtlasGlossaryTerm updatedTerm = glossaryService.updateTerm(adjustableRateMortgage); + assertNotNull(updatedTerm.getSeeAlso()); + assertEquals(updatedTerm.getSeeAlso().size(), 1); + } catch (AtlasBaseException e) { + fail("RelatedTerm association should've succeeded", e); + } + + } + + @DataProvider + public static Object[][] getGlossaryTermsProvider() { + return new Object[][]{ + // offset, limit, expected + {0, -1, 4}, + {0, 2, 2}, + {2, 5, 2}, + }; + } + + @Test(dataProvider = "getGlossaryTermsProvider", groups = "Glossary.GET.postUpdate", dependsOnGroups = "Glossary.UPDATE") + public void testGetGlossaryTerms(int offset, int limit, int expected) { + String guid = bankGlossary.getGuid(); + SortOrder sortOrder = SortOrder.ASCENDING; + + try { + List<AtlasGlossaryTerm> glossaryTerms = glossaryService.getGlossaryTerms(guid, offset, limit, sortOrder); + assertNotNull(glossaryTerms); + assertEquals(glossaryTerms.size(), expected); + } catch (AtlasBaseException e) { + fail("Glossary term fetching should've succeeded", e); + } + } + + @DataProvider + public Object[][] getGlossaryCategoriesProvider() { + return new Object[][]{ + // offset, limit, expected + {0, -1, 3}, + {0, 2, 2}, + {2, 5, 1}, + }; + } + + @Test(dataProvider = "getGlossaryCategoriesProvider", groups = "Glossary.GET.postUpdate", dependsOnGroups = "Glossary.UPDATE") + public void testGetGlossaryCategories(int offset, int limit, int expected) { + String guid = bankGlossary.getGuid(); + SortOrder sortOrder = SortOrder.ASCENDING; + + try { + List<AtlasRelatedCategoryHeader> glossaryCategories = glossaryService.getGlossaryCategories(guid, offset, limit, sortOrder); + assertNotNull(glossaryCategories); + assertEquals(glossaryCategories.size(), expected); + } catch (AtlasBaseException e) { + fail("Glossary term fetching should've succeeded"); + } + } + + @DataProvider + public Object[][] getCategoryTermsProvider() { + return new Object[][]{ + // offset, limit, expected + {0, -1, 2}, + {0, 2, 2}, + {1, 5, 1}, + {2, 5, 0}, + }; + } + + + @Test(dataProvider = "getCategoryTermsProvider", groups = "Glossary.GET.postUpdate", dependsOnGroups = "Glossary.UPDATE") + public void testGetCategoryTerms(int offset, int limit, int expected) { + for (AtlasGlossaryCategory c : Arrays.asList(accountCategory, mortgageCategory)) { + try { + List<AtlasRelatedTermHeader> categoryTerms = glossaryService.getCategoryTerms(c.getGuid(), offset, limit, SortOrder.ASCENDING); + assertNotNull(categoryTerms); + assertEquals(categoryTerms.size(), expected); + } catch (AtlasBaseException e) { + fail("Category term retrieval should've been a success", e); + } + } + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/atlas/blob/75415862/repository/src/test/java/org/apache/atlas/glossary/PaginationHelperTest.java ---------------------------------------------------------------------- diff --git a/repository/src/test/java/org/apache/atlas/glossary/PaginationHelperTest.java b/repository/src/test/java/org/apache/atlas/glossary/PaginationHelperTest.java new file mode 100644 index 0000000..c5c88f7 --- /dev/null +++ b/repository/src/test/java/org/apache/atlas/glossary/PaginationHelperTest.java @@ -0,0 +1,64 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.atlas.glossary; + +import org.testng.annotations.DataProvider; +import org.testng.annotations.Test; + +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.IntStream; + +import static org.testng.Assert.assertEquals; + +public class PaginationHelperTest { + @DataProvider + public Object[][] paginationProvider() { + return new Object[][] { + // maxSize, offset, limit, expected + {10, 0 , 5, 5}, + {10, 0 , -1, 10}, + {10, -1 , -1, 10}, + {10, -1 , 5, 5}, + {10, -1 , 11, 10}, + {10, 5 , 11, 5}, + {10, 2 , 11, 8}, + {10, 8 , 11, 2}, + {10, 5 , 1, 1}, + {10, 9 , 1, 1}, + {10, 10 , 5, 0}, + {10, 11 , 5, 0}, + {10, 20 , -1, 0}, + {5, 6 , -1, 0}, + {0, -1 , -1, 0}, + {0, 5 , 10, 0}, + {0, 0 , 10, 0}, + {1, -1 , -1, 1}, + {1, 5 , 10, 0}, + {1, 0 , 10, 1}, + }; + } + + @Test(dataProvider = "paginationProvider") + public void testPaginationHelper(int maxSize, int offset, int limit, int expectedSize) { + List<Integer> intList = IntStream.range(0, maxSize).boxed().collect(Collectors.toCollection(() -> new ArrayList<>(maxSize))); + GlossaryService.PaginationHelper helper = new GlossaryService.PaginationHelper<>(intList, offset, limit); + assertEquals(helper.getPaginatedList().size(), expectedSize); + } +} http://git-wip-us.apache.org/repos/asf/atlas/blob/75415862/repository/src/test/java/org/apache/atlas/repository/impexp/ZipFileResourceTestUtils.java ---------------------------------------------------------------------- diff --git a/repository/src/test/java/org/apache/atlas/repository/impexp/ZipFileResourceTestUtils.java b/repository/src/test/java/org/apache/atlas/repository/impexp/ZipFileResourceTestUtils.java index d8e916d..cd5fea0 100644 --- a/repository/src/test/java/org/apache/atlas/repository/impexp/ZipFileResourceTestUtils.java +++ b/repository/src/test/java/org/apache/atlas/repository/impexp/ZipFileResourceTestUtils.java @@ -29,6 +29,7 @@ import org.apache.atlas.repository.store.bootstrap.AtlasTypeDefStoreInitializer; import org.apache.atlas.store.AtlasTypeDefStore; import org.apache.atlas.type.AtlasType; import org.apache.atlas.type.AtlasTypeRegistry; +import org.apache.atlas.utils.AtlasJson; import org.apache.atlas.utils.TestResourceFileUtils; import org.apache.commons.io.FileUtils; import org.apache.commons.lang.StringUtils; @@ -38,10 +39,12 @@ import org.slf4j.LoggerFactory; import java.io.File; import java.io.FileInputStream; import java.io.IOException; +import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.Set; import static org.testng.Assert.assertEquals; @@ -56,6 +59,36 @@ public class ZipFileResourceTestUtils { return TestResourceFileUtils.getFileInputStream(fileName); } + public static List<String> getAllModels(String dirName) throws IOException { + List<String> ret = null; + File topModelsDir = new File(System.getProperty("user.dir") + "/../addons/models"); + File[] topModelsDirContents = topModelsDir.exists() ? topModelsDir.listFiles() : null; + + assertTrue(topModelsDirContents != null, topModelsDir.getAbsolutePath() + ": unable to find/read directory"); + if(topModelsDirContents != null) { + Arrays.sort(topModelsDirContents); + for (File modelDir : topModelsDirContents) { + if (modelDir.exists() && modelDir.isDirectory() && modelDir.getAbsolutePath().contains(dirName)) { + File[] models = modelDir.listFiles(); + Arrays.sort(models); + ret = new ArrayList<>(); + for (File model : Objects.requireNonNull(models)) { + ret.add(getFileContents(modelDir, model.getName())); + } + + } + + if (ret != null && ret.size() > 0) { + break; + } + } + } else { + throw new IOException("Unable to retrieve model contents."); + } + + return ret; + } + public static String getModelJson(String fileName) throws IOException { String ret = null; File topModelsDir = new File(System.getProperty("user.dir") + "/../addons/models"); @@ -145,12 +178,19 @@ public class ZipFileResourceTestUtils { return r; } - public static void loadModelFromJson(String fileName, AtlasTypeDefStore typeDefStore, AtlasTypeRegistry typeRegistry) throws IOException, AtlasBaseException { AtlasTypesDef typesFromJson = getAtlasTypesDefFromFile(fileName); createTypesAsNeeded(typesFromJson, typeDefStore, typeRegistry); } + public static void loadAllModels(String dirName, AtlasTypeDefStore typeDefStore, AtlasTypeRegistry typeRegistry) throws IOException, AtlasBaseException { + List<String> allModels = getAllModels(dirName); + for (String model : allModels) { + AtlasTypesDef typesFromJson = AtlasJson.fromJson(model, AtlasTypesDef.class); + createTypesAsNeeded(typesFromJson, typeDefStore, typeRegistry); + } + } + public static void loadModelFromResourcesJson(String fileName, AtlasTypeDefStore typeDefStore, AtlasTypeRegistry typeRegistry) throws IOException, AtlasBaseException { AtlasTypesDef typesFromJson = getAtlasTypesDefFromResourceFile(fileName); createTypesAsNeeded(typesFromJson, typeDefStore, typeRegistry); http://git-wip-us.apache.org/repos/asf/atlas/blob/75415862/repository/src/test/java/org/apache/atlas/repository/userprofile/UserProfileServiceTest.java ---------------------------------------------------------------------- diff --git a/repository/src/test/java/org/apache/atlas/repository/userprofile/UserProfileServiceTest.java b/repository/src/test/java/org/apache/atlas/repository/userprofile/UserProfileServiceTest.java index 8e19f79..23da3f9 100644 --- a/repository/src/test/java/org/apache/atlas/repository/userprofile/UserProfileServiceTest.java +++ b/repository/src/test/java/org/apache/atlas/repository/userprofile/UserProfileServiceTest.java @@ -24,6 +24,7 @@ import org.apache.atlas.model.SearchFilter; import org.apache.atlas.model.discovery.SearchParameters; import org.apache.atlas.model.profile.AtlasUserProfile; import org.apache.atlas.model.profile.AtlasUserSavedSearch; +import org.apache.atlas.model.typedef.AtlasEntityDef; import org.apache.atlas.model.typedef.AtlasTypesDef; import org.apache.atlas.repository.graph.AtlasGraphProvider; import org.apache.atlas.repository.util.FilterUtil; @@ -39,11 +40,15 @@ import javax.inject.Inject; import java.io.IOException; import java.util.ArrayList; import java.util.List; +import java.util.Optional; import static org.apache.atlas.graph.GraphSandboxUtil.useLocalSolr; import static org.apache.atlas.model.profile.AtlasUserSavedSearch.SavedSearchType.BASIC; import static org.apache.atlas.repository.impexp.ZipFileResourceTestUtils.loadModelFromJson; -import static org.testng.Assert.*; +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertFalse; +import static org.testng.Assert.assertNotNull; +import static org.testng.Assert.assertTrue; @Guice(modules = TestModules.TestOnlyModule.class) public class UserProfileServiceTest { @@ -80,7 +85,8 @@ public class UserProfileServiceTest { filteredTypeDefs = typeDefStore.searchTypesDef(searchFilter); assertNotNull(filteredTypeDefs); - assertEquals(filteredTypeDefs.getEntityDefs().size(), maxTypeDefs - 3); + Optional<AtlasEntityDef> anyInternal = filteredTypeDefs.getEntityDefs().stream().filter(e -> e.getSuperTypes().contains("__internal")).findAny(); + assertFalse(anyInternal.isPresent()); } @Test
