Skip to content

Commit

Permalink
Added some validation framework and work on specific model object val…
Browse files Browse the repository at this point in the history
…idators.
  • Loading branch information
elvisisking committed Jan 18, 2013
1 parent 971c5f7 commit 2599a2a
Show file tree
Hide file tree
Showing 21 changed files with 631 additions and 154 deletions.
10 changes: 5 additions & 5 deletions komodo-common/src/main/java/org/komodo/common/i18n/I18n.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
*/
public abstract class I18n {

class I18nProperties extends Properties {
private class I18nProperties extends Properties {

private static final long serialVersionUID = 297271848138379264L;

Expand Down Expand Up @@ -71,8 +71,6 @@ public synchronized Object put(final Object key,

}

private final Logger logger = LoggerFactory.getLogger(getClass());

/**
* @param pattern the message pattern (cannot be <code>null</code> or empty)
* @param args the arguments being used to replace placeholders in the message (can be <code>null</code> or empty)
Expand All @@ -84,6 +82,8 @@ public static String bind(final String pattern,
return String.format(pattern, args);
}

private final Logger logger = LoggerFactory.getLogger(getClass());

/**
* Should be called in a <code>static</code> block to load the properties file and assign values to the class string fields.
*
Expand Down Expand Up @@ -113,7 +113,7 @@ protected void initialize() {

// load properties file
InputStream stream = null;
IllegalStateException problem = null;
IllegalStateException problem = null;

try {
final Class thisClass = getClass();
Expand All @@ -126,7 +126,7 @@ protected void initialize() {
props.load(stream);

// log errors for any properties keys that don't have fields
for (String error : errors) {
for (final String error : errors) {
if (problem == null) {
problem = new IllegalStateException(error);
}
Expand Down
34 changes: 34 additions & 0 deletions komodo-common/src/main/java/org/komodo/common/util/ObjectUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* JBoss, Home of Professional Open Source.
*
* See the LEGAL.txt file distributed with this work for information regarding copyright ownership and licensing.
*
* See the AUTHORS.txt file distributed with this work for a full listing of individual contributors.
*/
package org.komodo.common.util;

/**
* Utilities for use with object types.
*/
public class ObjectUtil {

/**
* A empty object array constant.
*/
public static final Object[] EMPTY_ARRAY = {};

/**
* @param thisObj one of the objects being checked (can be <code>null</code>)
* @param thatObj the other object being checked (can be <code>null</code>)
* @return <code>true</code> if both strings are <code>null</code> or equals
*/
public static boolean matches(final Object thisObj,
final Object thatObj) {
if (thisObj == null) {
return (thatObj == null);
}

return thisObj.equals(thatObj);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class StringUtil {
/**
* A empty string array constant.
*/
public static final String[] EMPTY_STRING_ARRAY = {};
public static final String[] EMPTY_ARRAY = {};

/**
* @param text the string being checked (can be <code>null</code> or empty)
Expand All @@ -37,11 +37,7 @@ public static boolean isEmpty(final String text) {
*/
public static boolean matches(final String thisString,
final String thatString) {
if (thisString == null) {
return (thatString == null);
}

return thisString.equals(thatString);
return ObjectUtil.matches(thisString, thatString);
}

/**
Expand Down
61 changes: 47 additions & 14 deletions komodo-common/src/main/java/org/komodo/common/validate/Status.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
*/
package org.komodo.common.validate;

import java.util.ArrayList;
import java.util.List;
import org.komodo.common.util.CollectionUtil;
import org.komodo.common.util.HashCode;
import org.komodo.common.util.Precondition;

Expand Down Expand Up @@ -42,6 +45,7 @@ public enum Severity {
}

private final int code;
private List<Object> contexts;
private String message;
private final Severity severity;

Expand All @@ -68,13 +72,34 @@ public Status(final Severity severity,
this.message = message;
}

/**
* Contexts are not added if they already exist.
*
* @param newContexts the new status contexts (cannot be <code>null</code>)
*/
public void addContext(final Object... newContexts) {
Precondition.notNull(newContexts, "newContexts"); //$NON-NLS-1$

if (this.contexts == null) {
this.contexts = new ArrayList<Object>(3);
}

for (final Object context : newContexts) {
Precondition.notNull(context, "context"); //$NON-NLS-1$

if (this.contexts.contains(context)) {
this.contexts.add(context);
}
}
}

/**
* {@inheritDoc}
*
* @see java.lang.Comparable#compareTo(java.lang.Object)
*/
@Override
public int compareTo(Status that) {
public int compareTo(final Status that) {
if (this == that) {
return 0;
}
Expand All @@ -86,11 +111,11 @@ public int compareTo(Status that) {
if (this.severity == Severity.ERROR) {
return -1;
}

if (this.severity == Severity.WARNNING) {
return ((that.severity == Severity.ERROR) ? 1 : -1);
}

if (this.severity == Severity.INFO) {
return ((that.severity == Severity.OK)) ? -1 : 1;
}
Expand All @@ -105,7 +130,7 @@ public int compareTo(Status that) {
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
public boolean equals(final Object obj) {
if (this == obj) {
return true;
}
Expand All @@ -115,7 +140,8 @@ public boolean equals(Object obj) {
}

final Status that = (Status)obj;
return ((this.code == that.code) && (this.severity == that.severity));
return ((this.code == that.code) && (this.severity == that.severity) && CollectionUtil.matches(this.contexts,
that.contexts));
}

/**
Expand All @@ -125,6 +151,13 @@ public int getCode() {
return this.code;
}

/**
* @return the status contexts (can be <code>null</code>)
*/
public List<Object> getContexts() {
return this.contexts;
}

/**
* @return the message (can be <code>null</code> or empty)
*/
Expand All @@ -146,7 +179,7 @@ public Severity getSeverity() {
*/
@Override
public int hashCode() {
return HashCode.compute(this.code, this.severity);
return HashCode.compute(this.code, this.contexts, this.severity);
}

/**
Expand All @@ -156,13 +189,6 @@ public boolean isError() {
return (this.severity == Severity.ERROR);
}

/**
* @return <code>true</code> if status severity is {@link Severity#WARNNING}
*/
public boolean isWarning() {
return (this.severity == Severity.WARNNING);
}

/**
* @return <code>true</code> if status severity is {@link Severity#INFO}
*/
Expand All @@ -177,10 +203,17 @@ public boolean isOk() {
return (this.severity == Severity.OK);
}

/**
* @return <code>true</code> if status severity is {@link Severity#WARNNING}
*/
public boolean isWarning() {
return (this.severity == Severity.WARNNING);
}

/**
* @param newMessage the new message (can be <code>null</code> or empty)
*/
public void setMessage(String newMessage) {
public void setMessage(final String newMessage) {
this.message = newMessage;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ public void shouldDeriveParserTestVdbArtifacts() throws Exception {
assertPropertyValue(derivedArtifact, Schema.PropertyName.TYPE, Schema.Type.VIRTUAL.name());
assertPropertyValue(derivedArtifact, Schema.PropertyName.VISIBLE, "true");
assertPropertyValue(derivedArtifact, "model-prop", "model-value");
assertPropertyValue(derivedArtifact, Schema.PropertyName.METADATA_TYPE, Schema.DEFAULT_METADATA_TYPE);
assertPropertyValue(derivedArtifact, Schema.PropertyName.METADATA_TYPE, Schema.DEFAULT_METADATA_TYPE.name());
assertPropertyValue(derivedArtifact, Schema.PropertyName.METADATA, "DDL Here");

// sources and related document relationships
Expand Down Expand Up @@ -403,7 +403,7 @@ public void shouldDeriveTwitterVdbArtifacts() throws Exception {
} else if (!foundViewModel && "twitterview".equals(artifactName)) {
foundViewModel = true;
assertPropertyValue(derivedArtifact, Schema.PropertyName.TYPE, Schema.Type.VIRTUAL.name());
assertPropertyValue(derivedArtifact, Schema.PropertyName.METADATA_TYPE, Schema.DEFAULT_METADATA_TYPE);
assertPropertyValue(derivedArtifact, Schema.PropertyName.METADATA_TYPE, Schema.DEFAULT_METADATA_TYPE.name());
final String expected = "\n CREATE VIRTUAL PROCEDURE getTweets(query varchar) RETURNS (created_on varchar(25), from_user varchar(25), to_user varchar(25),\n"
+ " profile_image_url varchar(25), source varchar(25), text varchar(140)) AS\n"
+ " select tweet.* from\n"
Expand Down
2 changes: 1 addition & 1 deletion komodo-repository/src/test/resources/log4j.properties
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ log4j.logger.org.komodo.repository=INFO

# Uncomment to turn debug logging on
#log4j.logger.org.komodo.repository.AtomRepositoryManager=DEBUG
log4j.logger.org.komodo.repository.deriver.VdbDeriver=DEBUG
#log4j.logger.org.komodo.repository.deriver.VdbDeriver=DEBUG
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@
*/
package org.komodo.teiid.model.validate;

import java.util.ArrayList;
import java.util.List;
import org.komodo.common.util.Precondition;
import org.komodo.common.util.StringUtil;
import org.komodo.common.validate.Status;
import org.komodo.teiid.model.ModelObject;
import org.komodo.teiid.model.vdb.DataPolicy;
import org.komodo.teiid.model.vdb.Permission;

/**
* A validator for a Teiid {@link DataPolicy data policy}.
Expand All @@ -27,7 +30,40 @@ class DataPolicyValidator implements Validator {
public List<Status> validate(final ModelObject modelObject) throws IllegalArgumentException {
Precondition.instanceOf(modelObject, "modelObject", DataPolicy.class); //$NON-NLS-1$

return null;
final DataPolicy dataPolicy = (DataPolicy)modelObject;
final List<Status> errors = new ArrayList<Status>(3);

// make sure name is not empty
if (StringUtil.isEmpty(dataPolicy.getId())) {
final Status error = Error.EMPTY_DATA_POLICY_NAME.createStatus();
error.addContext(dataPolicy);
errors.add(error);
} else {
// make sure name is valid
// TODO implement schema name validation
}

// make sure there at least one permission
if (dataPolicy.getPermissions().isEmpty()) {
final Status error = Error.NO_DATA_POLICY_PERMISSIONS.createStatus();
error.addContext(dataPolicy);
errors.add(error);
} else {
// validate each permission
for (final Permission permission : dataPolicy.getPermissions()) {
for (final Status permissionError : Validators.SHARED.validate(permission)) {
permissionError.addContext(dataPolicy);
errors.add(permissionError);
}
}
}

// make sure role names are valid
// TODO validate data policy role name
// for (final String roleName : dataPolicy.getRoleNames()) {
// }

return errors;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.komodo.common.util.Precondition;
import org.komodo.common.util.StringUtil;
import org.komodo.common.validate.Status;
import org.komodo.teiid.model.ModelObject;
import org.komodo.teiid.model.vdb.Entry;
Expand All @@ -28,7 +30,37 @@ class EntryValidator implements Validator {
public List<Status> validate(final ModelObject modelObject) throws IllegalArgumentException {
Precondition.instanceOf(modelObject, "modelObject", Entry.class); //$NON-NLS-1$

final Entry entry = (Entry)modelObject;
final List<Status> errors = new ArrayList<Status>(3);

// make sure path is not empty
if (StringUtil.isEmpty(entry.getPath())) {
final Status error = Error.EMPTY_ENTRY_PATH.createStatus();
error.addContext(entry);
errors.add(error);
}

// validate properties
for (final Map.Entry<String, String> prop : entry.getProperties().entrySet()) {
if (StringUtil.isEmpty(prop.getKey())) {
final Status error = Error.EMPTY_ENTRY_PROPERTY_NAME.createStatus();
error.addContext(entry);
errors.add(error);
} else {
// make sure entry property name is valid
// TODO implement entry property name validation
}

if (StringUtil.isEmpty(prop.getKey())) {
final Status error = Error.EMPTY_ENTRY_PROPERTY_VALUE.createStatus();
error.addContext(entry);
errors.add(error);
} else {
// make sure entry property value is valid
// TODO implement entry property value validation
}
}

return errors;
}

Expand Down

0 comments on commit 2599a2a

Please sign in to comment.