Search in sources :

Example 1 with ApplicationModelContribution

use of com.mulesoft.tools.migration.step.category.ApplicationModelContribution in project mule-migration-assistant by mulesoft.

the class AbstractMigrationTaskTest method shouldExecuteAllStepsAppModelContributionAndElementPresent.

@Test
public void shouldExecuteAllStepsAppModelContributionAndElementPresent() {
    doReturn(newArrayList(mock(Element.class))).when(applicationModelMock).getNodes(any(XPathExpression.class));
    MigrationStepSelector selectorMock = mock(MigrationStepSelector.class);
    ApplicationModelContribution contrib = mock(ApplicationModelContribution.class);
    doReturn(mock(XPathExpression.class)).when(contrib).getAppliedTo();
    doReturn(newArrayList(contrib)).when(selectorMock).getApplicationModelContributionSteps();
    AbstractMigrationTask taskSpy = spy(AbstractMigrationTask.class);
    taskSpy.setApplicationModel(applicationModelMock);
    assertThat("It should return true", taskSpy.shouldExecuteAllSteps(selectorMock));
}
Also used : XPathExpression(org.jdom2.xpath.XPathExpression) ApplicationModelContribution(com.mulesoft.tools.migration.step.category.ApplicationModelContribution) Test(org.junit.Test)

Example 2 with ApplicationModelContribution

use of com.mulesoft.tools.migration.step.category.ApplicationModelContribution in project mule-migration-assistant by mulesoft.

the class AbstractMigrationTaskTest method execute.

@Test
public void execute() throws Exception {
    NamespaceContribution namespaceContributionMock = mock(NamespaceContribution.class);
    ApplicationModelContribution applicationModelContributionMock = mock(ApplicationModelContribution.class);
    ProjectStructureContribution projectStructureContributionMock = mock(ProjectStructureContribution.class);
    PomContribution pomContributionMock = mock(PomContribution.class);
    List<MigrationStep> steps = new ArrayList<>();
    steps.add(namespaceContributionMock);
    steps.add(applicationModelContributionMock);
    steps.add(projectStructureContributionMock);
    steps.add(pomContributionMock);
    InOrder inOrder = Mockito.inOrder(steps.toArray());
    migrationTask.setApplicationModel(applicationModelMock);
    ((MigrationTaskImpl) migrationTask).setMigrationSteps(new ArrayList<>(steps));
    migrationTask.execute(mock(MigrationReport.class));
    verify(namespaceContributionMock, times(1)).execute(any(ApplicationModel.class), any(MigrationReport.class));
    verify(applicationModelContributionMock, times(2)).getAppliedTo();
    verify(projectStructureContributionMock, times(1)).execute(isNull(), any(MigrationReport.class));
    verify(pomContributionMock, times(1)).execute(any(PomModel.class), any(MigrationReport.class));
    inOrder.verify(namespaceContributionMock).execute(any(ApplicationModel.class), any(MigrationReport.class));
    inOrder.verify(projectStructureContributionMock).execute(isNull(), any(MigrationReport.class));
    inOrder.verify(pomContributionMock).execute(any(PomModel.class), any(MigrationReport.class));
}
Also used : InOrder(org.mockito.InOrder) NamespaceContribution(com.mulesoft.tools.migration.step.category.NamespaceContribution) ArrayList(java.util.ArrayList) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) MigrationReport(com.mulesoft.tools.migration.step.category.MigrationReport) ApplicationModel(com.mulesoft.tools.migration.project.model.ApplicationModel) PomContribution(com.mulesoft.tools.migration.step.category.PomContribution) ProjectStructureContribution(com.mulesoft.tools.migration.step.category.ProjectStructureContribution) ApplicationModelContribution(com.mulesoft.tools.migration.step.category.ApplicationModelContribution) MigrationStep(com.mulesoft.tools.migration.step.MigrationStep) PomModel(com.mulesoft.tools.migration.project.model.pom.PomModel) Test(org.junit.Test)

Example 3 with ApplicationModelContribution

use of com.mulesoft.tools.migration.step.category.ApplicationModelContribution in project mule-migration-assistant by mulesoft.

the class AbstractMigrationTask method fetchAndProcessNodes.

