/** * Inserts the given {@link EligibleForRefund} into the database or updates it if it already * exists. Flushes changes to the database. * @param refundEntry the refund entry to insert/update. * @return the inserted/updated refund entry. * @throws RefundEntryAlreadyExistsException if a refund entry with the same transaction id as * in the given refund entry already exists. * @see CrudRepository#save(java.lang.Object) */ public EligibleForRefund save(EligibleForRefund refundEntry) throws RefundEntryAlreadyExistsException { try { return eligibleForRefundRepository.saveAndFlush(refundEntry); } catch (DataIntegrityViolationException e) { throw new RefundEntryAlreadyExistsException(); } } }
@Test public void testGetInvestorByEmail() { try { Investor investorFromDb = investorService.getInvestorByEmail("test@test.com"); assertEquals(investor, investorFromDb); } catch(InvestorNotFoundException e) { fail(e.getMessage()); } } }
@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 testSetKycUri() throws KycInfoNotSavedException, URISyntaxException { URI newKycUri = new URI("http://www.newtestlink.com/test"); kycInfoService.saveKycInfo(1, kycUri); try { kycInfoService.setKycUri(1, newKycUri.toASCIIString()); } catch(InvestorNotFoundException e) { fail(e.getMessage()); } Optional<KycInfo> oKycInfo = kycInfoRepository.findOptionalByInvestorId(1); assertKycInfo(oKycInfo.get(), 1, false, 0, false, newKycUri); }
@Test public void testKycReminder_nonexistentInvestor() throws Exception { when(mockKycInfoService.getKycInfoByInvestorId(INVESTOR_ID)).thenThrow(new InvestorNotFoundException()); MvcResult result = mockMvc.perform(MockMvcRequestBuilders.post(KYC_REMIND)) .andExpect(status().isBadRequest()) .andDo(print()) .andReturn(); 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 testStartKycWithMalformedUri() throws Exception { KycStartRequestDTO requestWithMalformedUri = new KycStartRequestDTO("this is not a url"); when(mockKycInfoService.getKycInfoByInvestorId(INVESTOR_ID)).thenThrow(new InvestorNotFoundException()); MvcResult result = mockMvc.perform(MockMvcRequestBuilders.post(KYC_INVESTOR_START) .contentType(MediaType.APPLICATION_JSON) .content(mapper.writeValueAsString(requestWithMalformedUri))) .andExpect(status().isBadRequest()) .andDo(print()) .andReturn(); assertThat(result.getResponse().getErrorMessage()).isEqualTo(KYC_LINK_REASON); }
@Test public void testSetKycStartEmailSent() throws KycInfoNotSavedException { kycInfoService.saveKycInfo(1, kycUri); try { kycInfoService.setKycStartEmailSent(1); } catch(InvestorNotFoundException e) { fail(e.getMessage()); } Optional<KycInfo> oKycInfo = kycInfoRepository.findOptionalByInvestorId(1); assertKycInfo(oKycInfo.get(), 1, true, 0, false, kycUri); }
@Test public void testSetNonexistentInvestorComplete() throws Exception { KycInfo testKycInfo = createKycInfo(true, 0, false); when(mockKycInfoService.getKycInfoByInvestorId(INVESTOR_ID)).thenThrow(new InvestorNotFoundException()); MvcResult result = mockMvc.perform(MockMvcRequestBuilders.post(KYC_INVESTOR_COMPLETE) .contentType(MediaType.APPLICATION_JSON)) .andExpect(status().isBadRequest()) .andDo(print()) .andReturn(); verify(mockKycInfoService, never()).setKycComplete(testKycInfo.getInvestorId(), true); assertThat(result.getResponse().getErrorMessage()).isEqualTo(NONEXISTENT_INVESTOR_REASON); }
@Test public void testSetKycComplete() throws KycInfoNotSavedException { kycInfoService.saveKycInfo(2, kycUri); Optional<KycInfo> oKycInfo = kycInfoRepository.findOptionalByInvestorId(2); assertKycInfo(oKycInfo.get(), 2, false, 0, false, kycUri); try { kycInfoService.setKycComplete(2, true); } catch(InvestorNotFoundException e) { fail(e.getMessage()); } oKycInfo = kycInfoRepository.findOptionalByInvestorId(2); assertKycInfo(oKycInfo.get(), 2, false, 0, true, kycUri); }
@Test public void testGetKycStatus() throws Exception { KycInfo kycInfo = createKycInfo(true, 0,true); when(mockKycInfoService.getKycInfoByInvestorId(INVESTOR_ID)).thenReturn(kycInfo); MvcResult result1 = mockMvc.perform(MockMvcRequestBuilders.get(KYC_INVESTOR_STATUS) .contentType(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()) .andDo(print()) .andReturn(); KycInfo actual = mapper.readValue(result1.getResponse().getContentAsString(), KycInfo.class); assertThat(kycInfo.equals(actual)); when(mockKycInfoService.getKycInfoByInvestorId(INVESTOR_ID)).thenThrow(new InvestorNotFoundException()); MvcResult result2 = mockMvc.perform(MockMvcRequestBuilders.get(KYC_INVESTOR_STATUS) .contentType(MediaType.APPLICATION_JSON)) .andExpect(status().isBadRequest()) .andDo(print()) .andReturn(); assertThat(result2.getResponse().getErrorMessage()).isEqualTo(NONEXISTENT_INVESTOR_REASON); }
@Test public void testSetKycCompleteByUuid() throws KycInfoNotSavedException { kycInfoService.saveKycInfo(50, kycUri); Optional<KycInfo> oKycInfo = kycInfoRepository.findOptionalByInvestorId(50); assertKycInfo(oKycInfo.get(), 50, false, 0, false, kycUri); try { kycInfoService.setKycCompleteByUuid(oKycInfo.get().getKycUuid(), true); } catch(InvestorNotFoundException e) { fail(e.getMessage()); } oKycInfo = kycInfoRepository.findOptionalByInvestorId(50); assertKycInfo(oKycInfo.get(), 50, false, 0, true, kycUri); }
@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)); }
@Test public void testIncreaseNumberOfRemindersSent() throws KycInfoNotSavedException { kycInfoService.saveKycInfo(1, kycUri); try { kycInfoService.increaseNumberOfRemindersSent(1); } catch(InvestorNotFoundException e) { fail(e.getMessage()); } Optional<KycInfo> oKycInfo = kycInfoRepository.findOptionalByInvestorId(1); assertKycInfo(oKycInfo.get(), 1, false, 1, false, kycUri); try { kycInfoService.increaseNumberOfRemindersSent(1); kycInfoService.increaseNumberOfRemindersSent(1); } catch (InvestorNotFoundException e) { fail(e.getMessage()); } oKycInfo = kycInfoRepository.findOptionalByInvestorId(1); assertKycInfo(oKycInfo.get(), 1, false, 3, false, kycUri); }
@Test public void testGetKycInfoByKycUuid() throws KycInfoNotSavedException { kycInfoService.saveKycInfo(1, kycUri); UUID uuid = kycInfoRepository.findOptionalByInvestorId(1).get().getKycUuid(); try { KycInfo kycInfo = kycInfoService.getKycInfoByKycUuid(uuid); assertKycInfo(kycInfo, 1, false, 0, false, kycUri); } catch(InvestorNotFoundException e) { fail(e.getMessage()); } }
@Test public void testGetKycInfoByInvestorId() throws KycInfoNotSavedException { kycInfoService.saveKycInfo(2000, kycUri); try { KycInfo kycInfo = kycInfoService.getKycInfoByInvestorId(2000); assertKycInfo(kycInfo, 2000, false, 0, false, kycUri); } catch (InvestorNotFoundException e) { fail(e.getMessage()); } try { kycInfoService.getKycInfoByInvestorId(1); fail("InvestorNotFoundException should have been thrown but wasn't."); } catch(InvestorNotFoundException expectedException) { } }
@Test public void testSetMultipleKycCompleteAndFindAll() throws KycInfoNotSavedException { kycInfoService.saveKycInfo(1, kycUri); kycInfoService.saveKycInfo(2, kycUri); kycInfoService.saveKycInfo(3, kycUri); try { kycInfoService.setKycComplete(1, true); kycInfoService.setKycComplete(3, true); } catch(InvestorNotFoundException e) { fail(e.getMessage()); } List<KycInfo> kycList = kycInfoRepository.findAll(); assertEquals(3, kycList.size()); List<KycInfo> kycCompleteList = kycInfoRepository.findAllByIsKycComplete(true); assertEquals(2, kycCompleteList.size()); }