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

Feature/mip 424 #22

Merged
merged 6 commits into from
Jul 13, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class MuleComponent {

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

MuleComponent(String componentName, String componentNamespace, Map<String, String> attributes) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.avioconsulting.mule.linter.rule.configuration

import com.avioconsulting.mule.linter.model.Application
import com.avioconsulting.mule.linter.model.Rule
import com.avioconsulting.mule.linter.model.RuleViolation

class OnErrorLogExceptionRule extends Rule {

static final String RULE_ID = 'ON_ERROR_LOG_EXCEPTION'
static final String RULE_NAME = 'On Error Enable Log Exception Rule'
static final String RULE_VIOLATION_MESSAGE = 'Log exception enabled is required for ' +
'on-error-continue and on-error-propagate'
private static final List<String> ON_ERROR_COMPONENTS = ['on-error-continue', 'on-error-propagate']
private static final String ATTRIBUTE_NAME = 'logException'
private static final String ATTRIBUTE_VALUE_CHECK = 'false'

OnErrorLogExceptionRule() {
this.ruleId = RULE_ID
this.ruleName = RULE_NAME
}

@Override
List<RuleViolation> execute(Application application) {
List<RuleViolation> violations = []
application.configurationFiles.each { file ->
ON_ERROR_COMPONENTS.each {
file.findComponents(it, file.MULE_CORE_NAMESPACE).each { comp ->
if (comp.getAttributeValue(ATTRIBUTE_NAME).equalsIgnoreCase(ATTRIBUTE_VALUE_CHECK)) {
violations.add(new RuleViolation(this, file.name, comp.lineNumber, RULE_VIOLATION_MESSAGE))
}
}
}
}
return violations
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,15 @@ class GlobalConfigRuleTest extends Specification {

then:
violations.size() == 2
violations[0].lineNumber == 9
violations[0].message.contains('config')
violations[0].fileName.contains('bad-global-with-router.xml')
violations[1].lineNumber == 10
violations[1].message.contains('listener-config')
violations[1].fileName.contains('bad-global-with-listener.xml')
RuleViolation rv = violations.find { it.fileName.contains('bad-global-with-router.xml')}
rv != null
rv.lineNumber == 9
rv.message.contains('config')

RuleViolation rv2 = violations.find { it.fileName.contains('bad-global-with-listener.xml')}
rv2 != null
rv2.lineNumber == 10
rv2.message.contains('listener-config')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this.

}

def 'Additional global configuration element check'() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
package com.avioconsulting.mule.linter.rule.configuration

import com.avioconsulting.mule.linter.TestApplication
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


@SuppressWarnings(['MethodName', 'MethodReturnTypeRequired', 'StaticFieldsBeforeInstanceFields'])
class OnErrorLogExceptionRuleTest extends Specification {

private final TestApplication testApp = new TestApplication()
private Application app

def setup() {
testApp.create()
testApp.addPom()
testApp.addConfig()
}

def cleanup() {
testApp.remove()
}

def 'Flows are correct'() {
given:
Rule rule = new OnErrorLogExceptionRule()

when:
app = new Application(testApp.appDir)
List<RuleViolation> violations = rule.execute(app)

then:
app.configurationFiles.size() == 3
violations.size() == 0
}

def 'on-error-continue and -propagate invalid'() {
given:
Rule rule = new OnErrorLogExceptionRule()

when:
testApp.addFile('src/main/mule/on-error-logging-exception.xml', BAD_CONFIG)
app = new Application(testApp.appDir)
List<RuleViolation> violations = rule.execute(app)

then:
app.configurationFiles.size() == 4
violations.size() == 2
violations[0].lineNumber == 23
violations[0].fileName.contains('on-error-logging-exception.xml')
violations[0].message.contains(OnErrorLogExceptionRule.RULE_VIOLATION_MESSAGE)
violations[1].lineNumber == 69
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we add check for message here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like-> OnErrorLogExceptionRule.RULE_VIOLATION_MESSAGE

}

private static final String BAD_CONFIG = '''<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core"
\t\txmlns="http://www.mulesoft.org/schema/mule/core"
\t\txmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
\t\txmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
\t\txsi:schemaLocation="http://www.mulesoft.org/schema/mule/core
\t\t\thttp://www.mulesoft.org/schema/mule/core/current/mule.xsd
\t\t\thttp://www.mulesoft.org/schema/mule/ee/core
\t\t\thttp://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd">
\t<sub-flow name="bad-sub-flow">
\t\t<try doc:name="Try" doc:id="246a02ea-45b3-4796-b41d-a57c0384bf7b" >
\t\t\t<ee:transform doc:name="Simple Transform">
\t\t\t<ee:message>
\t\t\t\t<ee:set-payload><![CDATA[%dw 2.0
output application/java
---
{
}]]></ee:set-payload>
\t\t\t</ee:message>
\t\t</ee:transform>
\t\t\t<error-handler >
\t\t\t\t<on-error-continue enableNotifications="true" logException="false"
\t\t\t\t\t\tdoc:name="Bad On Error Continue" >
\t\t\t\t\t<logger level="ERROR" doc:name="Log Error"
\t\t\t\t\t\t\tmessage='An Error Occured... Continuing'
\t\t\t\t\t\t\tcategory="com.avioconsulting.mulelinter"/>
\t\t\t\t</on-error-continue>
\t\t\t</error-handler>
\t\t</try>
\t\t<try doc:name="Try" >
\t\t\t<logger level="DEBUG" doc:name="Log End" message="Ending"
\t\t\t\t\tcategory="com.avioconsulting.mulelinter" />
\t\t\t<error-handler >
\t\t\t\t<on-error-propagate enableNotifications="true"
\t\t\t\t\t\tlogException="true" doc:name="On Error Propagate"
\t\t\t\t\t\tdoc:id="c59d99e4-e679-4f4f-aa10-02541a3428d2" >
\t\t\t\t\t<logger level="ERROR" doc:name="Log Another Error"
\t\t\t\t\t\tmessage='An Error Occured... Propagating'
\t\t\t\t\t\tcategory="com.avioconsulting.mulelinter" />
\t\t\t\t</on-error-propagate>
\t\t\t</error-handler>
\t\t</try>
\t</sub-flow>
\t<sub-flow name="bad-sub-flow-2">
\t\t<try doc:name="Try">
\t\t\t<ee:transform doc:name="Simple Transform">
\t\t\t<ee:message>
\t\t\t\t<ee:set-payload><![CDATA[%dw 2.0
output application/java
---
{
}]]></ee:set-payload>
\t\t\t</ee:message>
\t\t</ee:transform>
\t\t\t<error-handler >
\t\t\t\t<on-error-continue enableNotifications="true" logException="true"
\t\t\t\t\t\tdoc:name="On Error Continue" doc:id="28755785-b5a0-4403-ae4e-71223ac59a78" >
\t\t\t\t\t<logger level="ERROR" doc:name="Log Error"
\t\t\t\t\t\t\tmessage='An Error Occured... Continuing'
\t\t\t\t\t\t\tcategory="com.avioconsulting.mulelinter"/>
\t\t\t\t</on-error-continue>
\t\t\t</error-handler>
\t\t</try>
\t\t<try doc:name="Try">
\t\t\t<logger level="DEBUG" doc:name="Log End" message="Ending"
\t\t\t\t\tcategory="com.avioconsulting.mulelinter" />
\t\t\t<error-handler >
\t\t\t\t<on-error-propagate enableNotifications="true" logException="false"
\t\t\t\t\t\tdoc:name="Bad On Error Propagate" doc:id="dccce6bd-82dd-4223-a0cc-01bbe3f892cf" >
\t\t\t\t\t<logger level="ERROR" doc:name="Log Another Error"
\t\t\t\t\t\tmessage='An Error Occured... Propagating'
\t\t\t\t\t\tcategory="com.avioconsulting.mulelinter" />
\t\t\t\t</on-error-propagate>
\t\t\t</error-handler>
\t\t</try>
\t</sub-flow>
</mule>
'''
}
2 changes: 1 addition & 1 deletion src/test/resources/SampleMuleApp/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<version>1.0.0</version>
<packaging>mule-application</packaging>

<name>np-store-product-sys-api</name>
<name>sample-mule-app-sys-api</name>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down
9 changes: 0 additions & 9 deletions src/test/resources/SampleMuleApp/pom_bak.xml

This file was deleted.

25 changes: 18 additions & 7 deletions src/test/resources/SampleMuleApp/src/main/mule/business-logic.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,30 @@
http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd">
<sub-flow name="a-sub-flow">
<logger level="DEBUG" doc:name="Log Start" message="Starting" category="com.avioconsulting.mulelinter"/>
<ee:transform doc:name="Simple Transform">
<ee:message >
<ee:set-payload ><![CDATA[%dw 2.0
<try doc:name="Try" doc:id="f25ff1ca-a2bc-4e75-82e4-f60098e46953" >
<ee:transform doc:name="Simple Transform">
<ee:message>
<ee:set-payload><![CDATA[%dw 2.0
output application/java
---
{
}]]></ee:set-payload>
</ee:message>
</ee:transform>
<logger level="DEBUG"
doc:name="Log End"
message="Ending"
category="com.avioconsulting.mulelinter" />
<error-handler >
<on-error-continue enableNotifications="true" logException="true" doc:name="On Error Continue" doc:id="9ea642a7-c9f5-4c34-a147-2ed27034dffd" >
<logger level="ERROR" doc:name="Log Error" doc:id="8844c569-6f99-4e86-be18-e8c6d678dca6" message='"An Error Occured... Continuing"' category="com.avioconsulting.mulelinter"/>
</on-error-continue>
</error-handler>
</try>
<try doc:name="Try" doc:id="0263f77e-147f-407e-9bc6-7a263e54a3f7" >
<logger level="DEBUG" doc:name="Log End" message="Ending" category="com.avioconsulting.mulelinter" />
<error-handler >
<on-error-propagate enableNotifications="true" logException="true" doc:name="On Error Propagate" doc:id="be8f2b02-566b-420b-a473-de408585a1a5" >
<logger level="ERROR" doc:name="Log Another Error" doc:id="9db68cf6-2cda-41b1-8a4a-d4c574ca41c4" message='"An Error Occured... Continuing"' category="com.avioconsulting.mulelinter" />
</on-error-propagate>
</error-handler>
</try>
</sub-flow>
<sub-flow name="b-sub-flow">
<logger level="WARN"
Expand Down