private void fetchAndProcessNodes(MigrationReport report, ApplicationModelContribution s, List<Element> alreadyProcessed) {
    AtomicInteger processedElements = new AtomicInteger(0);
    List<Element> nodes = applicationModel.getNodes(s.getAppliedTo());
    nodes.stream().filter(n -> !alreadyProcessed.contains(n)).forEach(n -> {
        try {
            processedElements.incrementAndGet();
            s.execute(n, report);
        } catch (Exception e) {
            throw new MigrationStepException("Task execution exception (" + e.getMessage() + ") migrating node:" + lineSeparator() + outp.outputString(n), e);
        }
    });
    alreadyProcessed.addAll(nodes);
    nodes.removeAll(applicationModel.getNodes(s.getAppliedTo()));
    if (!nodes.isEmpty()) {
        // This recursive calls is here so if any task adds nodes to the config that would be processed by this task, those are
        // processed.
        // Also, this is recursive rather than iterative so in the case of a bug, we get a StackOverflow rather than an infinite
        // loop.
        fetchAndProcessNodes(report, s, alreadyProcessed);
    }
    report.addProcessedElements(processedElements.get());
}
Also used : System.lineSeparator(java.lang.System.lineSeparator) MigrationReport(com.mulesoft.tools.migration.step.category.MigrationReport) Collectors(java.util.stream.Collectors) ApplicationModel(com.mulesoft.tools.migration.project.model.ApplicationModel) Preconditions.checkState(com.google.common.base.Preconditions.checkState) XMLOutputter(org.jdom2.output.XMLOutputter) ArrayList(java.util.ArrayList) ExpressionMigrator(com.mulesoft.tools.migration.util.ExpressionMigrator) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) MigrationStep(com.mulesoft.tools.migration.step.MigrationStep) List(java.util.List) MigrationTaskException(com.mulesoft.tools.migration.exception.MigrationTaskException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExpressionMigratorAware(com.mulesoft.tools.migration.step.ExpressionMigratorAware) MigrationStepException(com.mulesoft.tools.migration.exception.MigrationStepException) MigrationAbortException(com.mulesoft.tools.migration.exception.MigrationAbortException) ReportingStep(com.mulesoft.tools.migration.step.ReportingStep) PomModel(com.mulesoft.tools.migration.project.model.pom.PomModel) ApplicationModelContribution(com.mulesoft.tools.migration.step.category.ApplicationModelContribution) Element(org.jdom2.Element) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Element(org.jdom2.Element) MigrationStepException(com.mulesoft.tools.migration.exception.MigrationStepException) MigrationTaskException(com.mulesoft.tools.migration.exception.MigrationTaskException) MigrationStepException(com.mulesoft.tools.migration.exception.MigrationStepException) MigrationAbortException(com.mulesoft.tools.migration.exception.MigrationAbortException)

Example 4 with ApplicationModelContribution

use of com.mulesoft.tools.migration.step.category.ApplicationModelContribution in project mule-migration-assistant by mulesoft.

the class DomainTest method execute.

@Test
public void execute() throws Exception {
    for (MigrationStep domainStep : domainSteps) {
        if (domainStep instanceof ApplicationModelContribution) {
            getElementsFromDocument(domainDoc, ((ApplicationModelContribution) domainStep).getAppliedTo().getExpression(), "domain").forEach(node -> domainStep.execute(node, report.getReport()));
        }
    }
    for (MigrationStep appStep : appSteps) {
        if (appStep instanceof ApplicationModelContribution) {
            getElementsFromDocument(originalDomainDoc, ((ApplicationModelContribution) appStep).getAppliedTo().getExpression(), "domain").forEach(node -> appStep.execute(node, report.getReport()));
            getElementsFromDocument(appDoc, ((ApplicationModelContribution) appStep).getAppliedTo().getExpression()).forEach(node -> appStep.execute(node, report.getReport()));
        }
    }
    XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
    String domainXmlString = outputter.outputString(domainDoc);
    String appXmlString = outputter.outputString(appDoc);
    assertThat(domainXmlString, isSimilarTo(IOUtils.toString(this.getClass().getClassLoader().getResource(domainTargetPath.toString()).toURI(), UTF_8)).ignoreComments().normalizeWhitespace());
    assertThat(appXmlString, isSimilarTo(IOUtils.toString(this.getClass().getClassLoader().getResource(appTargetPath.toString()).toURI(), UTF_8)).ignoreComments().normalizeWhitespace());
}
Also used : XMLOutputter(org.jdom2.output.XMLOutputter) ApplicationModelContribution(com.mulesoft.tools.migration.step.category.ApplicationModelContribution) MigrationStep(com.mulesoft.tools.migration.step.MigrationStep) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.Test)

Example 5 with ApplicationModelContribution

use of com.mulesoft.tools.migration.step.category.ApplicationModelContribution in project mule-migration-assistant by mulesoft.

the class DomainTest method setUp.

