public List<KeyPairs> fromCSV(InputStream inputStream) throws Exception { Reader reader = new InputStreamReader(inputStream); Iterable<CSVRecord> records = CSVFormat.RFC4180.withFirstRecordAsHeader().parse(reader); Iterable<CSVRecord> iterable = () -> records.iterator(); Stream<CSVRecord> stream = StreamSupport.stream(iterable.spliterator(), false); return stream.map((entry) -> { String ethAddress = entry.get(0); String btcAddress = entry.get(1); LOG.info("Reading: ETH={} BTC={}", ethAddress, btcAddress); return new KeyPairs(btcAddress, ethAddress, true); }).collect(Collectors.toList()); }
public boolean addKeyPairsIfNotPresent(KeyPairs keyPairs) { try { this.keyPairsRepository.saveAndFlush(keyPairs); return true; } catch (Exception e) { LOG.warn("Not loading key [{}, {}]. Error: {}", keyPairs.getPublicEth(), keyPairs.getPublicBtc(), e); return false; } }
private static boolean allMarkedAsUnavailable(List<KeyPairs> keyPairsList) { return keyPairsList.stream().allMatch((k) -> !k.getAvailable()); }
@Test public void testFindFirstOptionalByPublicEth() { Optional<KeyPairs> optionalKeyPairs = keyPairsRepository.findFirstOptionalByPublicEth("0x0eB5C5de600D088AB0260d068E9765022FD5173b"); LOG.info("Id returned: " + optionalKeyPairs.get().getId()); assertTrue(optionalKeyPairs.isPresent()); assertEquals(1, optionalKeyPairs.get().getId()); assertEquals("0x0eB5C5de600D088AB0260d068E9765022FD5173b", optionalKeyPairs.get().getPublicEth()); assertEquals(true, optionalKeyPairs.get().getAvailable()); }
@Test public void testFindFirstOptionalByPublicBtc() { Optional<KeyPairs> optionalKeyPairs = keyPairsRepository.findFirstOptionalByPublicBtc("n4CS3vyACkcvCyVLm2RS5tpvyy6NNvbCBr"); LOG.info("Id returned: " + optionalKeyPairs.get().getId()); assertTrue(optionalKeyPairs.isPresent()); assertEquals(10, optionalKeyPairs.get().getId()); assertEquals("n4CS3vyACkcvCyVLm2RS5tpvyy6NNvbCBr", optionalKeyPairs.get().getPublicBtc()); assertEquals(true, optionalKeyPairs.get().getAvailable()); }
@Test public void testFindOptionalById() { Optional<KeyPairs> optionalKeyPairs = keyPairsRepository.findById(1L); LOG.info("Id returned: " + optionalKeyPairs.get().getId()); assertTrue(optionalKeyPairs.isPresent()); assertEquals(1, optionalKeyPairs.get().getId()); assertEquals(true, optionalKeyPairs.get().getAvailable()); }
@Transactional(isolation = Isolation.READ_COMMITTED) public Optional<KeyPairs> getFreshKey() { try { return this.keyPairsRepository .findFirstOptionalByAvailableOrderByIdAsc(new Boolean(true)) .map((k) -> k.setAvailable(new Boolean(false))) .map((k) -> this.keyPairsRepository.save(k)); } catch (Exception e) { LOG.error("Error obtaining a fresh key.", e); return Optional.empty(); } }
@Test public void testFindFirstOptionalByAvailableOrderByIdDesc() { Optional<KeyPairs> optionalFindFirstAvailable = keyPairsRepository.findFirstOptionalByAvailableOrderByIdAsc(new Boolean(true)); assertTrue(optionalFindFirstAvailable.isPresent()); assertEquals(1, optionalFindFirstAvailable.get().getId()); assertEquals(true, optionalFindFirstAvailable.get().getAvailable()); }
.setPayInBitcoinAddress(keyPairs.getPublicBtc()) .setPayInEtherAddress(keyPairs.getPublicEth()) .setRefundBitcoinAddress(refundBitcoinAddress) .setRefundEtherAddress(addPrefixEtherIfNotExist(refundEthereumAddress)); .setBtc(keyPairs.getPublicBtc()) .setEther(keyPairs.getPublicEth()), HttpStatus.OK);
@Test public void testImport_1_Failed() throws Exception { KeyPairs k1 = new KeyPairs("mgfRrsDH56YfTgw3pWJg7j4yEaUgxJxxim", "0x0eB5C5de600D088AB0260d068E9765022FD5173b"); KeyPairs k2 = new KeyPairs("ms8Ux2eEMFTq4HKXiCiPGCXzrV9dPeALx", "0x0eB5C5de600D088AB0260d068E9765022FD5173b"); KeyPairs k3 = new KeyPairs("mhCKesVtR6coeLRGXg3V8D7cQmgjXzaQj2", "0x3Bf54439C6056B564C14986b08e46637dD438372"); KeyPairs k4 = new KeyPairs("muBFsGpYiDLAcTbqZ4vcHsf9dnUTjBoVKg", "0x2D4A5a6FDf6f29ce236251974572ead525eC319D"); List<KeyPairs> keyPairsList = Arrays.asList(k1, k2, k3, k4); when(mockCsvService.fromCSV(any())).thenReturn(keyPairsList); when(mockKeyPairsRepositoryService.addKeyPairsIfNotPresent(k1)).thenReturn(new Boolean(false)); when(mockKeyPairsRepositoryService.addKeyPairsIfNotPresent(k2)).thenReturn(new Boolean(true)); when(mockKeyPairsRepositoryService.addKeyPairsIfNotPresent(k3)).thenReturn(new Boolean(true)); when(mockKeyPairsRepositoryService.addKeyPairsIfNotPresent(k4)).thenReturn(new Boolean(true)); MvcResult mvcResult = this.mockMvc.perform(post("/keypairs/import") .with(httpBasic("user", "password")) .contentType(APPLICATION_OCTET_STREAM_VALUE) .content(IOUtils.toByteArray(csvStream))) .andExpect(status().isOk()) .andDo(print()) .andReturn(); assertThat(mvcResult.getResponse().getContentAsString()).isEqualTo( jsonKeyPairsImportResponse.write(new KeyPairsImportResponse(new Long(3), new Long(1))).getJson() ); }
@Test public void testImport_Successful() throws Exception { List<KeyPairs> keyPairsList = Arrays.asList( new KeyPairs("mgfRrsDH56YfTgw3pWJg7j4yEaUgxJxxim", "0x0eB5C5de600D088AB0260d068E9765022FD5173b"), new KeyPairs("ms8Ux2eEMFTq4HKXiCiPGCXzrV9dPeALx", "0x0eB5C5de600D088AB0260d068E9765022FD5173b"), new KeyPairs("mhCKesVtR6coeLRGXg3V8D7cQmgjXzaQj2", "0x3Bf54439C6056B564C14986b08e46637dD438372"), new KeyPairs("muBFsGpYiDLAcTbqZ4vcHsf9dnUTjBoVKg", "0x2D4A5a6FDf6f29ce236251974572ead525eC319D") ); when(mockCsvService.fromCSV(any())).thenReturn(keyPairsList); when(mockKeyPairsRepositoryService.addKeyPairsIfNotPresent(any())).thenReturn(new Boolean(true)); MvcResult mvcResult = this.mockMvc.perform(post("/keypairs/import") .with(httpBasic("user", "password")) .contentType(APPLICATION_OCTET_STREAM_VALUE) .content(IOUtils.toByteArray(csvStream))) .andExpect(status().isOk()) .andDo(print()) .andReturn(); assertThat(mvcResult.getResponse().getContentAsString()).isEqualTo( jsonKeyPairsImportResponse.write(new KeyPairsImportResponse(new Long(4), new Long(0))).getJson() ); }
@Test public void testAddKeyPairsIfNotPresent_New() { KeyPairs k1 = new KeyPairs("testPublicBtc", "testPublicEth", true); Optional<KeyPairs> resultFind = keyPairsRepositoryService.findByPublicBtc("testPublicBtc"); assertFalse(resultFind.isPresent()); boolean result1 = keyPairsRepositoryService.addKeyPairsIfNotPresent(k1); assertTrue(result1); }
@Test() public void testAddKeyPairsIfNotPresent_AlreadyInserted() { KeyPairs k1 = new KeyPairs("mgfRrsDH56YfTgw3pWJg7j4yEaUgxJxxim", "0x0eB5C5de600D088AB0260d068E9765022FD5173b", true); Optional<KeyPairs> resultFind = keyPairsRepositoryService.findByPublicBtc("mgfRrsDH56YfTgw3pWJg7j4yEaUgxJxxim"); assertTrue(resultFind.isPresent()); boolean result1 = keyPairsRepositoryService.addKeyPairsIfNotPresent(k1); assertFalse(result1); }