Commit 698a5652 by Shwetha GS

ATLAS-1171 Structured, high-level public APIs (mneethiraj via shwethags)

parent a52112d8
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ 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
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ 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.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>apache-atlas</artifactId>
<groupId>org.apache.atlas</groupId>
<version>0.8-incubating-SNAPSHOT</version>
</parent>
<artifactId>atlas-intg</artifactId>
<description>Apache Atlas Integration</description>
<name>Apache Atlas Integration</name>
<packaging>jar</packaging>
<properties>
<checkstyle.failOnViolation>true</checkstyle.failOnViolation>
</properties>
<dependencies>
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-jaxrs</artifactId>
<version>${codehaus.jackson.version}</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
/**
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.api;
import java.util.List;
import java.util.Map;
import org.apache.atlas.exception.AtlasBaseException;
import org.apache.atlas.model.instance.AtlasClassification;
import org.apache.atlas.model.instance.AtlasEntity;
import org.apache.atlas.model.instance.AtlasObjectId;
/**
* API to work with CRUD of Atlas entities.
*/
public interface AtlasApiEntities {
AtlasEntity createEntity(AtlasEntity entity) throws AtlasBaseException;
AtlasEntity getEntity(AtlasObjectId objId) throws AtlasBaseException;
AtlasEntity updateEntity(AtlasObjectId objId, AtlasEntity entity) throws AtlasBaseException;
AtlasEntity updateEntityAttributes(AtlasObjectId objId, Map<String, Object> attributes) throws AtlasBaseException;
void deleteEntity(AtlasObjectId objId) throws AtlasBaseException;
PList<AtlasEntity> searchEntities(SearchFilter filter) throws AtlasBaseException;
void addEntityClassification(AtlasObjectId entityId, AtlasClassification classification) throws AtlasBaseException;
void removeEntityClassification(AtlasObjectId entityId, String classificationName) throws AtlasBaseException;
List<AtlasClassification> getEntityClassifications(AtlasObjectId entityId) throws AtlasBaseException;
List<String> getEntityClassificationNames(AtlasObjectId entityId) throws AtlasBaseException;
}
/**
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.api;
import org.apache.atlas.exception.AtlasBaseException;
import org.apache.atlas.model.typedef.AtlasClassificationDef;
import org.apache.atlas.model.typedef.AtlasEntityDef;
import org.apache.atlas.model.typedef.AtlasEnumDef;
import org.apache.atlas.model.typedef.AtlasStructDef;
/**
* API to work with CRUD of Atlas types - enum/struct/classification/entity.
*/
public interface AtlasApiTypes {
AtlasEnumDef createEnumDef(AtlasEnumDef enumDef) throws AtlasBaseException;
AtlasEnumDef getEnumDefByName(String name) throws AtlasBaseException;
AtlasEnumDef getEnumDefByGuid(String guid) throws AtlasBaseException;
AtlasEnumDef updateEnumDefByName(String name, AtlasEnumDef enumDef) throws AtlasBaseException;
AtlasEnumDef updateEnumDefByGuid(String guid, AtlasEnumDef enumDef) throws AtlasBaseException;
void deleteEnumDefByName(String name) throws AtlasBaseException;
void deleteEnumDefByGuid(String guid) throws AtlasBaseException;
PList<AtlasEnumDef> searchEnumDefs(SearchFilter filter) throws AtlasBaseException;
AtlasStructDef createStructDef(AtlasStructDef structDef) throws AtlasBaseException;
AtlasStructDef getStructDefByName(String name) throws AtlasBaseException;
AtlasStructDef getStructDefByGuid(String guid) throws AtlasBaseException;
AtlasStructDef updateStructDefByName(String name, AtlasStructDef structDef) throws AtlasBaseException;
AtlasStructDef updateStructDefByGuid(String guid, AtlasStructDef structDef) throws AtlasBaseException;
void deleteStructDefByName(String name) throws AtlasBaseException;
void deleteStructDefByGuid(String guid) throws AtlasBaseException;
PList<AtlasStructDef> searchStructDefs(SearchFilter filter) throws AtlasBaseException;
AtlasClassificationDef createClassificationDef(AtlasClassificationDef classificationDef) throws AtlasBaseException;
AtlasClassificationDef getClassificationDefByName(String name) throws AtlasBaseException;
AtlasClassificationDef getClassificationDefByGuid(String guid) throws AtlasBaseException;
AtlasClassificationDef updateClassificationDefByName(String name, AtlasClassificationDef classificationDef)
throws AtlasBaseException;
AtlasClassificationDef updateClassificationDefByGuid(String guid, AtlasClassificationDef classificationDef)
throws AtlasBaseException;
void deleteClassificationDefByName(String name) throws AtlasBaseException;
void deleteClassificationDefByGuid(String guid) throws AtlasBaseException;
PList<AtlasClassificationDef> searchClassificationDefs(SearchFilter filter) throws AtlasBaseException;
AtlasEntityDef createEntityDef(AtlasEntityDef entityDef) throws AtlasBaseException;
AtlasEntityDef getEntityDefByName(String name) throws AtlasBaseException;
AtlasEntityDef getEntityDefByIdByGuid(String guid) throws AtlasBaseException;
AtlasEntityDef updateEntityDefByName(String name, AtlasEntityDef entityDef) throws AtlasBaseException;
AtlasEntityDef updateEntityDefByGuid(String guid, AtlasEntityDef entityDef) throws AtlasBaseException;
void deleteEntityDef(String name) throws AtlasBaseException;
void deleteEntityDefByGuid(String guid) throws AtlasBaseException;
PList<AtlasEntityDef> searchEntityDefs(SearchFilter filter) throws AtlasBaseException;
}
/**
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.api;
import java.util.List;
import org.codehaus.jackson.annotate.JsonAutoDetect;
import static org.codehaus.jackson.annotate.JsonAutoDetect.Visibility.PUBLIC_ONLY;
import static org.codehaus.jackson.annotate.JsonAutoDetect.Visibility.NONE;
import org.codehaus.jackson.annotate.JsonIgnoreProperties;
import org.codehaus.jackson.map.annotate.JsonSerialize;
import org.apache.atlas.api.SearchFilter.SortType;
/**
* Paged-list, for returning search results.
*/
@JsonAutoDetect(getterVisibility=PUBLIC_ONLY, setterVisibility=PUBLIC_ONLY, fieldVisibility=NONE)
@JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown=true)
public class PList<T> implements java.io.Serializable {
private static final long serialVersionUID = 1L;
private List<T> list = null;
private long startIndex = 0;
private int pageSize = 0;
private long totalCount = 0;
private String sortBy = null;
private SortType sortType = null;
public PList() {
}
public PList(List<T> list, long startIndex, int pageSize, long totalCount, SortType sortType, String sortBy) {
setList(list);
setStartIndex(startIndex);
setPageSize(pageSize);
setTotalCount(totalCount);
setSortType(sortType);
setSortBy(sortBy);
}
public List<T> getList() {
return list;
}
public void setList(List<T> list) {
this.list = list;
}
public long getStartIndex() {
return startIndex;
}
public void setStartIndex(long startIndex) {
this.startIndex = startIndex;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public long getTotalCount() {
return totalCount;
}
public void setTotalCount(long totalCount) {
this.totalCount = totalCount;
}
public SortType getSortType() {
return sortType;
}
public void setSortType(SortType sortType) {
this.sortType = sortType;
}
public String getSortBy() {
return sortBy;
}
public void setSortBy(String sortBy) {
this.sortBy = sortBy;
}
public StringBuilder toString(StringBuilder sb) {
if (sb == null) {
sb = new StringBuilder();
}
sb.append("PList<T>{");
sb.append("list='").append((list == null ? "null" : ("size:" + list.size()))).append('\'');
sb.append(", startIndex=").append(startIndex);
sb.append(", pageSize=").append(pageSize);
sb.append(", totalCount=").append(totalCount);
sb.append(", sortType=").append(sortType);
sb.append(", version=").append(sortBy);
sb.append('}');
return sb;
}
@Override
public String toString() {
return toString(new StringBuilder()).toString();
}
}
/**
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.api;
import java.util.HashMap;
import java.util.Map;
import org.codehaus.jackson.annotate.JsonAutoDetect;
import static org.codehaus.jackson.annotate.JsonAutoDetect.Visibility.PUBLIC_ONLY;
import static org.codehaus.jackson.annotate.JsonAutoDetect.Visibility.NONE;
import org.codehaus.jackson.annotate.JsonIgnoreProperties;
import org.codehaus.jackson.map.annotate.JsonSerialize;
/**
* Generic filter, to specify search criteria using name/value pairs.
*/
@JsonAutoDetect(getterVisibility=PUBLIC_ONLY, setterVisibility=PUBLIC_ONLY, fieldVisibility=NONE)
@JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown=true)
public class SearchFilter {
/**
* to specify whether the result should be sorted? If yes, whether asc or desc.
*/
public enum SortType { NONE, ASC, DESC };
private Map<String, String> params = null;
private long startIndex = 0;
private long maxRows = Long.MAX_VALUE;
private boolean getCount = true;
private String sortBy = null;
private SortType sortType = null;
public SearchFilter() {
setParams(null);
}
public SearchFilter(Map<String, String> params) {
setParams(params);
}
public Map<String, String> getParams() {
return params;
}
public void setParams(Map<String, String> params) {
this.params = params;
}
public String getParam(String name) {
String ret = null;
if (name != null && params != null) {
ret = params.get(name);
}
return ret;
}
public void setParam(String name, String value) {
if (name != null) {
if (params == null) {
params = new HashMap<String, String>();
}
params.put(name, value);
}
}
public long getStartIndex() {
return startIndex;
}
public void setStartIndex(long startIndex) {
this.startIndex = startIndex;
}
public long getMaxRows() {
return maxRows;
}
public void setMaxRows(long maxRows) {
this.maxRows = maxRows;
}
public boolean isGetCount() {
return getCount;
}
public void setGetCount(boolean getCount) {
this.getCount = getCount;
}
public String getSortBy() {
return sortBy;
}
public void setSortBy(String sortBy) {
this.sortBy = sortBy;
}
public SortType getSortType() {
return sortType;
}
public void setSortType(SortType sortType) {
this.sortType = sortType;
}
}
/**
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.exception;
/**
* Base Exception class for Atlas API.
*/
public class AtlasBaseException extends Exception {
public AtlasBaseException() {
}
public AtlasBaseException(String message) {
super(message);
}
public AtlasBaseException(String message, Throwable cause) {
super(message, cause);
}
public AtlasBaseException(Throwable cause) {
super(cause);
}
public AtlasBaseException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}
/**
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.model.instance;
import java.io.Serializable;
import java.util.Map;
import org.codehaus.jackson.annotate.JsonAutoDetect;
import static org.codehaus.jackson.annotate.JsonAutoDetect.Visibility.PUBLIC_ONLY;
import static org.codehaus.jackson.annotate.JsonAutoDetect.Visibility.NONE;
import org.codehaus.jackson.annotate.JsonIgnoreProperties;
import org.codehaus.jackson.map.annotate.JsonSerialize;
/**
* An instance of a classfication; it doesn't have an identity, this object exists only when associated with an entity.
*/
@JsonAutoDetect(getterVisibility=PUBLIC_ONLY, setterVisibility=PUBLIC_ONLY, fieldVisibility=NONE)
@JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown=true)
public class AtlasClassification extends AtlasStruct implements Serializable {
private static final long serialVersionUID = 1L;
public AtlasClassification() {
this(null, null);
}
public AtlasClassification(String typeName) {
this(typeName, null);
}
public AtlasClassification(String typeName, Map<String, Object> attributes) {
super(typeName, attributes);
}
public AtlasClassification(String typeName, String attrName, Object attrValue) {
super(typeName, attrName, attrValue);
}
public AtlasClassification(AtlasClassification other) {
if (other != null) {
setTypeName(other.getTypeName());
setAttributes(other.getAttributes());
}
}
}
/**
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.model.instance;
import java.io.Serializable;
import java.util.Date;
import java.util.Map;
import org.apache.atlas.model.typedef.AtlasEntityDef;
import org.codehaus.jackson.annotate.JsonAutoDetect;
import static org.codehaus.jackson.annotate.JsonAutoDetect.Visibility.PUBLIC_ONLY;
import static org.codehaus.jackson.annotate.JsonAutoDetect.Visibility.NONE;
import org.codehaus.jackson.annotate.JsonIgnoreProperties;
import org.codehaus.jackson.map.annotate.JsonSerialize;
/**
* An instance of an entity - like hive_table, hive_database.
*/
@JsonAutoDetect(getterVisibility=PUBLIC_ONLY, setterVisibility=PUBLIC_ONLY, fieldVisibility=NONE)
@JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown=true)
public class AtlasEntity extends AtlasStruct implements Serializable {
private static final long serialVersionUID = 1L;
/**
* Status of the entity - can be active or deleted. Deleted entities are not removed from Atlas store.
*/
public enum Status { STATUS_ACTIVE, STATUS_DELETED };
private String guid = null;
private Status status = Status.STATUS_ACTIVE;
private String createdBy = null;
private String updatedBy = null;
private Date createTime = null;
private Date updateTime = null;
private Long version = null;
public AtlasEntity() {
this(null, null);
}
public AtlasEntity(String typeName) {
this(typeName, null);
}
public AtlasEntity(AtlasEntityDef entityDef) {
this(entityDef != null ? entityDef.getName() : null, null);
}
public AtlasEntity(String typeName, Map<String, Object> attributes) {
super(typeName, attributes);
setGuid(null);
setStatus(null);
setCreatedBy(null);
setUpdatedBy(null);
setCreateTime(null);
setUpdateTime(null);
setVersion(null);
}
public AtlasEntity(AtlasEntity other) {
super(other);
if (other != null) {
setGuid(other.getGuid());
setStatus(other.getStatus());
setCreatedBy(other.getCreatedBy());
setUpdatedBy(other.getUpdatedBy());
setCreateTime(other.getCreateTime());
setUpdateTime(other.getUpdateTime());
setVersion(other.getVersion());
}
}
public String getGuid() {
return guid;
}
public void setGuid(String guid) {
this.guid = guid;
}
public Status getStatus() {
return status;
}
public void setStatus(Status status) {
this.status = status;
}
public String getCreatedBy() {
return createdBy;
}
public void setCreatedBy(String createdBy) {
this.createdBy = createdBy;
}
public String getUpdatedBy() {
return updatedBy;
}
public void setUpdatedBy(String updatedBy) {
this.updatedBy = updatedBy;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public Date getUpdateTime() {
return updateTime;
}
public void setUpdateTime(Date updateTime) {
this.updateTime = updateTime;
}
public Long getVersion() {
return version;
}
public void setVersion(Long version) {
this.version = version;
}
@Override
public StringBuilder toString(StringBuilder sb) {
if (sb == null) {
sb = new StringBuilder();
}
sb.append("AtlasEntity{");
sb.append("guid='").append(guid).append('\'');
sb.append(", status=").append(status);
sb.append(", createdBy='").append(createdBy).append('\'');
sb.append(", updatedBy='").append(updatedBy).append('\'');
dumpDateField(", createTime=", createTime, sb);
dumpDateField(", updateTime=", updateTime, sb);
sb.append(", version=").append(version);
sb.append(", ");
super.toString(sb);
sb.append('}');
return sb;
}
@Override
public boolean equals(Object o) {
if (this == o) { return true; }
if (o == null || getClass() != o.getClass()) { return false; }
if (!super.equals(o)) { return false; }
AtlasEntity that = (AtlasEntity) o;
if (guid != null ? !guid.equals(that.guid) : that.guid != null) { return false; }
if (status != null ? !status.equals(that.status) : that.status != null) { return false; }
if (createdBy != null ? !createdBy.equals(that.createdBy) : that.createdBy != null) { return false; }
if (updatedBy != null ? !updatedBy.equals(that.updatedBy) : that.updatedBy != null) { return false; }
if (createTime != null ? !createTime.equals(that.createTime) : that.createTime != null) { return false; }
if (updateTime != null ? !updateTime.equals(that.updateTime) : that.updateTime != null) { return false; }
if (version != null ? !version.equals(that.version) : that.version != null) { return false; }
return true;
}
@Override
public int hashCode() {
int result = super.hashCode();
result = 31 * result + guid != null ? guid.hashCode() : 0;
result = 31 * result + (status != null ? status.hashCode() : 0);
result = 31 * result + (createdBy != null ? createdBy.hashCode() : 0);
result = 31 * result + (updatedBy != null ? updatedBy.hashCode() : 0);
result = 31 * result + (createTime != null ? createTime.hashCode() : 0);
result = 31 * result + (updateTime != null ? updateTime.hashCode() : 0);
result = 31 * result + (version != null ? version.hashCode() : 0);
return result;
}
@Override
public String toString() {
return toString(new StringBuilder()).toString();
}
}
/**
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.model.instance;
import java.io.Serializable;
import java.util.Map;
import org.codehaus.jackson.annotate.JsonAutoDetect;
import static org.codehaus.jackson.annotate.JsonAutoDetect.Visibility.PUBLIC_ONLY;
import static org.codehaus.jackson.annotate.JsonAutoDetect.Visibility.NONE;
import org.codehaus.jackson.annotate.JsonIgnoreProperties;
import org.codehaus.jackson.map.annotate.JsonSerialize;
/**
* Reference to an object-instance of an Atlas type - like entity.
*/
@JsonAutoDetect(getterVisibility=PUBLIC_ONLY, setterVisibility=PUBLIC_ONLY, fieldVisibility=NONE)
@JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown=true)
public class AtlasObjectId implements Serializable {
private static final long serialVersionUID = 1L;
public static final String KEY_TYPENAME = "typeName";
public static final String KEY_GUID = "guid";
private String typeName;
private String guid;
public AtlasObjectId() {
this(null, null);
}
public AtlasObjectId(String typeName) {
this(typeName, null);
}
public AtlasObjectId(String typeName, String guid) {
setTypeName(typeName);
setGuid(guid);
}
public AtlasObjectId(AtlasObjectId other) {
if (other != null) {
setTypeName(other.getTypeName());
setGuid(other.getGuid());
}
}
public AtlasObjectId(Map objIdMap) {
if (objIdMap != null) {
Object t = objIdMap.get(KEY_TYPENAME);
Object g = objIdMap.get(KEY_GUID);
if (t != null) {
setTypeName(t.toString());
}
if (g != null) {
setGuid(g.toString());
}
}
}
public String getTypeName() {
return typeName;
}
public void setTypeName(String typeName) {
this.typeName = typeName;
}
public String getGuid() {
return guid;
}
public void setGuid(String guid) {
this.guid = guid;
}
public StringBuilder toString(StringBuilder sb) {
if (sb == null) {
sb = new StringBuilder();
}
sb.append("AtlasObjectId{");
sb.append("typeName='").append(typeName).append('\'');
sb.append(", guid='").append(guid).append('\'');
sb.append('}');
return sb;
}
@Override
public boolean equals(Object o) {
if (this == o) { return true; }
if (o == null || getClass() != o.getClass()) { return false; }
AtlasObjectId that = (AtlasObjectId) o;
if (typeName != null ? !typeName.equals(that.typeName) : that.typeName != null) { return false; }
if (guid != null ? !guid.equals(that.guid) : that.guid != null) { return false; }
return true;
}
@Override
public int hashCode() {
int result = typeName != null ? typeName.hashCode() : 0;
result = 31 * result + (guid != null ? guid.hashCode() : 0);
return result;
}
@Override
public String toString() {
return toString(new StringBuilder()).toString();
}
}
/**
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.model.instance;
import java.io.Serializable;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.codehaus.jackson.annotate.JsonAutoDetect;
import static org.codehaus.jackson.annotate.JsonAutoDetect.Visibility.PUBLIC_ONLY;
import static org.codehaus.jackson.annotate.JsonAutoDetect.Visibility.NONE;
import org.codehaus.jackson.annotate.JsonIgnoreProperties;
import org.codehaus.jackson.map.annotate.JsonSerialize;
/**
* Captures details of struct contents. Not instantiated directly, used only via AtlasEntity, AtlasClassification.
*/
@JsonAutoDetect(getterVisibility=PUBLIC_ONLY, setterVisibility=PUBLIC_ONLY, fieldVisibility=NONE)
@JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown=true)
public class AtlasStruct implements Serializable {
private static final long serialVersionUID = 1L;
public static final String SERIALIZED_DATE_FORMAT_STR = "yyyyMMdd-HH:mm:ss.SSS-Z";
public static final DateFormat DATE_FORMATTER = new SimpleDateFormat(SERIALIZED_DATE_FORMAT_STR);
private String typeName;
private Map<String, Object> attributes;
public AtlasStruct() {
this(null, null);
}
public AtlasStruct(String typeName) {
this(typeName, null);
}
public AtlasStruct(String typeName, Map<String, Object> attributes) {
setTypeName(typeName);
setAttributes(attributes);
}
public AtlasStruct(String typeName, String attrName, Object attrValue) {
setTypeName(typeName);
setAttribute(attrName, attrValue);
}
public AtlasStruct(AtlasStruct other) {
if (other != null) {
setTypeName(other.getTypeName());
setAttributes(other.getAttributes());
}
}
public String getTypeName() {
return typeName;
}
public void setTypeName(String typeName) {
this.typeName = typeName;
}
public Map<String, Object> getAttributes() {
return attributes;
}
public void setAttributes(Map<String, Object> attributes) {
this.attributes = attributes;
}
public boolean hasAttribute(String name) {
Map<String, Object> a = this.attributes;
return a != null ? a.containsKey(name) : null;
}
public Object getAttribute(String name) {
Map<String, Object> a = this.attributes;
return a != null ? a.get(name) : null;
}
public void setAttribute(String name, Object value) {
Map<String, Object> a = this.attributes;
if (a != null) {
a.put(name, value);
} else {
a = new HashMap<String, Object>();
a.put(name, value);
this.attributes = a;
}
}
public StringBuilder toString(StringBuilder sb) {
if (sb == null) {
sb = new StringBuilder();
}
sb.append("AtlasStruct{");
sb.append("typeName='").append(typeName).append('\'');
sb.append(", attributes=[");
dumpObjects(attributes, sb);
sb.append("]");
sb.append('}');
return sb;
}
@Override
public boolean equals(Object o) {
if (this == o) { return true; }
if (o == null || getClass() != o.getClass()) { return false; }
AtlasStruct that = (AtlasStruct) o;
if (typeName != null ? !typeName.equals(that.typeName) : that.typeName != null) { return false; }
if (attributes != null ? !attributes.equals(that.attributes) : that.attributes != null) { return false; }
return true;
}
@Override
public int hashCode() {
int result = (typeName != null ? typeName.hashCode() : 0);
result = 31 * result + (attributes != null ? attributes.hashCode() : 0);
return result;
}
@Override
public String toString() {
return toString(new StringBuilder()).toString();
}
public static StringBuilder dumpModelObjects(Collection<? extends AtlasStruct> objList, StringBuilder sb) {
if (sb == null) {
sb = new StringBuilder();
}
if (CollectionUtils.isNotEmpty(objList)) {
int i = 0;
for (AtlasStruct obj : objList) {
if (i > 0) {
sb.append(", ");
}
obj.toString(sb);
i++;
}
}
return sb;
}
public static StringBuilder dumpObjects(Collection<? extends Object> objects, StringBuilder sb) {
if (sb == null) {
sb = new StringBuilder();
}
if (CollectionUtils.isNotEmpty(objects)) {
int i = 0;
for (Object obj : objects) {
if (i > 0) {
sb.append(", ");
}
sb.append(obj);
i++;
}
}
return sb;
}
public static StringBuilder dumpObjects(Map<? extends Object, ? extends Object> objects, StringBuilder sb) {
if (sb == null) {
sb = new StringBuilder();
}
if (MapUtils.isNotEmpty(objects)) {
int i = 0;
for (Map.Entry<? extends Object, ? extends Object> e : objects.entrySet()) {
if (i > 0) {
sb.append(", ");
}
sb.append(e.getKey()).append(":").append(e.getValue());
i++;
}
}
return sb;
}
public static StringBuilder dumpDateField(String prefix, Date value, StringBuilder sb) {
sb.append(prefix);
if (value == null) {
sb.append(value);
} else {
sb.append(DATE_FORMATTER.format(value));
}
return sb;
}
}
/**
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.model.typedef;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.apache.commons.collections.CollectionUtils;
import org.codehaus.jackson.annotate.JsonAutoDetect;
import static org.codehaus.jackson.annotate.JsonAutoDetect.Visibility.PUBLIC_ONLY;
import static org.codehaus.jackson.annotate.JsonAutoDetect.Visibility.NONE;
import org.codehaus.jackson.annotate.JsonIgnoreProperties;
import org.codehaus.jackson.map.annotate.JsonSerialize;
/**
* class that captures details of a classification-type.
*/
@JsonAutoDetect(getterVisibility=PUBLIC_ONLY, setterVisibility=PUBLIC_ONLY, fieldVisibility=NONE)
@JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown=true)
public class AtlasClassificationDef extends AtlasStructDef implements java.io.Serializable {
private static final long serialVersionUID = 1L;
private Set<String> superTypes;
public AtlasClassificationDef() {
super();
setSuperTypes(null);
}
public AtlasClassificationDef(String name) {
this(name, null, null, null, null);
}
public AtlasClassificationDef(String name, String description) {
this(name, description, null, null, null);
}
public AtlasClassificationDef(String name, String description, String typeVersion) {
this(name, description, typeVersion, null, null);
}
public AtlasClassificationDef(String name, String description, String typeVersion,
List<AtlasAttributeDef> attributeDefs) {
this(name, description, typeVersion, attributeDefs, null);
}
public AtlasClassificationDef(String name, String description, String typeVersion,
List<AtlasAttributeDef> attributeDefs, Set<String> superTypes) {
super(name, description, typeVersion, attributeDefs);
setSuperTypes(superTypes);
}
public AtlasClassificationDef(AtlasClassificationDef other) {
super(other);
setSuperTypes(other != null ? other.getSuperTypes() : null);
}
public Set<String> getSuperTypes() {
return superTypes;
}
public void setSuperTypes(Set<String> superTypes) {
if (superTypes != null && this.superTypes == superTypes) {
return;
}
if (CollectionUtils.isEmpty(superTypes)) {
this.superTypes = Collections.emptySet();
} else {
this.superTypes = Collections.unmodifiableSet(new HashSet<String>(superTypes));
}
}
public boolean hasSuperType(String typeName) {
return hasSuperType(superTypes, typeName);
}
public void addSuperType(String typeName) {
Set<String> s = this.superTypes;
if (!hasSuperType(s, typeName)) {
s = new HashSet<String>(s);
s.add(typeName);
this.superTypes = Collections.unmodifiableSet(s);
}
}
public void removeSuperType(String typeName) {
Set<String> s = this.superTypes;
if (hasSuperType(s, typeName)) {
s = new HashSet<String>(s);
s.remove(typeName);
this.superTypes = Collections.unmodifiableSet(s);
}
}
private static boolean hasSuperType(Set<String> superTypes, String typeName) {
return superTypes != null && typeName != null && superTypes.contains(typeName);
}
@Override
public StringBuilder toString(StringBuilder sb) {
if (sb == null) {
sb = new StringBuilder();
}
sb.append("AtlasClassificationDef{");
super.toString(sb);
sb.append(", superTypes=[");
dumpObjects(superTypes, sb);
sb.append("]");
sb.append('}');
return sb;
}
@Override
public boolean equals(Object o) {
if (this == o) { return true; }
if (o == null || getClass() != o.getClass()) { return false; }
if (!super.equals(o)) { return false; }
AtlasClassificationDef that = (AtlasClassificationDef) o;
if (superTypes != null ? !superTypes.equals(that.superTypes) : that.superTypes != null) { return false; }
return true;
}
@Override
public int hashCode() {
int result = super.hashCode();
result = 31 * result + (superTypes != null ? superTypes.hashCode() : 0);
return result;
}
@Override
public String toString() {
return toString(new StringBuilder()).toString();
}
}
/**
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.model.typedef;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.apache.commons.collections.CollectionUtils;
import org.codehaus.jackson.annotate.JsonAutoDetect;
import static org.codehaus.jackson.annotate.JsonAutoDetect.Visibility.PUBLIC_ONLY;
import static org.codehaus.jackson.annotate.JsonAutoDetect.Visibility.NONE;
import org.codehaus.jackson.annotate.JsonIgnoreProperties;
import org.codehaus.jackson.map.annotate.JsonSerialize;
/**
* class that captures details of a entity-type.
*/
@JsonAutoDetect(getterVisibility=PUBLIC_ONLY, setterVisibility=PUBLIC_ONLY, fieldVisibility=NONE)
@JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown=true)
public class AtlasEntityDef extends AtlasStructDef implements java.io.Serializable {
private static final long serialVersionUID = 1L;
private Set<String> superTypes;
public AtlasEntityDef() {
this(null, null, null, null, null);
}
public AtlasEntityDef(String name) {
this(name, null, null, null, null);
}
public AtlasEntityDef(String name, String description) {
this(name, description, null, null, null);
}
public AtlasEntityDef(String name, String description, String typeVersion) {
this(name, description, typeVersion, null, null);
}
public AtlasEntityDef(String name, String description, String typeVersion, List<AtlasAttributeDef> attributeDefs) {
this(name, description, typeVersion, attributeDefs, null);
}
public AtlasEntityDef(String name, String description, String typeVersion, List<AtlasAttributeDef> attributeDefs,
Set<String> superTypes) {
super(name, description, typeVersion, attributeDefs);
setSuperTypes(superTypes);
}
public AtlasEntityDef(AtlasEntityDef other) {
super(other);
setSuperTypes(other != null ? other.getSuperTypes() : null);
}
public Set<String> getSuperTypes() {
return superTypes;
}
public void setSuperTypes(Set<String> superTypes) {
if (superTypes != null && this.superTypes == superTypes) {
return;
}
if (CollectionUtils.isEmpty(superTypes)) {
this.superTypes = Collections.emptySet();
} else {
this.superTypes = Collections.unmodifiableSet(new HashSet<String>(superTypes));
}
}
public boolean hasSuperType(String typeName) {
return hasSuperType(superTypes, typeName);
}
public void addSuperType(String typeName) {
Set<String> s = this.superTypes;
if (!hasSuperType(s, typeName)) {
s = new HashSet<String>(s);
s.add(typeName);
this.superTypes = Collections.unmodifiableSet(s);
}
}
public void removeSuperType(String typeName) {
Set<String> s = this.superTypes;
if (hasSuperType(s, typeName)) {
s = new HashSet<String>(s);
s.remove(typeName);
this.superTypes = Collections.unmodifiableSet(s);
}
}
private static boolean hasSuperType(Set<String> superTypes, String typeName) {
return superTypes != null && typeName != null && superTypes.contains(typeName);
}
@Override
public StringBuilder toString(StringBuilder sb) {
if (sb == null) {
sb = new StringBuilder();
}
sb.append("AtlasEntityDef{");
super.toString(sb);
sb.append(", superTypes=[");
dumpObjects(superTypes, sb);
sb.append("]");
sb.append('}');
return sb;
}
@Override
public boolean equals(Object o) {
if (this == o) { return true; }
if (o == null || getClass() != o.getClass()) { return false; }
if (!super.equals(o)) { return false; }
AtlasEntityDef that = (AtlasEntityDef) o;
if (superTypes != null ? !superTypes.equals(that.superTypes) : that.superTypes != null) { return false; }
return true;
}
@Override
public int hashCode() {
int result = super.hashCode();
result = 31 * result + (superTypes != null ? superTypes.hashCode() : 0);
return result;
}
@Override
public String toString() {
return toString(new StringBuilder()).toString();
}
}
/**
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.type;
import org.apache.atlas.exception.AtlasBaseException;
import org.apache.atlas.model.typedef.AtlasBaseTypeDef;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import static org.apache.atlas.model.typedef.AtlasStructDef.AtlasAttributeDef.COUNT_NOT_SET;
/**
* class that implements behaviour of an array-type.
*/
public class AtlasArrayType extends AtlasType {
private static final Logger LOG = LoggerFactory.getLogger(AtlasArrayType.class);
private final String elementTypeName;
private final int minCount;
private final int maxCount;
private AtlasType elementType;
public AtlasArrayType(AtlasType elementType) {
this(elementType, COUNT_NOT_SET, COUNT_NOT_SET);
}
public AtlasArrayType(AtlasType elementType, int minCount, int maxCount) {
super(AtlasBaseTypeDef.getArrayTypeName(elementType.getTypeName()));
this.elementTypeName = elementType.getTypeName();
this.minCount = minCount;
this.maxCount = maxCount;
this.elementType = elementType;
}
public AtlasArrayType(String elementTypeName) {
this(elementTypeName, COUNT_NOT_SET, COUNT_NOT_SET);
}
public AtlasArrayType(String elementTypeName, int minCount, int maxCount) {
super(AtlasBaseTypeDef.getArrayTypeName(elementTypeName));
this.elementTypeName = elementTypeName;
this.minCount = minCount;
this.maxCount = maxCount;
this.elementType = null;
}
public AtlasArrayType(String elementTypeName, AtlasTypeRegistry typeRegistry) throws AtlasBaseException {
this(elementTypeName, COUNT_NOT_SET, COUNT_NOT_SET, typeRegistry);
}
public AtlasArrayType(String elementTypeName, int minCount, int maxCount, AtlasTypeRegistry typeRegistry)
throws AtlasBaseException {
super(AtlasBaseTypeDef.getArrayTypeName(elementTypeName));
this.elementTypeName = elementTypeName;
this.minCount = minCount;
this.maxCount = maxCount;
this.resolveReferences(typeRegistry);
}
@Override
public void resolveReferences(AtlasTypeRegistry typeRegistry) throws AtlasBaseException {
elementType = typeRegistry.getType(elementTypeName);
if (elementType == null) {
String msg = elementTypeName + ": unknown element-type for array";
LOG.error(msg);
throw new AtlasBaseException(msg);
}
elementType.resolveReferences(typeRegistry);
}
@Override
public Collection<? extends Object> createDefaultValue() {
Collection<Object> ret = new ArrayList<Object>();
ret.add(elementType.createDefaultValue());
if (minCount != COUNT_NOT_SET) {
for (int i = 1; i < minCount; i++) {
ret.add(elementType.createDefaultValue());
}
}
return ret;
}
@Override
public boolean isValidValue(Object obj) {
if (obj != null) {
if (obj instanceof List || obj instanceof Set) {
Collection objList = (Collection) obj;
if (!isValidElementCount(objList.size())) {
return false;
}
for (Object element : objList) {
if (!elementType.isValidValue(element)) {
return false;
}
}
} else if (obj.getClass().isArray()) {
int arrayLen = Array.getLength(obj);
if (!isValidElementCount(arrayLen)) {
return false;
}
for (int i = 0; i < arrayLen; i++) {
if (!elementType.isValidValue(Array.get(obj, i))) {
return false;
}
}
} else {
return false; // invalid type
}
}
return true;
}
@Override
public Collection<? extends Object> getNormalizedValue(Object obj) {
if (obj == null) {
return null;
}
if (obj instanceof List || obj instanceof Set) {
List<Object> ret = new ArrayList<Object>();
Collection objList = (Collection) obj;
if (!isValidElementCount(objList.size())) {
return null;
}
for (Object element : objList) {
if (element != null) {
Object normalizedValue = elementType.getNormalizedValue(element);
if (normalizedValue != null) {
ret.add(normalizedValue);
} else {
return null; // invalid element value
}
} else {
ret.add(element);
}
}
return ret;
} else if (obj.getClass().isArray()) {
List<Object> ret = new ArrayList<Object>();
int arrayLen = Array.getLength(obj);
if (!isValidElementCount(arrayLen)) {
return null;
}
for (int i = 0; i < arrayLen; i++) {
Object element = Array.get(obj, i);
if (element != null) {
Object normalizedValue = elementType.getNormalizedValue(element);
if (normalizedValue != null) {
ret.add(normalizedValue);
} else {
return null; // invalid element value
}
} else {
ret.add(element);
}
}
return ret;
}
return null;
}
@Override
public boolean validateValue(Object obj, String objName, List<String> messages) {
boolean ret = true;
if (obj != null) {
if (obj instanceof List || obj instanceof Set) {
Collection objList = (Collection) obj;
if (!isValidElementCount(objList.size())) {
ret = false;
messages.add(objName + ": incorrect number of values. found=" + objList.size()
+ "; expected: minCount=" + minCount + ", maxCount=" + maxCount);
}
int idx = 0;
for (Object element : objList) {
ret = elementType.validateValue(element, objName + "[" + idx + "]", messages) && ret;
idx++;
}
} else if (obj.getClass().isArray()) {
int arrayLen = Array.getLength(obj);
if (!isValidElementCount(arrayLen)) {
ret = false;
messages.add(objName + ": incorrect number of values. found=" + arrayLen
+ "; expected: minCount=" + minCount + ", maxCount=" + maxCount);
}
for (int i = 0; i < arrayLen; i++) {
ret = elementType.validateValue(Array.get(obj, i), objName + "[" + i + "]", messages) && ret;
}
} else {
ret = false;
messages.add(objName + "=" + obj + ": invalid value for type " + getTypeName());
}
}
return ret;
}
private boolean isValidElementCount(int count) {
if (minCount != COUNT_NOT_SET) {
if (count < minCount) {
return false;
}
}
if (maxCount != COUNT_NOT_SET) {
if (count > maxCount) {
return false;
}
}
return true;
}
}
/**
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.type;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.atlas.exception.AtlasBaseException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.atlas.model.instance.AtlasClassification;
import org.apache.atlas.model.typedef.AtlasClassificationDef;
import org.apache.commons.collections.CollectionUtils;
/**
* class that implements behaviour of a classification-type.
*/
public class AtlasClassificationType extends AtlasStructType {
private static final Logger LOG = LoggerFactory.getLogger(AtlasClassificationType.class);
private final AtlasClassificationDef classificationDef;
private List<AtlasClassificationType> superTypes = Collections.emptyList();
private Set<String> allSuperTypes = Collections.emptySet();
public AtlasClassificationType(AtlasClassificationDef classificationDef) {
super(classificationDef);
this.classificationDef = classificationDef;
}
public AtlasClassificationType(AtlasClassificationDef classificationDef, AtlasTypeRegistry typeRegistry)
throws AtlasBaseException {
super(classificationDef);
this.classificationDef = classificationDef;
resolveReferences(typeRegistry);
}
@Override
public void resolveReferences(AtlasTypeRegistry typeRegistry) throws AtlasBaseException {
super.resolveReferences(typeRegistry);
List<AtlasClassificationType> s = new ArrayList<AtlasClassificationType>();
Set<String> allS = new HashSet<String>();
for (String superTypeName : classificationDef.getSuperTypes()) {
AtlasType superType = typeRegistry.getType(superTypeName);
if (superType != null && superType instanceof AtlasClassificationType) {
AtlasClassificationType superClassificationType = (AtlasClassificationType)superType;
superClassificationType.resolveReferences(typeRegistry);
s.add(superClassificationType);
allS.add(superTypeName);
if (CollectionUtils.isNotEmpty(superClassificationType.getAllSuperTypes())) {
allS.addAll(superClassificationType.getAllSuperTypes());
}
} else {
String msg = superTypeName + ((superType == null) ? ": unknown" : ": incompatible");
msg += (" supertype in classification " + classificationDef.getName());
LOG.error(msg);
throw new AtlasBaseException(msg);
}
}
this.superTypes = Collections.unmodifiableList(s);
this.allSuperTypes = allS;
}
public Set<String> getSuperTypes() {
return classificationDef.getSuperTypes();
}
public Set<String> getAllSuperTypes() { return allSuperTypes; }
public boolean isSuperTypeOf(AtlasClassificationType classificationType) {
return classificationType != null ? classificationType.getAllSuperTypes().contains(this.getTypeName()) : false;
}
public boolean isSubTypeOf(AtlasClassificationType classificationType) {
return classificationType != null ? allSuperTypes.contains(classificationType.getTypeName()) : false;
}
@Override
public AtlasClassification createDefaultValue() {
AtlasClassification ret = new AtlasClassification(classificationDef.getName());
populateDefaultValues(ret);
return ret;
}
@Override
public boolean isValidValue(Object obj) {
if (obj != null) {
for (AtlasClassificationType superType : superTypes) {
if (!superType.isValidValue(obj)) {
return false;
}
}
return super.isValidValue(obj);
}
return true;
}
@Override
public Object getNormalizedValue(Object obj) {
Object ret = null;
if (obj != null) {
if (isValidValue(obj)) {
if (obj instanceof AtlasClassification) {
normalizeAttributeValues((AtlasClassification) obj);
ret = obj;
} else if (obj instanceof Map) {
normalizeAttributeValues((Map) obj);
ret = obj;
}
}
}
return ret;
}
@Override
public boolean validateValue(Object obj, String objName, List<String> messages) {
boolean ret = true;
if (obj != null) {
for (AtlasClassificationType superType : superTypes) {
ret = superType.validateValue(obj, objName, messages) && ret;
}
ret = super.validateValue(obj, objName, messages) && ret;
}
return ret;
}
public void normalizeAttributeValues(AtlasClassification classification) {
if (classification != null) {
for (AtlasClassificationType superType : superTypes) {
superType.normalizeAttributeValues(classification);
}
super.normalizeAttributeValues(classification);
}
}
@Override
public void normalizeAttributeValues(Map<String, Object> obj) {
if (obj != null) {
for (AtlasClassificationType superType : superTypes) {
superType.normalizeAttributeValues(obj);
}
super.normalizeAttributeValues(obj);
}
}
public void populateDefaultValues(AtlasClassification classification) {
if (classification != null) {
for (AtlasClassificationType superType : superTypes) {
superType.populateDefaultValues(classification);
}
super.populateDefaultValues(classification);
}
}
}
/**
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.type;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.atlas.exception.AtlasBaseException;
import org.apache.atlas.model.instance.AtlasEntity;
import org.apache.atlas.model.typedef.AtlasEntityDef;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* class that implements behaviour of an entity-type.
*/
public class AtlasEntityType extends AtlasStructType {
private static final Logger LOG = LoggerFactory.getLogger(AtlasEntityType.class);
private final AtlasEntityDef entityDef;
private List<AtlasEntityType> superTypes = Collections.emptyList();
private Set<String> allSuperTypes = Collections.emptySet();
public AtlasEntityType(AtlasEntityDef entityDef) {
super(entityDef);
this.entityDef = entityDef;
}
public AtlasEntityType(AtlasEntityDef entityDef, AtlasTypeRegistry typeRegistry) throws AtlasBaseException {
super(entityDef);
this.entityDef = entityDef;
resolveReferences(typeRegistry);
}
@Override
public void resolveReferences(AtlasTypeRegistry typeRegistry) throws AtlasBaseException {
super.resolveReferences(typeRegistry);
List<AtlasEntityType> s = new ArrayList<AtlasEntityType>();
Set<String> allS = new HashSet<String>();
for (String superTypeName : entityDef.getSuperTypes()) {
AtlasType superType = typeRegistry.getType(superTypeName);
if (superType != null && superType instanceof AtlasEntityType) {
AtlasEntityType superEntityType = (AtlasEntityType)superType;
superEntityType.resolveReferences(typeRegistry);
s.add(superEntityType);
allS.add(superTypeName);
if (CollectionUtils.isNotEmpty(superEntityType.getAllSuperTypes())) {
allS.addAll(superEntityType.getAllSuperTypes());
}
} else {
String msg = superTypeName + ((superType == null) ? ": unknown" : ": incompatible");
msg += (" supertype in entity " + entityDef.getName());
LOG.error(msg);
throw new AtlasBaseException(msg);
}
}
this.superTypes = Collections.unmodifiableList(s);
this.allSuperTypes = Collections.unmodifiableSet(allS);
}
public Set<String> getSuperTypes() {
return entityDef.getSuperTypes();
}
public Set<String> getAllSuperTypes() {
return allSuperTypes;
}
public boolean isSuperTypeOf(AtlasEntityType entityType) {
return entityType != null ? entityType.getAllSuperTypes().contains(this.getTypeName()) : false;
}
public boolean isSubTypeOf(AtlasEntityType entityType) {
return entityType != null ? allSuperTypes.contains(entityType.getTypeName()) : false;
}
@Override
public AtlasEntity createDefaultValue() {
AtlasEntity ret = new AtlasEntity(entityDef.getName());
populateDefaultValues(ret);
return ret;
}
@Override
public boolean isValidValue(Object obj) {
if (obj != null) {
for (AtlasEntityType superType : superTypes) {
if (!superType.isValidValue(obj)) {
return false;
}
}
return super.isValidValue(obj);
}
return true;
}
@Override
public Object getNormalizedValue(Object obj) {
Object ret = null;
if (obj != null) {
if (isValidValue(obj)) {
if (obj instanceof AtlasEntity) {
normalizeAttributeValues((AtlasEntity) obj);
ret = obj;
} else if (obj instanceof Map) {
normalizeAttributeValues((Map) obj);
ret = obj;
}
}
}
return ret;
}
@Override
public boolean validateValue(Object obj, String objName, List<String> messages) {
boolean ret = true;
if (obj != null) {
for (AtlasEntityType superType : superTypes) {
ret = superType.validateValue(obj, objName, messages) && ret;
}
ret = super.validateValue(obj, objName, messages) && ret;
}
return ret;
}
public void normalizeAttributeValues(AtlasEntity ent) {
if (ent != null) {
for (AtlasEntityType superType : superTypes) {
superType.normalizeAttributeValues(ent);
}
super.normalizeAttributeValues(ent);
}
}
@Override
public void normalizeAttributeValues(Map<String, Object> obj) {
if (obj != null) {
for (AtlasEntityType superType : superTypes) {
superType.normalizeAttributeValues(obj);
}
super.normalizeAttributeValues(obj);
}
}
public void populateDefaultValues(AtlasEntity ent) {
if (ent != null) {
for (AtlasEntityType superType : superTypes) {
superType.populateDefaultValues(ent);
}
super.populateDefaultValues(ent);
}
}
}
/**
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.type;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import org.apache.atlas.model.typedef.AtlasEnumDef;
import org.apache.atlas.model.typedef.AtlasEnumDef.AtlasEnumElementDef;
/**
* class that implements behaviour of an enum-type.
*/
public class AtlasEnumType extends AtlasType {
private final AtlasEnumDef enumDef;
private final Map<String, AtlasEnumElementDef> elementDefs;
private final String defaultValue;
public AtlasEnumType(AtlasEnumDef enumDef) {
super(enumDef.getName());
Map<String, AtlasEnumElementDef> e = new HashMap<String, AtlasEnumElementDef>();
for (AtlasEnumElementDef elementDef : enumDef.getElementDefs()) {
e.put(elementDef.getValue().toLowerCase(), elementDef);
}
String d = enumDef.getDefaultValue();
if (d == null) {
AtlasEnumElementDef defElem = enumDef.getElementDefs().size() > 0 ? enumDef.getElementDefs().get(0) : null;
if (defElem != null) {
d = defElem.getValue();
}
}
this.enumDef = enumDef;
this.elementDefs = Collections.unmodifiableMap(e);
this.defaultValue = d;
}
@Override
public Object createDefaultValue() {
return defaultValue;
}
@Override
public boolean isValidValue(Object obj) {
if (obj != null) {
return elementDefs.containsKey(obj.toString().toLowerCase());
}
return true;
}
@Override
public Object getNormalizedValue(Object obj) {
if (obj != null) {
AtlasEnumElementDef elementDef = elementDefs.get(obj.toString().toLowerCase());
if (elementDef != null) {
return elementDef.getValue();
}
}
return null;
}
}
/**
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.type;
import org.apache.atlas.exception.AtlasBaseException;
import org.apache.atlas.model.typedef.AtlasBaseTypeDef;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.*;
/**
* class that implements behaviour of a map-type.
*/
public class AtlasMapType extends AtlasType {
private static final Logger LOG = LoggerFactory.getLogger(AtlasEntityType.class);
private final String keyTypeName;
private final String valueTypeName;
private AtlasType keyType;
private AtlasType valueType;
public AtlasMapType(String keyTypeName, String valueTypeName) {
super(AtlasBaseTypeDef.getMapTypeName(keyTypeName, valueTypeName));
this.keyTypeName = keyTypeName;
this.valueTypeName = valueTypeName;
}
public AtlasMapType(AtlasType keyType, AtlasType valueType) {
super(AtlasBaseTypeDef.getMapTypeName(keyType.getTypeName(), valueType.getTypeName()));
this.keyTypeName = keyType.getTypeName();
this.valueTypeName = valueType.getTypeName();
this.keyType = keyType;
this.valueType = valueType;
}
public AtlasMapType(String keyTypeName, String valueTypeName, AtlasTypeRegistry typeRegistry)
throws AtlasBaseException {
super(AtlasBaseTypeDef.getMapTypeName(keyTypeName, valueTypeName));
this.keyTypeName = keyTypeName;
this.valueTypeName = valueTypeName;
resolveReferences(typeRegistry);
}
@Override
public void resolveReferences(AtlasTypeRegistry typeRegistry) throws AtlasBaseException {
this.keyType = typeRegistry.getType(keyTypeName);
this.valueType = typeRegistry.getType(valueTypeName);
if (keyType == null) {
String msg = keyTypeName + ": unknown key-type for map";
LOG.error(msg);
throw new AtlasBaseException(msg);
}
if (valueType == null) {
String msg = valueTypeName + ": unknown value-type for map";
LOG.error(msg);
throw new AtlasBaseException(msg);
}
keyType.resolveReferences(typeRegistry);
valueType.resolveReferences(typeRegistry);
}
@Override
public Map<Object, Object> createDefaultValue() {
Map<Object, Object> ret = new HashMap<Object, Object>();
ret.put(keyType.createDefaultValue(), valueType.createDefaultValue());
return ret;
}
@Override
public boolean isValidValue(Object obj) {
if (obj != null) {
if (obj instanceof Map) {
Map<Object, Objects> map = (Map<Object, Objects>) obj;
for (Map.Entry e : map.entrySet()) {
if (!keyType.isValidValue(e.getKey()) || !valueType.isValidValue(e.getValue())) {
return false; // invalid key/value
}
}
} else {
return false; // invalid type
}
}
return true;
}
@Override
public Map<Object, Object> getNormalizedValue(Object obj) {
if (obj == null) {
return null;
}
if (obj instanceof Map) {
Map<Object, Object> ret = new HashMap<Object, Object>();
Map<Object, Objects> map = (Map<Object, Objects>) obj;
for (Map.Entry e : map.entrySet()) {
Object normalizedKey = keyType.getNormalizedValue(e.getKey());
if (normalizedKey != null) {
Object value = e.getValue();
if (value != null) {
Object normalizedValue = valueType.getNormalizedValue(e.getValue());
if (normalizedValue != null) {
ret.put(normalizedKey, normalizedValue);
} else {
return null; // invalid value
}
} else {
ret.put(normalizedKey, value);
}
} else {
return null; // invalid key
}
}
return ret;
}
return null;
}
@Override
public boolean validateValue(Object obj, String objName, List<String> messages) {
boolean ret = true;
if (obj != null) {
if (obj instanceof Map) {
Map<Object, Objects> map = (Map<Object, Objects>) obj;
for (Map.Entry e : map.entrySet()) {
Object key = e.getKey();
if (!keyType.isValidValue(key)) {
ret = false;
messages.add(objName + "." + key + ": invalid key for type " + getTypeName());
} else {
Object value = e.getValue();
ret = valueType.validateValue(value, objName + "." + key, messages) && ret;
}
}
} else {
ret = false;
messages.add(objName + "=" + obj + ": invalid value for type " + getTypeName());
}
}
return ret;
}
}
/**
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.type;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.apache.atlas.exception.AtlasBaseException;
import org.apache.atlas.model.typedef.AtlasBaseTypeDef;
import java.util.List;
/**
* base class that declares interface for all Atlas types.
*/
public abstract class AtlasType {
private static final Gson GSON =
new GsonBuilder().setDateFormat(AtlasBaseTypeDef.SERIALIZED_DATE_FORMAT_STR).create();
private final String typeName;
public AtlasType(String typeName) {
this.typeName = typeName;
}
public void resolveReferences(AtlasTypeRegistry typeRegistry) throws AtlasBaseException {
}
public String getTypeName() { return typeName; }
public abstract Object createDefaultValue();
public abstract boolean isValidValue(Object obj);
public abstract Object getNormalizedValue(Object obj);
public boolean validateValue(Object obj, String objName, List<String> messages) {
boolean ret = isValidValue(obj);
if (!ret) {
messages.add(objName + "=" + obj + ": invalid value for type " + getTypeName());
}
return ret;
}
public static String toJson(Object obj) {
return GSON.toJson(obj);
}
public static <T> T fromJson(String jsonStr, Class<T> type) {
return GSON.fromJson(jsonStr, type);
}
}
/**
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.model.instance;
import org.apache.atlas.model.ModelTestUtil;
import org.apache.atlas.model.typedef.AtlasClassificationDef;
import org.apache.atlas.type.AtlasType;
import org.apache.atlas.type.AtlasClassificationType;
import org.apache.atlas.type.AtlasTypeRegistry;
import org.testng.annotations.Test;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
public class TestAtlasClassification {
@Test
public void testClassificationSerDe() {
AtlasClassificationDef classificationDef = ModelTestUtil.getClassificationDef();
AtlasTypeRegistry typeRegistry = ModelTestUtil.getTypesRegistry();
AtlasType dataType = typeRegistry.getType(classificationDef.getName());
assertTrue(dataType instanceof AtlasClassificationType);
AtlasClassification ent1 = ModelTestUtil.newClassification(classificationDef, typeRegistry);
String jsonString = AtlasType.toJson(ent1);
AtlasClassification ent2 = AtlasType.fromJson(jsonString, AtlasClassification.class);
((AtlasClassificationType)dataType).normalizeAttributeValues(ent2);
assertEquals(ent2, ent1, "Incorrect serialization/deserialization of AtlasClassification");
}
@Test
public void testClassificationSerDeWithSuperType() {
AtlasClassificationDef classificationDef = ModelTestUtil.getClassificationDefWithSuperType();
AtlasTypeRegistry typeRegistry = ModelTestUtil.getTypesRegistry();
AtlasType dataType = typeRegistry.getType(classificationDef.getName());
assertTrue(dataType instanceof AtlasClassificationType);
AtlasClassification ent1 = ((AtlasClassificationType)dataType).createDefaultValue();
String jsonString = AtlasType.toJson(ent1);
AtlasClassification ent2 = AtlasType.fromJson(jsonString, AtlasClassification.class);
((AtlasClassificationType)dataType).normalizeAttributeValues(ent2);
assertEquals(ent2, ent1, "Incorrect serialization/deserialization of AtlasClassification with superType");
}
@Test
public void testClassificationSerDeWithSuperTypes() {
AtlasClassificationDef classificationDef = ModelTestUtil.getClassificationDefWithSuperTypes();
AtlasTypeRegistry typeRegistry = ModelTestUtil.getTypesRegistry();
AtlasType dataType = typeRegistry.getType(classificationDef.getName());
assertTrue(dataType instanceof AtlasClassificationType);
AtlasClassification ent1 = ((AtlasClassificationType)dataType).createDefaultValue();
String jsonString = AtlasType.toJson(ent1);
AtlasClassification ent2 = AtlasType.fromJson(jsonString, AtlasClassification.class);
((AtlasClassificationType)dataType).normalizeAttributeValues(ent2);
assertEquals(ent2, ent1, "Incorrect serialization/deserialization of AtlasClassification with superTypes");
}
}
/**
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.model.instance;
import org.apache.atlas.model.ModelTestUtil;
import org.apache.atlas.model.typedef.AtlasEntityDef;
import org.apache.atlas.type.AtlasType;
import org.apache.atlas.type.AtlasEntityType;
import org.apache.atlas.type.AtlasTypeRegistry;
import org.testng.annotations.Test;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
public class TestAtlasEntity {
@Test
public void testEntitySerDe() {
AtlasEntityDef entityDef = ModelTestUtil.getEntityDef();
AtlasTypeRegistry typeRegistry = ModelTestUtil.getTypesRegistry();
AtlasType dataType = typeRegistry.getType(entityDef.getName());
assertTrue(dataType instanceof AtlasEntityType);
AtlasEntity ent1 = ((AtlasEntityType)dataType).createDefaultValue();
String jsonString = AtlasType.toJson(ent1);
AtlasEntity ent2 = AtlasType.fromJson(jsonString, AtlasEntity.class);
((AtlasEntityType)dataType).normalizeAttributeValues(ent2);
assertEquals(ent2, ent1, "Incorrect serialization/deserialization of AtlasEntity");
}
@Test
public void testEntitySerDeWithSuperType() {
AtlasEntityDef entityDef = ModelTestUtil.getEntityDefWithSuperType();
AtlasTypeRegistry typeRegistry = ModelTestUtil.getTypesRegistry();
AtlasType dataType = typeRegistry.getType(entityDef.getName());
assertTrue(dataType instanceof AtlasEntityType);
AtlasEntity ent1 = ((AtlasEntityType)dataType).createDefaultValue();
String jsonString = AtlasType.toJson(ent1);
AtlasEntity ent2 = AtlasType.fromJson(jsonString, AtlasEntity.class);
((AtlasEntityType)dataType).normalizeAttributeValues(ent2);
assertEquals(ent2, ent1, "Incorrect serialization/deserialization of AtlasEntity with superType");
}
@Test
public void testEntitySerDeWithSuperTypes() {
AtlasEntityDef entityDef = ModelTestUtil.getEntityDefWithSuperTypes();
AtlasTypeRegistry typeRegistry = ModelTestUtil.getTypesRegistry();
AtlasType dataType = typeRegistry.getType(entityDef.getName());
assertTrue(dataType instanceof AtlasEntityType);
AtlasEntity ent1 = ((AtlasEntityType)dataType).createDefaultValue();
String jsonString = AtlasType.toJson(ent1);
AtlasEntity ent2 = AtlasType.fromJson(jsonString, AtlasEntity.class);
((AtlasEntityType)dataType).normalizeAttributeValues(ent2);
assertEquals(ent2, ent1, "Incorrect serialization/deserialization of AtlasEntity with superTypes");
}
}
/**
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.model.typedef;
import org.apache.atlas.model.ModelTestUtil;
import org.apache.atlas.type.AtlasType;
import org.testng.annotations.Test;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.assertFalse;
public class TestAtlasClassificationDef {
@Test
public void testClassificationDefSerDeEmpty() {
AtlasClassificationDef classificationDef = new AtlasClassificationDef("emptyClassificationDef");
String jsonString = AtlasType.toJson(classificationDef);
AtlasClassificationDef classificationDef2 = AtlasType.fromJson(jsonString, AtlasClassificationDef.class);
assertEquals(classificationDef2, classificationDef,
"Incorrect serialization/deserialization of AtlasClassificationDef");
}
@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");
}
@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");
}
@Test
public void testClassificationDefSerDeWithSuperTypes() {
AtlasClassificationDef classificationDef = ModelTestUtil.getClassificationDefWithSuperTypes();
String jsonString = AtlasType.toJson(classificationDef);
AtlasClassificationDef classificationDef2 = AtlasType.fromJson(jsonString, AtlasClassificationDef.class);
assertEquals(classificationDef2, classificationDef,
"Incorrect serialization/deserialization of AtlasClassificationDef with superTypes");
}
@Test
public void testClassificationDefHasSuperTypeWithNoSuperType() {
AtlasClassificationDef classificationDef = ModelTestUtil.getClassificationDef();
for (String superType : classificationDef.getSuperTypes()) {
assertTrue(classificationDef.hasSuperType(superType));
}
assertFalse(classificationDef.hasSuperType("01234-xyzabc-;''-)("));
}
@Test
public void testClassificationDefHasSuperTypeWithSuperType() {
AtlasClassificationDef classificationDef = ModelTestUtil.getClassificationDefWithSuperTypes();
for (String superType : classificationDef.getSuperTypes()) {
assertTrue(classificationDef.hasSuperType(superType));
}
assertFalse(classificationDef.hasSuperType("01234-xyzabc-;''-)("));
}
}
/**
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.model.typedef;
import org.apache.atlas.model.ModelTestUtil;
import org.apache.atlas.type.AtlasType;
import org.testng.annotations.Test;
import java.util.HashSet;
import java.util.Set;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.assertFalse;
public class TestAtlasEntityDef {
@Test
public void testEntityDefSerDeEmpty() {
AtlasEntityDef entityDef = new AtlasEntityDef("emptyEntityDef");
String jsonString = AtlasType.toJson(entityDef);
AtlasEntityDef entityDef2 = AtlasType.fromJson(jsonString, AtlasEntityDef.class);
assertEquals(entityDef2, entityDef, "Incorrect serialization/deserialization of AtlasEntityDef");
}
@Test
public void testEntityDefSerDe() {
AtlasEntityDef entityDef = ModelTestUtil.getEntityDef();
String jsonString = AtlasType.toJson(entityDef);
AtlasEntityDef entityDef2 = AtlasType.fromJson(jsonString, AtlasEntityDef.class);
assertEquals(entityDef2, entityDef, "Incorrect serialization/deserialization of AtlasEntityDef");
}
@Test
public void testEntityDefSerDeWithSuperType() {
AtlasEntityDef entityDef = ModelTestUtil.getEntityDefWithSuperType();
String jsonString = AtlasType.toJson(entityDef);
AtlasEntityDef entityDef2 = AtlasType.fromJson(jsonString, AtlasEntityDef.class);
assertEquals(entityDef2, entityDef, "Incorrect serialization/deserialization of AtlasEntityDef with superType");
}
@Test
public void testEntityDefSerDeWithSuperTypes() {
AtlasEntityDef entityDef = ModelTestUtil.getEntityDefWithSuperTypes();
String jsonString = AtlasType.toJson(entityDef);
AtlasEntityDef entityDef2 = AtlasType.fromJson(jsonString, AtlasEntityDef.class);
assertEquals(entityDef2, entityDef,
"Incorrect serialization/deserialization of AtlasEntityDef with superTypes");
}
@Test
public void testEntityDefAddSuperType() {
AtlasEntityDef entityDef = ModelTestUtil.newEntityDef();
String newSuperType = "newType-abcd-1234";
entityDef.addSuperType(newSuperType);
assertTrue(entityDef.hasSuperType(newSuperType));
}
@Test
public void testEntityDefDefRemoveElement() {
AtlasEntityDef entityDef = ModelTestUtil.newEntityDefWithSuperTypes();
for (String superType : entityDef.getSuperTypes()) {
entityDef.removeSuperType(superType);
assertFalse(entityDef.hasSuperType(superType));
}
}
@Test
public void testEntityDefSetSuperTypes() {
AtlasEntityDef entityDef = ModelTestUtil.newEntityDefWithSuperTypes();
Set<String> oldSuperTypes = entityDef.getSuperTypes();
Set<String> newSuperTypes = new HashSet<String>();
newSuperTypes.add("newType-abcd-1234");
entityDef.setSuperTypes(newSuperTypes);
for (String superType : oldSuperTypes) {
assertFalse(entityDef.hasSuperType(superType));
}
for (String superType : newSuperTypes) {
assertTrue(entityDef.hasSuperType(superType));
}
}
@Test
public void testEntityDefHasSuperTypeWithNoSuperType() {
AtlasEntityDef entityDef = ModelTestUtil.getEntityDef();
for (String superType : entityDef.getSuperTypes()) {
assertTrue(entityDef.hasSuperType(superType));
}
assertFalse(entityDef.hasSuperType("01234-xyzabc-;''-)("));
}
@Test
public void testEntityDefHasSuperTypeWithNoSuperTypes() {
AtlasEntityDef entityDef = ModelTestUtil.getEntityDefWithSuperTypes();
for (String superType : entityDef.getSuperTypes()) {
assertTrue(entityDef.hasSuperType(superType));
}
assertFalse(entityDef.hasSuperType("01234-xyzabc-;''-)("));
}
}
/**
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.model.typedef;
import java.util.ArrayList;
import java.util.List;
import org.apache.atlas.model.ModelTestUtil;
import org.apache.atlas.model.typedef.AtlasEnumDef.AtlasEnumElementDef;
import org.apache.atlas.type.AtlasType;
import org.testng.annotations.Test;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertTrue;
public class TestAtlasEnumDef {
@Test
public void testEnumDefSerDeEmpty() {
AtlasEnumDef enumDef1 = new AtlasEnumDef();
String jsonString = AtlasType.toJson(enumDef1);
AtlasEnumDef enumDef2 = AtlasType.fromJson(jsonString, AtlasEnumDef.class);
assertEquals(enumDef1, enumDef2, "Incorrect serialization/deserialization of AtlasEnumDef");
}
@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");
}
@Test
public void testEnumDefHasElement() {
AtlasEnumDef enumDef = ModelTestUtil.getEnumDef();
for (AtlasEnumElementDef elementDef : enumDef.getElementDefs()) {
assertTrue(enumDef.hasElement(elementDef.getValue()));
}
assertFalse(enumDef.hasElement("01234-xyzabc-;''-)("));
}
@Test
public void testEnumDefAddElement() {
AtlasEnumDef enumDef = ModelTestUtil.newEnumDef();
String newElement = "newElement-abcd-1234";
enumDef.addElement(new AtlasEnumElementDef(newElement, "A new element", enumDef.getElementDefs().size()));
assertTrue(enumDef.hasElement(newElement));
}
@Test
public void testEnumDefRemoveElement() {
AtlasEnumDef enumDef = ModelTestUtil.newEnumDef();
if (enumDef.getElementDefs().size() > 0) {
String elementValue = enumDef.getElementDefs().get(0).getValue();
assertTrue(enumDef.hasElement(elementValue));
enumDef.removeElement(elementValue);
assertFalse(enumDef.hasElement(elementValue));
}
}
@Test
public void testEnumDefSetElementDefs() {
AtlasEnumDef enumDef = ModelTestUtil.newEnumDef();
List<AtlasEnumElementDef> oldElements = enumDef.getElementDefs();
List<AtlasEnumElementDef> newElements = new ArrayList<AtlasEnumElementDef>();
newElements.add(new AtlasEnumElementDef("newElement", "new Element", 100));
enumDef.setElementDefs(newElements);
for (AtlasEnumElementDef elementDef : oldElements) {
assertFalse(enumDef.hasElement(elementDef.getValue()));
}
for (AtlasEnumElementDef elementDef : newElements) {
assertTrue(enumDef.hasElement(elementDef.getValue()));
}
}
}
/**
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.model.typedef;
import java.util.List;
import org.apache.atlas.model.ModelTestUtil;
import org.apache.atlas.model.typedef.AtlasStructDef.AtlasAttributeDef;
import org.apache.atlas.type.AtlasType;
import org.testng.annotations.Test;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.assertFalse;
public class TestAtlasStructDef {
@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");
}
@Test
public void testStructDefSerDe() {
AtlasStructDef structDef = ModelTestUtil.getStructDef();
String jsonString = AtlasType.toJson(structDef);
AtlasStructDef structDef2 = AtlasType.fromJson(jsonString, AtlasStructDef.class);
assertEquals(structDef2, structDef, "Incorrect serialization/deserialization of AtlasStructDef");
}
@Test
public void testStructDefHasAttribute() {
AtlasStructDef structDef = ModelTestUtil.getStructDef();
for (AtlasAttributeDef attributeDef : structDef.getAttributeDefs()) {
assertTrue(structDef.hasAttribute(attributeDef.getName()));
}
assertFalse(structDef.hasAttribute("01234-xyzabc-;''-)("));
}
@Test
public void testStructDefAddAttribute() {
AtlasStructDef structDef = ModelTestUtil.newStructDef();
structDef.addAttribute(new AtlasAttributeDef("newAttribute", AtlasBaseTypeDef.ATLAS_TYPE_INT));
assertTrue(structDef.hasAttribute("newAttribute"));
}
@Test
public void testStructDefRemoveAttribute() {
AtlasStructDef structDef = ModelTestUtil.newStructDef();
String attrName = structDef.getAttributeDefs().get(0).getName();
assertTrue(structDef.hasAttribute(attrName));
structDef.removeAttribute(attrName);
assertFalse(structDef.hasAttribute(attrName));
}
@Test
public void testStructDefSetAttributeDefs() {
AtlasStructDef structDef = ModelTestUtil.newStructDef();
List<AtlasAttributeDef> oldAttributes = structDef.getAttributeDefs();
List<AtlasAttributeDef> newttributes = ModelTestUtil.newAttributeDefsWithAllBuiltInTypes("newAttributes");
structDef.setAttributeDefs(newttributes);
for (AtlasAttributeDef attributeDef : oldAttributes) {
assertFalse(structDef.hasAttribute(attributeDef.getName()));
}
for (AtlasAttributeDef attributeDef : newttributes) {
assertTrue(structDef.hasAttribute(attributeDef.getName()));
}
}
}
/**
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.type;
import org.apache.atlas.type.AtlasBuiltInTypes.AtlasIntType;
import org.testng.annotations.Test;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.*;
import static org.testng.Assert.*;
public class TestAtlasArrayType {
private final AtlasArrayType intArrayType = new AtlasArrayType(new AtlasIntType());
private final Object[] validValues;
private final Object[] invalidValues;
{
List<Integer> intList = new ArrayList<Integer>();
Set<Integer> intSet = new HashSet<Integer>();
Integer[] intArray = new Integer[] { 1, 2, 3 };
List<Object> objList = new ArrayList<Object>();
Set<Object> objSet = new HashSet<Object>();
Object[] objArray = new Object[] { 1, 2, 3 };
List<String> strList = new ArrayList<String>();
Set<String> strSet = new HashSet<String>();
String[] strArray = new String[] { "1", "2", "3" };
for (int i = 0; i < 10; i++) {
intList.add(i);
intSet.add(i);
objList.add(i);
objSet.add(i);
strList.add(Integer.toString(i));
strSet.add(Integer.toString(i));
}
validValues = new Object[] {
null, new Integer[] { }, intList, intSet, intArray, objList, objSet, objArray, strList, strSet, strArray,
new byte[] { 1 }, new short[] { 1 }, new int[] { 1 }, new long[] { 1 }, new float[] { 1 },
new double[] { 1 }, new BigInteger[] { BigInteger.valueOf(1) }, new BigDecimal[] { BigDecimal.valueOf(1)},
};
invalidValues = new Object[] {
new String[] { "1", "abcd", "3", "xyz", "5" }, "1", Byte.valueOf((byte)1), Short.valueOf((short)1),
Integer.valueOf(1), Long.valueOf(1L), Float.valueOf(1), Double.valueOf(1), BigInteger.valueOf(1),
BigDecimal.valueOf(1),
};
}
@Test
public void testArrayTypeDefaultValue() {
Collection defValue = intArrayType.createDefaultValue();
assertEquals(defValue.size(), 1);
}
@Test
public void testArrayTypeIsValidValue() {
for (Object value : validValues) {
assertTrue(intArrayType.isValidValue(value), "value=" + value);
}
for (Object value : invalidValues) {
assertFalse(intArrayType.isValidValue(value), "value=" + value);
}
}
@Test
public void testArrayTypeGetNormalizedValue() {
assertNull(intArrayType.getNormalizedValue(null), "value=" + null);
for (Object value : validValues) {
if (value == null) {
continue;
}
Collection normalizedValue = intArrayType.getNormalizedValue(value);
assertNotNull(normalizedValue, "value=" + value);
}
for (Object value : invalidValues) {
assertNull(intArrayType.getNormalizedValue(value), "value=" + value);
}
}
@Test
public void testArrayTypeValidateValue() {
List<String> messages = new ArrayList<String>();
for (Object value : validValues) {
assertTrue(intArrayType.validateValue(value, "testObj", messages));
assertEquals(messages.size(), 0, "value=" + value);
}
for (Object value : invalidValues) {
assertFalse(intArrayType.validateValue(value, "testObj", messages));
assertTrue(messages.size() > 0, "value=" + value);
messages.clear();
}
}
}
/**
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.type;
import org.apache.atlas.type.AtlasBuiltInTypes.AtlasBigDecimalType;
import org.testng.annotations.Test;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import static org.testng.Assert.*;
public class TestAtlasBigDecimalType {
private final AtlasBigDecimalType bigDecimalType = new AtlasBigDecimalType();
private final Object[] validValues = {
null, Byte.valueOf((byte)1), Short.valueOf((short)1), Integer.valueOf(1), Long.valueOf(1L), Float.valueOf(1),
Double.valueOf(1), BigInteger.valueOf(1), BigDecimal.valueOf(1), "1",
};
private final Object[] negativeValues = {
Byte.valueOf((byte)-1), Short.valueOf((short)-1), Integer.valueOf(-1), Long.valueOf(-1L), Float.valueOf(-1),
Double.valueOf(-1), BigInteger.valueOf(-1), BigDecimal.valueOf(-1), "-1",
};
private final Object[] invalidValues = { "", "12ab", "abcd", "-12ab" };
@Test
public void testBigDecimalTypeDefaultValue() {
BigDecimal defValue = bigDecimalType.createDefaultValue();
assertEquals(defValue, BigDecimal.valueOf(0));
}
@Test
public void testBigDecimalTypeIsValidValue() {
for (Object value : validValues) {
assertTrue(bigDecimalType.isValidValue(value), "value=" + value);
}
for (Object value : negativeValues) {
assertTrue(bigDecimalType.isValidValue(value), "value=" + value);
}
for (Object value : invalidValues) {
assertFalse(bigDecimalType.isValidValue(value), "value=" + value);
}
}
@Test
public void testBigDecimalTypeGetNormalizedValue() {
assertNull(bigDecimalType.getNormalizedValue(null), "value=" + null);
for (Object value : validValues) {
if (value == null) {
continue;
}
BigDecimal normalizedValue = bigDecimalType.getNormalizedValue(value);
assertNotNull(normalizedValue, "value=" + value);
assertEquals(normalizedValue, BigDecimal.valueOf(1), "value=" + value);
}
for (Object value : negativeValues) {
BigDecimal normalizedValue = bigDecimalType.getNormalizedValue(value);
assertNotNull(normalizedValue, "value=" + value);
assertEquals(normalizedValue, BigDecimal.valueOf(-1), "value=" + value);
}
for (Object value : invalidValues) {
assertNull(bigDecimalType.getNormalizedValue(value), "value=" + value);
}
}
@Test
public void testBigDecimalTypeValidateValue() {
List<String> messages = new ArrayList<String>();
for (Object value : validValues) {
assertTrue(bigDecimalType.validateValue(value, "testObj", messages));
assertEquals(messages.size(), 0, "value=" + value);
}
for (Object value : negativeValues) {
assertTrue(bigDecimalType.validateValue(value, "testObj", messages));
assertEquals(messages.size(), 0, "value=" + value);
}
for (Object value : invalidValues) {
assertFalse(bigDecimalType.validateValue(value, "testObj", messages));
assertEquals(messages.size(), 1, "value=" + value);
messages.clear();
}
}
}
/**
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.type;
import org.apache.atlas.type.AtlasBuiltInTypes.AtlasBigIntegerType;
import org.testng.annotations.Test;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import static org.testng.Assert.*;
public class TestAtlasBigIntegerType {
private final AtlasBigIntegerType bigIntegerType = new AtlasBigIntegerType();
private final Object[] validValues = {
null, Byte.valueOf((byte)1), Short.valueOf((short)1), Integer.valueOf(1), Long.valueOf(1L), Float.valueOf(1),
Double.valueOf(1), BigInteger.valueOf(1), BigDecimal.valueOf(1), "1",
};
private final Object[] negativeValues = {
Byte.valueOf((byte)-1), Short.valueOf((short)-1), Integer.valueOf(-1), Long.valueOf(-1L), Float.valueOf(-1),
Double.valueOf(-1), BigInteger.valueOf(-1), BigDecimal.valueOf(-1), "-1",
};
private final Object[] invalidValues = { "", "12ab", "abcd", "-12ab", };
@Test
public void testBigIntegerTypeDefaultValue() {
BigInteger defValue = bigIntegerType.createDefaultValue();
assertEquals(defValue, BigInteger.valueOf(0));
}
@Test
public void testBigIntegerTypeIsValidValue() {
for (Object value : validValues) {
assertTrue(bigIntegerType.isValidValue(value), "value=" + value);
}
for (Object value : negativeValues) {
assertTrue(bigIntegerType.isValidValue(value), "value=" + value);
}
for (Object value : invalidValues) {
assertFalse(bigIntegerType.isValidValue(value), "value=" + value);
}
}
@Test
public void testBigIntegerTypeGetNormalizedValue() {
assertNull(bigIntegerType.getNormalizedValue(null), "value=" + null);
for (Object value : validValues) {
if (value == null) {
continue;
}
BigInteger normalizedValue = bigIntegerType.getNormalizedValue(value);
assertNotNull(normalizedValue, "value=" + value);
assertEquals(normalizedValue, BigInteger.valueOf(1), "value=" + value);
}
for (Object value : negativeValues) {
BigInteger normalizedValue = bigIntegerType.getNormalizedValue(value);
assertNotNull(normalizedValue, "value=" + value);
assertEquals(normalizedValue, BigInteger.valueOf(-1), "value=" + value);
}
for (Object value : invalidValues) {
assertNull(bigIntegerType.getNormalizedValue(value), "value=" + value);
}
}
@Test
public void testBigIntegerTypeValidateValue() {
List<String> messages = new ArrayList<String>();
for (Object value : validValues) {
assertTrue(bigIntegerType.validateValue(value, "testObj", messages));
assertEquals(messages.size(), 0, "value=" + value);
}
for (Object value : negativeValues) {
assertTrue(bigIntegerType.validateValue(value, "testObj", messages));
assertEquals(messages.size(), 0, "value=" + value);
}
for (Object value : invalidValues) {
assertFalse(bigIntegerType.validateValue(value, "testObj", messages));
assertEquals(messages.size(), 1, "value=" + value);
messages.clear();
}
}
}
/**
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.type;
import java.util.ArrayList;
import java.util.List;
import org.apache.atlas.type.AtlasBuiltInTypes.AtlasBooleanType;
import org.testng.annotations.Test;
import static org.testng.Assert.*;
public class TestAtlasBooleanType {
private final AtlasBooleanType booleanType = new AtlasBooleanType();
private final Object[] validValues = { null, Boolean.TRUE, Boolean.FALSE, "true", "false", "TRUE", "FALSE", };
private final Object[] invalidValues = { };
@Test
public void testBooleanTypeDefaultValue() {
Boolean defValue = booleanType.createDefaultValue();
assertFalse(defValue);
}
@Test
public void testBooleanTypeIsValidValue() {
for (Object value : validValues) {
assertTrue(booleanType.isValidValue(value), "value=" + value);
}
for (Object value : invalidValues) {
assertFalse(booleanType.isValidValue(value), "value=" + value);
}
}
@Test
public void testBooleanTypeGetNormalizedValue() {
Object[] trueValues = { Boolean.TRUE, "true", "TRUE", "tRuE", "TrUe" };
Object[] falseValues = { Boolean.FALSE, "false", "FALSE", "fAlSe", "FaLsE" };
assertNull(booleanType.getNormalizedValue(null), "value=" + null);
for (Object value : trueValues) {
Boolean normalizedValue = booleanType.getNormalizedValue(value);
assertNotNull(normalizedValue, "value=" + value);
assertTrue(normalizedValue, "value=" + value);
}
for (Object value : falseValues) {
Boolean normalizedValue = booleanType.getNormalizedValue(value);
assertNotNull(normalizedValue, "value=" + value);
assertFalse(normalizedValue, "value=" + value);
}
}
@Test
public void testBooleanTypeValidateValue() {
List<String> messages = new ArrayList<String>();
for (Object value : validValues) {
assertTrue(booleanType.validateValue(value, "testObj", messages));
assertEquals(messages.size(), 0, "value=" + value);
}
for (Object value : invalidValues) {
assertFalse(booleanType.validateValue(value, "testObj", messages));
assertEquals(messages.size(), 1, "value=" + value);
messages.clear();
}
}
}
/**
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.type;
import org.apache.atlas.type.AtlasBuiltInTypes.AtlasByteType;
import org.testng.annotations.Test;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import static org.testng.Assert.*;
public class TestAtlasByteType {
private final AtlasByteType byteType = new AtlasByteType();
private final Object[] validValues = {
null, Byte.valueOf((byte)1), Short.valueOf((short)1), Integer.valueOf(1), Long.valueOf(1L), Float.valueOf(1),
Double.valueOf(1), BigInteger.valueOf(1), BigDecimal.valueOf(1), "1",
};
private final Object[] negativeValues = {
Byte.valueOf((byte)-1), Short.valueOf((short)-1), Integer.valueOf(-1), Long.valueOf(-1L), Float.valueOf(-1),
Double.valueOf(-1), BigInteger.valueOf(-1), BigDecimal.valueOf(-1), "-1",
};
private final Object[] invalidValues = { "", };
@Test
public void testByteTypeDefaultValue() {
Byte defValue = byteType.createDefaultValue();
assertEquals(defValue, Byte.valueOf((byte)0));
}
@Test
public void testByteTypeIsValidValue() {
for (Object value : validValues) {
assertTrue(byteType.isValidValue(value), "value=" + value);
}
for (Object value : negativeValues) {
assertTrue(byteType.isValidValue(value), "value=" + value);
}
for (Object value : invalidValues) {
assertFalse(byteType.isValidValue(value), "value=" + value);
}
}
@Test
public void testByteTypeGetNormalizedValue() {
assertNull(byteType.getNormalizedValue(null), "value=" + null);
for (Object value : validValues) {
if (value == null) {
continue;
}
Byte normalizedValue = byteType.getNormalizedValue(value);
assertNotNull(normalizedValue, "value=" + value);
assertEquals(normalizedValue, Byte.valueOf((byte)1), "value=" + value);
}
for (Object value : negativeValues) {
Byte normalizedValue = byteType.getNormalizedValue(value);
assertNotNull(normalizedValue, "value=" + value);
assertEquals(normalizedValue, Byte.valueOf((byte)-1), "value=" + value);
}
for (Object value : invalidValues) {
assertNull(byteType.getNormalizedValue(value), "value=" + value);
}
}
@Test
public void testByteTypeValidateValue() {
List<String> messages = new ArrayList<String>();
for (Object value : validValues) {
assertTrue(byteType.validateValue(value, "testObj", messages));
assertEquals(messages.size(), 0, "value=" + value);
}
for (Object value : negativeValues) {
assertTrue(byteType.validateValue(value, "testObj", messages));
assertEquals(messages.size(), 0, "value=" + value);
}
for (Object value : invalidValues) {
assertFalse(byteType.validateValue(value, "testObj", messages));
assertEquals(messages.size(), 1, "value=" + value);
messages.clear();
}
}
}
/**
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.type;
import java.util.*;
import org.apache.atlas.exception.AtlasBaseException;
import org.apache.atlas.model.ModelTestUtil;
import org.apache.atlas.model.instance.AtlasClassification;
import org.apache.atlas.model.typedef.AtlasBaseTypeDef;
import org.apache.atlas.model.typedef.AtlasClassificationDef;
import org.testng.annotations.Test;
import static org.testng.Assert.*;
public class TestAtlasClassificationType {
private final AtlasClassificationType classificationType;
private final List<Object> validValues = new ArrayList<Object>();
private final List<Object> invalidValues = new ArrayList<Object>();
{
classificationType = getClassificationType(ModelTestUtil.getClassificationDefWithSuperTypes());
AtlasClassification invalidValue1 = classificationType.createDefaultValue();
AtlasClassification invalidValue2 = classificationType.createDefaultValue();
Map<String, Object> invalidValue3 = classificationType.createDefaultValue().getAttributes();
// invalid value for int
invalidValue1.setAttribute(ModelTestUtil.getDefaultAttributeName(AtlasBaseTypeDef.ATLAS_TYPE_INT), "xyz");
// invalid value for date
invalidValue2.setAttribute(ModelTestUtil.getDefaultAttributeName(AtlasBaseTypeDef.ATLAS_TYPE_DATE), "xyz");
// invalid value for bigint
invalidValue3.put(ModelTestUtil.getDefaultAttributeName(AtlasBaseTypeDef.ATLAS_TYPE_BIGINTEGER), "xyz");
validValues.add(null);
validValues.add(classificationType.createDefaultValue());
validValues.add(classificationType.createDefaultValue().getAttributes()); // Map<String, Object>
invalidValues.add(invalidValue1);
invalidValues.add(invalidValue2);
invalidValues.add(invalidValue3);
invalidValues.add(new AtlasClassification()); // no values for mandatory attributes
invalidValues.add(new HashMap<Object, Object>()); // no values for mandatory attributes
invalidValues.add(1); // incorrect datatype
invalidValues.add(new HashSet()); // incorrect datatype
invalidValues.add(new ArrayList()); // incorrect datatype
invalidValues.add(new String[] {}); // incorrect datatype
}
@Test
public void testClassificationTypeDefaultValue() {
AtlasClassification defValue = classificationType.createDefaultValue();
assertNotNull(defValue);
assertEquals(defValue.getTypeName(), classificationType.getTypeName());
}
@Test
public void testClassificationTypeIsValidValue() {
for (Object value : validValues) {
assertTrue(classificationType.isValidValue(value), "value=" + value);
}
for (Object value : invalidValues) {
assertFalse(classificationType.isValidValue(value), "value=" + value);
}
}
@Test
public void testClassificationTypeGetNormalizedValue() {
assertNull(classificationType.getNormalizedValue(null), "value=" + null);
for (Object value : validValues) {
if (value == null) {
continue;
}
Object normalizedValue = classificationType.getNormalizedValue(value);
assertNotNull(normalizedValue, "value=" + value);
}
for (Object value : invalidValues) {
assertNull(classificationType.getNormalizedValue(value), "value=" + value);
}
}
@Test
public void testClassificationTypeValidateValue() {
List<String> messages = new ArrayList<String>();
for (Object value : validValues) {
assertTrue(classificationType.validateValue(value, "testObj", messages));
assertEquals(messages.size(), 0, "value=" + value);
}
for (Object value : invalidValues) {
assertFalse(classificationType.validateValue(value, "testObj", messages));
assertTrue(messages.size() > 0, "value=" + value);
messages.clear();
}
}
private static AtlasClassificationType getClassificationType(AtlasClassificationDef classificationDef) {
try {
return new AtlasClassificationType(classificationDef, ModelTestUtil.getTypesRegistry());
} catch (AtlasBaseException excp) {
return null;
}
}
}
/**
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.type;
import org.apache.atlas.model.typedef.AtlasBaseTypeDef;
import org.apache.atlas.type.AtlasBuiltInTypes.AtlasDateType;
import org.testng.annotations.Test;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Date;
import java.util.ArrayList;
import java.util.List;
import static org.testng.Assert.*;
public class TestAtlasDateType {
private final AtlasDateType dateType = new AtlasDateType();
private final Object[] validValues = {
null, Byte.valueOf((byte)1), Short.valueOf((short)1), Integer.valueOf(1), Long.valueOf(1L), Float.valueOf(1),
Double.valueOf(1), BigInteger.valueOf(1), BigDecimal.valueOf(1), "1",
};
private final Object[] negativeValues = {
Byte.valueOf((byte)-1), Short.valueOf((short)-1), Integer.valueOf(-1), Long.valueOf(-1L), Float.valueOf(-1),
Double.valueOf(-1), BigInteger.valueOf(-1), BigDecimal.valueOf(-1), "-1",
};
private final Object[] invalidValues = { "", "12ab", "abcd", "-12ab", };
private final Date now = new Date();
private final String strNow = AtlasBaseTypeDef.DATE_FORMATTER.format(now);
@Test
public void testDateTypeDefaultValue() {
Date defValue = dateType.createDefaultValue();
assertEquals(defValue, new Date(0));
}
@Test
public void testDateTypeIsValidValue() {
for (Object value : validValues) {
assertTrue(dateType.isValidValue(value), "value=" + value);
}
assertTrue(dateType.isValidValue(now), "value=" + now);
assertTrue(dateType.isValidValue(strNow), "value=" + strNow);
for (Object value : negativeValues) {
assertTrue(dateType.isValidValue(value), "value=" + value);
}
for (Object value : invalidValues) {
assertFalse(dateType.isValidValue(value), "value=" + value);
}
}
@Test
public void testDateTypeGetNormalizedValue() {
assertNull(dateType.getNormalizedValue(null), "value=" + null);
for (Object value : validValues) {
if (value == null) {
continue;
}
Date normalizedValue = dateType.getNormalizedValue(value);
assertNotNull(normalizedValue, "value=" + value);
assertEquals(normalizedValue, new Date(1), "value=" + value);
}
assertNotNull(dateType.getNormalizedValue(now), "value=" + now);
assertEquals(dateType.getNormalizedValue(now), now, "value=" + now);
assertNotNull(dateType.getNormalizedValue(strNow), "value=" + strNow);
assertEquals(dateType.getNormalizedValue(now), now, "value=" + now);
for (Object value : negativeValues) {
Date normalizedValue = dateType.getNormalizedValue(value);
assertNotNull(normalizedValue, "value=" + value);
assertEquals(normalizedValue, new Date(-1), "value=" + value);
}
for (Object value : invalidValues) {
assertNull(dateType.getNormalizedValue(value), "value=" + value);
}
}
@Test
public void testDateTypeValidateValue() {
List<String> messages = new ArrayList<String>();
for (Object value : validValues) {
assertTrue(dateType.validateValue(value, "testObj", messages));
assertEquals(messages.size(), 0, "value=" + value);
}
assertTrue(dateType.validateValue(now, "testObj", messages));
assertEquals(messages.size(), 0, "value=" + now);
assertTrue(dateType.validateValue(strNow, "testObj", messages));
assertEquals(messages.size(), 0, "value=" + strNow);
for (Object value : negativeValues) {
assertTrue(dateType.validateValue(value, "testObj", messages));
assertEquals(messages.size(), 0, "value=" + value);
}
for (Object value : invalidValues) {
assertFalse(dateType.validateValue(value, "testObj", messages));
assertEquals(messages.size(), 1, "value=" + value);
messages.clear();
}
}
}
/**
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.type;
import org.apache.atlas.type.AtlasBuiltInTypes.AtlasDoubleType;
import org.testng.annotations.Test;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import static org.testng.Assert.*;
public class TestAtlasDoubleType {
private final AtlasDoubleType doubleType = new AtlasDoubleType();
private final Object[] validValues = {
null, Byte.valueOf((byte)1), Short.valueOf((short)1), Integer.valueOf(1), Long.valueOf(1L), Float.valueOf(1),
Double.valueOf(1), BigInteger.valueOf(1), BigDecimal.valueOf(1), "1",
};
private final Object[] negativeValues = {
Byte.valueOf((byte)-1), Short.valueOf((short)-1), Integer.valueOf(-1), Long.valueOf(-1L), Float.valueOf(-1),
Double.valueOf(-1), BigInteger.valueOf(-1), BigDecimal.valueOf(-1), "-1",
};
private final Object[] invalidValues = { "", "12ab", "abcd", "-12ab", };
@Test
public void testDoubleTypeDefaultValue() {
Double defValue = doubleType.createDefaultValue();
assertEquals(defValue, Double.valueOf(0));
}
@Test
public void testDoubleTypeIsValidValue() {
for (Object value : validValues) {
assertTrue(doubleType.isValidValue(value), "value=" + value);
}
for (Object value : negativeValues) {
assertTrue(doubleType.isValidValue(value), "value=" + value);
}
for (Object value : invalidValues) {
assertFalse(doubleType.isValidValue(value), "value=" + value);
}
}
@Test
public void testDoubleTypeGetNormalizedValue() {
assertNull(doubleType.getNormalizedValue(null), "value=" + null);
for (Object value : validValues) {
if (value == null) {
continue;
}
Double normalizedValue = doubleType.getNormalizedValue(value);
assertNotNull(normalizedValue, "value=" + value);
assertEquals(normalizedValue, Double.valueOf(1), "value=" + value);
}
for (Object value : negativeValues) {
Double normalizedValue = doubleType.getNormalizedValue(value);
assertNotNull(normalizedValue, "value=" + value);
assertEquals(normalizedValue, Double.valueOf(-1), "value=" + value);
}
for (Object value : invalidValues) {
assertNull(doubleType.getNormalizedValue(value), "value=" + value);
}
}
@Test
public void testDoubleTypeValidateValue() {
List<String> messages = new ArrayList<String>();
for (Object value : validValues) {
assertTrue(doubleType.validateValue(value, "testObj", messages));
assertEquals(messages.size(), 0, "value=" + value);
}
for (Object value : negativeValues) {
assertTrue(doubleType.validateValue(value, "testObj", messages));
assertEquals(messages.size(), 0, "value=" + value);
}
for (Object value : invalidValues) {
assertFalse(doubleType.validateValue(value, "testObj", messages));
assertEquals(messages.size(), 1, "value=" + value);
messages.clear();
}
}
}
/**
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.type;
import java.util.*;
import org.apache.atlas.exception.AtlasBaseException;
import org.apache.atlas.model.ModelTestUtil;
import org.apache.atlas.model.instance.AtlasEntity;
import org.apache.atlas.model.typedef.AtlasBaseTypeDef;
import org.apache.atlas.model.typedef.AtlasEntityDef;
import org.testng.annotations.Test;
import static org.testng.Assert.*;
public class TestAtlasEntityType {
private final AtlasEntityType entityType;
private final List<Object> validValues = new ArrayList<Object>();
private final List<Object> invalidValues = new ArrayList<Object>();
{
entityType = getEntityType(ModelTestUtil.getEntityDefWithSuperTypes());
AtlasEntity invalidValue1 = entityType.createDefaultValue();
AtlasEntity invalidValue2 = entityType.createDefaultValue();
Map<String, Object> invalidValue3 = entityType.createDefaultValue().getAttributes();
// invalid value for int
invalidValue1.setAttribute(ModelTestUtil.getDefaultAttributeName(AtlasBaseTypeDef.ATLAS_TYPE_INT), "xyz");
// invalid value for date
invalidValue2.setAttribute(ModelTestUtil.getDefaultAttributeName(AtlasBaseTypeDef.ATLAS_TYPE_DATE), "xyz");
// invalid value for bigint
invalidValue3.put(ModelTestUtil.getDefaultAttributeName(AtlasBaseTypeDef.ATLAS_TYPE_BIGINTEGER), "xyz");
validValues.add(null);
validValues.add(entityType.createDefaultValue());
validValues.add(entityType.createDefaultValue().getAttributes()); // Map<String, Object>
invalidValues.add(invalidValue1);
invalidValues.add(invalidValue2);
invalidValues.add(invalidValue3);
invalidValues.add(new AtlasEntity()); // no values for mandatory attributes
invalidValues.add(new HashMap<Object, Object>()); // no values for mandatory attributes
invalidValues.add(1); // incorrect datatype
invalidValues.add(new HashSet()); // incorrect datatype
invalidValues.add(new ArrayList()); // incorrect datatype
invalidValues.add(new String[] {}); // incorrect datatype
}
@Test
public void testEntityTypeDefaultValue() {
AtlasEntity defValue = entityType.createDefaultValue();
assertNotNull(defValue);
assertEquals(defValue.getTypeName(), entityType.getTypeName());
}
@Test
public void testEntityTypeIsValidValue() {
for (Object value : validValues) {
assertTrue(entityType.isValidValue(value), "value=" + value);
}
for (Object value : invalidValues) {
assertFalse(entityType.isValidValue(value), "value=" + value);
}
}
@Test
public void testEntityTypeGetNormalizedValue() {
assertNull(entityType.getNormalizedValue(null), "value=" + null);
for (Object value : validValues) {
if (value == null) {
continue;
}
Object normalizedValue = entityType.getNormalizedValue(value);
assertNotNull(normalizedValue, "value=" + value);
}
for (Object value : invalidValues) {
assertNull(entityType.getNormalizedValue(value), "value=" + value);
}
}
@Test
public void testEntityTypeValidateValue() {
List<String> messages = new ArrayList<String>();
for (Object value : validValues) {
assertTrue(entityType.validateValue(value, "testObj", messages));
assertEquals(messages.size(), 0, "value=" + value);
}
for (Object value : invalidValues) {
assertFalse(entityType.validateValue(value, "testObj", messages));
assertTrue(messages.size() > 0, "value=" + value);
messages.clear();
}
}
private static AtlasEntityType getEntityType(AtlasEntityDef entityDef) {
try {
return new AtlasEntityType(entityDef, ModelTestUtil.getTypesRegistry());
} catch (AtlasBaseException excp) {
return null;
}
}
}
/**
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.type;
import org.apache.atlas.type.AtlasBuiltInTypes.AtlasFloatType;
import org.testng.annotations.Test;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import static org.testng.Assert.*;
public class TestAtlasFloatType {
private final AtlasFloatType floatType = new AtlasFloatType();
private final Object[] validValues = {
null, Byte.valueOf((byte)1), Short.valueOf((short)1), Integer.valueOf(1), Long.valueOf(1L), Float.valueOf(1),
Double.valueOf(1), BigInteger.valueOf(1), BigDecimal.valueOf(1), "1",
};
private final Object[] negativeValues = {
Byte.valueOf((byte)-1), Short.valueOf((short)-1), Integer.valueOf(-1), Long.valueOf(-1L), Float.valueOf(-1),
Double.valueOf(-1), BigInteger.valueOf(-1), BigDecimal.valueOf(-1), "-1",
};
private final Object[] invalidValues = { "", "12ab", "abcd", "-12ab", };
@Test
public void testFloatTypeDefaultValue() {
Float defValue = floatType.createDefaultValue();
assertEquals(defValue, Float.valueOf(0));
}
@Test
public void testFloatTypeIsValidValue() {
for (Object value : validValues) {
assertTrue(floatType.isValidValue(value), "value=" + value);
}
for (Object value : negativeValues) {
assertTrue(floatType.isValidValue(value), "value=" + value);
}
for (Object value : invalidValues) {
assertFalse(floatType.isValidValue(value), "value=" + value);
}
}
@Test
public void testFloatTypeGetNormalizedValue() {
assertNull(floatType.getNormalizedValue(null), "value=" + null);
for (Object value : validValues) {
if (value == null) {
continue;
}
Float normalizedValue = floatType.getNormalizedValue(value);
assertNotNull(normalizedValue, "value=" + value);
assertEquals(normalizedValue, Float.valueOf(1), "value=" + value);
}
for (Object value : negativeValues) {
Float normalizedValue = floatType.getNormalizedValue(value);
assertNotNull(normalizedValue, "value=" + value);
assertEquals(normalizedValue, Float.valueOf(-1), "value=" + value);
}
for (Object value : invalidValues) {
assertNull(floatType.getNormalizedValue(value), "value=" + value);
}
}
@Test
public void testFloatTypeValidateValue() {
List<String> messages = new ArrayList<String>();
for (Object value : validValues) {
assertTrue(floatType.validateValue(value, "testObj", messages));
assertEquals(messages.size(), 0, "value=" + value);
}
for (Object value : negativeValues) {
assertTrue(floatType.validateValue(value, "testObj", messages));
assertEquals(messages.size(), 0, "value=" + value);
}
for (Object value : invalidValues) {
assertFalse(floatType.validateValue(value, "testObj", messages));
assertEquals(messages.size(), 1, "value=" + value);
messages.clear();
}
}
}
/**
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.type;
import org.apache.atlas.type.AtlasBuiltInTypes.AtlasIntType;
import org.testng.annotations.Test;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import static org.testng.Assert.*;
public class TestAtlasIntType {
private final AtlasIntType intType = new AtlasIntType();
private final Object[] validValues = {
null, Byte.valueOf((byte)1), Short.valueOf((short)1), Integer.valueOf(1), Long.valueOf(1L), Float.valueOf(1),
Double.valueOf(1), BigInteger.valueOf(1), BigDecimal.valueOf(1), "1",
};
private final Object[] negativeValues = {
Byte.valueOf((byte)-1), Short.valueOf((short)-1), Integer.valueOf(-1), Long.valueOf(-1L), Float.valueOf(-1),
Double.valueOf(-1), BigInteger.valueOf(-1), BigDecimal.valueOf(-1), "-1",
};
private final Object[] invalidValues = { "", "12ab", "abcd", "-12ab", };
@Test
public void testIntTypeDefaultValue() {
Integer defValue = intType.createDefaultValue();
assertEquals(defValue, Integer.valueOf(0));
}
@Test
public void testIntTypeIsValidValue() {
for (Object value : validValues) {
assertTrue(intType.isValidValue(value), "value=" + value);
}
for (Object value : negativeValues) {
assertTrue(intType.isValidValue(value), "value=" + value);
}
for (Object value : invalidValues) {
assertFalse(intType.isValidValue(value), "value=" + value);
}
}
@Test
public void testIntTypeGetNormalizedValue() {
assertNull(intType.getNormalizedValue(null), "value=" + null);
for (Object value : validValues) {
if (value == null) {
continue;
}
Integer normalizedValue = intType.getNormalizedValue(value);
assertNotNull(normalizedValue, "value=" + value);
assertEquals(normalizedValue, Integer.valueOf(1), "value=" + value);
}
for (Object value : negativeValues) {
Integer normalizedValue = intType.getNormalizedValue(value);
assertNotNull(normalizedValue, "value=" + value);
assertEquals(normalizedValue, Integer.valueOf(-1), "value=" + value);
}
for (Object value : invalidValues) {
assertNull(intType.getNormalizedValue(value), "value=" + value);
}
}
@Test
public void testIntTypeValidateValue() {
List<String> messages = new ArrayList<String>();
for (Object value : validValues) {
assertTrue(intType.validateValue(value, "testObj", messages));
assertEquals(messages.size(), 0, "value=" + value);
}
for (Object value : negativeValues) {
assertTrue(intType.validateValue(value, "testObj", messages));
assertEquals(messages.size(), 0, "value=" + value);
}
for (Object value : invalidValues) {
assertFalse(intType.validateValue(value, "testObj", messages));
assertEquals(messages.size(), 1, "value=" + value);
messages.clear();
}
}
}
/**
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.type;
import org.apache.atlas.type.AtlasBuiltInTypes.AtlasLongType;
import org.testng.annotations.Test;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import static org.testng.Assert.*;
public class TestAtlasLongType {
private final AtlasLongType longType = new AtlasLongType();
private final Object[] validValues = {
null, Byte.valueOf((byte)1), Short.valueOf((short)1), Integer.valueOf(1), Long.valueOf(1L), Float.valueOf(1),
Double.valueOf(1), BigInteger.valueOf(1), BigDecimal.valueOf(1), "1",
};
private final Object[] negativeValues = {
Byte.valueOf((byte)-1), Short.valueOf((short)-1), Integer.valueOf(-1), Long.valueOf(-1L), Float.valueOf(-1),
Double.valueOf(-1), BigInteger.valueOf(-1), BigDecimal.valueOf(-1), "-1",
};
private final Object[] invalidValues = { "", "12ab", "abcd", "-12ab", };
@Test
public void testLongTypeDefaultValue() {
Long defValue = longType.createDefaultValue();
assertEquals(defValue, Long.valueOf(0));
}
@Test
public void testLongTypeIsValidValue() {
for (Object value : validValues) {
assertTrue(longType.isValidValue(value), "value=" + value);
}
for (Object value : negativeValues) {
assertTrue(longType.isValidValue(value), "value=" + value);
}
for (Object value : invalidValues) {
assertFalse(longType.isValidValue(value), "value=" + value);
}
}
@Test
public void testLongTypeGetNormalizedValue() {
assertNull(longType.getNormalizedValue(null), "value=" + null);
for (Object value : validValues) {
if (value == null) {
continue;
}
Long normalizedValue = longType.getNormalizedValue(value);
assertNotNull(normalizedValue, "value=" + value);
assertEquals(normalizedValue, Long.valueOf(1), "value=" + value);
}
for (Object value : negativeValues) {
Long normalizedValue = longType.getNormalizedValue(value);
assertNotNull(normalizedValue, "value=" + value);
assertEquals(normalizedValue, Long.valueOf(-1), "value=" + value);
}
for (Object value : invalidValues) {
assertNull(longType.getNormalizedValue(value), "value=" + value);
}
}
@Test
public void testLongTypeValidateValue() {
List<String> messages = new ArrayList<String>();
for (Object value : validValues) {
assertTrue(longType.validateValue(value, "testObj", messages));
assertEquals(messages.size(), 0, "value=" + value);
}
for (Object value : negativeValues) {
assertTrue(longType.validateValue(value, "testObj", messages));
assertEquals(messages.size(), 0, "value=" + value);
}
for (Object value : invalidValues) {
assertFalse(longType.validateValue(value, "testObj", messages));
assertEquals(messages.size(), 1, "value=" + value);
messages.clear();
}
}
}
/**
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.type;
import org.apache.atlas.type.AtlasBuiltInTypes.AtlasIntType;
import org.testng.annotations.Test;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static org.testng.Assert.*;
public class TestAtlasMapType {
private final AtlasMapType intIntMapType = new AtlasMapType(new AtlasIntType(), new AtlasIntType());
private final Object[] validValues;
private final Object[] invalidValues;
{
Map<String, Integer> strIntMap = new HashMap<String, Integer>();
Map<String, Double> strDoubleMap = new HashMap<String, Double>();
Map<String, String> strStringMap = new HashMap<String, String>();
Map<Integer, Integer> intIntMap = new HashMap<Integer, Integer>();
Map<Object, Object> objObjMap = new HashMap<Object, Object>();
Map<Object, Object> invObjObjMap1 = new HashMap<Object, Object>();
Map<Object, Object> invObjObjMap2 = new HashMap<Object, Object>();
for (int i = 0; i < 10; i++) {
strIntMap.put(Integer.toString(i), i);
strDoubleMap.put(Integer.toString(i), Double.valueOf(i));
strStringMap.put(Integer.toString(i), Integer.toString(i));
intIntMap.put(i, i);
objObjMap.put(i, i);
}
invObjObjMap1.put("xyz", "123"); // invalid key
invObjObjMap2.put("123", "xyz"); // invalid value
validValues = new Object[] {
null, new HashMap<String, Integer>(), new HashMap<Object, Object>(), strIntMap, strDoubleMap, strStringMap,
intIntMap, objObjMap,
};
invalidValues = new Object[] { invObjObjMap1, invObjObjMap2, };
}
@Test
public void testMapTypeDefaultValue() {
Map<Object, Object> defValue = intIntMapType.createDefaultValue();
assertEquals(defValue.size(), 1);
}
@Test
public void testMapTypeIsValidValue() {
for (Object value : validValues) {
assertTrue(intIntMapType.isValidValue(value), "value=" + value);
}
for (Object value : invalidValues) {
assertFalse(intIntMapType.isValidValue(value), "value=" + value);
}
}
@Test
public void testMapTypeGetNormalizedValue() {
assertNull(intIntMapType.getNormalizedValue(null), "value=" + null);
for (Object value : validValues) {
if (value == null) {
continue;
}
Map<Object, Object> normalizedValue = intIntMapType.getNormalizedValue(value);
assertNotNull(normalizedValue, "value=" + value);
}
for (Object value : invalidValues) {
assertNull(intIntMapType.getNormalizedValue(value), "value=" + value);
}
}
@Test
public void testMapTypeValidateValue() {
List<String> messages = new ArrayList<String>();
for (Object value : validValues) {
assertTrue(intIntMapType.validateValue(value, "testObj", messages));
assertEquals(messages.size(), 0, "value=" + value);
}
for (Object value : invalidValues) {
assertFalse(intIntMapType.validateValue(value, "testObj", messages));
assertTrue(messages.size() > 0, "value=" + value);
messages.clear();
}
}
}
/**
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.type;
import org.apache.atlas.model.instance.AtlasObjectId;
import org.apache.atlas.type.AtlasBuiltInTypes.AtlasObjectIdType;
import org.testng.annotations.Test;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static org.testng.Assert.*;
public class TestAtlasObjectIdType {
private final AtlasObjectIdType objectIdType = new AtlasObjectIdType();
private final Object[] validValues;
private final Object[] invalidValues;
{
Map<String, String> objectId1 = new HashMap<String, String>();
Map<Object, Object> objectId2 = new HashMap<Object, Object>();
Map<Object, Object> objectId3 = new HashMap<Object, Object>();
Map<Object, Object> objectId4 = new HashMap<Object, Object>();
Map<Object, Object> objectId5 = new HashMap<Object, Object>();
objectId1.put(AtlasObjectId.KEY_TYPENAME, "testType");
objectId1.put(AtlasObjectId.KEY_GUID, "guid-1234");
objectId2.put(AtlasObjectId.KEY_TYPENAME, "testType");
objectId2.put(AtlasObjectId.KEY_GUID, 1234);
objectId3.put(AtlasObjectId.KEY_TYPENAME, "testType"); // no guid
objectId4.put(AtlasObjectId.KEY_GUID, "guid-1234"); // no typeName
objectId4.put(AtlasObjectId.KEY_TYPENAME + "-invalid", "testType");
objectId5.put(AtlasObjectId.KEY_GUID + "-invalid", "guid-1234");
validValues = new Object[] {
null, objectId1, objectId2, new AtlasObjectId(), new AtlasObjectId("testType", "guid-1234"), };
invalidValues = new Object[] {
objectId3, objectId4, objectId5, Byte.valueOf((byte)1), Short.valueOf((short)1), Integer.valueOf(1),
Long.valueOf(1L), Float.valueOf(1), Double.valueOf(1), BigInteger.valueOf(1), BigDecimal.valueOf(1), "1",
"", "12ab", "abcd", "-12ab",
};
}
@Test
public void testObjectIdTypeDefaultValue() {
AtlasObjectId defValue = objectIdType.createDefaultValue();
assertNotNull(defValue);
}
@Test
public void testObjectIdTypeIsValidValue() {
for (Object value : validValues) {
assertTrue(objectIdType.isValidValue(value), "value=" + value);
}
for (Object value : invalidValues) {
assertFalse(objectIdType.isValidValue(value), "value=" + value);
}
}
@Test
public void testObjectIdTypeGetNormalizedValue() {
assertNull(objectIdType.getNormalizedValue(null), "value=" + null);
for (Object value : validValues) {
if (value == null) {
continue;
}
AtlasObjectId normalizedValue = objectIdType.getNormalizedValue(value);
assertNotNull(normalizedValue, "value=" + value);
if (value instanceof AtlasObjectId) {
assertEquals(normalizedValue, value, "value=" + value);
} else if (value instanceof Map) {
assertEquals(normalizedValue.getTypeName(), ((Map)value).get(AtlasObjectId.KEY_TYPENAME).toString(),
"value=" + value);
assertEquals(normalizedValue.getGuid(), ((Map)value).get(AtlasObjectId.KEY_GUID).toString(),
"value=" + value);
}
}
}
@Test
public void testObjectIdTypeValidateValue() {
List<String> messages = new ArrayList<String>();
for (Object value : validValues) {
assertTrue(objectIdType.validateValue(value, "testObj", messages));
assertEquals(messages.size(), 0, "value=" + value);
}
for (Object value : invalidValues) {
assertFalse(objectIdType.validateValue(value, "testObj", messages));
assertEquals(messages.size(), 1, "value=" + value);
messages.clear();
}
}
}
/**
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.type;
import org.apache.atlas.type.AtlasBuiltInTypes.AtlasShortType;
import org.testng.annotations.Test;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import static org.testng.Assert.*;
public class TestAtlasShortType {
private final AtlasShortType shortType = new AtlasShortType();
private final Object[] validValues = {
null, Byte.valueOf((byte)1), Short.valueOf((short)1), Integer.valueOf(1), Long.valueOf(1L), Float.valueOf(1),
Double.valueOf(1), BigInteger.valueOf(1), BigDecimal.valueOf(1), "1",
};
private final Object[] negativeValues = {
Byte.valueOf((byte)-1), Short.valueOf((short)-1), Integer.valueOf(-1), Long.valueOf(-1L), Float.valueOf(-1),
Double.valueOf(-1), BigInteger.valueOf(-1), BigDecimal.valueOf(-1), "-1",
};
private final Object[] invalidValues = { "", "12ab", "abcd", "-12ab", };
@Test
public void testShortTypeDefaultValue() {
Short defValue = shortType.createDefaultValue();
assertEquals(defValue, Short.valueOf((short)0));
}
@Test
public void testShortTypeIsValidValue() {
for (Object value : validValues) {
assertTrue(shortType.isValidValue(value), "value=" + value);
}
for (Object value : negativeValues) {
assertTrue(shortType.isValidValue(value), "value=" + value);
}
for (Object value : invalidValues) {
assertFalse(shortType.isValidValue(value), "value=" + value);
}
}
@Test
public void testShortTypeGetNormalizedValue() {
assertNull(shortType.getNormalizedValue(null), "value=" + null);
for (Object value : validValues) {
if (value == null) {
continue;
}
Short normalizedValue = shortType.getNormalizedValue(value);
assertNotNull(normalizedValue, "value=" + value);
assertEquals(normalizedValue, Short.valueOf((short)1), "value=" + value);
}
for (Object value : negativeValues) {
Short normalizedValue = shortType.getNormalizedValue(value);
assertNotNull(normalizedValue, "value=" + value);
assertEquals(normalizedValue, Short.valueOf((short)-1), "value=" + value);
}
for (Object value : invalidValues) {
assertNull(shortType.getNormalizedValue(value), "value=" + value);
}
}
@Test
public void testShortTypeValidateValue() {
List<String> messages = new ArrayList<String>();
for (Object value : validValues) {
assertTrue(shortType.validateValue(value, "testObj", messages));
assertEquals(messages.size(), 0, "value=" + value);
}
for (Object value : negativeValues) {
assertTrue(shortType.validateValue(value, "testObj", messages));
assertEquals(messages.size(), 0, "value=" + value);
}
for (Object value : invalidValues) {
assertFalse(shortType.validateValue(value, "testObj", messages));
assertEquals(messages.size(), 1, "value=" + value);
messages.clear();
}
}
}
/**
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.type;
import org.apache.atlas.type.AtlasBuiltInTypes.AtlasStringType;
import org.testng.annotations.Test;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import static org.testng.Assert.*;
public class TestAtlasStringType {
private final AtlasStringType stringType = new AtlasStringType();
private final Object[] validValues = {
null, Byte.valueOf((byte)1), Short.valueOf((short)1), Integer.valueOf(1), Long.valueOf(1L), Float.valueOf(1),
Double.valueOf(1), BigInteger.valueOf(1), BigDecimal.valueOf(1), "1", "", "12ab", "abcd", "-12ab",
};
private final Object[] invalidValues = { };
@Test
public void testStringTypeDefaultValue() {
String defValue = stringType.createDefaultValue();
assertEquals(defValue, "");
}
@Test
public void testStringTypeIsValidValue() {
for (Object value : validValues) {
assertTrue(stringType.isValidValue(value), "value=" + value);
}
for (Object value : invalidValues) {
assertFalse(stringType.isValidValue(value), "value=" + value);
}
}
@Test
public void testStringTypeGetNormalizedValue() {
assertNull(stringType.getNormalizedValue(null), "value=" + null);
for (Object value : validValues) {
if (value == null) {
continue;
}
String normalizedValue = stringType.getNormalizedValue(value);
assertNotNull(normalizedValue, "value=" + value);
assertEquals(normalizedValue, value.toString(), "value=" + value);
}
}
@Test
public void testStringTypeValidateValue() {
List<String> messages = new ArrayList<String>();
for (Object value : validValues) {
assertTrue(stringType.validateValue(value, "testObj", messages));
assertEquals(messages.size(), 0, "value=" + value);
}
for (Object value : invalidValues) {
assertFalse(stringType.validateValue(value, "testObj", messages));
assertEquals(messages.size(), 1, "value=" + value);
messages.clear();
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment