From e85f3ae464644a41f2845043d8bf27598a52b317 Mon Sep 17 00:00:00 2001 From: Mark Brophy <36955467+m-brophy@users.noreply.github.com> Date: Tue, 16 Jan 2024 15:38:33 +0000 Subject: [PATCH] WINDUPRULE-1041 jakarta-api-to-quarkus groovy rules (#1051) * jakarta-api-to-quarkus groovy rules * source tag and ruleset naming changes * misnaming corrected * update number of test iterations because of new pom.xml file (cherry picked from commit fa0bfa5ec89bb31fc17517060eedfb40eda9ccb5) --- .../jakarta-api-to-quarkus.windup.groovy | 72 +++++++++++++++++++ .../data/application-jakarta-api/pom.xml | 53 ++++++++++++++ .../src/main/java/sample/HelloWorld.java | 47 ++++++++++++ .../jakarta-api-to-quarkus.windup.test.xml | 24 +++++++ .../javaee-pom-to-quarkus.windup.test.xml | 14 ++-- 5 files changed, 203 insertions(+), 7 deletions(-) create mode 100644 rules/rules-reviewed/quarkus/java-ee/jakarta-api-to-quarkus.windup.groovy create mode 100644 rules/rules-reviewed/quarkus/java-ee/tests/data/application-jakarta-api/pom.xml create mode 100644 rules/rules-reviewed/quarkus/java-ee/tests/data/application-jakarta-api/src/main/java/sample/HelloWorld.java create mode 100644 rules/rules-reviewed/quarkus/java-ee/tests/jakarta-api-to-quarkus.windup.test.xml diff --git a/rules/rules-reviewed/quarkus/java-ee/jakarta-api-to-quarkus.windup.groovy b/rules/rules-reviewed/quarkus/java-ee/jakarta-api-to-quarkus.windup.groovy new file mode 100644 index 000000000..9e2d5d3f7 --- /dev/null +++ b/rules/rules-reviewed/quarkus/java-ee/jakarta-api-to-quarkus.windup.groovy @@ -0,0 +1,72 @@ +package quarkus.javaee + +import org.jboss.windup.ast.java.data.TypeReferenceLocation +import org.jboss.windup.config.GraphRewrite +import org.jboss.windup.config.Variables +import org.jboss.windup.config.metadata.TechnologyReference +import org.jboss.windup.config.operation.Iteration +import org.jboss.windup.config.operation.iteration.AbstractIterationOperation +import org.jboss.windup.graph.model.FileLocationModel +import org.jboss.windup.graph.model.FileReferenceModel +import org.jboss.windup.graph.model.ProjectModel +import org.jboss.windup.graph.model.WindupVertexFrame +import org.jboss.windup.graph.model.resource.FileModel +import org.jboss.windup.project.condition.Artifact +import org.jboss.windup.project.condition.Project +import org.jboss.windup.reporting.category.IssueCategory +import org.jboss.windup.reporting.category.IssueCategoryRegistry +import org.jboss.windup.reporting.config.Hint +import org.jboss.windup.reporting.config.Link +import org.jboss.windup.rules.apps.java.condition.JavaClass +import org.ocpsoft.rewrite.config.And +import org.ocpsoft.rewrite.context.EvaluationContext + +import java.util.stream.Collectors +import java.util.stream.StreamSupport + +final IssueCategory mandatoryIssueCategory = new IssueCategoryRegistry().getByID(IssueCategoryRegistry.MANDATORY) +final Link guideLink = Link.to("Quarkus - Guides", "https://quarkus.io/guides/resteasy-reactive") + +static boolean matchesProject(GraphRewrite event, FileLocationModel payload) { + final Iterable previouslyFound = Optional.ofNullable(Variables.instance(event).findVariable("discard")).orElse(Collections.emptySet()) + final Set projectModels = StreamSupport.stream(previouslyFound.spliterator(), false) + .map { + if (it instanceof FileReferenceModel) return ((FileReferenceModel) it).getFile().getProjectModel() + else if (it instanceof FileModel) return ((FileModel) it).getProjectModel() + else return null + } + .collect (Collectors.toSet()) + final boolean matchesProject = projectModels.isEmpty() || projectModels.stream().anyMatch{payload.getFile().belongsToProject(it)} + return matchesProject +} + +ruleSet("jakarta-api-to-quarkus-groovy") + .addSourceTechnology(new TechnologyReference("java-ee", null)) + .addTargetTechnology(new TechnologyReference("quarkus", null)) + .addRule() + .when( + And.all( + JavaClass.references("jakarta.ws.rs.{*}").at(TypeReferenceLocation.IMPORT).as("discard"), + Project.dependsOnArtifact(Artifact.withGroupId("jakarta.platform").andArtifactId("jakarta.jakartaee-api")).as("dependency") + ) + ) + .perform( + Iteration.over("dependency").perform( + new AbstractIterationOperation() { + void perform(GraphRewrite event, EvaluationContext context, FileLocationModel payload) { + if (matchesProject(event, payload)) { + ((Hint) Hint.titled("Replace jakarta JAX-RS dependency") + .withText(""" + At least one Java class importing from the `jakarta.ws.rs` package has been found so the dependency `jakarta.platform:jakarta.jakartaee-api` has to be replaced with `io.quarkus:quarkus-resteasy-reactive` artifact. + """) + .withIssueCategory(mandatoryIssueCategory) + .with(guideLink) + .withEffort(1) + ).performParameterized(event, context, payload) + } + } + } + ) + .endIteration() + ) + .withId("jakarta-api-to-quarkus-groovy-00000") \ No newline at end of file diff --git a/rules/rules-reviewed/quarkus/java-ee/tests/data/application-jakarta-api/pom.xml b/rules/rules-reviewed/quarkus/java-ee/tests/data/application-jakarta-api/pom.xml new file mode 100644 index 000000000..e5f3c1b74 --- /dev/null +++ b/rules/rules-reviewed/quarkus/java-ee/tests/data/application-jakarta-api/pom.xml @@ -0,0 +1,53 @@ + + + + 4.0.0 + + org.jboss.eap.quickstarts + quickstart-parent + + 7.2.0.GA + ../pom.xml + + helloworld-rs + war + Quickstart: helloworld-rs + A simple Hello World project that uses JAX-RS + + + + Apache License, Version 2.0 + http://www.apache.org/licenses/LICENSE-2.0.html + repo + + + + + + + jakarta.platform + jakarta.jakartaee-api + 9.0.0 + provided + + + diff --git a/rules/rules-reviewed/quarkus/java-ee/tests/data/application-jakarta-api/src/main/java/sample/HelloWorld.java b/rules/rules-reviewed/quarkus/java-ee/tests/data/application-jakarta-api/src/main/java/sample/HelloWorld.java new file mode 100644 index 000000000..137cd4f0a --- /dev/null +++ b/rules/rules-reviewed/quarkus/java-ee/tests/data/application-jakarta-api/src/main/java/sample/HelloWorld.java @@ -0,0 +1,47 @@ +/* + * JBoss, Home of Professional Open Source + * Copyright 2015, Red Hat, Inc. and/or its affiliates, and individual + * contributors by the @authors tag. See the copyright.txt in the + * distribution for a full listing of individual contributors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jboss.as.quickstarts.rshelloworld; + +import jakarta.ws.rs.GET; +import jakarta.ws.rs.Path; +import jakarta.ws.rs.Produces; + +/** + * A simple REST service which is able to say hello to someone using HelloService Please take a look at the web.xml where JAX-RS + * is enabled + * + * @author gbrey@redhat.com + * + */ + +@Path("/") +public class HelloWorld { + @GET + @Path("/json") + @Produces({ "application/json" }) + public String getHelloWorldJSON() { + return "{\"result\":\"" + helloService.createHelloMessage("World") + "\"}"; + } + + @GET + @Path("/xml") + @Produces({ "application/xml" }) + public String getHelloWorldXML() { + return "" + helloService.createHelloMessage("World") + ""; + } + +} diff --git a/rules/rules-reviewed/quarkus/java-ee/tests/jakarta-api-to-quarkus.windup.test.xml b/rules/rules-reviewed/quarkus/java-ee/tests/jakarta-api-to-quarkus.windup.test.xml new file mode 100644 index 000000000..fb2aeb112 --- /dev/null +++ b/rules/rules-reviewed/quarkus/java-ee/tests/jakarta-api-to-quarkus.windup.test.xml @@ -0,0 +1,24 @@ + + + data/* + ../jakarta-api-to-quarkus.windup.groovy + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/rules/rules-reviewed/quarkus/java-ee/tests/javaee-pom-to-quarkus.windup.test.xml b/rules/rules-reviewed/quarkus/java-ee/tests/javaee-pom-to-quarkus.windup.test.xml index 1236f4513..fcaa0a520 100644 --- a/rules/rules-reviewed/quarkus/java-ee/tests/javaee-pom-to-quarkus.windup.test.xml +++ b/rules/rules-reviewed/quarkus/java-ee/tests/javaee-pom-to-quarkus.windup.test.xml @@ -10,7 +10,7 @@ - + @@ -22,7 +22,7 @@ - + @@ -34,7 +34,7 @@ - + @@ -46,7 +46,7 @@ - + @@ -58,7 +58,7 @@ - + @@ -70,7 +70,7 @@ - + @@ -82,7 +82,7 @@ - +