@Before
public void setUp() throws Exception {
    originalDomainDoc = getDocument(this.getClass().getClassLoader().getResource(domainConfigPath.toString()).toURI().getPath());
    domainDoc = getDocument(this.getClass().getClassLoader().getResource(domainConfigPath.toString()).toURI().getPath());
    appDoc = getDocument(this.getClass().getClassLoader().getResource(appConfigPath.toString()).toURI().getPath());
    MelToDwExpressionMigrator expressionMigrator = new MelToDwExpressionMigrator(report.getReport(), mock(ApplicationModel.class));
    domainModel = mock(ApplicationModel.class);
    when(domainModel.getNodes(any(String.class))).thenAnswer(invocation -> getElementsFromDocument(domainDoc, (String) invocation.getArguments()[0], "domain"));
    when(domainModel.getNode(any(String.class))).thenAnswer(invocation -> getElementsFromDocument(domainDoc, (String) invocation.getArguments()[0], "domain").iterator().next());
    when(domainModel.getNodeOptional(any(String.class))).thenAnswer(invocation -> {
        List<Element> elementsFromDocument = getElementsFromDocument(domainDoc, (String) invocation.getArguments()[0], "domain");
        if (elementsFromDocument.isEmpty()) {
            return empty();
        } else {
            return of(elementsFromDocument.iterator().next());
        }
    });
    when(domainModel.getProjectBasePath()).thenReturn(temp.newFolder().toPath());
    when(domainModel.getPomModel()).thenReturn(of(mock(PomModel.class)));
    final Map<Path, Document> domainModelDocs = new HashMap<>();
    domainModelDocs.put(domainConfigPath, domainDoc);
    when(domainModel.getApplicationDocuments()).thenReturn(domainModelDocs);
    doAnswer(invocation -> {
        String prefix = invocation.getArgument(0);
        String uri = invocation.getArgument(1);
        String schemaLocation = invocation.getArgument(2);
        Namespace namespace = Namespace.getNamespace(prefix, uri);
        addNameSpace(namespace, schemaLocation, domainDoc);
        return null;
    }).when(domainModel).addNameSpace(anyString(), anyString(), anyString());
    appModel = mock(ApplicationModel.class);
    when(appModel.getNodes(any(String.class))).thenAnswer(invocation -> getElementsFromDocuments((String) invocation.getArguments()[0]));
    when(appModel.getNode(any(String.class))).thenAnswer(invocation -> getElementsFromDocuments((String) invocation.getArguments()[0]).iterator().next());
    when(appModel.getNodeOptional(any(String.class))).thenAnswer(invocation -> {
        List<Element> elementsFromDocument = getElementsFromDocuments((String) invocation.getArguments()[0]);
        if (elementsFromDocument.isEmpty()) {
            return empty();
        } else {
            return of(elementsFromDocument.iterator().next());
        }
    });
    when(appModel.getProjectBasePath()).thenReturn(temp.newFolder().toPath());
    when(appModel.getPomModel()).thenReturn(of(mock(PomModel.class)));
    final Map<Path, Document> appDomainModelDocs = new HashMap<>();
    appDomainModelDocs.put(domainConfigPath, originalDomainDoc);
    when(appModel.getDomainDocuments()).thenReturn(appDomainModelDocs);
    doAnswer(invocation -> {
        String prefix = invocation.getArgument(0);
        String uri = invocation.getArgument(1);
        String schemaLocation = invocation.getArgument(2);
        Namespace namespace = Namespace.getNamespace(prefix, uri);
        addNameSpace(namespace, schemaLocation, originalDomainDoc);
        addNameSpace(namespace, schemaLocation, appDoc);
        return null;
    }).when(appModel).addNameSpace(anyString(), anyString(), anyString());
    List<AbstractMigrationTask> coreMigrationTasks = new ArrayList<>();
    coreMigrationTasks.add(new MuleCoreComponentsMigrationTask());
    coreMigrationTasks.add(new HTTPMigrationTask());
    coreMigrationTasks.add(new DbMigrationTask());
    coreMigrationTasks.add(new EndpointsMigrationTask());
    coreMigrationTasks.add(new JmsDomainMigrationTask());
    coreMigrationTasks.add(new JmsMigrationTask());
    coreMigrationTasks.add(new DomainAppMigrationTask());
    coreMigrationTasks.add(new SpringMigrationTask());
    coreMigrationTasks.add(new HTTPCleanupTask());
    coreMigrationTasks.add(new MigrationCleanTask());
    coreMigrationTasks.add(new PostprocessGeneral());
    coreMigrationTasks.add(new PostprocessMuleApplication());
    appSteps = new ArrayList<>();
    domainSteps = new ArrayList<>();
    for (AbstractMigrationTask task : coreMigrationTasks) {
        if (task.getApplicableProjectTypes().contains(MULE_FOUR_DOMAIN)) {
            for (MigrationStep step : task.getSteps()) {
                if (step instanceof ExpressionMigratorAware) {
                    ((ExpressionMigratorAware) step).setExpressionMigrator(expressionMigrator);
                }
                if (step instanceof ApplicationModelContribution) {
                    ((ApplicationModelContribution) step).setApplicationModel(domainModel);
                }
                domainSteps.add(step);
            }
        }
        if (task.getApplicableProjectTypes().contains(MULE_FOUR_APPLICATION)) {
            for (MigrationStep step : task.getSteps()) {
                if (step instanceof ExpressionMigratorAware) {
                    ((ExpressionMigratorAware) step).setExpressionMigrator(expressionMigrator);
                }
                if (step instanceof ApplicationModelContribution) {
                    ((ApplicationModelContribution) step).setApplicationModel(appModel);
                }
                appSteps.add(step);
            }
        }
    }
}
Also used : DbMigrationTask(com.mulesoft.tools.migration.library.mule.tasks.DbMigrationTask) EndpointsMigrationTask(com.mulesoft.tools.migration.library.mule.tasks.EndpointsMigrationTask) HashMap(java.util.HashMap) Element(org.jdom2.Element) ArrayList(java.util.ArrayList) ApplicationModel(com.mulesoft.tools.migration.project.model.ApplicationModel) SpringMigrationTask(com.mulesoft.tools.migration.library.mule.tasks.SpringMigrationTask) MigrationCleanTask(com.mulesoft.tools.migration.library.mule.tasks.MigrationCleanTask) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Document(org.jdom2.Document) DocumentHelper.getElementsFromDocument(com.mulesoft.tools.migration.helper.DocumentHelper.getElementsFromDocument) DocumentHelper.getDocument(com.mulesoft.tools.migration.helper.DocumentHelper.getDocument) ExpressionMigratorAware(com.mulesoft.tools.migration.step.ExpressionMigratorAware) ApplicationModelContribution(com.mulesoft.tools.migration.step.category.ApplicationModelContribution) MigrationStep(com.mulesoft.tools.migration.step.MigrationStep) JmsMigrationTask(com.mulesoft.tools.migration.library.mule.tasks.JmsMigrationTask) JmsDomainMigrationTask(com.mulesoft.tools.migration.library.mule.tasks.JmsDomainMigrationTask) Path(java.nio.file.Path) PostprocessGeneral(com.mulesoft.tools.migration.library.mule.tasks.PostprocessGeneral) PostprocessMuleApplication(com.mulesoft.tools.migration.library.mule.tasks.PostprocessMuleApplication) DomainAppMigrationTask(com.mulesoft.tools.migration.library.mule.tasks.DomainAppMigrationTask) AbstractMigrationTask(com.mulesoft.tools.migration.task.AbstractMigrationTask) HTTPCleanupTask(com.mulesoft.tools.migration.library.mule.tasks.HTTPCleanupTask) Namespace(org.jdom2.Namespace) MelToDwExpressionMigrator(com.mulesoft.tools.migration.library.tools.MelToDwExpressionMigrator) HTTPMigrationTask(com.mulesoft.tools.migration.library.mule.tasks.HTTPMigrationTask) MuleCoreComponentsMigrationTask(com.mulesoft.tools.migration.library.mule.tasks.MuleCoreComponentsMigrationTask) Before(org.junit.Before)

