Coverage Report - com.aurea.maven.plugins.sonic.sdm.SdmValidatePropertiesMojo
 
Classes in this File Line Coverage Branch Coverage Complexity
SdmValidatePropertiesMojo
0%
0/63
0%
0/30
5.5
 
 1  
 package com.aurea.maven.plugins.sonic.sdm;
 2  
 
 3  
 import java.io.File;
 4  
 import java.io.FileInputStream;
 5  
 import java.util.Iterator;
 6  
 import java.util.Properties;
 7  
 
 8  
 import org.apache.maven.plugin.MojoExecutionException;
 9  
 import org.apache.maven.plugin.MojoFailureException;
 10  
 import org.codehaus.plexus.archiver.Archiver;
 11  
 import org.codehaus.plexus.archiver.manager.ArchiverManager;
 12  
 import org.codehaus.plexus.util.xml.XmlStreamReader;
 13  
 import org.codehaus.plexus.util.xml.Xpp3Dom;
 14  
 import org.codehaus.plexus.util.xml.Xpp3DomBuilder;
 15  
 
 16  
 import com.aurea.maven.plugins.sonic.utils.FileUtilities;
 17  
 
 18  
 /**
 19  
  * validates the *.tailoring.properties files.
 20  
  * 
 21  
  * @goal sdm-validate-properties
 22  
  */
 23  
 
 24  0
 public class SdmValidatePropertiesMojo extends AbstractSdmMojo {
 25  
 
 26  
   /**
 27  
    * The Sonic Target Version Definition. (not used)
 28  
    * 
 29  
    * @parameter default-value="2015"
 30  
    * @required
 31  
    */
 32  
   protected String          sonicTarget;
 33  
 
 34  
   /**
 35  
    * fails the build if there are warnings validating the tailoring properties files.
 36  
    * 
 37  
    * @parameter property="sonicesb.failOnValidationWarning" default-value="false"
 38  
    * @required
 39  
    */
 40  
   protected boolean         failOnValidationWarning;
 41  
 
 42  
   /**
 43  
    * To look up Archiver/UnArchiver implementations.
 44  
    * 
 45  
    * @component
 46  
    * @required
 47  
    */
 48  
   protected ArchiverManager archiverManager;
 49  
 
 50  
   /**
 51  
    * This method is called from the framework to actually perform the work.
 52  
    * 
 53  
    * @throws MojoExecutionException
 54  
    *           MojoExecutionException
 55  
    * @throws MojoFailureException
 56  
    *           MojoFailureException
 57  
    */
 58  
   protected void doExecute() throws MojoExecutionException, MojoFailureException {
 59  
 
 60  
     try {
 61  
       // Now we generate a generated tailoring file as a template for environment specific properties.
 62  
       //if (sonicTarget.equals("8")) {
 63  0
         validateTopologies();
 64  
 //      } else {
 65  
 //        validateProperties();
 66  
 //      }
 67  
 
 68  
       // Finally we package everything up in one archive. That archive is THE maven artifact that will be deployed,
 69  
       // installed etc.
 70  0
       File sdmAssemblyFile = new File(getOutputDirectory(), getFinalAssemblyFileName());
 71  0
       Archiver archiver = archiverManager.getArchiver("zip");
 72  0
       archiver.setDestFile(sdmAssemblyFile);
 73  0
       archiver.addDirectory(getTargetSDMDir());
 74  
       //archiver.createArchive();
 75  0
       project.getArtifact().setFile(sdmAssemblyFile);
 76  0
     } catch (Exception e) {
 77  0
       throw new MojoExecutionException("Error creating SDM Model - " + e.getMessage(), e);
 78  0
     }
 79  0
   }
 80  
 
 81  
   private void validateTopologies() throws MojoExecutionException {
 82  0
     getLog().info("Validating the Topology files");
 83  
 
 84  0
     boolean isValid = true;
 85  
 
 86  0
     Properties generated = null;
 87  0
     Properties compare = null;
 88  
 
 89  
     // Reading the Generated Topology File
 90  0
     generated = buildPropertiesFromTopology(getGeneratedTopologyFile());
 91  
 
 92  0
     for (String toCompare : FileUtilities.getFileList(getTargetSDMDir().getAbsolutePath(), "*.Topology.xml",
 93  
         "generated.Topology.xml")) {
 94  0
       getLog().info("------------------------------------------------------------------------");
 95  0
       getLog().info("Verifying Topology file: " + toCompare);
 96  0
       compare = buildPropertiesFromTopology(new File(getTargetSDMDir() + "/" + toCompare));
 97  
 
 98  
       // this is on purpose to ensure that all .tailoring.properties files get validated in first pass
 99  0
       isValid = validateProperties(generated, compare) && isValid;
 100  
     }
 101  
 
 102  0
     if (!isValid) {
 103  0
       throw new MojoExecutionException("Properties validation failed");
 104  
     }
 105  0
   }
 106  
 
 107  
   private void validateProperties() throws MojoExecutionException {
 108  
 
 109  0
     boolean isValid = true;
 110  
 
 111  0
     Properties generated = null;
 112  0
     Properties compare = null;
 113  
 
 114  
     try {
 115  0
       generated = new Properties();
 116  0
       FileInputStream fis = new FileInputStream(getGeneratedPropertiesFile());
 117  0
       generated.load(fis);
 118  0
       fis.close();
 119  0
     } catch (Exception e) {
 120  0
       throw new MojoExecutionException("Could not read generated properties files: " + getGeneratedPropertiesFile());
 121  0
     }
 122  
 
 123  0
     for (String toCompare : FileUtilities.getFileList(getTargetSDMDir().getAbsolutePath(), "*.tailoring.properties",
 124  
         "generated.tailoring.properties")) {
 125  
       try {
 126  0
         getLog().info("------------------------------------------------------------------------");
 127  0
         getLog().info("Verifying property file: " + toCompare);
 128  0
         compare = new Properties();
 129  0
         FileInputStream fis = new FileInputStream(getTargetSDMDir() + "/" + toCompare);
 130  0
         compare.load(fis);
 131  0
         fis.close();
 132  0
       } catch (Exception e) {
 133  0
         getLog().warn("Could not read " + getTargetSDMDir() + "/" + toCompare);
 134  0
         isValid = false;
 135  0
       }
 136  
 
 137  
       // this is on purpose to ensure that all .tailoring.properties files get validated in first pass
 138  0
       isValid = validateProperties(generated, compare) && isValid;
 139  
     }
 140  
 
 141  0
     if (!isValid) {
 142  0
       throw new MojoExecutionException("Properties validation failed");
 143  
     }
 144  0
   }
 145  
 
 146  
   private boolean validateProperties(final Properties _source, final Properties _toCompare) {
 147  
 
 148  0
     boolean result = true;
 149  
 
 150  0
     for (Iterator<Object> it = (Iterator<Object>) _source.keySet().iterator(); it.hasNext();) {
 151  0
       Object key = it.next();
 152  0
       String value = _toCompare.getProperty(key.toString());
 153  
 
 154  0
       if (value == null) {
 155  0
         getLog().warn("Property file does not contain key: " + key);
 156  
         // if (((String) key).indexOf(" ") == -1) {
 157  0
         if (result) {
 158  
           // only set if not already failed
 159  0
           result = !failOnValidationWarning;
 160  
         }
 161  
 
 162  
         // }
 163  0
       } else if (value.trim().length() == 0) {
 164  0
         getLog().warn("Property has no value defined : " + key);
 165  
         // if (((String) key).indexOf(" ") == -1)
 166  0
         if (result) {
 167  
           // only set if not already failed
 168  0
           result = !failOnValidationWarning;
 169  
         }
 170  
         // }
 171  
       }
 172  0
     }
 173  
 
 174  0
     return result;
 175  
   }
 176  
 
 177  
 }