Skip to content

Commit

Permalink
feat: add message entity read-only rest api
Browse files Browse the repository at this point in the history
I've used this commit to cleanup some code too. The previous changes
for the new UI introduced some minor code smells into the simulator.
  • Loading branch information
bbortt committed Oct 21, 2023
1 parent 1d4dcbf commit 54f7e49
Show file tree
Hide file tree
Showing 74 changed files with 2,464 additions and 1,045 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import org.citrusframework.simulator.repository.RepositoryConfig;
import org.citrusframework.simulator.scenario.ScenarioBeanNameGenerator;
import org.citrusframework.variable.dictionary.json.JsonPathMappingDataDictionary;
import org.citrusframework.simulator.service.QueryFilterAdapterFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -165,9 +164,4 @@ public JsonPathMappingDataDictionary outboundJsonDataDictionary() {

return outboundJsonDataDictionary;
}

@Bean
public QueryFilterAdapterFactory queryFilterAdapterFactory(SimulatorConfigurationProperties cfg) {
return new QueryFilterAdapterFactory(cfg);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("api/scenario")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

package org.citrusframework.simulator.endpoint;

import java.util.Optional;

import org.citrusframework.context.TestContext;
import org.citrusframework.exceptions.CitrusRuntimeException;
import org.citrusframework.message.Message;
Expand All @@ -30,6 +28,8 @@
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.Optional;

/**
* Endpoint message handler for handling messages that were sent or received on a endpoint.
*/
Expand Down Expand Up @@ -75,19 +75,16 @@ private Optional<Long> extractExecutionId(TestContext context) {
return Optional.of(Long.parseLong(executionId));
}
} catch (NumberFormatException e) {
LOG.error("Error parsing the execution id. Was expection a Long", e);
LOG.error("Error parsing the execution id. Was expecting a Long", e);
}
return Optional.empty();
}

