Skip to content

Commit

Permalink
Fixed #126 Handle BLOB column values in sql query action
Browse files Browse the repository at this point in the history
  • Loading branch information
christophd committed Sep 21, 2016
1 parent 46b5248 commit b3d3fcf
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 7 deletions.
Expand Up @@ -25,6 +25,7 @@
import com.consol.citrus.validation.script.sql.SqlResultSetScriptValidator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.commons.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.util.CollectionUtils;
Expand Down Expand Up @@ -146,15 +147,19 @@ private void fillContextVariables(Map<String, List<String>> columnValuesMap, Tes
private void fillColumnValuesMap(List<Map<String, Object>> results, Map<String, List<String>> columnValuesMap) {
for (Map<String, Object> row : results) {
for (Entry<String, Object> column : row.entrySet()) {
String columnValue;
String columnName = column.getKey();
if (columnValuesMap.containsKey(columnName)) {
columnValuesMap.get(columnName).add((column.getValue() == null ?
null : column.getValue().toString()));
if (!columnValuesMap.containsKey(columnName)) {
columnValuesMap.put(columnName, new ArrayList<String>());
}

if (column.getValue() instanceof byte[]) {
columnValue = Base64.encodeBase64String((byte[]) column.getValue());
} else {
List<String> columnValues = new ArrayList<String>();
columnValues.add((column.getValue() == null ? null : column.getValue().toString()));
columnValuesMap.put(columnName, columnValues);
columnValue = column.getValue() == null ? null : column.getValue().toString();
}

columnValuesMap.get(columnName).add((columnValue));
}
}
}
Expand Down
Expand Up @@ -27,10 +27,12 @@
import org.testng.Assert;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import org.apache.commons.codec.binary.Base64;

import java.util.*;

import static org.mockito.Mockito.*;
import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.when;


/**
Expand Down Expand Up @@ -834,4 +836,29 @@ public void testResultSetValidationWithVariableAndFunction() {
context.getVariables().put("progressVar", "progress");
executeSQLQueryAction.execute(context);
}

@Test
public void testBinaryBlobColumnValues() {
String sql = "select ORDERTYPE, BINARY_DATA from orders where ID=5";
reset(jdbcTemplate);

Map<String, Object> resultMap = new HashMap<String, Object>();
resultMap.put("ORDERTYPE", "small");
resultMap.put("BINARY_DATA", "some_binary_data".getBytes());

when(jdbcTemplate.queryForList(sql)).thenReturn(Collections.singletonList(resultMap));

List<String> stmts = Collections.singletonList(sql);
executeSQLQueryAction.setStatements(stmts);

Map<String, String> extractVariables = new HashMap<String, String>();
extractVariables.put("BINARY_DATA", "binaryData");
executeSQLQueryAction.setExtractVariables(extractVariables);

executeSQLQueryAction.execute(context);

Assert.assertNotNull(context.getVariable("${binaryData}"));
Assert.assertEquals(context.getVariable("${binaryData}"), Base64.encodeBase64String("some_binary_data".getBytes()));
Assert.assertEquals(new String(Base64.decodeBase64(context.getVariable("${binaryData}"))), "some_binary_data");
}
}

0 comments on commit b3d3fcf

Please sign in to comment.