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

fix(INTEROP-37): allow string data type in case the database is not H2 #30

Merged
merged 2 commits into from
Dec 11, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,9 @@ abstract class MessageRepositoryConfiguration {
matchIfMissing = true)
static class Jdbc {

@Value("${camel.messageRepository.datasourceName}")
private String datasourceName;

@Bean
public MessageRepository jdbcMessageRepository() {
return new JdbcMessageRepository(datasourceName);
return new JdbcMessageRepository();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@
import org.apache.camel.support.DefaultMessage;
import org.apache.camel.support.MessageHelper;
import org.apache.camel.support.service.ServiceSupport;
import org.hisp.hieboot.camel.RuntimeCamelHieBootException;
import org.hisp.hieboot.camel.spi.MessageRepository;
import org.hisp.hieboot.camel.spi.RepositoryMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import java.io.IOException;
import java.util.ArrayList;
Expand All @@ -32,9 +34,26 @@ public class JdbcMessageRepository extends ServiceSupport implements MessageRepo
@Autowired
private CamelContext camelContext;

@Value("${camel.messageRepository.datasourceName}")
private String dataSourceName;

public JdbcMessageRepository(String dataSourceName) {
public ProducerTemplate getProducerTemplate() {
return producerTemplate;
}

public void setProducerTemplate(ProducerTemplate producerTemplate) {
this.producerTemplate = producerTemplate;
}

public CamelContext getCamelContext() {
return camelContext;
}

public void setCamelContext(CamelContext camelContext) {
this.camelContext = camelContext;
}

public void setDataSourceName(String dataSourceName) {
this.dataSourceName = dataSourceName;
}

Expand Down Expand Up @@ -102,12 +121,22 @@ protected List<RepositoryMessage> toRepositoryMessages(List<Map<String, Object>>
Message message = new DefaultMessage(camelContext);
message.setBody(row.get("body"));

byte[] headersAsBytes = (byte[]) row.get("headers");
Object headers = row.get("headers");
Map<String, Object> headersAsMap;
try {
headersAsMap = OBJECT_MAPPER.readValue(headersAsBytes, Map.class);
} catch (IOException e) {
throw new RuntimeException(e);
if (headers instanceof byte[]) {
try {
headersAsMap = OBJECT_MAPPER.readValue( (byte[]) headers, Map.class);
} catch (IOException e) {
throw new RuntimeException(e);
}
} else if (headers instanceof String) {
try {
headersAsMap = OBJECT_MAPPER.readValue((String) headers, Map.class);
} catch (IOException e) {
throw new RuntimeCamelHieBootException(e);
}
} else {
throw new RuntimeCamelHieBootException(String.format("Unsupported data type when attempting to unmarshal Camel message headers from database: [%s]. Hint: are you sure you are using a supported database?", headers.getClass().getName()));
}

message.setHeaders(headersAsMap);
Expand Down Expand Up @@ -148,4 +177,5 @@ protected List<RepositoryMessage> doRetrieve(Exchange jdbcExchange) {
}
return toRepositoryMessages(replyJdbcExchange.getMessage().getBody(List.class));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.hisp.hieboot.camel.impl;

import org.apache.camel.impl.DefaultCamelContext;
import org.hisp.hieboot.camel.RuntimeCamelHieBootException;
import org.junit.jupiter.api.Test;

import java.util.List;
import java.util.Map;

import static org.junit.Assert.assertThrows;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class JdbcMessageRepositoryTestCase {

@Test
public void testToRepositoryMessagesGivenUnsupportedHeadersDataType() {
JdbcMessageRepository jdbcMessageRepository = new JdbcMessageRepository();
jdbcMessageRepository.setCamelContext(new DefaultCamelContext());
RuntimeCamelHieBootException runtimeCamelHieBootException = assertThrows(RuntimeCamelHieBootException.class, () -> jdbcMessageRepository.toRepositoryMessages(List.of(Map.of("headers", 1))));
assertEquals("Unsupported data type when attempting to unmarshal Camel message headers from database: [java.lang.Integer]. Hint: are you sure you are using a supported database?", runtimeCamelHieBootException.getMessage());
}
}
Loading