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

Add support to headers type #21

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
57 changes: 53 additions & 4 deletions src/main/java/com/zeroclue/jmeter/protocol/amqp/AMQPPublisher.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
package com.zeroclue.jmeter.protocol.amqp;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeoutException;
import java.util.function.Function;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import org.apache.commons.lang3.StringUtils;
import org.apache.jmeter.config.Argument;
import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.samplers.Entry;
import org.apache.jmeter.samplers.Interruptible;
import org.apache.jmeter.samplers.SampleResult;
import org.apache.jmeter.testelement.property.JMeterProperty;
import org.apache.jmeter.testelement.property.TestElementProperty;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -36,6 +41,7 @@ public class AMQPPublisher extends AMQPSampler implements Interruptible {
private static final long serialVersionUID = -8420658040465788497L;

private static final Logger log = LoggerFactory.getLogger(AMQPPublisher.class);
private static final Map<String, Function<String, Object>> CONVERTERS = getConverters();

//++ These are JMX names, and must not be changed
private static final String MESSAGE = "AMQPPublisher.Message";
Expand Down Expand Up @@ -321,7 +327,15 @@ private Map<String, Object> prepareHeaders() {
Arguments headers = getHeaders();

if (headers != null) {
return new HashMap<>(headers.getArgumentsAsMap());
final Map<String, Object> preparedHeaders = new HashMap<>();
for (final JMeterProperty property : headers) {
final Argument arg = (Argument) property.getObjectValue();
final String type = arg.getDescription();
final Object value = StringUtils.isBlank(type) ? arg.getValue() : convert(type, arg.getValue());
preparedHeaders.put(arg.getName(), value);
}

return preparedHeaders;
}

return Collections.emptyMap();
Expand All @@ -340,4 +354,39 @@ private String formatHeaders() {

return sb.toString();
}

private static Object convert(final String type, final String value) {
final Function<String, Object> converter = CONVERTERS.get(type);

if (converter == null) {
log.error("No converter found for type '{}'", type);
throw new NullPointerException("No converter found for type " + type);
}

return converter.apply(value);
}

private static Map<String, Function<String, Object>> getConverters() {
final Map<String, Function<String, Object>> converters = new HashMap<>();
converters.put("integer", Integer::valueOf);
converters.put("date", AMQPPublisher::convertDate);
converters.put("string", v -> v);
return converters;
}

private static Date convertDate(final String dateString) {
final Date date;

if (StringUtils.isNumeric(dateString)) {
date = new Date(Long.parseLong(dateString));
} else {
try {
date = new SimpleDateFormat().parse(dateString);
} catch (ParseException e) {
throw new IllegalArgumentException(e);
}
}

return date;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.zeroclue.jmeter.protocol.amqp.gui;

import org.apache.jmeter.config.Argument;
import org.apache.jmeter.config.gui.ArgumentsPanel;
import org.apache.jorphan.gui.ObjectTableModel;
import org.apache.jorphan.reflect.Functor;

public class AMQPHeadersPanel extends ArgumentsPanel {

public AMQPHeadersPanel(final String label) {
super(label);
}

@Override
protected void initializeTableModel() {
tableModel = new ObjectTableModel(new String[] { COLUMN_RESOURCE_NAMES_0, COLUMN_RESOURCE_NAMES_1, "Type" },
Argument.class,
new Functor[] {
new Functor("getName"), // $NON-NLS-1$
new Functor("getValue"), // $NON-NLS-1$
new Functor("getDescription") }, // $NON-NLS-1$
new Functor[] {
new Functor("setName"), // $NON-NLS-1$
new Functor("setValue"), // $NON-NLS-1$
new Functor("setDescription") }, // $NON-NLS-1$
new Class[] { String.class, String.class, String.class });
}

@Override
protected Argument makeNewArgument() {
return new Argument("", "", null, "string");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class AMQPPublisherGui extends AMQPSamplerGui {
private final JCheckBox persistent = new JCheckBox("Persistent", AMQPPublisher.DEFAULT_PERSISTENT);
private final JCheckBox useTx = new JCheckBox("Use Transactions", AMQPPublisher.DEFAULT_USE_TX);

private final ArgumentsPanel headers = new ArgumentsPanel("Headers");
private final ArgumentsPanel headers = new AMQPHeadersPanel("Headers");

private JPanel mainPanel;

Expand Down