Skip to content

Commit

Permalink
Merge pull request #30 from dhis2/INTEROP-37-fix
Browse files Browse the repository at this point in the history
fix(INTEROP-37): allow string data type in case the database is not H2
  • Loading branch information
cjmamo authored Dec 11, 2024
2 parents c727b6c + 5ab80b2 commit 03d2ff1
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 10 deletions.
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());
}
}

0 comments on commit 03d2ff1

Please sign in to comment.