问题
so here I'm trying to invoke a java class that goes and retrieves information from a database the problem is that it sometimes works and sometimes it doesn't and well I'm quite not seeing it and would love it if you could share your insights, I have a DAO class that has a method that receives a number and uses a SQL statement through JDBC to connect, then I map the information as I need it in a Map<String, String>
then returns that map, I'm trying to compare values of that map to those of the response of the API I'm testing, I got a print
statement in my feature file that prints what it got from that, sometimes it has values sometimes it doesn't.
The thing is that when I check the account numbers on the database through programs like DBeaver the values associated are there and if I make a public static void main
method in the DAO class that just prints the value of the map it always has values so I don't know why when I run my feature it prints nothing and so my feature fails because it says it expected a null value.
This is my feature file:
* def session = call read('classpath:TokenSession.feature@Session') { cliente: <numeroCuenta> }
* configure ssl = true
* url basicURL + '/api/v1.0/clientes'
* path <numeroCuenta> , 'estado-cuentas'
* param tipo_documento = 1
* param pais = 'CHL'
* header Authorization = 'Bearer ' + session.accessToken
* header x-sesion-id = session.idSession
* header x-canal-id = '280'
* method get
#Consulta a BD
* def dao = Java.type('dao.DaoClientes')
* def sql = new dao()
* def query = sql.identificacionCliente('<numeroCuenta>')
* print 'QUERY DE BASE DE DATOS: ', query
* status 200
* print 'Respuesta ms: ' , response
* match response.nombre_apellido == query.nombre
* string cuenta = response.id_cuenta
* match cuenta == query.idCuenta
* match response.tipo_tarjeta == query.marcaTarjeta
* match query.numeroTarjeta contains response.digitos_ult_tarjeta
* string titular = response.titular
* match titular == query.titular
And this is my DAO.
package dao;
import db.DbConnFactory;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;
public class DaoClientes {
public Map<String, String> identificacionCliente(String rut) throws SQLException {
Map<String, String> map = new HashMap<>();
DbConnFactory db = new DbConnFactory();
Connection con = db.getConnection();
try {
Statement statement = con.createStatement();
ResultSet rs = statement.executeQuery("select cli.id_cliente, cli.documento_identificacion || cli.digito_verificador as rut,\n" +
" per.primer_nombre || ' ' || per.apellido_paterno as nombre," +
" cta.id_cuenta," +
" tar.numero_tarjeta, tar.fecha_emision_tarjeta," +
" marca.descripcion as marcaTarjeta," +
" rol.descripcion as tipoCliente" +
" from admmcli.mcli_cliente cli" +
" inner join admmcli.mcli_persona per" +
" on cli.id_cliente = per.id_cliente" +
" inner join admmcta.mcta_maestro_cuenta cta" +
" on cli.id_cliente = cta.id_cliente" +
" inner join admmcta.mcta_maestro_tarjeta tar" +
" on cli.id_cliente = tar.id_cliente" +
" inner join admmcta.mcta_marca_tarjeta marca" +
" on tar.id_marca_tarjeta = marca.id" +
" inner join admmref.mref_tipo_rol_cliente rol" +
" on tar.id_tipo_tarjeta = rol.id" +
" where tar.indicador_vigente = 1" +
" and cli.documento_identificacion || cli.digito_verificador = '" + rut + "'" +
" order by tar.tarjeta_abierta desc, tar.fecha_emision_tarjeta desc" +
"");
while (rs.next()) {
if (rs.getString("tipoCliente").equalsIgnoreCase("TITULAR")) {
map.put("nombre", rs.getString("nombre"));
map.put("idCuenta", rs.getString("id_cuenta"));
map.put("marcaTarjeta", rs.getString("marcaTarjeta"));
map.put("numeroTarjeta", rs.getString("numero_tarjeta").substring(15, 19));
map.put("id_Cliente", rs.getString("id_cliente"));
if (rs.getString("tipoCliente").equalsIgnoreCase("TITULAR"))
map.put("titular", "true");
else
map.put("titular", "false");
}
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
// close db connection
if (con != null)
con.close();
}
return map;
}
}
回答1:
I see the issue, the information I'm feeding to the method may have a letter "K" so when I put those in the example section I have to put it in quotes, therefore it gets passed to the method as "########K" instead of ########K and the result is that I get an empty Map, so the question now would be, how do i avoid the quotes being passed to the method?
Backslashes in def query = sql.identificacionCliente('<numeroCuenta>')
don't work.
来源:https://stackoverflow.com/questions/58825578/karate-java-function-problems