Skip to content

Commit

Permalink
Merge pull request #15 from avioconsulting/feature/MIP-395
Browse files Browse the repository at this point in the history
Feature/mip 395
  • Loading branch information
kkingavio authored Jul 9, 2020
2 parents 266a8f0 + a9ccd8e commit 8f1a973
Show file tree
Hide file tree
Showing 25 changed files with 510 additions and 486 deletions.
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,5 @@ dependencies {
test {
useJUnitPlatform()
}

[codenarcMain, codenarcTest]*.ignoreFailures = true
767 changes: 396 additions & 371 deletions codenarc-intellij.xml

Large diffs are not rendered by default.

79 changes: 33 additions & 46 deletions config/codenarc/codenarc.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,51 +4,38 @@
xsi:noNamespaceSchemaLocation="http://codenarc.org/ruleset-schema.xsd">

<description>
Basic rule set.
These rules are intended to be broadly (if not universally) applicable.
Complete rule set, AVIO Inspection.
</description>

<rule class='org.codenarc.rule.basic.AssertWithinFinallyBlockRule'/>
<rule class='org.codenarc.rule.basic.AssignmentInConditionalRule'/>
<rule class='org.codenarc.rule.basic.BigDecimalInstantiationRule'/>
<rule class='org.codenarc.rule.basic.BitwiseOperatorInConditionalRule'/>
<rule class='org.codenarc.rule.basic.BooleanGetBooleanRule'/>
<rule class='org.codenarc.rule.basic.BrokenNullCheckRule'/>
<rule class='org.codenarc.rule.basic.BrokenOddnessCheckRule'/>
<rule class='org.codenarc.rule.basic.ClassForNameRule'/>
<rule class='org.codenarc.rule.basic.ComparisonOfTwoConstantsRule'/>
<rule class='org.codenarc.rule.basic.ComparisonWithSelfRule'/>
<rule class='org.codenarc.rule.basic.ConstantAssertExpressionRule'/>
<rule class='org.codenarc.rule.basic.ConstantTernaryExpressionRule'/>
<rule class='org.codenarc.rule.basic.ConstantIfExpressionRule'/>
<rule class='org.codenarc.rule.basic.DeadCodeRule'/>
<rule class='org.codenarc.rule.basic.DoubleNegativeRule'/>
<rule class='org.codenarc.rule.basic.DuplicateCaseStatementRule'/>
<rule class='org.codenarc.rule.basic.DuplicateMapKeyRule'/>
<rule class='org.codenarc.rule.basic.DuplicateSetValueRule'/>
<rule class='org.codenarc.rule.basic.EmptyCatchBlockRule'/>
<rule class='org.codenarc.rule.basic.EmptyClassRule'/>
<rule class='org.codenarc.rule.basic.EmptyElseBlockRule'/>
<rule class='org.codenarc.rule.basic.EmptyFinallyBlockRule'/>
<rule class='org.codenarc.rule.basic.EmptyForStatementRule'/>
<rule class='org.codenarc.rule.basic.EmptyIfStatementRule'/>
<rule class='org.codenarc.rule.basic.EmptySwitchStatementRule'/>
<rule class='org.codenarc.rule.basic.EmptySynchronizedStatementRule'/>
<rule class='org.codenarc.rule.basic.EmptyStaticInitializerRule'/>
<rule class='org.codenarc.rule.basic.EmptyTryBlockRule'/>
<rule class='org.codenarc.rule.basic.EmptyWhileStatementRule'/>
<rule class='org.codenarc.rule.basic.EmptyInstanceInitializerRule'/>
<rule class='org.codenarc.rule.basic.EmptyMethodRule'/>
<rule class='org.codenarc.rule.basic.EqualsAndHashCodeRule'/>
<rule class='org.codenarc.rule.basic.EqualsOverloadedRule'/>
<rule class='org.codenarc.rule.basic.ExplicitGarbageCollectionRule'/>
<rule class='org.codenarc.rule.basic.ForLoopShouldBeWhileLoopRule'/>
<rule class='org.codenarc.rule.basic.HardCodedWindowsFileSeparatorRule'/>
<rule class='org.codenarc.rule.basic.HardCodedWindowsRootDirectoryRule'/>
<rule class='org.codenarc.rule.basic.IntegerGetIntegerRule'/>
<rule class='org.codenarc.rule.basic.MultipleUnaryOperatorsRule'/>
<rule class='org.codenarc.rule.basic.RandomDoubleCoercedToZeroRule'/>
<rule class='org.codenarc.rule.basic.RemoveAllOnSelfRule'/>
<rule class='org.codenarc.rule.basic.ReturnFromFinallyBlockRule'/>
<rule class='org.codenarc.rule.basic.ThrowExceptionFromFinallyBlockRule'/>

<ruleset-ref path='rulesets/basic.xml'/>
<ruleset-ref path='rulesets/braces.xml'/>
<ruleset-ref path='rulesets/comments.xml'/>
<ruleset-ref path='rulesets/concurrency.xml'/>
<ruleset-ref path='rulesets/convention.xml'>
<exclude name='CompileStatic'/>
<exclude name='TrailingComma'/>
<exclude name='ImplicitClosureParameter'/>
</ruleset-ref>
<ruleset-ref path='rulesets/design.xml'/>
<ruleset-ref path='rulesets/dry.xml'/>
<ruleset-ref path='rulesets/enhanced.xml'/>
<ruleset-ref path='rulesets/exceptions.xml'/>
<ruleset-ref path='rulesets/formatting.xml'/>
<ruleset-ref path='rulesets/generic.xml'/>
<ruleset-ref path='rulesets/grails.xml'/>
<ruleset-ref path='rulesets/groovyism.xml'/>
<ruleset-ref path='rulesets/imports.xml'/>
<ruleset-ref path='rulesets/jdbc.xml'/>
<ruleset-ref path='rulesets/junit.xml'/>
<ruleset-ref path='rulesets/logging.xml'/>
<ruleset-ref path='rulesets/naming.xml'/>
<ruleset-ref path='rulesets/size.xml'/>
<ruleset-ref path='rulesets/security.xml'>
<exclude name='JavaIoPackageAccess'/>
</ruleset-ref>
<ruleset-ref path='rulesets/serialization.xml'/>
<ruleset-ref path='rulesets/unnecessary.xml'>
<exclude name='UnnecessaryReturnKeyword'/>
</ruleset-ref>
<ruleset-ref path='rulesets/unused.xml'/>
</ruleset>
3 changes: 2 additions & 1 deletion src/main/groovy/com/avioconsulting/mule/MuleLinter.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class MuleLinter {
this.app = new Application(new File(applicationDirectory))
}

@SuppressWarnings('UnnecessaryObjectReferences')
void runLinter() {
// Build a list of rules
RuleSet rules = new RuleSet()
Expand All @@ -29,7 +30,7 @@ class MuleLinter {
rules.addRule(new MunitVersionRule('2.2.1'))
rules.addRule(new MuleRuntimeVersionRule('4.2.1'))
rules.addRule(new PropertyFileNamingRule(['dev', 'test']))
rules.addRule(new PropertyFilePropertyCountRule(['test', 'uat'], '${env}.properties'))
rules.addRule(new PropertyFilePropertyCountRule(['uat'], '${env}.properties'))
rules.addRule(new LoggerCategoryExistsRule())
rules.addRule(new LoggerMessageExistsRule())

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,5 @@ class Application {
MuleArtifact getMuleArtifact() {
return muleArtifact
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.avioconsulting.mule.linter.model


import groovy.xml.slurpersupport.GPathResult
import groovy.xml.slurpersupport.Node

Expand All @@ -9,11 +8,12 @@ import groovy.xml.slurpersupport.Node
*/
class ConfigurationFile extends ProjectFile {

private static final String MULE_CORE_NAMESPACE = 'http://www.mulesoft.org/schema/mule/core'
MuleXmlParser parser
private final GPathResult configXml
private final Boolean exists
private Map<String, String> GlobalConfig = ['sub-flow': 'http://www.mulesoft.org/schema/mule/core',
'flow' : 'http://www.mulesoft.org/schema/mule/core']
private Map<String, String> globalConfig = ['sub-flow':MULE_CORE_NAMESPACE,
'flow':MULE_CORE_NAMESPACE]

ConfigurationFile(File file) {
super(file)
Expand All @@ -23,6 +23,7 @@ class ConfigurationFile extends ProjectFile {
configXml = parser.parse(file)
} else {
exists = false
configXml = null
}
}

Expand All @@ -31,22 +32,21 @@ class ConfigurationFile extends ProjectFile {
}

void addAdditionalGlobalConfig(Map<String, String> noneGlobalElements) {
GlobalConfig += noneGlobalElements
globalConfig += noneGlobalElements
}

List<MuleComponent> findGlobalConfigs() {
List<MuleComponent> componentList = []
def childNodes = configXml.childNodes()
def comps = []
childNodes.each {
node ->
if (!checkElementExists( node, GlobalConfig )) {
comps.add(node)
List<Node> childNodes = configXml.childNodes() as List<Node>
List<Node> comps = []
childNodes.each { node ->
if (!checkElementExists(node, globalConfig)) {
comps.add(node)
}
}

comps.each { comp ->
componentList.add(new MuleComponent(comp.name(), comp.namespaceURI(), comp.attributes))
componentList.add(new MuleComponent(comp.name(), comp.namespaceURI(), comp.attributes()))
}
return componentList
}
Expand All @@ -59,7 +59,7 @@ class ConfigurationFile extends ProjectFile {
*/
Boolean checkElementExists(Node node, Map<String, String> elements) {
Map<String, String> found = elements.findAll {
it.key == node.name && it.value == node.namespaceURI()
it.key == node.name() && it.value == node.namespaceURI()
}
return (found.size() > 0)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,5 @@ class GitIgnoreFile extends ProjectFile {
Boolean contains(String expression) {
return expressions.contains(expression)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class LoggerComponent extends MuleComponent {
private final String category

LoggerComponent(Map<String, String> attributes) {
super(attributes)
super(COMPONENT_NAME, COMPONENT_NAMESPACE, attributes)
this.docName = attributes.get('{http://www.mulesoft.org/schema/mule/documentation}name')
this.message = attributes.get('message')
this.level = attributes.get('level')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class MuleArtifact extends ProjectFile {

public static final String MULE_ARTIFACT_JSON = 'mule-artifact.json'

private JsonMap muleArtifact;
private JsonMap muleArtifact
private final Boolean exists

MuleArtifact(File f) {
Expand All @@ -23,11 +23,6 @@ class MuleArtifact extends ProjectFile {
}
}

private void parseMuleArtifact() {
JsonSlurper slurper = new JsonSlurper()
muleArtifact = slurper.parse(file)
}

JsonArray getSecureProperties() {
return muleArtifact.secureProperties
}
Expand All @@ -53,12 +48,17 @@ class MuleArtifact extends ProjectFile {
}

Object getProperty(String propertyName) {
def meta = this.metaClass.getMetaProperty(propertyName)
if(meta) {
MetaProperty meta = this.metaClass.getMetaProperty(propertyName)
if (meta) {
meta.getProperty(this)
} else {
return muleArtifact[propertyName]
}
}

private void parseMuleArtifact() {
JsonSlurper slurper = new JsonSlurper()
muleArtifact = slurper.parse(file) as JsonMap
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@ class MuleComponent {

private final String componentName
private final String componentNamespace
private final Map<String, String> attributes = [:]
private final Map<String, String> attributes
private final List<MuleComponent> children

MuleComponent(String componentName, String componentNamespace, Map<String, String> attributes) {
this.componentName = componentName
this.componentNamespace = componentNamespace
this.attributes = attributes
this(componentName, componentNamespace, attributes, null)
}

MuleComponent(String componentName, String componentNamespace, Map<String, String> attributes,
List<MuleComponent> children) {
this(componentName,componentNamespace,attributes)
this.componentName = componentName
this.componentNamespace = componentNamespace
this.attributes = attributes
this.children = children
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class PomFile extends ProjectFile {
pomXml = parser.parse(file)
} else {
exists = false
pomXml = null
}
}

Expand All @@ -41,7 +42,7 @@ class PomFile extends ProjectFile {
}

PomProperty getPomProperty(String propertyName) throws IllegalArgumentException {
GPathResult p = pomProperties[propertyName]
GPathResult p = pomProperties[propertyName] as GPathResult
if (p == null) {
throw new IllegalArgumentException('Property doesn\'t exist')
}
Expand All @@ -58,7 +59,7 @@ class PomFile extends ProjectFile {
}

private GPathResult getPomProperties() {
return pomXml[PROPERTIES]
return pomXml[PROPERTIES] as GPathResult
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ class RuleSet {

List<Rule> rules = []

RuleSet() {
}

void addRule(Rule rule) {
rules.add(rule)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ import groovy.json.JsonParser;
* @see groovy.json.JsonParserType
* @since 1.8.0
*/
@SuppressWarnings('All')
public class JsonSlurper {

private int maxSizeForInMemory = 2000000;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.avioconsulting.mule.linter.rule.configuration

import com.avioconsulting.mule.linter.model.Application
import com.avioconsulting.mule.linter.model.ConfigurationFile
import com.avioconsulting.mule.linter.model.MuleComponent
import com.avioconsulting.mule.linter.model.Rule
import com.avioconsulting.mule.linter.model.RuleViolation
Expand All @@ -12,9 +11,8 @@ class GlobalConfigRule extends Rule {
static final String RULE_NAME = 'Global mule configuration xml exists and contain required configuration.'
static final String RULE_VIOLATION_MESSAGE = 'Mule configuration xml contain global configuration: '
static final String FILE_MISSING_VIOLATION_MESSAGE = 'Mule global configuration xml does not exist'

static final String DEFAULT_FILE_NAME = 'globals.xml'
static Map<String, String> noneGlobalElements = [:]
static String DEFAULT_FILE_NAME = 'globals.xml'
String globalFileName

GlobalConfigRule(String globalFileName, Map<String, String> noneGlobalElements) {
Expand Down Expand Up @@ -60,4 +58,5 @@ class GlobalConfigRule extends Rule {

return violations
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,5 @@ class GitIgnoreRule extends Rule {

return violations
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.avioconsulting.mule.linter.rule.muleArtifact
package com.avioconsulting.mule.linter.rule.muleartifact

import com.avioconsulting.mule.linter.model.Application
import com.avioconsulting.mule.linter.model.Rule
Expand All @@ -11,7 +11,7 @@ class MuleArtifactHasSecurePropertiesRule extends Rule {
static final String RULE_NAME = 'mule-artifact.json should contain certain secured properties'
static final List<String> DEFAULT_PROPERTIES = ['anypoint.platform.client_id', 'anypoint.platform.client_secret']

private List secureProperties
private final List secureProperties

MuleArtifactHasSecurePropertiesRule() {
this([], true)
Expand All @@ -29,10 +29,11 @@ class MuleArtifactHasSecurePropertiesRule extends Rule {
JsonArray sprops = app.muleArtifact.secureProperties
secureProperties.each { prop ->
if (!sprops.contains(prop)) {
violations.add(new RuleViolation(this, app.muleArtifact.name, sprops.lineNumber, 'The secureProperties array does not contain the property ' + prop))
violations.add(new RuleViolation(this, app.muleArtifact.name, sprops.lineNumber,
'The secureProperties array does not contain the property ' + prop))
}
}
return violations
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class PropertyFilePropertyCountRule extends Rule {
this.pattern = pattern
}

@SuppressWarnings('UnnecessaryGetter')
@Override
List<RuleViolation> execute(Application app) {
List<RuleViolation> violations = []
Expand All @@ -54,11 +55,12 @@ class PropertyFilePropertyCountRule extends Rule {
return validPropertyFilenames
}

@SuppressWarnings('UnnecessaryGetter')
List getValidPropertyFiles(List validPropertyFilenames, List propertyFiles) {
List<PropertyFile> validPropertyFiles = []
propertyFiles.each {
if (it.getName() in validPropertyFilenames) {
validPropertyFiles.add(it)
validPropertyFiles.add(it as PropertyFile)
}
}
return validPropertyFiles
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class JsonArray<T> extends ArrayList {
this.lineNumber = lineNumber;
}

Integer getLineNumber() {
public Integer getLineNumber() {
return lineNumber;
}

Expand Down
Loading

0 comments on commit 8f1a973

Please sign in to comment.