Skip to content

Commit

Permalink
Adding tests for scm.GitDiff and update notebook so git diff now works
Browse files Browse the repository at this point in the history
Signed-off-by: John Matthews <jwmatthews@gmail.com>
  • Loading branch information
jwmatthews committed Feb 18, 2024
1 parent e2872d8 commit 10d1e70
Show file tree
Hide file tree
Showing 8 changed files with 359 additions and 182 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,11 @@
" ## Solved Example Filename\n",
" Filename: \"{solved_example_file_name}\"\n",
"\n",
" ## Solved Example source code that has been migrated from Java EE to Quarkus\n",
" ```java\n",
" {solved_file_contents}\n",
" ## Solved Example Git Diff \n",
" This diff of the solved example shows what changes we made in past to address a similar problem.\n",
" Please consider this heavily in your response.\n",
" ```diff\n",
" {solved_example_diff}\n",
" ```\n",
"\n",
" ## Input file name\n",
Expand Down Expand Up @@ -242,15 +244,17 @@
"cmt_src_path = \"../../samples/sample_repos/cmt\"\n",
"cmt_diff = GitDiff(cmt_src_path)\n",
"\n",
"# Ensure we checked out the 'quarkus' branch of the CMT repo\n",
"cmt_diff.checkout_branch(\"quarkus\")\n",
"\n",
"helloworld_src_path = \"../../samples/sample_repos/helloworld-mdb-quarkus\"\n",
"helloworld_diff = GitDiff(helloworld_src_path)\n",
"\n",
"# We want to be sure the the 'quarkus' branch of both repos has been checked out\n",
"# so it's available to consult\n",
"cmt_diff.checkout_branch(\"quarkus\")\n",
"helloworld_diff.checkout_branch(\"quarkus\")\n",
"# Resetting to 'main' as what is currently checked out\n",
"cmt_diff.checkout_branch(\"main\")\n",
"# Resetting to 'main' for HelloWorld as that represents th initial state of the repo\n",
"helloworld_diff.checkout_branch(\"main\")\n",
"\n",
"# Now we can extract the info we need\n",
Expand All @@ -266,12 +270,14 @@
"\n",
"cmt_entry = cmt_rule_data[\"incidents\"][0]\n",
"solved_example_file_name = cmt_report.get_cleaned_file_path(cmt_entry[\"uri\"])\n",
"solved_file_contents = cmt_diff.get_file_contents(solved_example_file_name, \"quarkus\")\n",
"##\n",
"## Todo: Update so we pass in a diff of what changed\n",
"## We do not have the ability to extract a diff working at present\n",
"# solved_example_diff = cmt_diff.get_patch_for_file('main', 'quarkus', solved_example_file_name)\n",
"##"
"# solved_file_contents = cmt_diff.get_file_contents(solved_example_file_name, \"quarkus\")\n",
"\n",
"start_commit_id = cmt_diff.get_commit_from_branch(\"main\").hexsha\n",
"end_commit_id = cmt_diff.get_commit_from_branch(\"quarkus\").hexsha\n",
"\n",
"solved_example_diff = cmt_diff.get_patch_for_file(\n",
" start_commit_id, end_commit_id, solved_example_file_name\n",
")"
]
},
{
Expand All @@ -290,11 +296,11 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Saved template to output/gpt-3.5-turbo-16k/helloworldmdb/custom-ruleset/jms-to-reactive-quarkus-00010/few_shot/template.txt\n",
"Saved result to output/gpt-3.5-turbo-16k/helloworldmdb/custom-ruleset/jms-to-reactive-quarkus-00010/few_shot/result.txt\n",
"Saved updated java file to output/gpt-3.5-turbo-16k/helloworldmdb/custom-ruleset/jms-to-reactive-quarkus-00010/few_shot/updated_file.java\n",
"Saved original java file to output/gpt-3.5-turbo-16k/helloworldmdb/custom-ruleset/jms-to-reactive-quarkus-00010/few_shot/original_file.java\n",
"Saved Quarkus version from Git to output/gpt-3.5-turbo-16k/helloworldmdb/custom-ruleset/jms-to-reactive-quarkus-00010/few_shot/quarkus_version_from_git.java\n"
"Saved template to output/gpt-4-1106-preview/helloworldmdb/custom-ruleset/jms-to-reactive-quarkus-00010/few_shot/template.txt\n",
"Saved result to output/gpt-4-1106-preview/helloworldmdb/custom-ruleset/jms-to-reactive-quarkus-00010/few_shot/result.txt\n",
"Saved updated java file to output/gpt-4-1106-preview/helloworldmdb/custom-ruleset/jms-to-reactive-quarkus-00010/few_shot/updated_file.java\n",
"Saved original java file to output/gpt-4-1106-preview/helloworldmdb/custom-ruleset/jms-to-reactive-quarkus-00010/few_shot/original_file.java\n",
"Saved Quarkus version from Git to output/gpt-4-1106-preview/helloworldmdb/custom-ruleset/jms-to-reactive-quarkus-00010/few_shot/quarkus_version_from_git.java\n"
]
}
],
Expand All @@ -306,13 +312,13 @@
" \"analysis_message\": analysis_message,\n",
" \"analysis_lineNumber\": analysis_lineNumber,\n",
" \"solved_example_file_name\": solved_example_file_name,\n",
" # \"solved_example_diff\": solved_example_diff\n",
" \"solved_file_contents\": solved_file_contents,\n",
" \"solved_example_diff\": solved_example_diff,\n",
" # \"solved_file_contents\": solved_file_contents,\n",
"}\n",
"formatted_prompt = template_with_solved_example_and_analysis.format(**template_args)\n",
"\n",
"model_name = \"gpt-4-1106-preview\"\n",
"# model_name=\"gpt-3.5-turbo-16k\"\n",
"# model_name = \"gpt-4-1106-preview\"\n",
"model_name = \"gpt-3.5-turbo-16k\"\n",
"\n",
"llm = ChatOpenAI(temperature=0.1, model_name=model_name)\n",
"prompt = PromptTemplate.from_template(template_with_solved_example_and_analysis)\n",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
## Reasoning
1. The issue found from static code analysis is that the `@MessageDriven` annotation is not supported in Quarkus and needs to be replaced with a CDI scope annotation like `@ApplicationScoped`.
2. In the solved example, the `@MessageDriven` annotation was replaced with `@ApplicationScoped` to make the class a CDI bean.
3. The `@Incoming` annotation from the `org.eclipse.microprofile.reactive.messaging` package is used to specify the incoming channel for the message-driven bean.
4. The `Log` class from the `io.quarkus.logging` package is used to log the received message.
5. The `HelloWorldMDB` class is annotated with `@ApplicationScoped` to make it a CDI bean.
2. Looking at the solved example, we can see that the `@MessageDriven` annotation was replaced with `@ApplicationScoped`.
3. We need to update the import statements to use the correct annotations for Quarkus. The import statements for `@MessageDriven`, `@ActivationConfigProperty`, `@Message`, `@MessageListener`, and `@TextMessage` should be removed. Instead, we need to import `org.eclipse.microprofile.reactive.messaging.Incoming` for the `@Incoming` annotation and `io.quarkus.logging.Log` for the logging statement.
4. The `onMessage` method should be updated to accept a `String` parameter instead of a `Message` parameter. The logging statement should also be updated to use `Log.info` instead of `LOGGER.info`.

