All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.redsolo.vcm.commands.VerifyPropertyExistsCommand Maven / Gradle / Ivy

package com.github.redsolo.vcm.commands;

import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import net.lingala.zip4j.exception.ZipException;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;

import com.beust.jcommander.Parameter;
import com.beust.jcommander.Parameters;
import com.github.redsolo.vcm.Model;
import com.github.redsolo.vcm.ModelResource;

@Parameters(commandDescription = "Verifies that certain properties exists in components")
public class VerifyPropertyExistsCommand  extends AbstractVerifyModelCommand {
	private static Logger log = Logger.getLogger(VerifyPropertyExistsCommand.class);

	@Parameter(description = "properties to verify (comma separated list)", names = { "-p", "--props" }) 
	private String verifyProperties;

	private Map propNamesToVerify;

	@Override
	public String getName() {
		return "verify-propexists";
	}

	@Override
	protected void validateParameters(MainConfiguration mainConfiguration) {
		if (verifyProperties != null) {
			propNamesToVerify = new HashMap();
			for (String keyString : StringUtils.split(verifyProperties, ",")) {
				propNamesToVerify.put(keyString.toLowerCase(), keyString);
			}
		}
	}

	@Override
	protected void executeModel(Model model) throws IOException, ZipException {
		ModelResource variables = ResourceDataParser.getVariables(model.getResourceData());
		if (variables == null) {
			log.warn(String.format("VariableSpace node not found in file '%s'", model.getFile()));
		} else {
			if (propNamesToVerify != null) {
			    HashSet missingProperties = new HashSet(propNamesToVerify.keySet());
                missingProperties.removeAll(getPropertyNames(variables));
			    
				for (String propertyName : missingProperties) {
                    log.warn(String.format("Property '%s' does not exist in model %s", 
                            propNamesToVerify.get(propertyName), model.getFile()));
                    setVerificationFailed(true);        
				}
			}
		}
	}
	
	private Set getPropertyNames(ModelResource variables)  {
	    HashSet set = new HashSet();
        for (ModelResource variable : variables.getResources()) {
            if (variable.getValue("Name") != null) {
                set.add(variable.getValue("Name").toString().toLowerCase());
            }
        }
        return set;
	}

	public String getVerifyProperties() {
		return verifyProperties;
	}

	public void setVerifyProperties(String verifyProperties) {
		this.verifyProperties = verifyProperties;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy