Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MIP-325 Able to parse an app and print property file names #6

Merged
merged 5 commits into from
Jul 1, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/main/groovy/com/avioconsulting/mule/MuleLinter.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import com.avioconsulting.mule.linter.rule.pom.MuleMavenPluginVersionRule
import com.avioconsulting.mule.linter.rule.pom.MunitVersionRule
import com.avioconsulting.mule.linter.rule.pom.MuleRuntimeVersionRule
import com.avioconsulting.mule.linter.rule.pom.PomExistsRule
import com.avioconsulting.mule.linter.rule.property.PropertyFileNamingRule

class MuleLinter {

Expand All @@ -23,6 +24,7 @@ class MuleLinter {
rules.addRule(new MuleMavenPluginVersionRule('3.3.5'))
rules.addRule(new MunitVersionRule('2.2.1'))
rules.addRule(new MuleRuntimeVersionRule('4.2.1'))
rules.addRule(new PropertyFileNamingRule(['dev', 'test']))

// Create the executor
RuleExecutor exe = new RuleExecutor(app, rules)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import picocli.CommandLine
@CommandLine.Command(name = 'MuleLinter', header = '%n@|green Mule Linter|@')
class MuleLinterCli implements Runnable {

@CommandLine.Option(names = ['-r', '--rules'], required = true, description = 'Rule definition file')
@CommandLine.Option(names = ['-r', '--rules'], required = false, description = 'Rule definition file')
String rules

@CommandLine.Option(names = ['-d', '--dir'], required = true, description = 'Application Directory')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ class Application {
File applicationPath
List<ProjectFile> files = []
PomFile pomFile
String name

Application(File applicationPath) {
this.applicationPath = applicationPath
if (!this.applicationPath.exists()) {
throw new FileNotFoundException( APPLICATION_DOES_NOT_EXIST + applicationPath.absolutePath)
}
pomFile = new PomFile(applicationPath, POM_FILE)
this.name = applicationPath.name
}

File getApplicationPath() {
Expand All @@ -34,4 +36,22 @@ class Application {
return file.exists()
}

String getName() {
return name
}

void setName(String name) {
this.name = name
}

List<ProjectFile> getPropertyFiles() {
List<ProjectFile> files = []
applicationPath.eachDirRecurse { dir ->
dir.eachFileMatch(~/.*.properties/) { file ->
files.add(new ProjectFile(file))
}
}
kkingavio marked this conversation as resolved.
Show resolved Hide resolved
return files
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package com.avioconsulting.mule.linter.model
class ProjectFile {

File file
String name

ProjectFile(File f) {
file = f
this.name = f.name
}

File getFile() {
Expand All @@ -16,4 +18,12 @@ class ProjectFile {
this.file = file
}

String getName() {
return name
}

void setName(String name) {
this.name = name
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.avioconsulting.mule.linter.rule.property

import com.avioconsulting.mule.linter.model.Application
import com.avioconsulting.mule.linter.model.RuleViolation
import com.avioconsulting.mule.linter.model.Rule
import groovy.text.SimpleTemplateEngine

@SuppressWarnings(['GStringExpressionWithinString'])
class PropertyFileNamingRule extends Rule {

static final String RULE_ID = 'PROPERTY_FILE_NAMING'
static final String RULE_NAME = 'Property File Naming Rule'
static final String RULE_VIOLATION_MESSAGE = 'Missing property file, files must match naming pattern: '
static final String DEFAULT_PATTERN = '${appname}-${env}.properties'

String[] environments
String pattern

/**
* A new PropertyFileNamingRule for a list of environments. This ensures that
* there is at least one file that matches the pattern '${appname}-${env}.properties'
* for each environment.
*
* @param environments List of environments to check for files
*/
PropertyFileNamingRule(List<String> environments) {
this(environments, DEFAULT_PATTERN)
}

/**
* A new PropertyFileNamingRule for a list of environments. This ensures that
* there is at least one file that matches the pattern for each environment.
* Possible pattern variables ${env} and ${appname}.
*
* @param environments List of environments to check for files
* @param pattern String pattern to search. ex. '${appname}-${env}.properties'
*/
PropertyFileNamingRule(List<String> environments, String pattern) {
this.ruleId = RULE_ID
this.ruleName = RULE_NAME
this.environments = environments
this.pattern = pattern
}

@Override
List<RuleViolation> execute(Application app) {
List<RuleViolation> violations = []

List propertyFilenames = []
app.propertyFiles.each {
kkingavio marked this conversation as resolved.
Show resolved Hide resolved
propertyFilenames.add(it.name)
}

environments.each { env ->
Map<String, String> binding = ['appname':app.name, 'env':env]
String fileName = new SimpleTemplateEngine().createTemplate(pattern).make(binding)

if (!(fileName in propertyFilenames)) {
violations.add(new RuleViolation(this, fileName, 0, RULE_VIOLATION_MESSAGE + pattern))
}
}

return violations
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.avioconsulting.mule.linter.rule.property

import com.avioconsulting.mule.linter.model.Application
import com.avioconsulting.mule.linter.model.Rule
import com.avioconsulting.mule.linter.model.RuleViolation
import spock.lang.Specification

class PropertyFileNamingRuleTest extends Specification {

@SuppressWarnings(['MethodName', 'MethodReturnTypeRequired', 'GStringExpressionWithinString'])
kkingavio marked this conversation as resolved.
Show resolved Hide resolved
def 'Property File Naming Rule check'() {
given:
Rule rule = new PropertyFileNamingRule(['dev', 'test', 'prod'], '${appname}-${env}.properties')

when:
File appDir = new File(this.class.classLoader.getResource(application).file)
Application app = new Application(appDir)
List<RuleViolation> violations = rule.execute(app)

then:
violations.size() == size

where:
application | size
'SampleMuleApp' | 1
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
user=jallen
password=![abcdef==]

db.port = 1521
db.host = localhost
db.user = areed
db.secret = BillsRule!
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
user=jallen
password=![abcdef==]

db.port = 1521
db.host = localhost
db.user = areed
db.secret = BillsRule!
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
user=jallen
password=![abcdef==]

db.port = 1521
db.host = localhost
db.user = areed
db.secret = BillsRule!