## Updated File
```java
Expand All @@ -25,16 +24,9 @@
*/
package org.jboss.as.quickstarts.mdb;

import java.util.logging.Logger;
import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
import org.eclipse.microprofile.reactive.messaging.Incoming;
import io.quarkus.logging.Log;
import jakarta.enterprise.context.ApplicationScoped;
import org.eclipse.microprofile.reactive.messaging.Incoming;

/**
* <p>
Expand All @@ -44,26 +36,11 @@ import jakarta.enterprise.context.ApplicationScoped;
* @author Serge Pagop (spagop@redhat.com)
*/
@ApplicationScoped
public class HelloWorldQueueMDB implements MessageListener {

private static final Logger LOGGER = Logger.getLogger(HelloWorldQueueMDB.class.toString());
public class HelloWorldQueueMDB {

/**
* @see MessageListener#onMessage(Message)
*/
@Incoming("HELLOWORLDMDBQueue")
public void onMessage(Message rcvMessage) {
TextMessage msg = null;
try {
if (rcvMessage instanceof TextMessage) {
msg = (TextMessage) rcvMessage;
Log.info("Received Message from queue: " + msg.getText());
} else {
LOGGER.warning("Message of wrong type: " + rcvMessage.getClass().getName());
}
} catch (JMSException e) {
throw new RuntimeException(e);
}
public void onMessage(String msg) {
Log.info("Received Message from queue: " + msg);
}
}
```
Original file line number Diff line number Diff line change
Expand Up @@ -30,47 +30,62 @@
## Solved Example Filename
Filename: "src/main/java/org/jboss/as/quickstarts/cmt/mdb/HelloWorldMDB.java"

