private Investor createInvestor() { try { return investorService.getInvestorByEmail(INVESTOR_EMAIL); } catch (InvestorNotFoundException e) { return investorService.saveTransactionless( new Investor(new java.util.Date(), INVESTOR_EMAIL, "token", "walletAddress", "payInEtherPublicKey", "payInBitcoinPublicKey", "refundEtherAddress", "refundBitcoinAddress", "ipAddress")); } }
@Bean public InvestorService investorService(InvestorRepository investorRepository) { return new InvestorService(investorRepository); }
@Override public Investor getAssociatedInvestor() throws MissingTransactionInformationException { try { return getInvestorService().getInvestorByBitcoinAddress(getReceivingAddress()); } catch (MissingTransactionInformationException e) { throw e; } catch (Exception e) { throw new MissingTransactionInformationException("Couldn't fetch associated investor.", e); } }
private RemindSingleKycResponseDTO remindKyc(long investorId, URI kycUri) throws NonexistentInvestorException { Investor investor; try { investor = investorService.getInvestorByInvestorId(investorId); KycReminderEmailMessage kycReminderEmailMessage = messageFactory.makeKycReminderEmailMessage(investor, kycUri); messageService.send(kycReminderEmailMessage); return new RemindSingleKycResponseDTO(investorId, true); } catch(InvestorNotFoundException e) { throw new NonexistentInvestorException(); } }
@RabbitListener( bindings = @QueueBinding(value = @Queue(value = KYC_REMINDER_EMAIL_SENT_QUEUE, autoDelete = "false"), exchange = @Exchange( value = ICONATOR_ENTRY_EXCHANGE, type = ExchangeTypes.TOPIC, ignoreDeclarationExceptions = "true", durable = "true" ), key = KYC_REMINDER_EMAIL_SENT_ROUTING_KEY) ) public void receiveMessage(KycReminderEmailSentMessage message) { try { long investorId = investorService.getInvestorByEmail(message.getEmailAddress()).getId(); kycInfoService.increaseNumberOfRemindersSent(investorId); LOG.debug("Increased numberOfRemindersSent for investor with ID {} by 1", investorId); } catch (InvestorNotFoundException e) { LOG.error("Investor with address {} not found", message.getEmailAddress()); } }
List<Investor> investorList = investorService.getAllInvestors(); List<Long> alreadySentList = kycInfoService.getAllInvestorIdWhereStartKycEmailSent();
@Override public Investor getAssociatedInvestor() throws MissingTransactionInformationException { try { return getInvestorService().getInvestorByEthereumAddress(getReceivingAddress()); } catch (MissingTransactionInformationException e) { throw e; } catch (Exception e) { throw new MissingTransactionInformationException("Couldn't fetch associated investor.", e); } }
private Investor createAndSaveInvestor(ECKey key) { return investorService.saveTransactionless(buildInvestor(key)); }
private StartSingleKycResponseDTO initiateKyc(long investorId, URI kycUri) throws NonexistentInvestorException, KycLinkException, UnexpectedException { Investor investor; try { investor = investorService.getInvestorByInvestorId(investorId); kycInfoService.saveKycInfo(investorId, kycUri); if(kycUri == null) { kycUri = new URI(linkCreator.getKycLink(investorId)); kycInfoService.setKycUri(investorId, kycUri.toASCIIString()); } KycStartEmailMessage kycStartEmail = messageFactory.makeKycStartEmailMessage(investor, kycUri); messageService.send(kycStartEmail); return new StartSingleKycResponseDTO(investorId, true); } catch(KycInfoNotSavedException e) { LOG.error("KYC Info not saved.", e); throw new UnexpectedException(); } catch(InvestorNotFoundException e) { throw new NonexistentInvestorException(); } catch(URISyntaxException e) { throw new KycLinkException(); } }
@RabbitListener( bindings = @QueueBinding(value = @Queue(value = KYC_START_EMAIL_SENT_QUEUE, autoDelete = "false"), exchange = @Exchange( value = ICONATOR_ENTRY_EXCHANGE, type = ExchangeTypes.TOPIC, ignoreDeclarationExceptions = "true", durable = "true" ), key = KYC_START_EMAIL_SENT_ROUTING_KEY) ) public void receiveMessage(KycStartEmailSentMessage message) { try { long investorId = investorService.getInvestorByEmail(message.getEmailAddress()).getId(); kycInfoService.setKycStartEmailSent(investorId); LOG.debug("Set kycStartEmailSent for investor with ID {} to true", investorId); } catch (InvestorNotFoundException e) { LOG.error("Investor with address {} not found", message.getEmailAddress()); } }
@Test public void testStartKycForAll() throws Exception { List<Investor> investorList = new ArrayList<>(); List<Long> kycStartedList = new ArrayList<>(); Investor investor1 = new Investor(new Date(), "investor1@test.com", "investor1token").setId(1); Investor investor2 = new Investor(new Date(), "investor2@test.com", "investor2token").setId(2); Investor investor3 = new Investor(new Date(), "investor3@test.com", "investor3token").setId(3); Investor investor4 = new Investor(new Date(), "investor4@test.com", "investor4token").setId(4); investorList.add(investor1); investorList.add(investor2); investorList.add(investor3); investorList.add(investor4); kycStartedList.add(2L); when(mockInvestorService.getAllInvestors()).thenReturn(investorList); when(mockKycInfoService.getAllInvestorIdWhereStartKycEmailSent()).thenReturn(kycStartedList); when(mockLinkCreatorService.getKycLink(anyLong())).thenReturn(KYC_LINK); when(mockPaymentLogService.hasInvestorInvested(1)).thenReturn(true); when(mockPaymentLogService.hasInvestorInvested(3)).thenReturn(false); when(mockPaymentLogService.hasInvestorInvested(4)).thenReturn(true); MvcResult result = mockMvc.perform(MockMvcRequestBuilders.post(KYC_START_ALL) .contentType(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()) .andDo(print()) .andReturn(); StartAllKycResponseDTO responseDTO = mapper.readValue(result.getResponse().getContentAsString(), StartAllKycResponseDTO.class); assertThat(responseDTO.getKycStartedList()).containsExactly(1L, 4L); assertThat(responseDTO.getErrorList()).isEmpty(); }
@Test public void testKycReminder() throws Exception { Investor investor = new Investor(new Date(), "test@test.com", "1234"); KycReminderEmailMessage message = new KycReminderEmailMessage(build(investor), KYC_LINK); KycInfo kycInfo = createKycInfo(true, 0, false); when(mockKycInfoService.getKycInfoByInvestorId(INVESTOR_ID)).thenReturn(kycInfo); when(mockInvestorService.getInvestorByInvestorId(INVESTOR_ID)).thenReturn(investor); when(mockMessageFactory.makeKycReminderEmailMessage(eq(investor), eq(new URI(KYC_LINK)))).thenReturn(message); MvcResult result = mockMvc.perform(MockMvcRequestBuilders.post(KYC_REMIND)) .andExpect(status().isOk()) .andDo(print()) .andReturn(); verify(mockMessageService, times(1)).send(any(KycReminderEmailMessage.class)); assertThat(result.getResponse().getContentAsString()).isEqualTo( jsonRemindSingleKycResponse.write(new RemindSingleKycResponseDTO(INVESTOR_ID, true)).getJson() ); }
@Test public void testGetInvestorByEmail() { try { Investor investorFromDb = investorService.getInvestorByEmail("test@test.com"); assertEquals(investor, investorFromDb); } catch(InvestorNotFoundException e) { fail(e.getMessage()); } } }
@Bean public InvestorService investorService(InvestorRepository investorRepository) { return new InvestorService(investorRepository); }
@Test public void testStartKycWithNonexistentInvestor() throws Exception { when(mockKycInfoService.getKycInfoByInvestorId(INVESTOR_ID)).thenThrow(new InvestorNotFoundException()); when(mockInvestorService.getInvestorByInvestorId(INVESTOR_ID)).thenThrow(new InvestorNotFoundException()); MvcResult result = mockMvc.perform(MockMvcRequestBuilders.post(KYC_INVESTOR_START) .contentType(MediaType.APPLICATION_JSON) .content(mapper.writeValueAsString(kycStartRequest))) .andExpect(status().isBadRequest()) .andDo(print()) .andReturn(); verify(mockMessageService, never()).send(any(KycStartEmailMessage.class)); assertThat(result.getResponse().getErrorMessage()).isEqualTo(NONEXISTENT_INVESTOR_REASON); }
@Test public void testGetInvestorByInvestorId() { try { Investor investorFromDb = investorService.getInvestorByInvestorId(investor.getId()); assertEquals(investor, investorFromDb); } catch(InvestorNotFoundException e) { fail(e.getMessage()); } }
@Test public void testStartKycWithExistingInvestor() throws Exception { Investor investor = new Investor(new Date(), "test@test.com", "1234"); KycStartEmailMessage message = new KycStartEmailMessage(build(investor), KYC_LINK); when(mockKycInfoService.getKycInfoByInvestorId(INVESTOR_ID)).thenThrow(new InvestorNotFoundException()); when(mockInvestorService.getInvestorByInvestorId(INVESTOR_ID)).thenReturn(investor); when(mockMessageFactory.makeKycStartEmailMessage(eq(investor), eq(new URI(KYC_LINK)))).thenReturn(message); // start with link MvcResult result1 = mockMvc.perform(MockMvcRequestBuilders.post(KYC_INVESTOR_START) .contentType(MediaType.APPLICATION_JSON) .content(mapper.writeValueAsString(kycStartRequest))) .andExpect(status().isOk()) .andDo(print()) .andReturn(); assertThat(result1.getResponse().getContentAsString()).isEqualTo( jsonStartSingleKycResponse.write(new StartSingleKycResponseDTO(INVESTOR_ID, true)).getJson() ); // start without providing link when(mockLinkCreatorService.getKycLink(INVESTOR_ID)).thenReturn(KYC_LINK); MvcResult result2 = mockMvc.perform(MockMvcRequestBuilders.post(KYC_INVESTOR_START) .contentType(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()) .andDo(print()) .andReturn(); assertThat(result2.getResponse().getContentAsString()).isEqualTo( jsonStartSingleKycResponse.write(new StartSingleKycResponseDTO(INVESTOR_ID, true)).getJson() ); verify(mockMessageService, times(2)).send(eq(message)); }