From 7679cd506659af1f91ca71d507eb81040a8a6e33 Mon Sep 17 00:00:00 2001 From: Inal Arsanukaev Date: Wed, 26 Apr 2017 15:16:06 +0300 Subject: [PATCH] Ft/hook 18/fix gen webhook key (#9) * HOOK-18: fixed bug with webhook generating webhook key * fixed hookDaoImpl * fix test and minor improving code --- .../rbkmoney/hooker/dao/impl/HookDaoImpl.java | 22 +++++++++++-------- .../rbkmoney/hooker/dao/HookDaoImplTest.java | 3 +++ 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/rbkmoney/hooker/dao/impl/HookDaoImpl.java b/src/main/java/com/rbkmoney/hooker/dao/impl/HookDaoImpl.java index 7ffef9f..638fc7a 100644 --- a/src/main/java/com/rbkmoney/hooker/dao/impl/HookDaoImpl.java +++ b/src/main/java/com/rbkmoney/hooker/dao/impl/HookDaoImpl.java @@ -16,6 +16,7 @@ import org.springframework.jdbc.core.RowMapper; import org.springframework.jdbc.core.namedparam.MapSqlParameterSource; import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; import org.springframework.jdbc.support.GeneratedKeyHolder; +import org.springframework.jdbc.support.KeyHolder; import org.springframework.transaction.annotation.Transactional; import java.util.*; @@ -162,8 +163,8 @@ public class HookDaoImpl implements HookDao { @Override @Transactional public Hook create(Hook hook) { - KeyPair keyPair = createPairKey(hook.getPartyId()); - hook.setPubKey(keyPair.getPublKey()); + String pubKey = createOrGetPubKey(hook.getPartyId()); + hook.setPubKey(pubKey); hook.setEnabled(true); final String sql = "INSERT INTO hook.webhook(party_id, url) " + @@ -259,24 +260,27 @@ public class HookDaoImpl implements HookDao { @Autowired Signer signer; - private KeyPair createPairKey(String partyId) { + private String createOrGetPubKey(String partyId) { final String sql = "INSERT INTO hook.party_key(party_id, priv_key, pub_key) " + "VALUES (:party_id, :priv_key, :pub_key) " + - "ON CONFLICT(party_id) DO NOTHING"; + "ON CONFLICT(party_id) DO UPDATE SET party_id=:party_id RETURNING pub_key"; KeyPair keyPair = signer.generateKeys(); MapSqlParameterSource params = new MapSqlParameterSource() .addValue("party_id", partyId) .addValue("priv_key", keyPair.getPrivKey()) .addValue("pub_key", keyPair.getPublKey()); + String pubKey = null; try { - jdbcTemplate.update(sql, params); - } catch (DataAccessException e) { - log.warn("WebhookKeyDaoImpl.createPairKey error", e); + KeyHolder keyHolder = new GeneratedKeyHolder(); + jdbcTemplate.update(sql, params, keyHolder); + pubKey = (String) keyHolder.getKeys().get("pub_key"); + } catch (DataAccessException | NullPointerException | ClassCastException e) { + log.warn("WebhookKeyDaoImpl.createOrGetPubKey error", e); throw new DaoException(e); } - log.info("Key with party_id = {} added to table", partyId); - return keyPair; + log.info("Key with party_id = {} added to table. PubKey {}", partyId, pubKey); + return pubKey; } diff --git a/src/test/java/com/rbkmoney/hooker/dao/HookDaoImplTest.java b/src/test/java/com/rbkmoney/hooker/dao/HookDaoImplTest.java index c886871..51bdf78 100644 --- a/src/test/java/com/rbkmoney/hooker/dao/HookDaoImplTest.java +++ b/src/test/java/com/rbkmoney/hooker/dao/HookDaoImplTest.java @@ -44,6 +44,7 @@ public class HookDaoImplTest extends AbstractIntegrationTest { eventFilterByCode.getInvoice().setShopId(1); WebhookParams webhookParams = new WebhookParams("123", eventFilterByCode, "https://google.com"); Hook hook = hookDao.create(HookConverter.convert(webhookParams)); + String pubKey1 = hook.getPubKey(); ids.add(hook.getId()); webhookAdditionalFilters.clear(); webhookAdditionalFilters.add(new WebhookAdditionalFilter(EventType.INVOICE_STATUS_CHANGED, 78, "unpaid", null)); @@ -55,7 +56,9 @@ public class HookDaoImplTest extends AbstractIntegrationTest { webhookAdditionalFilters.add(new WebhookAdditionalFilter(EventType.INVOICE_STATUS_CHANGED)); webhookParams = new WebhookParams("123", EventFilterUtils.getEventFilter(webhookAdditionalFilters), "https://2ch.hk/b"); hook = hookDao.create(HookConverter.convert(webhookParams)); + String pubKey2 = hook.getPubKey(); ids.add(hook.getId()); + Assert.assertEquals(pubKey1, pubKey2); } @After