## Solved Example source code that has been migrated from Java EE to Quarkus
```java
/*
* 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.cmt.mdb;

import org.eclipse.microprofile.reactive.messaging.Incoming;

import io.quarkus.logging.Log;
import jakarta.enterprise.context.ApplicationScoped;

/**
* <p>
* A simple Message Driven Bean that asynchronously receives and processes the messages that are sent to the queue.
* </p>
*
* @author Serge Pagop (spagop@redhat.com)
*
*/
@ApplicationScoped
public class HelloWorldMDB {

@Incoming("CMTQueue")
public void onMessage(String msg) {
Log.info("Received Message: " + msg);
}
}
## Solved Example Git Diff
This diff of the solved example shows what changes we made in past to address a similar problem.
Please consider this heavily in your response.
```diff
@@ -16,14 +16,10 @@
*/
package org.jboss.as.quickstarts.cmt.mdb;

-import java.util.logging.Logger;
+import org.eclipse.microprofile.reactive.messaging.Incoming;

-import javax.ejb.ActivationConfigProperty;
-import javax.ejb.MessageDriven;
-import javax.jms.JMSException;
-import javax.jms.Message;
-import javax.jms.MessageListener;
-import javax.jms.TextMessage;
+import io.quarkus.logging.Log;
+import jakarta.enterprise.context.ApplicationScoped;