private Optional<String> extractCitrusMessageId(Message message) {
Object headerValue = message.getHeader(MessageHeaders.ID);
if (headerValue != null && headerValue instanceof String) {
String stringHeaderValue = (String) headerValue;
if (StringUtils.hasText(stringHeaderValue)) {
return Optional.of(stringHeaderValue);
}
if (message.getHeader(MessageHeaders.ID) instanceof String stringHeaderValue && StringUtils.hasText(stringHeaderValue)) {
return Optional.of(stringHeaderValue);
}

return Optional.empty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import org.citrusframework.http.message.HttpMessage;
import org.citrusframework.message.Message;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@ private String getRequestContent(HttpServletRequest request) throws IOException
}

private String getResponseContent(HttpServletRequest request, HttpServletResponse response, Object handler) {
if (handler instanceof HttpMessageController) {
HttpMessageController handlerController = (HttpMessageController) handler;
if (handler instanceof HttpMessageController handlerController) {
ResponseEntity<?> responseEntity = handlerController.getResponseCache(request);
if (responseEntity != null) {
return TypeConversionUtils.convertIfNecessary(responseEntity.getBody(), String.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@
* @author Christoph Deppisch
*/
@Configuration
@ConditionalOnWebApplication
@AutoConfigureAfter(SimulatorAutoConfiguration.class)
@EnableConfigurationProperties(SimulatorRestConfigurationProperties.class)
@ConditionalOnProperty(prefix = "citrus.simulator.rest", value = "enabled", havingValue = "true", matchIfMissing = true)
@ConditionalOnWebApplication
public class SimulatorRestAutoConfiguration {

@Autowired(required = false)
Expand Down Expand Up @@ -233,7 +233,7 @@ protected HandlerInterceptor[] interceptors() {
}
interceptors.add(new LoggingHandlerInterceptor());
interceptors.add(httpInterceptor());
return interceptors.toArray(new HandlerInterceptor[interceptors.size()]);
return interceptors.toArray(new HandlerInterceptor[0]);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.Environment;

import jakarta.annotation.PostConstruct;

/**
* @author Christoph Deppisch
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ public class SimulatorJmsConfigurationProperties implements EnvironmentAware {
private void loadProperties() {
inboundDestination = env.getProperty(SIMULATOR_INBOUND_DESTINATION_PROPERTY, env.getProperty(SIMULATOR_INBOUND_DESTINATION_ENV, inboundDestination));
replyDestination = env.getProperty(SIMULATOR_REPLY_DESTINATION_PROPERTY, env.getProperty(SIMULATOR_REPLY_DESTINATION_ENV, replyDestination));
synchronous = Boolean.valueOf(env.getProperty(SIMULATOR_SYNC_PROPERTY, env.getProperty(SIMULATOR_SYNC_ENV, String.valueOf(synchronous))));
useSoap = Boolean.valueOf(env.getProperty(SIMULATOR_SOAP_ENVELOPE_PROPERTY, env.getProperty(SIMULATOR_SOAP_ENVELOPE_ENV, String.valueOf(useSoap))));
pubSubDomain = Boolean.valueOf(env.getProperty(SIMULATOR_PUB_SUB_DOMAIN_PROPERTY, env.getProperty(SIMULATOR_PUB_SUB_DOMAIN_ENV, String.valueOf(pubSubDomain))));
synchronous = Boolean.parseBoolean(env.getProperty(SIMULATOR_SYNC_PROPERTY, env.getProperty(SIMULATOR_SYNC_ENV, String.valueOf(synchronous))));
useSoap = Boolean.parseBoolean(env.getProperty(SIMULATOR_SOAP_ENVELOPE_PROPERTY, env.getProperty(SIMULATOR_SOAP_ENVELOPE_ENV, String.valueOf(useSoap))));
pubSubDomain = Boolean.parseBoolean(env.getProperty(SIMULATOR_PUB_SUB_DOMAIN_PROPERTY, env.getProperty(SIMULATOR_PUB_SUB_DOMAIN_ENV, String.valueOf(pubSubDomain))));

log.info("Using the simulator configuration: {}", this.toString());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
* Base abstract class for entities which will hold definitions for created and last modified by attributes.
*/
@MappedSuperclass
@EntityListeners({ AuditingEntityListener.class })
@JsonIgnoreProperties(value = { "createdDate", "lastModifiedDate" }, allowGetters = true)
@EntityListeners({AuditingEntityListener.class})
@JsonIgnoreProperties(value = {"createdDate", "lastModifiedDate"}, allowGetters = true)
public abstract class AbstractAuditingEntity<T, I> implements Serializable {

private static final long serialVersionUID = 1L;
Expand Down Expand Up @@ -48,15 +48,23 @@ public void setLastModifiedDate(Instant lastModifiedDate) {

public static abstract class AuditingEntityBuilder<B extends AuditingEntityBuilder<B, E, A>, E extends AbstractAuditingEntity<E, A>, A> {

@SuppressWarnings("unchecked")
public B createdDate(ZonedDateTime createdDate) {
getEntity().setCreatedDate(createdDate.toInstant());
return (B) this;
return createdDate(createdDate.toInstant());
}

@SuppressWarnings("unchecked")
public B createdDate(Instant createdDate) {
getEntity().setCreatedDate(createdDate);
return (B) this;
}

public B lastModifiedDate(ZonedDateTime lastModifiedDate) {
getEntity().setLastModifiedDate(lastModifiedDate.toInstant());
return lastModifiedDate(lastModifiedDate.toInstant());
}

@SuppressWarnings("unchecked")
public B lastModifiedDate(Instant lastModifiedDate) {
getEntity().setLastModifiedDate(lastModifiedDate);
return (B) this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,15 @@
import jakarta.persistence.OneToMany;
import jakarta.persistence.OrderBy;
import jakarta.validation.constraints.NotEmpty;
import org.apache.commons.lang3.StringUtils;

import java.io.Serial;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Map;
import java.util.Objects;

/**
* JPA entity for representing inbound and outbound messages
Expand All @@ -52,7 +56,7 @@ public class Message extends AbstractAuditingEntity<Message, Long> implements Se
* Actual direction as a numerical representation of {@link Direction}
*/
@Column(nullable = false, updatable = false)
private Integer direction;
private Integer direction = Direction.UNKNOWN.getId();

@Lob
@Column(columnDefinition = "CLOB", updatable = false)
Expand All @@ -67,15 +71,19 @@ public class Message extends AbstractAuditingEntity<Message, Long> implements Se
private Collection<MessageHeader> headers = new ArrayList<>();

@ManyToOne
@JsonIgnoreProperties(value = { "scenarioParameters", "scenarioActions", "scenarioMessages" }, allowSetters = true)
@JsonIgnoreProperties(value = {"scenarioParameters", "scenarioActions", "scenarioMessages"}, allowSetters = true)
private ScenarioExecution scenarioExecution;

public static MessageBuilder builder() {
return new MessageBuilder();
}

public Long getMessageId() {
return messageId;
}

public ScenarioExecution getScenarioExecution() {
return scenarioExecution;
void setMessageId(Long messageId) {
this.messageId = messageId;
}

public Direction getDirection() {
Expand Down Expand Up @@ -134,15 +142,32 @@ public String getScenarioName() {
return null;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o instanceof Message message) {
return messageId != null && messageId.equals(message.messageId);
}
return false;
}

@Override
public int hashCode() {
// see https://vladmihalcea.com/how-to-implement-equals-and-hashcode-using-the-jpa-entity-identifier/
return getClass().hashCode();
}

@Override
public String toString() {
return "Message{" +
"messageId='" + getMessageId() + "'" +
", createdDate='" + getCreatedDate() + "'" +
", direction='" + getDirection() + "'" +
", payload='" + getPayload() + "'" +
", citrusMessageId='" + getCitrusMessageId() + "'" +
"}";
"messageId='" + getMessageId() + "'" +
", createdDate='" + getCreatedDate() + "'" +
", direction='" + getDirection() + "'" +
", payload='" + getPayload() + "'" +
", citrusMessageId='" + getCitrusMessageId() + "'" +
"}";
}

public enum Direction {
Expand All @@ -166,4 +191,49 @@ public static Direction fromId(int id) {
.orElse(Direction.UNKNOWN);
}
}

public static class MessageBuilder extends AuditingEntityBuilder<MessageBuilder, Message, Long> {

private final Message message = new Message();

public Message build() {
return message;
}

@Override
protected Message getEntity() {
return message;
}

public MessageBuilder direction(Direction direction) {
message.setDirection(direction);
return this;
}

public MessageBuilder payload(String payload) {
message.setPayload(payload);
return this;
}

public MessageBuilder citrusMessageId(String citrusMessageId) {
message.setCitrusMessageId(citrusMessageId);
return this;
}

public MessageBuilder headers(Map<String, Object> headers) {
headers.entrySet().stream()
.map(header -> {
if (header.getValue() != null
&& StringUtils.isNotEmpty(header.getValue().toString())) {
return new MessageHeader(header.getKey(), header.getValue().toString());
} else {
return null;
}
})
.filter(Objects::nonNull)
.toList()
.forEach(message::addHeader);
return this;
}
}
}
Loading

0 comments on commit 54f7e49

Please sign in to comment.