Aggregations

ApplicationModelContribution (com.mulesoft.tools.migration.step.category.ApplicationModelContribution)5 MigrationStep (com.mulesoft.tools.migration.step.MigrationStep)4 ApplicationModel (com.mulesoft.tools.migration.project.model.ApplicationModel)3 ArrayList (java.util.ArrayList)3 PomModel (com.mulesoft.tools.migration.project.model.pom.PomModel)2 ExpressionMigratorAware (com.mulesoft.tools.migration.step.ExpressionMigratorAware)2 MigrationReport (com.mulesoft.tools.migration.step.category.MigrationReport)2 Element (org.jdom2.Element)2 XMLOutputter (org.jdom2.output.XMLOutputter)2 Test (org.junit.Test)2 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)2 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)1 Preconditions.checkState (com.google.common.base.Preconditions.checkState)1 Lists.newArrayList (com.google.common.collect.Lists.newArrayList)1 MigrationAbortException (com.mulesoft.tools.migration.exception.MigrationAbortException)1 MigrationStepException (com.mulesoft.tools.migration.exception.MigrationStepException)1 MigrationTaskException (com.mulesoft.tools.migration.exception.MigrationTaskException)1 DocumentHelper.getDocument (com.mulesoft.tools.migration.helper.DocumentHelper.getDocument)1 DocumentHelper.getElementsFromDocument (com.mulesoft.tools.migration.helper.DocumentHelper.getElementsFromDocument)1 DbMigrationTask (com.mulesoft.tools.migration.library.mule.tasks.DbMigrationTask)1