/**
* <p>
@@ -33,28 +29,11 @@ import javax.jms.TextMessage;
* @author Serge Pagop (spagop@redhat.com)
*
*/
-@MessageDriven(name = "HelloWorldMDB", activationConfig = {
- @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
- @ActivationConfigProperty(propertyName = "destination", propertyValue = "queue/CMTQueue"),
- @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge") })
-public class HelloWorldMDB implements MessageListener {
+@ApplicationScoped
+public class HelloWorldMDB {

- private static final Logger LOGGER = Logger.getLogger(HelloWorldMDB.class.toString());
-
- /**
- * @see MessageListener#onMessage(Message)
- */
- public void onMessage(Message rcvMessage) {
- TextMessage msg = null;
- try {
- if (rcvMessage instanceof TextMessage) {
- msg = (TextMessage) rcvMessage;
- LOGGER.info("Received Message: " + msg.getText());
- } else {
- LOGGER.warning("Message of wrong type: " + rcvMessage.getClass().getName());
- }
- } catch (JMSException e) {
- throw new RuntimeException(e);
- }
+ @Incoming("CMTQueue")
+ public void onMessage(String msg) {
+ Log.info("Received Message: " + msg);
}
}

```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,9 @@
*/
package org.jboss.as.quickstarts.mdb;

import java.util.logging.Logger;
import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
import org.eclipse.microprofile.reactive.messaging.Incoming;
import io.quarkus.logging.Log;
import jakarta.enterprise.context.ApplicationScoped;
import org.eclipse.microprofile.reactive.messaging.Incoming;

/**
* <p>
Expand All @@ -36,25 +29,10 @@
* @author Serge Pagop (spagop@redhat.com)
*/
@ApplicationScoped
public class HelloWorldQueueMDB implements MessageListener {

private static final Logger LOGGER = Logger.getLogger(HelloWorldQueueMDB.class.toString());
public class HelloWorldQueueMDB {

/**
* @see MessageListener#onMessage(Message)
*/
@Incoming("HELLOWORLDMDBQueue")
public void onMessage(Message rcvMessage) {
TextMessage msg = null;
try {
if (rcvMessage instanceof TextMessage) {
msg = (TextMessage) rcvMessage;
Log.info("Received Message from queue: " + msg.getText());
} else {
LOGGER.warning("Message of wrong type: " + rcvMessage.getClass().getName());
}
} catch (JMSException e) {
throw new RuntimeException(e);
}
public void onMessage(String msg) {
Log.info("Received Message from queue: " + msg);
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
## Reasoning

To migrate the Java EE Message Driven Bean (MDB) to Quarkus, we need to address the issue identified by the static code analysis. The `@MessageDriven` annotation is not supported in Quarkus, and we need to use Contexts and Dependency Injection (CDI) instead. Here are the steps we will take:
To migrate the Java EE Message Driven Bean (MDB) to Quarkus, we need to address the issue identified by the static code analysis, which is the use of `@MessageDriven` annotation. Quarkus does not support EJBs, and therefore we need to use CDI (Contexts and Dependency Injection) beans instead.

Here are the steps we will follow:

1. Replace the `@MessageDriven` annotation with `@ApplicationScoped` to define the bean as a CDI managed bean with application scope.
2. Use MicroProfile Reactive Messaging to handle the JMS messages. This is done by using the `@Incoming` annotation to mark the method that will receive the messages.
3. Replace the Java Util Logging with Quarkus logging (`io.quarkus.logging.Log`) for consistency with Quarkus standards.
4. Remove the `MessageListener` interface and its `onMessage` method signature, as it is specific to Java EE MDBs and not required in Quarkus.
5. The `ActivationConfigProperty` annotations are no longer needed because the configuration will be handled differently in Quarkus, typically through the `application.properties` file.
6. Update the `pom.xml` file to include the Quarkus Reactive Messaging dependency if it's not already present.
2. Remove the `MessageListener` interface and its `onMessage` method signature, as Quarkus uses the MicroProfile Reactive Messaging API for handling messages.
3. Use the `@Incoming` annotation to mark the method that will be invoked for each message received on the specified channel.
4. Replace the `Logger` from `java.util.logging` with `io.quarkus.logging.Log` which is the preferred logging framework in Quarkus.
5. Remove the `ActivationConfigProperty` annotations as they are specific to EJBs and not needed in Quarkus.
6. Update the `pom.xml` to include the Quarkus Reactive Messaging dependency if not already present.
7. Ensure that the application properties or configuration files are updated to define the connection to the message broker and the channel configuration.

The solved example shows how the MDB is converted to a CDI bean and uses MicroProfile Reactive Messaging to consume messages. We will follow a similar pattern for the input file.
Now, let's proceed with the migration.

## Updated File

```java
package org.jboss.as.quickstarts.mdb;

Expand All @@ -21,7 +25,7 @@ import org.eclipse.microprofile.reactive.messaging.Incoming;

/**
* <p>
* A simple bean that asynchronously receives and processes the messages that are sent to the queue.
* A simple bean that asynchronously receives and processes the messages that are sent to the queue using Quarkus.
* </p>
*
* @author Serge Pagop (spagop@redhat.com)
Expand All @@ -30,20 +34,29 @@ import org.eclipse.microprofile.reactive.messaging.Incoming;
public class HelloWorldQueueMDB {

@Incoming("HELLOWORLDMDBQueue")
public void receiveMessage(String messageText) {
Log.info("Received Message from queue: " + messageText);
public void onMessage(String msg) {
Log.info("Received Message from queue: " + msg);
}
}
```

### Notes on pom.xml and Configuration
- Ensure that the `quarkus-smallrye-reactive-messaging` dependency is included in the `pom.xml` file.
- The `application.properties` file should contain the configuration for the message queue, similar to the `ActivationConfigProperty` values from the original Java EE code. For example:
```
mp.messaging.incoming.HELLOWORLDMDBQueue.connector=smallrye-jms
mp.messaging.incoming.HELLOWORLDMDBQueue.destination=queue/HELLOWORLDMDBQueue
```
- The `connector` property would depend on the specific JMS connector being used in the Quarkus application.
- The `destination` property should match the JNDI name of the JMS queue.

By following these steps and making the necessary updates, the Java EE MDB should be successfully migrated to a Quarkus application.
### Additional Notes:

- The `@Incoming` annotation's value should match the name of the channel configured in the `application.properties` file for the incoming messages from the queue.
- The `pom.xml` file should include the `quarkus-smallrye-reactive-messaging` dependency to enable MicroProfile Reactive Messaging:

```xml
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-smallrye-reactive-messaging</artifactId>
</dependency>
```

- The `application.properties` file should be updated to include the configuration for the message broker and the channel, for example:

```properties
mp.messaging.incoming.HELLOWORLDMDBQueue.connector=smallrye-jms
mp.messaging.incoming.HELLOWORLDMDBQueue.destination=queue/HELLOWORLDMDBQueue
```

- Ensure that the message broker is properly configured and accessible from the Quarkus application.
Loading

0 comments on commit 10d1e70

Please sign in to comment.