Skip to content

Commit

Permalink
Fix for issue #564 (#614)
Browse files Browse the repository at this point in the history
  • Loading branch information
jesica-fera authored Apr 6, 2022
1 parent 95e5eb5 commit a1b7dcb
Show file tree
Hide file tree
Showing 16 changed files with 389 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright (c) 2020, Mulesoft, LLC. All rights reserved.
* Use of this source code is governed by a BSD 3-Clause License
* license that can be found in the LICENSE.txt file.
*/
package com.mulesoft.tools.migration.step;

import com.mulesoft.tools.migration.step.category.MigrationReport;
import com.mulesoft.tools.migration.step.util.XmlDslUtils;
import org.jdom2.Element;
import org.jdom2.Namespace;

import java.util.List;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.collect.Lists.newArrayList;

/**
* Takes unsupported or blocked elements and comments them, mark them as failed
*
* @author Mulesoft Inc.
* @since 1.0.0
*/
public abstract class AbstractUnsupportedElementsMigrationStep extends AbstractApplicationModelMigrationStep {

public static final String COMPONENTS_UNSUPPORTED_ERROR_KEY = "components.unsupported";
public static final String NAME_ATTRIBUTE_TEMPLATE = "(name = %s)";

public AbstractUnsupportedElementsMigrationStep(Namespace namespace) {
checkArgument(getUnsupportedElements() != null, "The unsupported elements list must not be null.");
this.setAppliedTo(XmlDslUtils.getAllElementsFromNamespaceXpathSelector(namespace.getURI(), getUnsupportedElements(), false));
this.setNamespacesContributions(newArrayList(namespace));
}

public abstract List<String> getUnsupportedElements();

@Override
public void execute(Element node, MigrationReport report) throws RuntimeException {
if (getUnsupportedElements().contains(node.getName())) {
report.report(COMPONENTS_UNSUPPORTED_ERROR_KEY, node, node,
report.getComponentKey(node) + " " + String.format(NAME_ATTRIBUTE_TEMPLATE, node.getAttributeValue("name")));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ public enum Level {
*/
Map<String, ComponentMigrationStatus> getComponents();

String getComponentKey(Element element);

/**
* Increments the component success migration count.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import org.apache.commons.io.FileUtils;
import org.jdom2.Attribute;
Expand Down Expand Up @@ -738,6 +739,20 @@ public static String getXPathSelector(String namespaceUri, String elementName, b
return format("%s[namespace-uri() = '%s' and local-name() = '%s']", topLevel ? "/*/*" : "//*", namespaceUri, elementName);
}

/**
* Get Xpath expression to select all elements from a given namespace on the configuration file
*
* @param namespaceUri the namespace URI
* @param topLevel is a top level element
* @return a String with the expression
*/
public static String getAllElementsFromNamespaceXpathSelector(String namespaceUri, List<String> elements, boolean topLevel) {
String localNameExpression = elements.stream()
.map(e -> String.format("local-name() = '%s'", e))
.collect(Collectors.joining(" or "));
return format("%s[namespace-uri() = '%s' and (%s)]", topLevel ? "/*/*" : "//*", namespaceUri, localNameExpression);
}

/**
* Get Xpath expression to select Mule core elements on the configuration file
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Copyright (c) 2020, Mulesoft, LLC. All rights reserved.
* Use of this source code is governed by a BSD 3-Clause License
* license that can be found in the LICENSE.txt file.
*/
package com.mulesoft.tools.migration.step;

import com.google.common.collect.ImmutableList;
import com.mulesoft.tools.migration.step.category.MigrationReport;
import org.jdom2.Element;
import org.jdom2.Namespace;
import org.junit.Before;
import org.junit.Test;

import java.util.List;

import static com.mulesoft.tools.migration.step.AbstractUnsupportedElementsMigrationStep.COMPONENTS_UNSUPPORTED_ERROR_KEY;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.ArgumentMatchers.isA;
import static org.mockito.Mockito.*;
import static org.powermock.api.mockito.PowerMockito.when;

/**
* @author Mulesoft Inc.
*/
public class AbstractUnsupportedElementsMigrationStepTest {

public static final Namespace TEST_NAMESPACE = Namespace.getNamespace("test", "http://test.com");

private AbstractUnsupportedElementsMigrationStep migrationStep;
private Element elementMock;
private Element parentElementMock;
private MigrationReport reportMock;
private static final String BLOCKED_ELEMENT_NAME = "blocked";

@Before
public void setUp() throws Exception {
migrationStep = new AbstractUnsupportedElementsMigrationStepTest.MigrationStepImpl();
elementMock = mock(Element.class);
parentElementMock = mock(Element.class);
when(elementMock.getName()).thenReturn(BLOCKED_ELEMENT_NAME);
when(elementMock.getParentElement()).thenReturn(parentElementMock);
when(elementMock.getNamespace()).thenReturn(TEST_NAMESPACE);
reportMock = mock(MigrationReport.class);
when(reportMock.getComponentKey(elementMock)).thenReturn(BLOCKED_ELEMENT_NAME);
}

@Test
public void testExecute_handleUnsupported() {
migrationStep.execute(elementMock, reportMock);
verify(reportMock).report(eq(COMPONENTS_UNSUPPORTED_ERROR_KEY), eq(elementMock), eq(elementMock), isA(String.class));
}

@Test
public void testExecute_handleAllSupported() {
when(elementMock.getName()).thenReturn("another_supported_element");
migrationStep.execute(elementMock, reportMock);
verifyZeroInteractions(reportMock);
}

@Test(expected = IllegalArgumentException.class)
public void testExecute_NullUnsupportedList() {
new AbstractUnsupportedElementsMigrationStep(TEST_NAMESPACE) {

@Override
public List<String> getUnsupportedElements() {
return null;
}
}.execute(elementMock, reportMock);
}

private static final class MigrationStepImpl extends AbstractUnsupportedElementsMigrationStep {

public MigrationStepImpl() {
super(TEST_NAMESPACE);
}

@Override
public List<String> getUnsupportedElements() {
return ImmutableList.of(BLOCKED_ELEMENT_NAME);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ public class MigrationGapsTestCase extends AbstractEndToEndTestCase {
public static Object[] params() {
return new Object[] {
"gaps/unknown_element",
"gaps/unknown_namespace"
"gaps/unknown_namespace",
"gaps/salesforce_config_oauth_user_pass"
};
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns="http://www.mulesoft.org/schema/mule/core"
xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:sfdc="http://www.mulesoft.org/schema/mule/sfdc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd">
<sfdc:config-oauth-user-pass name="Salesforce__Basic_Authentication"
consumerKey="${cbms.sfdc.consumerkey}" consumerSecret="${cbms.sfdc.consumersecret}"
username="${cbms.sfdc.username}" password="${cbms.sfdc.password}"
securityToken="${cbms.sfdc.securitytoken}" tokenEndpoint="${cbms.sfdc.authurl}"
doc:name="ct_salesforce" readTimeout="${cbms.sfdc.read.timeout}"
disableSessionInvalidation="true">
<reconnect count="${cbms.sfdc.reconnect.attemptcount}"
frequency="${cbms.sfdc.reconnect.frequency}" />
</sfdc:config-oauth-user-pass>

<sfdc:config-oauth-user-pass name="Salesforce__Basic_Authentication2"
consumerKey="${cbms.sfdc.consumerkey}" consumerSecret="${cbms.sfdc.consumersecret}"
username="${cbms.sfdc.username}" password="${cbms.sfdc.password}"
securityToken="${cbms.sfdc.securitytoken}" tokenEndpoint="${cbms.sfdc.authurl}"
doc:name="ct_salesforce" readTimeout="${cbms.sfdc.read.timeout}"
disableSessionInvalidation="true">
<reconnect count="${cbms.sfdc.reconnect.attemptcount}"
frequency="${cbms.sfdc.reconnect.frequency}" />
</sfdc:config-oauth-user-pass>
</mule>
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>org.mule.migrated</groupId>
<artifactId>salesforce_config_oauth_user_pass</artifactId>
<version>1.0.0-M4-SNAPSHOT</version>
<packaging>mule-application</packaging>
<description>Application migrated with MMA</description>
<dependencies>
<dependency>
<groupId>com.mulesoft.mule.modules</groupId>
<artifactId>mule-compatibility-module</artifactId>
<version>1.4.0</version>
<classifier>mule-plugin</classifier>
</dependency>
<dependency>
<groupId>com.mulesoft.connectors</groupId>
<artifactId>mule-salesforce-connector</artifactId>
<version>10.14.0</version>
<classifier>mule-plugin</classifier>
</dependency>
</dependencies>
<repositories>
<repository>
<id>mulesoft-releases</id>
<name>MuleSoft Releases Repository</name>
<url>https://repository.mulesoft.org/releases/</url>
</repository>
<repository>
<id>anypoint-exchange</id>
<name>Anypoint Exchange</name>
<url>https://maven.anypoint.mulesoft.com/api/v1/maven</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<snapshots>
<enabled>true</enabled>
</snapshots>
<id>mulesoft-releases</id>
<name>MuleSoft Releases Repository</name>
<url>https://repository.mulesoft.org/releases/</url>
</pluginRepository>
</pluginRepositories>
<build>
<plugins>
<plugin>
<groupId>org.mule.tools.maven</groupId>
<artifactId>mule-maven-plugin</artifactId>
<version>3.2.1</version>
<extensions>true</extensions>
<configuration />
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
{
"projectType": "MULE_THREE_APPLICATION",
"projectName": "input",
"connectorsMigrated":
[
"com.mulesoft.connectors:mule-salesforce-connector:VERSION"
],
"numberOfMuleComponents": 3,
"numberOfMuleComponentsMigrated": 1,
"componentDetails":
{
"mule":
{
"success": 1,
"failure": 0
},
"sfdc:config-oauth-user-pass":
{
"success": 0,
"failure": 2
}
},
"numberOfMELExpressions": 0,
"numberOfMELExpressionsMigrated": 0,
"numberOfMELExpressionLines": 0,
"numberOfMELExpressionLinesMigrated": 0,
"numberOfDWTransformations": 0,
"numberOfDWTransformationsMigrated": 0,
"numberOfDWTransformationLines": 0,
"numberOfDWTransformationLinesMigrated": 0,
"detailedMessages":
[
{
"level": "ERROR",
"key": "components.unsupported",
"component": "sfdc:config-oauth-user-pass",
"lineNumber": 4,
"columnNumber": 403,
"message": "The migration of 'sfdc:config-oauth-user-pass (name = Salesforce__Basic_Authentication)' is not supported.",
"filePath": "src/main/mule/mule-config.xml",
"documentationLinks":
[]
},
{
"level": "ERROR",
"key": "components.unsupported",
"component": "sfdc:config-oauth-user-pass",
"lineNumber": 12,
"columnNumber": 404,
"message": "The migration of 'sfdc:config-oauth-user-pass (name = Salesforce__Basic_Authentication2)' is not supported.",
"filePath": "src/main/mule/mule-config.xml",
"documentationLinks":
[]
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd">

<!--Migration ERROR: The migration of 'sfdc:config-oauth-user-pass' (name Salesforce__Basic_Authentication) is not supported.-->
<!-- For more information refer to:-->
<!-- * https://docs.mulesoft.com/mule-runtime/4.3/migration-connectors-->
<!-- * https://github.com/mulesoft/mule-migration-assistant/blob/master/docs/user-docs/migration-tool.adoc#unsupported_connectors-->
<sfdc:config-oauth-user-pass xmlns:sfdc="http://www.mulesoft.org/schema/mule/sfdc" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" name="Salesforce__Basic_Authentication" consumerKey="${cbms.sfdc.consumerkey}" consumerSecret="${cbms.sfdc.consumersecret}" username="${cbms.sfdc.username}" password="${cbms.sfdc.password}" securityToken="${cbms.sfdc.securitytoken}" tokenEndpoint="${cbms.sfdc.authurl}" doc:name="ct_salesforce" readTimeout="${cbms.sfdc.read.timeout}" disableSessionInvalidation="true">
<reconnect xmlns="http://www.mulesoft.org/schema/mule/core" count="${cbms.sfdc.reconnect.attemptcount}" frequency="${cbms.sfdc.reconnect.frequency}" />
</sfdc:config-oauth-user-pass>

<!--Migration ERROR: The migration of 'sfdc:config-oauth-user-pass' (name Salesforce__Basic_Authentication2) is not supported.-->
<!-- For more information refer to:-->
<!-- * https://docs.mulesoft.com/mule-runtime/4.3/migration-connectors-->
<!-- * https://github.com/mulesoft/mule-migration-assistant/blob/master/docs/user-docs/migration-tool.adoc#unsupported_connectors-->
<sfdc:config-oauth-user-pass xmlns:sfdc="http://www.mulesoft.org/schema/mule/sfdc" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" name="Salesforce__Basic_Authentication2" consumerKey="${cbms.sfdc.consumerkey}" consumerSecret="${cbms.sfdc.consumersecret}" username="${cbms.sfdc.username}" password="${cbms.sfdc.password}" securityToken="${cbms.sfdc.securitytoken}" tokenEndpoint="${cbms.sfdc.authurl}" doc:name="ct_salesforce" readTimeout="${cbms.sfdc.read.timeout}" disableSessionInvalidation="true">
<reconnect xmlns="http://www.mulesoft.org/schema/mule/core" count="${cbms.sfdc.reconnect.attemptcount}" frequency="${cbms.sfdc.reconnect.frequency}" />
</sfdc:config-oauth-user-pass>
</mule>
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@
"numberOfDWTransformationsMigrated": 0,
"numberOfDWTransformationLines": 0,
"numberOfDWTransformationLinesMigrated": 0,
"detailedMessages": []
}
"detailedMessages": [
]
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd">
<mule xmlns="http://www.mulesoft.org/schema/mule/core"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd">

<flow name="unknown-elements">
<unknown-source>
<logger message="nested" />
<unknown-nested />
<logger message="nested"/>
<unknown-nested/>
</unknown-source>

<logger message="bye" />

<unknown-operation />

<logger message="bye"/>
<unknown-operation/>
</flow>

</mule>
Loading

0 comments on commit a1b7dcb

Please sign in to comment.